1 package org.apache.torque.engine.database.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23 import java.util.StringTokenizer;
24
25 import org.apache.commons.lang.StringUtils;
26
27 /***
28 * A <code>NameGenerator</code> implementation for Java-esque names.
29 *
30 * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a>
31 * @author <a href="mailto:byron_foster@byron_foster@yahoo.com>Byron Foster</a>
32 * @version $Id: JavaNameGenerator.java 473814 2006-11-11 22:30:30Z tv $
33 */
34 public class JavaNameGenerator implements NameGenerator
35 {
36 /***
37 * <code>inputs</code> should consist of two elements, the
38 * original name of the database element and the method for
39 * generating the name. There are currently three methods:
40 * <code>CONV_METHOD_NOCHANGE</code> - xml names are converted
41 * directly to java names without modification.
42 * <code>CONV_METHOD_UNDERSCORE</code> will capitalize the first
43 * letter, remove underscores, and capitalize each letter before
44 * an underscore. All other letters are lowercased. "javaname"
45 * works the same as the <code>CONV_METHOD_JAVANAME</code> method
46 * but will not lowercase any characters.
47 *
48 * @param inputs list expected to contain two parameters, element
49 * 0 contains name to convert, element 1 contains method for conversion.
50 * @return The generated name.
51 * @see org.apache.torque.engine.database.model.NameGenerator
52 */
53 public String generateName(List inputs)
54 {
55 String schemaName = (String) inputs.get(0);
56 String method = (String) inputs.get(1);
57 String javaName = null;
58
59 if (CONV_METHOD_UNDERSCORE.equals(method))
60 {
61 javaName = underscoreMethod(schemaName);
62 }
63 else if (CONV_METHOD_UNDERSCORE_OMIT_SCHEMA.equals(method))
64 {
65 javaName = underscoreOmitSchemaMethod(schemaName);
66 }
67 else if (CONV_METHOD_JAVANAME.equals(method))
68 {
69 javaName = javanameMethod(schemaName);
70 }
71 else if (CONV_METHOD_NOCHANGE.equals(method))
72 {
73 javaName = nochangeMethod(schemaName);
74 }
75 else
76 {
77
78
79 javaName = underscoreMethod(schemaName);
80 }
81
82 return javaName;
83 }
84
85 /***
86 * Converts a database schema name to java object name. Removes
87 * <code>STD_SEPARATOR_CHAR</code> and <code>SCHEMA_SEPARATOR_CHAR</code>,
88 * capitilizes first letter of name and each letter after the
89 * <code>STD_SEPERATOR</code> and <code>SCHEMA_SEPARATOR_CHAR</code>,
90 * converts the rest of the letters to lowercase.
91 *
92 * @param schemaName name to be converted.
93 * @return converted name.
94 * @see org.apache.torque.engine.database.model.NameGenerator
95 * @see #underscoreMethod(String)
96 */
97 protected String underscoreMethod(String schemaName)
98 {
99 StringBuffer name = new StringBuffer();
100
101
102
103 StringTokenizer tok = new StringTokenizer
104 (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
105 while (tok.hasMoreTokens())
106 {
107 String namePart = ((String) tok.nextElement()).toLowerCase();
108 name.append(StringUtils.capitalize(namePart));
109 }
110
111
112
113 schemaName = name.toString();
114 name = new StringBuffer();
115 tok = new StringTokenizer
116 (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR));
117 while (tok.hasMoreTokens())
118 {
119 String namePart = (String) tok.nextElement();
120 name.append(StringUtils.capitalize(namePart));
121 }
122 return name.toString();
123 }
124
125 /***
126 * Converts a database schema name to java object name.
127 * First, it removes all characters before the last occurence of
128 * .<code>SCHEMA_SEPARATOR_CHAR</code>. Then, in a second step, removes
129 * <code>STD_SEPARATOR_CHAR</code>, capitilizes first letter of
130 * name and each letter after the <code>STD_SEPERATOR</code>,
131 * and converts the rest of the letters to lowercase.
132 *
133 * @param schemaName name to be converted.
134 * @return converted name.
135 * @see org.apache.torque.engine.database.model.NameGenerator
136 * @see #underscoreOmitSchemaMethod(String)
137 */
138 protected String underscoreOmitSchemaMethod(String schemaName)
139 {
140
141 int lastDotPos = schemaName.lastIndexOf(SCHEMA_SEPARATOR_CHAR);
142 if (lastDotPos != -1)
143 {
144 schemaName = schemaName.substring(lastDotPos + 1);
145 }
146 StringBuffer name = new StringBuffer();
147 StringTokenizer tok = new StringTokenizer
148 (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
149 while (tok.hasMoreTokens())
150 {
151 String namePart = ((String) tok.nextElement()).toLowerCase();
152 name.append(StringUtils.capitalize(namePart));
153 }
154 return name.toString();
155 }
156
157 /***
158 * Converts a database schema name to java object name. Operates
159 * same as underscoreMethod but does not convert anything to
160 * lowercase.
161 *
162 * @param schemaName name to be converted.
163 * @return converted name.
164 * @see org.apache.torque.engine.database.model.NameGenerator
165 * @see #underscoreMethod(String)
166 */
167 protected String javanameMethod(String schemaName)
168 {
169 StringBuffer name = new StringBuffer();
170 StringTokenizer tok = new StringTokenizer
171 (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
172 while (tok.hasMoreTokens())
173 {
174 String namePart = (String) tok.nextElement();
175 name.append(StringUtils.capitalize(namePart));
176 }
177
178
179
180 schemaName = name.toString();
181 name = new StringBuffer();
182
183 tok = new StringTokenizer
184 (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR));
185 while (tok.hasMoreTokens())
186 {
187 String namePart = (String) tok.nextElement();
188 name.append(StringUtils.capitalize(namePart));
189 }
190 return name.toString();
191 }
192
193 /***
194 * Converts a database schema name to java object name. In this
195 * case no conversion is made.
196 *
197 * @param name name to be converted.
198 * @return The <code>name</code> parameter, unchanged.
199 */
200 protected final String nochangeMethod(String name)
201 {
202 return name;
203 }
204 }