| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Utils |
|
| 0.0;0 |
| 1 | /* |
|
| 2 | * Copyright 2004 Chris Nelson |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
|
| 7 | * Unless required by applicable law or agreed to in writing, |
|
| 8 | * software distributed under the License is distributed on an "AS IS" BASIS, |
|
| 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 10 | * See the License for the specific language governing permissions and limitations under the License. |
|
| 11 | */ |
|
| 12 | package org.trails.util; |
|
| 13 | ||
| 14 | import ognl.Ognl; |
|
| 15 | import ognl.OgnlException; |
|
| 16 | import org.apache.commons.lang.StringUtils; |
|
| 17 | import org.apache.oro.text.perl.Perl5Util; |
|
| 18 | import org.trails.exception.TrailsRuntimeException; |
|
| 19 | ||
| 20 | import java.util.ArrayList; |
|
| 21 | import java.util.HashMap; |
|
| 22 | ||
| 23 | ||
| 24 | /** |
|
| 25 | * @author fus8882 |
|
| 26 | */ |
|
| 27 | 4 | public class Utils |
| 28 | { |
|
| 29 | ||
| 30 | /* key used to internationalize Trails in Tapestry components and pages */ |
|
| 31 | public static final String APPLY_MESSAGE = "org.trails.i18n.apply"; |
|
| 32 | public static final String APPLY_AND_RETURN_MESSAGE = "org.trails.i18n.applyAndReturn"; |
|
| 33 | public static final String REMOVE_MESSAGE = "org.trails.i18n.remove"; |
|
| 34 | public static final String CANCEL_MESSAGE = "org.trails.i18n.cancel"; |
|
| 35 | public static final String ADD_NEW_MESSAGE = "org.trails.i18n.addNew"; |
|
| 36 | 4 | public static String DEFAULT = "Default"; |
| 37 | ||
| 38 | public static Class classForName(String className) |
|
| 39 | { |
|
| 40 | try |
|
| 41 | { |
|
| 42 | 16 | return Class.forName(className); |
| 43 | 0 | } catch (ClassNotFoundException e) |
| 44 | { |
|
| 45 | 0 | throw new TrailsRuntimeException(e, null); |
| 46 | } |
|
| 47 | } |
|
| 48 | ||
| 49 | public static String unqualify(String className) |
|
| 50 | { |
|
| 51 | 8 | return className.substring(className.lastIndexOf(".") + 1); |
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Thank you, AndroMDA project... |
|
| 56 | * Linguistically pluralizes a singular noun. <p/> |
|
| 57 | * <ul> |
|
| 58 | * <li><code>noun</code> becomes <code>nouns</code></li> |
|
| 59 | * <li><code>key</code> becomes <code>keys</code></li> |
|
| 60 | * <li><code>word</code> becomes <code>words</code></li> |
|
| 61 | * <li><code>property</code> becomes <code>properties</code></li> |
|
| 62 | * <li><code>bus</code> becomes <code>busses</code></li> |
|
| 63 | * <li><code>boss</code> becomes <code>bosses</code></li> |
|
| 64 | * </ul> |
|
| 65 | * <p/> |
|
| 66 | * Whitespace as well as <code>null></code> arguments will return an empty |
|
| 67 | * String. |
|
| 68 | * </p> |
|
| 69 | * |
|
| 70 | * @param singularNoun A singularNoun to pluralize |
|
| 71 | * @return The plural of the argument singularNoun |
|
| 72 | */ |
|
| 73 | public static String pluralize(String singularNoun) |
|
| 74 | { |
|
| 75 | 64 | String pluralNoun = singularNoun; |
| 76 | ||
| 77 | 64 | int nounLength = pluralNoun.length(); |
| 78 | ||
| 79 | 64 | if (nounLength == 1) |
| 80 | { |
|
| 81 | 0 | pluralNoun = pluralNoun + 's'; |
| 82 | 64 | } else if (nounLength > 1) |
| 83 | { |
|
| 84 | 64 | char secondToLastChar = pluralNoun.charAt(nounLength - 2); |
| 85 | ||
| 86 | 64 | if (pluralNoun.endsWith("y")) |
| 87 | { |
|
| 88 | 8 | switch (secondToLastChar) |
| 89 | { |
|
| 90 | case 'a': // fall-through |
|
| 91 | case 'e': // fall-through |
|
| 92 | case 'i': // fall-through |
|
| 93 | case 'o': // fall-through |
|
| 94 | case 'u': |
|
| 95 | 4 | pluralNoun = pluralNoun + 's'; |
| 96 | 4 | break; |
| 97 | default: |
|
| 98 | 8 | pluralNoun = pluralNoun.substring(0, nounLength - 1) |
| 99 | 4 | + "ies"; |
| 100 | } |
|
| 101 | 56 | } else if (pluralNoun.endsWith("s")) |
| 102 | { |
|
| 103 | 4 | switch (secondToLastChar) |
| 104 | { |
|
| 105 | case 's': |
|
| 106 | 4 | pluralNoun = pluralNoun + "es"; |
| 107 | 4 | break; |
| 108 | default: |
|
| 109 | 0 | pluralNoun = pluralNoun + "ses"; |
| 110 | } |
|
| 111 | } else |
|
| 112 | { |
|
| 113 | 52 | pluralNoun = pluralNoun + 's'; |
| 114 | } |
|
| 115 | } |
|
| 116 | 64 | 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 | 4460 | if (name != null) |
| 126 | { |
|
| 127 | 4460 | ArrayList<String> words = new ArrayList<String>(); |
| 128 | 4460 | Perl5Util perl = new Perl5Util(); |
| 129 | 11808 | while (perl.match("/(\\w+?)([A-Z].*)/", name)) |
| 130 | { |
|
| 131 | 2888 | String word = perl.group(1); |
| 132 | 2888 | name = perl.group(2); |
| 133 | 2888 | words.add(StringUtils.capitalize(word)); |
| 134 | } |
|
| 135 | 4460 | words.add(StringUtils.capitalize(name)); |
| 136 | 4460 | return StringUtils.join(words.iterator(), " "); |
| 137 | } |
|
| 138 | 0 | 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 | 12 | if (type.getName().contains("CGLIB")) |
| 148 | { |
|
| 149 | 8 | return type.getSuperclass(); |
| 150 | 4 | } else return type; |
| 151 | } |
|
| 152 | ||
| 153 | public static void executeOgnlExpression(String ognlExpression, Object member, Object model) |
|
| 154 | { |
|
| 155 | 4 | HashMap context = new HashMap(); |
| 156 | 4 | context.put("member", member); |
| 157 | ||
| 158 | try |
|
| 159 | { |
|
| 160 | 4 | Ognl.getValue(ognlExpression + "(#member)", context, model); |
| 161 | 0 | } catch (OgnlException e) |
| 162 | { |
|
| 163 | 0 | throw new TrailsRuntimeException(e, model.getClass()); |
| 164 | } |
|
| 165 | 4 | } |
| 166 | } |