View Javadoc

1   package org.andromda.core.common;
2   
3   import java.text.CharacterIterator;
4   import java.text.StringCharacterIterator;
5   
6   /***
7    * A utility object for doing string manipulation operations that are commonly 
8    * needed by the code generation templates.
9    * 
10   * @author Matthias Bohlen
11   * @author Chris Shaw
12   */
13  public class StringUtilsHelper {
14      
15      /***
16       * <p>Capitalizes a string.  That is, it returns "Hamburger" when
17       * eating a "hamburger".</p>
18       *
19       * @deprecated - use upperCaseFirstLetter
20       *
21       * @param s the input string
22       * @return String the output string
23       */
24      public static String capitalize(String s) {
25          return s.substring(0,1).toUpperCase() + s.substring(1);
26      }
27  
28      /*** 
29      * <p>Capitalizes a string. That is, it returns "HamburgerStall" 
30      * when receiving a "hamburgerStall".</p> 
31      * 
32      * @param s the input string 
33      * @return String the output string. 
34      */
35      public static String upperCaseFirstLetter(String s) {
36          if (s != null && s.length() > 0) {
37              return s.substring(0, 1).toUpperCase() + s.substring(1);
38          } else {
39              return s;
40          }
41      }
42  
43      /*** 
44      * <p>Removes the capitalization of a string. That is, it returns 
45      * "hamburgerStall" when receiving a "HamburgerStall".</p> 
46      * 
47      * @param s the input string 
48      * @return String the output string. 
49      */
50      public static String lowerCaseFirstLetter(String s) {
51          if (s != null && s.length() > 0) {
52              return s.substring(0, 1).toLowerCase() + s.substring(1);
53          } else {
54              return s;
55          }
56      }
57  
58  
59      /***
60       * <p>Converts a string following the Java naming conventions to a
61       * database attribute name.  For example convert customerName to
62       * CUSTOMER_NAME.</p>
63       *
64       * @param s string to convert
65       * @param separator character used to separate words     
66       * @return string converted to database attribute format
67       */
68      public static String toDatabaseAttributeName(String s, String separator) {
69          StringBuffer databaseAttributeName = new StringBuffer();
70          StringCharacterIterator iter = new StringCharacterIterator(
71                  lowerCaseFirstLetter(s));
72          
73          for (char character = iter.first(); character != CharacterIterator.DONE; 
74                  character = iter.next()) {
75  
76              if (Character.isUpperCase(character)) {
77                  databaseAttributeName.append(separator);
78              }
79              
80              character = Character.toUpperCase(character);                
81              databaseAttributeName.append(character);
82          }
83          
84          return databaseAttributeName.toString();
85      }
86      
87      /***
88       * <p>Returns a consistent name for a relation, independent from
89       * the end of the relation one is looking at.</p>
90       *
91       * <p>In order to guarantee consistency with relation names, they
92       * must appear the same whichever angle (ie entity) that you come
93       * from.  For example, if you are at Customer end of a
94       * relationship to an Address then your relation may appear with
95       * the name Customer-Address.  But if you are in the Address
96       * entity looking at the Customer then you will get an error
97       * because the relation will be called Address-Customer.  A simple
98       * way to guarantee that both ends of the relationship have the
99       * same name is merely to use alphabetical ordering.</p>
100      *
101      * @param roleName       name of role in relation
102      * @param targetRoleName name of target role in relation
103      * @param separator      character used to separate words
104      * @return uniform mapping name (in alphabetical order)
105      */
106     public static String toRelationName(String roleName, String targetRoleName, 
107             String separator) {
108         if (roleName.compareTo(targetRoleName) <= 0) {
109             return (roleName + separator + targetRoleName);
110         }
111         
112         return (targetRoleName + separator + roleName);        
113     }
114 
115     /***
116      * <p>Replaces a given suffix of the source string with a new one.
117      * If the suffix isn't present, the string is returned
118      * unmodified.</p>
119      *
120      * @param src the <code>String</code> for which the suffix should be replaced
121      * @param suffixOld a <code>String</code> with the suffix that should be replaced
122      * @param suffixNew a <code>String</code> with the new suffix
123      * @return a <code>String</code> with the given suffix replaced or
124      *         unmodified if the suffix isn't present
125      */
126     public static String replaceSuffix(String src, String suffixOld, String suffixNew) {
127         if (src.endsWith(suffixOld)) {
128             return src.substring(0, src.length()-suffixOld.length())+suffixNew;
129         }
130 
131         return src;
132     }
133 
134     /***
135      * <p>Checks if a given type name is a Java primitive type.</p>
136      *
137      * @param name a <code>String</code> with the name of the type
138      * @return <code>true</code> if <code>name</code> is a Java
139      *         primitive type; <code>false</code> if not
140      */
141     public static boolean isPrimitiveType(String name) {
142         return (   "void".equals(name)
143                 || "char".equals(name)
144                 || "byte".equals(name)
145                 || "short".equals(name)
146                 || "int".equals(name)
147                 || "long".equals(name)
148                 || "float".equals(name)
149                 || "double".equals(name)
150                 || "boolean".equals(name) );
151     }
152 
153     /***
154      * <p>Returns the type class name for a Java primitive.</p>
155      *
156      * @param name a <code>String</code> with the name of the type
157      * @return a <code>String</code> with the name of the
158      *         corresponding java.lang wrapper class if
159      *         <code>name</code> is a Java primitive type;
160      *         <code>false</code> if not
161      */
162     public static String getPrimitiveClassName(String name) {
163         if (!isPrimitiveType(name)) {
164             return null;
165         }
166 
167         if ("void".equals(name)) {
168             return null;
169         }
170         if ("char".equals(name)) {
171             return "java.lang.Character";
172         }
173         if ("int".equals(name)) {
174             return "java.lang.Integer";
175         }
176 
177         return "java.lang."+upperCaseFirstLetter(name);
178     }
179 
180 }