1 package org.apache.torque.engine.database.model;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.List;
58 import java.util.StringTokenizer;
59
60 import org.apache.commons.lang.StringUtils;
61
62 /***
63 * A <code>NameGenerator</code> implementation for Java-esque names.
64 *
65 * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a>
66 * @author <a href="mailto:byron_foster@byron_foster@yahoo.com>Byron Foster</a>
67 * @version $Id: JavaNameGenerator.java,v 1.1 2003/02/10 13:20:58 mpoeschl Exp $
68 */
69 public class JavaNameGenerator implements NameGenerator
70 {
71 /***
72 * <code>inputs</code> should consist of two elements, the
73 * original name of the database element and the method for
74 * generating the name. There are currently three methods:
75 * <code>CONV_METHOD_NOCHANGE</code> - xml names are converted
76 * directly to java names without modification.
77 * <code>CONV_METHOD_UNDERSCORE</code> will capitalize the first
78 * letter, remove underscores, and capitalize each letter before
79 * an underscore. All other letters are lowercased. "javaname"
80 * works the same as the <code>CONV_METHOD_JAVANAME</code> method
81 * but will not lowercase any characters.
82 *
83 * @param inputs list expected to contain two parameters, element
84 * 0 contains name to convert, element 1 contains method for conversion.
85 * @return The generated name.
86 * @see org.apache.torque.engine.database.model.NameGenerator
87 */
88 public String generateName(List inputs)
89 {
90 String schemaName = (String) inputs.get(0);
91 String method = (String) inputs.get(1);
92 String javaName = null;
93
94 if (CONV_METHOD_UNDERSCORE.equals(method))
95 {
96 javaName = underscoreMethod(schemaName);
97 }
98 else if (CONV_METHOD_JAVANAME.equals(method))
99 {
100 javaName = javanameMethod(schemaName);
101 }
102 else if (CONV_METHOD_NOCHANGE.equals(method))
103 {
104 javaName = nochangeMethod(schemaName);
105 }
106 else
107 {
108 // if for some reason nothing is defined then we default
109 // to the traditional method.
110 javaName = underscoreMethod(schemaName);
111 }
112
113 return javaName;
114 }
115
116 /***
117 * Converts a database schema name to java object name. Removes
118 * <code>STD_SEPARATOR_CHAR</code>, capitilizes first letter of
119 * name and each letter after the <code>STD_SEPERATOR</code>,
120 * converts the rest of the letters to lowercase.
121 *
122 * @param schemaName name to be converted.
123 * @return converted name.
124 * @see org.apache.torque.engine.database.model.NameGenerator
125 * @see #underscoreMethod(String)
126 */
127 protected String underscoreMethod(String schemaName)
128 {
129 StringBuffer name = new StringBuffer();
130 StringTokenizer tok = new StringTokenizer
131 (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
132 while (tok.hasMoreTokens())
133 {
134 String namePart = ((String) tok.nextElement()).toLowerCase();
135 name.append(StringUtils.capitalise(namePart));
136 }
137 return name.toString();
138 }
139
140 /***
141 * Converts a database schema name to java object name. Operates
142 * same as underscoreMethod but does not convert anything to
143 * lowercase.
144 *
145 * @param schemaName name to be converted.
146 * @return converted name.
147 * @see org.apache.torque.engine.database.model.NameGenerator
148 * @see #underscoreMethod(String)
149 */
150 protected String javanameMethod(String schemaName)
151 {
152 StringBuffer name = new StringBuffer();
153 StringTokenizer tok = new StringTokenizer
154 (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
155 while (tok.hasMoreTokens())
156 {
157 String namePart = (String) tok.nextElement();
158 name.append(StringUtils.capitalise(namePart));
159 }
160 return name.toString();
161 }
162
163 /***
164 * Converts a database schema name to java object name. In this
165 * case no conversion is made.
166 *
167 * @param name name to be converted.
168 * @return The <code>name</code> parameter, unchanged.
169 */
170 protected final String nochangeMethod(String name)
171 {
172 return name;
173 }
174 }
This page was automatically generated by Maven