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.trails.util;
013    
014    import ognl.Ognl;
015    import ognl.OgnlException;
016    import org.apache.commons.lang.StringUtils;
017    import org.apache.oro.text.perl.Perl5Util;
018    import org.trails.exception.TrailsRuntimeException;
019    
020    import java.util.ArrayList;
021    import java.util.HashMap;
022    
023    
024    /**
025     * @author fus8882
026     */
027    public class Utils
028    {
029    
030            /* key used to internationalize Trails in Tapestry components and pages */
031            public static final String APPLY_MESSAGE = "org.trails.i18n.apply";
032            public static final String APPLY_AND_RETURN_MESSAGE = "org.trails.i18n.applyAndReturn";
033            public static final String REMOVE_MESSAGE = "org.trails.i18n.remove";
034            public static final String CANCEL_MESSAGE = "org.trails.i18n.cancel";
035            public static final String ADD_NEW_MESSAGE = "org.trails.i18n.addNew";
036            public static String DEFAULT = "Default";
037    
038            public static Class classForName(String className)
039            {
040                    try
041                    {
042                            return Class.forName(className);
043                    } catch (ClassNotFoundException e)
044                    {
045                            throw new TrailsRuntimeException(e, null);
046                    }
047            }
048    
049            public static String unqualify(String className)
050            {
051                    return className.substring(className.lastIndexOf(".") + 1);
052            }
053    
054            /**
055             * Thank you, AndroMDA project...
056             * Linguistically pluralizes a singular noun. <p/>
057             * <ul>
058             * <li><code>noun</code> becomes <code>nouns</code></li>
059             * <li><code>key</code> becomes <code>keys</code></li>
060             * <li><code>word</code> becomes <code>words</code></li>
061             * <li><code>property</code> becomes <code>properties</code></li>
062             * <li><code>bus</code> becomes <code>busses</code></li>
063             * <li><code>boss</code> becomes <code>bosses</code></li>
064             * </ul>
065             * <p/>
066             * Whitespace as well as <code>null></code> arguments will return an empty
067             * String.
068             * </p>
069             *
070             * @param singularNoun A singularNoun to pluralize
071             * @return The plural of the argument singularNoun
072             */
073            public static String pluralize(String singularNoun)
074            {
075                    String pluralNoun = singularNoun;
076    
077                    int nounLength = pluralNoun.length();
078    
079                    if (nounLength == 1)
080                    {
081                            pluralNoun = pluralNoun + 's';
082                    } else if (nounLength > 1)
083                    {
084                            char secondToLastChar = pluralNoun.charAt(nounLength - 2);
085    
086                            if (pluralNoun.endsWith("y"))
087                            {
088                                    switch (secondToLastChar)
089                                    {
090                                            case 'a': // fall-through
091                                            case 'e': // fall-through
092                                            case 'i': // fall-through
093                                            case 'o': // fall-through
094                                            case 'u':
095                                                    pluralNoun = pluralNoun + 's';
096                                                    break;
097                                            default:
098                                                    pluralNoun = pluralNoun.substring(0, nounLength - 1)
099                                                            + "ies";
100                                    }
101                            } else if (pluralNoun.endsWith("s"))
102                            {
103                                    switch (secondToLastChar)
104                                    {
105                                            case 's':
106                                                    pluralNoun = pluralNoun + "es";
107                                                    break;
108                                            default:
109                                                    pluralNoun = pluralNoun + "ses";
110                                    }
111                            } else
112                            {
113                                    pluralNoun = pluralNoun + 's';
114                            }
115                    }
116                    return pluralNoun;
117            }
118    
119            /**
120             * @param name
121             * @return the uncamelcased display friendly version of this
122             */
123            public static String unCamelCase(String name)
124            {
125                    if (name != null)
126                    {
127                            ArrayList<String> words = new ArrayList<String>();
128                            Perl5Util perl = new Perl5Util();
129                            while (perl.match("/(\\w+?)([A-Z].*)/", name))
130                            {
131                                    String word = perl.group(1);
132                                    name = perl.group(2);
133                                    words.add(StringUtils.capitalize(word));
134                            }
135                            words.add(StringUtils.capitalize(name));
136                            return StringUtils.join(words.iterator(), " ");
137                    }
138                    return null;
139            }
140    
141            /**
142             * @param type the (usable) super type if passed a CGLIB enhanced class
143             * @return
144             */
145            public static Class checkForCGLIB(Class type)
146            {
147                    if (type.getName().contains("CGLIB"))
148                    {
149                            return type.getSuperclass();
150                    } else return type;
151            }
152    
153            public static void executeOgnlExpression(String ognlExpression, Object member, Object model)
154            {
155                    HashMap context = new HashMap();
156                    context.put("member", member);
157    
158                    try
159                    {
160                            Ognl.getValue(ognlExpression + "(#member)", context, model);
161                    } catch (OgnlException e)
162                    {
163                            throw new TrailsRuntimeException(e, model.getClass());
164                    }
165            }
166    }