View Javadoc

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