%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.engine.database.model.JavaNameGenerator |
|
|
1 | package org.apache.torque.engine.database.model; |
|
2 | ||
3 | /* |
|
4 | * Licensed to the Apache Software Foundation (ASF) under one |
|
5 | * or more contributor license agreements. See the NOTICE file |
|
6 | * distributed with this work for additional information |
|
7 | * regarding copyright ownership. The ASF licenses this file |
|
8 | * to you under the Apache License, Version 2.0 (the |
|
9 | * "License"); you may not use this file except in compliance |
|
10 | * with the License. You may obtain a copy of the License at |
|
11 | * |
|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
13 | * |
|
14 | * Unless required by applicable law or agreed to in writing, |
|
15 | * software distributed under the License is distributed on an |
|
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
17 | * KIND, either express or implied. See the License for the |
|
18 | * specific language governing permissions and limitations |
|
19 | * under the License. |
|
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 | 153 | 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 | 34408 | String schemaName = (String) inputs.get(0); |
56 | 34408 | String method = (String) inputs.get(1); |
57 | 34408 | String javaName = null; |
58 | ||
59 | 34408 | if (CONV_METHOD_UNDERSCORE.equals(method)) |
60 | { |
|
61 | 34255 | javaName = underscoreMethod(schemaName); |
62 | 34255 | } |
63 | 153 | else if (CONV_METHOD_UNDERSCORE_OMIT_SCHEMA.equals(method)) |
64 | { |
|
65 | 34 | javaName = underscoreOmitSchemaMethod(schemaName); |
66 | 34 | } |
67 | 119 | else if (CONV_METHOD_JAVANAME.equals(method)) |
68 | { |
|
69 | 34 | javaName = javanameMethod(schemaName); |
70 | 34 | } |
71 | 85 | else if (CONV_METHOD_NOCHANGE.equals(method)) |
72 | { |
|
73 | 34 | javaName = nochangeMethod(schemaName); |
74 | 34 | } |
75 | else |
|
76 | { |
|
77 | // if for some reason nothing is defined then we default |
|
78 | // to the traditional method. |
|
79 | 51 | javaName = underscoreMethod(schemaName); |
80 | } |
|
81 | ||
82 | 34408 | 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 | 34306 | StringBuffer name = new StringBuffer(); |
100 | ||
101 | // remove the STD_SEPARATOR_CHARs and capitalize |
|
102 | // the tokens |
|
103 | 34306 | StringTokenizer tok = new StringTokenizer |
104 | (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); |
|
105 | 75871 | while (tok.hasMoreTokens()) |
106 | { |
|
107 | 41565 | String namePart = ((String) tok.nextElement()).toLowerCase(); |
108 | 41565 | name.append(StringUtils.capitalize(namePart)); |
109 | 41565 | } |
110 | ||
111 | // remove the SCHEMA_SEPARATOR_CHARs and capitalize |
|
112 | // the tokens |
|
113 | 34306 | schemaName = name.toString(); |
114 | 34306 | name = new StringBuffer(); |
115 | 34306 | tok = new StringTokenizer |
116 | (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR)); |
|
117 | 68629 | while (tok.hasMoreTokens()) |
118 | { |
|
119 | 34323 | String namePart = (String) tok.nextElement(); |
120 | 34323 | name.append(StringUtils.capitalize(namePart)); |
121 | 34323 | } |
122 | 34306 | 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 | // take only part after last dot |
|
141 | 34 | int lastDotPos = schemaName.lastIndexOf(SCHEMA_SEPARATOR_CHAR); |
142 | 34 | if (lastDotPos != -1) |
143 | { |
|
144 | 17 | schemaName = schemaName.substring(lastDotPos + 1); |
145 | } |
|
146 | 34 | StringBuffer name = new StringBuffer(); |
147 | 34 | StringTokenizer tok = new StringTokenizer |
148 | (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); |
|
149 | 102 | while (tok.hasMoreTokens()) |
150 | { |
|
151 | 68 | String namePart = ((String) tok.nextElement()).toLowerCase(); |
152 | 68 | name.append(StringUtils.capitalize(namePart)); |
153 | 68 | } |
154 | 34 | 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 | 34 | StringBuffer name = new StringBuffer(); |
170 | 34 | StringTokenizer tok = new StringTokenizer |
171 | (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); |
|
172 | 119 | while (tok.hasMoreTokens()) |
173 | { |
|
174 | 85 | String namePart = (String) tok.nextElement(); |
175 | 85 | name.append(StringUtils.capitalize(namePart)); |
176 | 85 | } |
177 | ||
178 | // remove the SCHEMA_SEPARATOR_CHARs and capitalize |
|
179 | // the tokens |
|
180 | 34 | schemaName = name.toString(); |
181 | 34 | name = new StringBuffer(); |
182 | ||
183 | 34 | tok = new StringTokenizer |
184 | (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR)); |
|
185 | 85 | while (tok.hasMoreTokens()) |
186 | { |
|
187 | 51 | String namePart = (String) tok.nextElement(); |
188 | 51 | name.append(StringUtils.capitalize(namePart)); |
189 | 51 | } |
190 | 34 | 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 | 34 | return name; |
203 | } |
|
204 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |