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