KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > model > JavaNameGenerator


1 package org.apache.torque.engine.database.model;
2
3 /*
4  * Copyright 2001-2002,2004 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 JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
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,v 1.4 2005/02/23 17:32:09 tfischer Exp $
30  */

31 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 JavaDoc generateName(List JavaDoc inputs)
51     {
52         String JavaDoc schemaName = (String JavaDoc) inputs.get(0);
53         String JavaDoc method = (String JavaDoc) inputs.get(1);
54         String JavaDoc javaName = null;
55
56         if (CONV_METHOD_UNDERSCORE.equals(method))
57         {
58             javaName = underscoreMethod(schemaName);
59         }
60         else if (CONV_METHOD_UNDERSCORE_OMIT_SCHEMA.equals(method))
61         {
62             javaName = underscoreOmitSchemaMethod(schemaName);
63         }
64         else if (CONV_METHOD_JAVANAME.equals(method))
65         {
66             javaName = javanameMethod(schemaName);
67         }
68         else if (CONV_METHOD_NOCHANGE.equals(method))
69         {
70             javaName = nochangeMethod(schemaName);
71         }
72         else
73         {
74             // if for some reason nothing is defined then we default
75
// to the traditional method.
76
javaName = underscoreMethod(schemaName);
77         }
78
79         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 JavaDoc underscoreMethod(String JavaDoc schemaName)
95     {
96         StringBuffer JavaDoc name = new StringBuffer JavaDoc();
97         
98         // remove the STD_SEPARATOR_CHARs and capitalize
99
// the tokens
100
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc
101             (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
102         while (tok.hasMoreTokens())
103         {
104             String JavaDoc namePart = ((String JavaDoc) tok.nextElement()).toLowerCase();
105             name.append(StringUtils.capitalize(namePart));
106         }
107         
108         // remove the SCHEMA_SEPARATOR_CHARs and capitalize
109
// the tokens
110
schemaName = name.toString();
111         name = new StringBuffer JavaDoc();
112         tok = new StringTokenizer JavaDoc
113             (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR));
114         while (tok.hasMoreTokens())
115         {
116             String JavaDoc namePart = (String JavaDoc) tok.nextElement();
117             name.append(StringUtils.capitalize(namePart));
118         }
119         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 JavaDoc underscoreOmitSchemaMethod(String JavaDoc schemaName)
136     {
137         // take only part after last dot
138
int lastDotPos = schemaName.lastIndexOf(SCHEMA_SEPARATOR_CHAR);
139         if (lastDotPos != -1) {
140             schemaName = schemaName.substring(lastDotPos + 1);
141         }
142         StringBuffer JavaDoc name = new StringBuffer JavaDoc();
143         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc
144             (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
145         while (tok.hasMoreTokens())
146         {
147             String JavaDoc namePart = ((String JavaDoc) tok.nextElement()).toLowerCase();
148             name.append(StringUtils.capitalize(namePart));
149         }
150         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 JavaDoc javanameMethod(String JavaDoc schemaName)
164     {
165         StringBuffer JavaDoc name = new StringBuffer JavaDoc();
166         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc
167             (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
168         while (tok.hasMoreTokens())
169         {
170             String JavaDoc namePart = (String JavaDoc) tok.nextElement();
171             name.append(StringUtils.capitalize(namePart));
172         }
173
174         // remove the SCHEMA_SEPARATOR_CHARs and capitalize
175
// the tokens
176
schemaName = name.toString();
177         name = new StringBuffer JavaDoc();
178         
179         tok = new StringTokenizer JavaDoc
180             (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR));
181         while (tok.hasMoreTokens())
182         {
183             String JavaDoc namePart = (String JavaDoc) tok.nextElement();
184             name.append(StringUtils.capitalize(namePart));
185         }
186         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 JavaDoc nochangeMethod(String JavaDoc name)
197     {
198         return name;
199     }
200 }
201
Popular Tags