View Javadoc
1   package org.andromda.schema2xmi;
2   
3   import org.apache.commons.lang.StringUtils;
4   
5   /**
6    * Provides formatting functions, when converting SQL names to model names.
7    *
8    * @author Chad Brandon
9    */
10  public class SqlToModelNameFormatter
11  {
12      /**
13       * Converts a table name to an class name.
14       *
15       * @param name the name of the table.
16       * @return the new class name.
17       */
18      public static String toClassName(String name)
19      {
20          return toCamelCase(name);
21      }
22  
23      /**
24       * Converts a column name to an attribute name.
25       *
26       * @param name the name of the column
27       * @return the new attribute name.
28       */
29      public static String toAttributeName(String name)
30      {
31          return StringUtils.uncapitalize(toClassName(name));
32      }
33  
34      /**
35       * Turns a table name into a model element class name.
36       *
37       * @param name the table name.
38       * @return the new class name.
39       */
40      public static String toCamelCase(String name)
41      {
42          StringBuilder buffer = new StringBuilder();
43          String[] tokens = name.split("_|\\s+");
44          if (tokens != null && tokens.length > 0)
45          {
46              for (int ctr = 0; ctr < tokens.length; ctr++)
47              {
48                  buffer.append(StringUtils.capitalize(tokens[ctr].toLowerCase()));
49              }
50          }
51          else
52          {
53              buffer.append(StringUtils.capitalize(name.toLowerCase()));
54          }
55          return buffer.toString();
56      }
57  }