001    /*
002     * Copyright 2004 Chris Nelson
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
007     * Unless required by applicable law or agreed to in writing,
008     * software distributed under the License is distributed on an "AS IS" BASIS,
009     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010     * See the License for the specific language governing permissions and limitations under the License.
011     */
012    package org.tynamo.util;
013    
014    import ognl.Ognl;
015    import ognl.OgnlException;
016    import org.tynamo.exception.TynamoRuntimeException;
017    
018    import java.util.Collection;
019    import java.util.HashMap;
020    
021    
022    public class Utils
023    {
024    
025            /* keys used to internationalize Tynamo in Tapestry components and pages */
026            public static final String APPLY_MESSAGE = "org.tynamo.i18n.apply";
027            public static final String APPLY_AND_RETURN_MESSAGE = "org.tynamo.i18n.applyAndReturn";
028            public static final String REMOVE_MESSAGE = "org.tynamo.i18n.remove";
029            public static final String CANCEL_MESSAGE = "org.tynamo.i18n.cancel";
030            public static final String ADD_NEW_MESSAGE = "org.tynamo.i18n.addNew";
031    
032            public static final String EDIT_MESSAGE = "org.tynamo.i18n.edit";
033            public static final String SHOW_MESSAGE = "org.tynamo.i18n.show";
034            public static final String LIST_MESSAGE = "org.tynamo.i18n.list";
035            public static final String NEW_MESSAGE = "org.tynamo.i18n.new";
036    
037            public static final String ADDED_MESSAGE = "org.tynamo.i18n.added";
038            public static final String SAVED_MESSAGE = "org.tynamo.i18n.saved";
039    
040            public static String DEFAULT = "Default";
041    
042            public static Class classForName(String className)
043            {
044                    try
045                    {
046                            return Class.forName(className);
047                    } catch (ClassNotFoundException e)
048                    {
049                            throw new TynamoRuntimeException(e, null);
050                    }
051            }
052    
053            public static String unqualify(String className)
054            {
055                    return className.substring(className.lastIndexOf(".") + 1);
056            }
057    
058            /**
059             * Thank you, AndroMDA project...
060             * Linguistically pluralizes a singular noun. <p/>
061             * <ul>
062             * <li><code>noun</code> becomes <code>nouns</code></li>
063             * <li><code>key</code> becomes <code>keys</code></li>
064             * <li><code>word</code> becomes <code>words</code></li>
065             * <li><code>property</code> becomes <code>properties</code></li>
066             * <li><code>bus</code> becomes <code>busses</code></li>
067             * <li><code>boss</code> becomes <code>bosses</code></li>
068             * </ul>
069             * <p/>
070             * Whitespace as well as <code>null></code> arguments will return an empty
071             * String.
072             * </p>
073             *
074             * @param singularNoun A singularNoun to pluralize
075             * @return The plural of the argument singularNoun
076             */
077            public static String pluralize(String singularNoun)
078            {
079                    String pluralNoun = singularNoun;
080    
081                    int nounLength = pluralNoun.length();
082    
083                    if (nounLength == 1)
084                    {
085                            pluralNoun = pluralNoun + 's';
086                    } else if (nounLength > 1)
087                    {
088                            char secondToLastChar = pluralNoun.charAt(nounLength - 2);
089    
090                            if (pluralNoun.endsWith("y"))
091                            {
092                                    switch (secondToLastChar)
093                                    {
094                                            case 'a': // fall-through
095                                            case 'e': // fall-through
096                                            case 'i': // fall-through
097                                            case 'o': // fall-through
098                                            case 'u':
099                                                    pluralNoun = pluralNoun + 's';
100                                                    break;
101                                            default:
102                                                    pluralNoun = pluralNoun.substring(0, nounLength - 1)
103                                                            + "ies";
104                                    }
105                            } else if (pluralNoun.endsWith("s"))
106                            {
107                                    switch (secondToLastChar)
108                                    {
109                                            case 's':
110                                                    pluralNoun = pluralNoun + "es";
111                                                    break;
112                                            default:
113                                                    pluralNoun = pluralNoun + "ses";
114                                    }
115                            } else
116                            {
117                                    pluralNoun = pluralNoun + 's';
118                            }
119                    }
120                    return pluralNoun;
121            }
122    
123            /**
124             * @param type the (usable) super type if passed a CGLIB enhanced class
125             * @return
126             */
127            public static Class checkForCGLIB(Class type)
128            {
129                    if (type.getName().contains("CGLIB"))
130                    {
131                            return type.getSuperclass();
132                    } else return type;
133            }
134    
135            public static void executeOgnlExpression(String ognlExpression, Object member, Object model)
136            {
137                    HashMap context = new HashMap();
138                    context.put("member", member);
139    
140                    try
141                    {
142                            Ognl.getValue(ognlExpression + "(#member)", context, model);
143                    } catch (OgnlException e)
144                    {
145                            throw new TynamoRuntimeException(e, model.getClass());
146                    }
147            }
148    
149            /**
150             * Tests whether or not a name matches against at least one exclude pattern.
151             *
152             * @param name The name to match. Must not be null.
153             * @param exclusionPatterns the list of exclude patterns to test against
154             * @return true when the name matches against at least one exclude pattern, or false otherwise.
155             */
156            public static boolean isExcluded(String name, Collection<String> exclusionPatterns)
157            {
158                    for (String exclusionPattern : exclusionPatterns)
159                    {
160                            if (name.matches(exclusionPattern))
161                            {
162                                    return true;
163                            }
164                    }
165    
166                    return false;
167            }
168    }