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 final String LISTALL_LINK_MESSAGE = "org.tynamo.component.listalllink";
041
042 /**
043 * Thank you, AndroMDA project...
044 * Linguistically pluralizes a singular noun. <p/>
045 * <ul>
046 * <li><code>noun</code> becomes <code>nouns</code></li>
047 * <li><code>key</code> becomes <code>keys</code></li>
048 * <li><code>word</code> becomes <code>words</code></li>
049 * <li><code>property</code> becomes <code>properties</code></li>
050 * <li><code>bus</code> becomes <code>busses</code></li>
051 * <li><code>boss</code> becomes <code>bosses</code></li>
052 * </ul>
053 * <p/>
054 * Whitespace as well as <code>null></code> arguments will return an empty
055 * String.
056 * </p>
057 *
058 * @param singularNoun A singularNoun to pluralize
059 * @return The plural of the argument singularNoun
060 */
061 public static String pluralize(String singularNoun)
062 {
063 String pluralNoun = singularNoun;
064
065 int nounLength = pluralNoun.length();
066
067 if (nounLength == 1)
068 {
069 pluralNoun = pluralNoun + 's';
070 } else if (nounLength > 1)
071 {
072 char secondToLastChar = pluralNoun.charAt(nounLength - 2);
073
074 if (pluralNoun.endsWith("y"))
075 {
076 switch (secondToLastChar)
077 {
078 case 'a': // fall-through
079 case 'e': // fall-through
080 case 'i': // fall-through
081 case 'o': // fall-through
082 case 'u':
083 pluralNoun = pluralNoun + 's';
084 break;
085 default:
086 pluralNoun = pluralNoun.substring(0, nounLength - 1)
087 + "ies";
088 }
089 } else if (pluralNoun.endsWith("s"))
090 {
091 switch (secondToLastChar)
092 {
093 case 's':
094 pluralNoun = pluralNoun + "es";
095 break;
096 default:
097 pluralNoun = pluralNoun + "ses";
098 }
099 } else
100 {
101 pluralNoun = pluralNoun + 's';
102 }
103 }
104 return pluralNoun;
105 }
106
107 /**
108 * @param type the (usable) super type if passed a CGLIB enhanced class
109 * @return
110 */
111 public static Class checkForCGLIB(Class type)
112 {
113 if (type.getName().contains("CGLIB"))
114 {
115 return type.getSuperclass();
116 } else return type;
117 }
118
119 public static void executeOgnlExpression(String ognlExpression, Object member, Object model)
120 {
121 HashMap context = new HashMap();
122 context.put("member", member);
123
124 try
125 {
126 Ognl.getValue(ognlExpression + "(#member)", context, model);
127 } catch (OgnlException e)
128 {
129 throw new TynamoRuntimeException(e, model.getClass());
130 }
131 }
132
133 /**
134 * Tests whether or not a name matches against at least one exclude pattern.
135 *
136 * @param name The name to match. Must not be null.
137 * @param exclusionPatterns the list of exclude patterns to test against
138 * @return true when the name matches against at least one exclude pattern, or false otherwise.
139 */
140 public static boolean isExcluded(String name, Collection<String> exclusionPatterns)
141 {
142 for (String exclusionPattern : exclusionPatterns)
143 {
144 if (name.matches(exclusionPattern))
145 {
146 return true;
147 }
148 }
149
150 return false;
151 }
152 }