%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.engine.database.model.JavaNameGenerator |
|
|
1 | package org.apache.torque.engine.database.model; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.List; |
|
20 | import java.util.StringTokenizer; |
|
21 | ||
22 | import org.apache.commons.lang.StringUtils; |
|
23 | ||
24 | /** |
|
25 | * A <code>NameGenerator</code> implementation for Java-esque names. |
|
26 | * |
|
27 | * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a> |
|
28 | * @author <a href="mailto:byron_foster@byron_foster@yahoo.com>Byron Foster</a> |
|
29 | * @version $Id: JavaNameGenerator.java 239626 2005-08-24 12:19:51Z henning $ |
|
30 | */ |
|
31 | 9 | public class JavaNameGenerator implements NameGenerator |
32 | { |
|
33 | /** |
|
34 | * <code>inputs</code> should consist of two elements, the |
|
35 | * original name of the database element and the method for |
|
36 | * generating the name. There are currently three methods: |
|
37 | * <code>CONV_METHOD_NOCHANGE</code> - xml names are converted |
|
38 | * directly to java names without modification. |
|
39 | * <code>CONV_METHOD_UNDERSCORE</code> will capitalize the first |
|
40 | * letter, remove underscores, and capitalize each letter before |
|
41 | * an underscore. All other letters are lowercased. "javaname" |
|
42 | * works the same as the <code>CONV_METHOD_JAVANAME</code> method |
|
43 | * but will not lowercase any characters. |
|
44 | * |
|
45 | * @param inputs list expected to contain two parameters, element |
|
46 | * 0 contains name to convert, element 1 contains method for conversion. |
|
47 | * @return The generated name. |
|
48 | * @see org.apache.torque.engine.database.model.NameGenerator |
|
49 | */ |
|
50 | public String generateName(List inputs) |
|
51 | { |
|
52 | 2024 | String schemaName = (String) inputs.get(0); |
53 | 2024 | String method = (String) inputs.get(1); |
54 | 2024 | String javaName = null; |
55 | ||
56 | 2024 | if (CONV_METHOD_UNDERSCORE.equals(method)) |
57 | { |
|
58 | 2015 | javaName = underscoreMethod(schemaName); |
59 | } |
|
60 | 9 | else if (CONV_METHOD_UNDERSCORE_OMIT_SCHEMA.equals(method)) |
61 | { |
|
62 | 2 | javaName = underscoreOmitSchemaMethod(schemaName); |
63 | } |
|
64 | 7 | else if (CONV_METHOD_JAVANAME.equals(method)) |
65 | { |
|
66 | 2 | javaName = javanameMethod(schemaName); |
67 | } |
|
68 | 5 | else if (CONV_METHOD_NOCHANGE.equals(method)) |
69 | { |
|
70 | 2 | javaName = nochangeMethod(schemaName); |
71 | } |
|
72 | else |
|
73 | { |
|
74 | // if for some reason nothing is defined then we default |
|
75 | // to the traditional method. |
|
76 | 3 | javaName = underscoreMethod(schemaName); |
77 | } |
|
78 | ||
79 | 2024 | return javaName; |
80 | } |
|
81 | ||
82 | /** |
|
83 | * Converts a database schema name to java object name. Removes |
|
84 | * <code>STD_SEPARATOR_CHAR</code> and <code>SCHEMA_SEPARATOR_CHAR</code>, |
|
85 | * capitilizes first letter of name and each letter after the |
|
86 | * <code>STD_SEPERATOR</code> and <code>SCHEMA_SEPARATOR_CHAR</code>, |
|
87 | * converts the rest of the letters to lowercase. |
|
88 | * |
|
89 | * @param schemaName name to be converted. |
|
90 | * @return converted name. |
|
91 | * @see org.apache.torque.engine.database.model.NameGenerator |
|
92 | * @see #underscoreMethod(String) |
|
93 | */ |
|
94 | protected String underscoreMethod(String schemaName) |
|
95 | { |
|
96 | 2018 | StringBuffer name = new StringBuffer(); |
97 | ||
98 | // remove the STD_SEPARATOR_CHARs and capitalize |
|
99 | // the tokens |
|
100 | 2018 | StringTokenizer tok = new StringTokenizer |
101 | (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); |
|
102 | 4463 | while (tok.hasMoreTokens()) |
103 | { |
|
104 | 2445 | String namePart = ((String) tok.nextElement()).toLowerCase(); |
105 | 2445 | name.append(StringUtils.capitalize(namePart)); |
106 | } |
|
107 | ||
108 | // remove the SCHEMA_SEPARATOR_CHARs and capitalize |
|
109 | // the tokens |
|
110 | 2018 | schemaName = name.toString(); |
111 | 2018 | name = new StringBuffer(); |
112 | 2018 | tok = new StringTokenizer |
113 | (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR)); |
|
114 | 4037 | while (tok.hasMoreTokens()) |
115 | { |
|
116 | 2019 | String namePart = (String) tok.nextElement(); |
117 | 2019 | name.append(StringUtils.capitalize(namePart)); |
118 | } |
|
119 | 2018 | return name.toString(); |
120 | } |
|
121 | ||
122 | /** |
|
123 | * Converts a database schema name to java object name. |
|
124 | * First, it removes all characters before the last occurence of |
|
125 | * .<code>SCHEMA_SEPARATOR_CHAR</code>. Then, in a second step, removes |
|
126 | * <code>STD_SEPARATOR_CHAR</code>, capitilizes first letter of |
|
127 | * name and each letter after the <code>STD_SEPERATOR</code>, |
|
128 | * and converts the rest of the letters to lowercase. |
|
129 | * |
|
130 | * @param schemaName name to be converted. |
|
131 | * @return converted name. |
|
132 | * @see org.apache.torque.engine.database.model.NameGenerator |
|
133 | * @see #underscoreOmitSchemaMethod(String) |
|
134 | */ |
|
135 | protected String underscoreOmitSchemaMethod(String schemaName) |
|
136 | { |
|
137 | // take only part after last dot |
|
138 | 2 | int lastDotPos = schemaName.lastIndexOf(SCHEMA_SEPARATOR_CHAR); |
139 | 2 | if (lastDotPos != -1) { |
140 | 1 | schemaName = schemaName.substring(lastDotPos + 1); |
141 | } |
|
142 | 2 | StringBuffer name = new StringBuffer(); |
143 | 2 | StringTokenizer tok = new StringTokenizer |
144 | (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); |
|
145 | 6 | while (tok.hasMoreTokens()) |
146 | { |
|
147 | 4 | String namePart = ((String) tok.nextElement()).toLowerCase(); |
148 | 4 | name.append(StringUtils.capitalize(namePart)); |
149 | } |
|
150 | 2 | return name.toString(); |
151 | } |
|
152 | ||
153 | /** |
|
154 | * Converts a database schema name to java object name. Operates |
|
155 | * same as underscoreMethod but does not convert anything to |
|
156 | * lowercase. |
|
157 | * |
|
158 | * @param schemaName name to be converted. |
|
159 | * @return converted name. |
|
160 | * @see org.apache.torque.engine.database.model.NameGenerator |
|
161 | * @see #underscoreMethod(String) |
|
162 | */ |
|
163 | protected String javanameMethod(String schemaName) |
|
164 | { |
|
165 | 2 | StringBuffer name = new StringBuffer(); |
166 | 2 | StringTokenizer tok = new StringTokenizer |
167 | (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); |
|
168 | 7 | while (tok.hasMoreTokens()) |
169 | { |
|
170 | 5 | String namePart = (String) tok.nextElement(); |
171 | 5 | name.append(StringUtils.capitalize(namePart)); |
172 | } |
|
173 | ||
174 | // remove the SCHEMA_SEPARATOR_CHARs and capitalize |
|
175 | // the tokens |
|
176 | 2 | schemaName = name.toString(); |
177 | 2 | name = new StringBuffer(); |
178 | ||
179 | 2 | tok = new StringTokenizer |
180 | (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR)); |
|
181 | 5 | while (tok.hasMoreTokens()) |
182 | { |
|
183 | 3 | String namePart = (String) tok.nextElement(); |
184 | 3 | name.append(StringUtils.capitalize(namePart)); |
185 | } |
|
186 | 2 | return name.toString(); |
187 | } |
|
188 | ||
189 | /** |
|
190 | * Converts a database schema name to java object name. In this |
|
191 | * case no conversion is made. |
|
192 | * |
|
193 | * @param name name to be converted. |
|
194 | * @return The <code>name</code> parameter, unchanged. |
|
195 | */ |
|
196 | protected final String nochangeMethod(String name) |
|
197 | { |
|
198 | 2 | return name; |
199 | } |
|
200 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |