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