01: package simpleorm.quickstart;
02:
03: /**
04: * This is the standard name conversion used by SimpleORMGenerator.
05: * Performs a sort camel case conversion.<p>
06: *
07: * Examples<br>
08: * Table My_NEW_table" converts to "MyNEWTable".<br>
09: * Field My_NEW_field" converts to "fldMyNEWField"<br>
10: * ForeignKey "My_NEW_F_Key" converts to refMyNewFKey"<br>
11: *
12: * @author <a href="mailto:richard.schmidt@inform6.com">Richard Schmidt</a>
13: */
14: public class DefaultFormatter implements INiceNameFormatter {
15: /**
16: * To create a reasonable class name from the table name,
17: * capitalize first character and convert a_b or a-b to aB
18: */
19: protected String niceName(String _name, boolean capFirst) {
20: String name = _name.toLowerCase();
21:
22: StringBuffer newName = new StringBuffer(name.length());
23: boolean capNextChar = capFirst;
24: char nextChar;
25:
26: for (int i = 0; i < name.length(); i++) {
27: nextChar = name.charAt(i);
28:
29: if (i == 0) {
30: nextChar = Character.toLowerCase(nextChar);
31: }
32:
33: if ((nextChar == '-') || (nextChar == '_')) {
34: capNextChar = true;
35:
36: continue;
37: }
38:
39: // skip junk characters
40: if (!Character.isJavaIdentifierPart(nextChar)) {
41: continue;
42: }
43:
44: if (capNextChar) {
45: nextChar = Character.toUpperCase(nextChar);
46: }
47:
48: newName.append(nextChar);
49: capNextChar = false;
50: }
51:
52: // check to see if a java reserved word. If so, add _ at end
53: name = newName.toString() + " ";
54:
55: if ("abstract byte byvalue cast catch case const implements extends native return super volatile synchronized this throw throws try import instanceof default switch do while class true false if continue break private public protected return char boolean float int double short long void static for exception else finally final finalize new package "
56: .indexOf(name) >= 0) {
57: newName.append('_');
58: }
59:
60: return newName.toString();
61: }
62:
63: /**
64: * Return the table name camel cased.
65: * @see simpleorm.quickstart.INiceName#niceNameForTable(Table)
66: */
67: public String niceNameForTable(String table) {
68: return niceName(table, true);
69: }
70:
71: /**
72: * Return the field name camel case with "fld" as a prefix.
73: * @see simpleorm.quickstart.INiceName#niceNameForColumn(Column)
74: */
75: public String niceNameForColumn(String table, String column) {
76: return "fld" + niceName(column, true);
77: }
78:
79: /**
80: * Return the foreign key table camel cased with "ref" as a prefix.
81: * @see simpleorm.quickstart.INiceName#niceNameForColumn(Column)
82: */
83: public String niceNameForForeignKey(String localTable,
84: String foreignTable) {
85: return "ref" + niceName(foreignTable, true);
86: }
87: }
|