1 package org.ocltf.translation;
2
3 import org.apache.commons.beanutils.PropertyUtils;
4 import org.apache.commons.lang.ObjectUtils;
5 import org.apache.commons.lang.StringUtils;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.ocltf.model.AssociationEndFacade;
9 import org.ocltf.model.ElementFacade;
10 import org.ocltf.utils.ExceptionUtils;
11
12 /***
13 * Contains translation utilities.
14 *
15 * @author Chad Brandon
16 */
17 public class TranslationUtils {
18
19 private static Log logger = LogFactory.getLog(TranslationUtils.class);
20
21 /***
22 * <p><code>TranslationUtils</code> instances should NOT be constructed in
23 * standard programming. Instead, the class should be used as
24 * <code>TranslationUtils.replacePattern(" some pattern ");</code>.</p>
25 *
26 * <p>This constructor is public to permit tools that require a JavaBean
27 * instance to operate.</p>
28 */
29 public TranslationUtils() {}
30
31 /***
32 * Searches for and replaces the specified pattern
33 * with braces around it, like so --> "{pattern}" every time it
34 * occurs in the string.
35 *
36 * @param string the string to to perform replacement on.
37 * @param pattern the pattern to find
38 * @param replaceWith the pattern to place the existing one with.
39 * @return String the string will all replacements
40 */
41 public static String replacePattern(String string, String pattern, String replaceWith) {
42 String methodName = "replacePattern";
43 if (string != null) {
44 ExceptionUtils.checkNull(methodName, "pattern", pattern);
45 ExceptionUtils.checkNull(methodName, "replaceWith", replaceWith);
46 string = StringUtils.replace(string, "{" + pattern + "}", replaceWith);
47 }
48 return string;
49 }
50
51 /***
52 * Searches for and replaces the specified pattern
53 * with braces around it, like so --> "{pattern}" the first time
54 * it occurs in the string
55 *
56 * @param string the string to to perform replacement on.
57 * @param pattern the pattern to find
58 * @param replaceWith the pattern to place the existing one with.
59 * @return String the string will all replacements
60 */
61 public static String replaceFirstPattern(String string, String pattern, String replaceWith) {
62 String methodName = "replacePattern";
63 if (string != null) {
64 ExceptionUtils.checkNull(methodName, "pattern", pattern);
65 ExceptionUtils.checkNull(methodName, "replaceWith", replaceWith);
66 string = StringUtils.replaceOnce(string, "{" + pattern + "}", replaceWith);
67 }
68 return string;
69 }
70
71 /***
72 * Returns true if the specified pattern with braces around it,
73 * like so --> "{pattern}" exists in the string.
74 *
75 * @param string the string to to perform replacement on.
76 * @param pattern the pattern to find
77 * @return boolean true if the string contains the pattern, false otherwise
78 */
79 public static boolean containsPattern(String string, String pattern) {
80 boolean containsPattern = string != null && pattern != null;
81 if (containsPattern) {
82 containsPattern = StringUtils.contains(string, "{" + pattern + "}");
83 }
84 return containsPattern;
85 }
86
87 /***
88 * Calls the object's toString method and trims the value.
89 * Returns and empty string if null is given.
90 *
91 * @param object the object to use.
92 * @return String
93 */
94 public static String trimToEmpty(Object object) {
95 return StringUtils.trimToEmpty(ObjectUtils.toString(object));
96 }
97
98 /***
99 * Calls the object's toString method and deletes
100 * any whitespace from the value.
101 * Returns and empty string if null is given.
102 *
103 * @param object the object to deleteWhite space from.
104 * @return String
105 */
106 public static String deleteWhitespace(Object object) {
107 return StringUtils.deleteWhitespace(ObjectUtils.toString(object));
108 }
109
110 /***
111 * Retrieves the "starting" property name from one
112 * that is nested, for example, will return '<name1>'
113 * from the string --> <name1>.<name2>.<name3>. If the property
114 * isn't nested, then just return the name that is passed in.
115 *
116 * @param property the property.
117 * @return String
118 */
119 public static String getStartingProperty(String property) {
120 StringUtils.trimToEmpty(property);
121 int dotIndex = property.indexOf('.');
122 if (dotIndex != -1) {
123 property = property.substring(0, dotIndex);
124 }
125 return property;
126 }
127
128 /***
129 * Removes any extra whitepace --> does not remove the
130 * spaces between the words. Only removes
131 * tabs and newline characters. This is to allow everything
132 * to be on one line while keeping the spaces between words.
133 * @param string
134 * @return String the string with the removed extra spaces.
135 */
136 public static String removeExtraWhitespace(String string) {
137
138 string = StringUtils.trimToEmpty(string);
139
140
141 string = string.replaceAll("//s{2,}", " ");
142
143 return string;
144 }
145
146 /***
147 * Just retriieves properties from a bean, but gives
148 * a more informational error when the property can't
149 * be retrieved, it also cleans the resulting property
150 * from any excess white space
151 * @param bean the bean from which to retrieve the property
152 * @param property the property name
153 * @return Object the value of the property
154 */
155 public static Object getProperty(Object bean, String property) {
156 String methodName = "getProperty";
157 try {
158 Object value = PropertyUtils.getProperty(bean, property);
159 return value;
160 }catch(Exception ex) {
161 String errMsg = "Error performing " + methodName
162 + " with bean (" + bean + ") and property (" + property + ")";
163 logger.error(errMsg, ex);
164 throw new TranslatorException(errMsg, ex);
165 }
166 }
167
168 /***
169 * Just retriieves properties from a bean, but gives
170 * a more informational error when the property can't
171 * be retrieved, it also cleans the resulting property
172 * from any excess white space
173 * @param bean the bean from which to retrieve the property
174 * @param property the property name
175 * @return Object the value of the property
176 */
177 public static String getPropertyAsString(Object bean, String property) {
178 return TranslationUtils.trimToEmpty(
179 TranslationUtils.getProperty(bean, property));
180 }
181
182 /***
183 * Returns true if the passed in <code>name</code> is a connecting
184 * end and its multiplicity is many.
185 * @param element the ElementFacade to check.
186 * @param name the name of the association.
187 * @return boolean true/false
188 */
189 public static boolean isManyConnectingAssociationEnd(ElementFacade element, String name) {
190 String methodName = "isConnectingEnd";
191 ExceptionUtils.checkNull(methodName, "element", element);
192 ExceptionUtils.checkEmpty(methodName, "name", name);
193 AssociationEndFacade associationEnd =
194 element.getType().findConnectingAssociationEnd(
195 StringUtils.deleteWhitespace(name));
196 return associationEnd != null && associationEnd.isMany();
197 }
198
199 }