View Javadoc

1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Copyright 2001-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;
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,v 1.1.2.2 2004/05/20 04:34:15 seade 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 generateName(List inputs)
51      {
52          String schemaName = (String) inputs.get(0);
53          String method = (String) inputs.get(1);
54          String javaName = null;
55  
56          if (CONV_METHOD_UNDERSCORE.equals(method))
57          {
58              javaName = underscoreMethod(schemaName);
59          }
60          else if (CONV_METHOD_JAVANAME.equals(method))
61          {
62              javaName = javanameMethod(schemaName);
63          }
64          else if (CONV_METHOD_NOCHANGE.equals(method))
65          {
66              javaName = nochangeMethod(schemaName);
67          }
68          else
69          {
70              // if for some reason nothing is defined then we default
71              // to the traditional method.
72              javaName = underscoreMethod(schemaName);
73          }
74  
75          return javaName;
76      }
77  
78      /***
79       * Converts a database schema name to java object name.  Removes
80       * <code>STD_SEPARATOR_CHAR</code>, capitilizes first letter of
81       * name and each letter after the <code>STD_SEPERATOR</code>,
82       * converts the rest of the letters to lowercase.
83       *
84       * @param schemaName name to be converted.
85       * @return converted name.
86       * @see org.apache.torque.engine.database.model.NameGenerator
87       * @see #underscoreMethod(String)
88       */
89      protected String underscoreMethod(String schemaName)
90      {
91          StringBuffer name = new StringBuffer();
92          StringTokenizer tok = new StringTokenizer
93              (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
94          while (tok.hasMoreTokens())
95          {
96              String namePart = ((String) tok.nextElement()).toLowerCase();
97              name.append(StringUtils.capitalise(namePart));
98          }
99          return name.toString();
100     }
101 
102     /***
103      * Converts a database schema name to java object name.  Operates
104      * same as underscoreMethod but does not convert anything to
105      * lowercase.
106      *
107      * @param schemaName name to be converted.
108      * @return converted name.
109      * @see org.apache.torque.engine.database.model.NameGenerator
110      * @see #underscoreMethod(String)
111      */
112     protected String javanameMethod(String schemaName)
113     {
114         StringBuffer name = new StringBuffer();
115         StringTokenizer tok = new StringTokenizer
116             (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
117         while (tok.hasMoreTokens())
118         {
119             String namePart = (String) tok.nextElement();
120             name.append(StringUtils.capitalise(namePart));
121         }
122         return name.toString();
123     }
124 
125     /***
126      * Converts a database schema name to java object name.  In this
127      * case no conversion is made.
128      *
129      * @param name name to be converted.
130      * @return The <code>name</code> parameter, unchanged.
131      */
132     protected final String nochangeMethod(String name)
133     {
134         return name;
135     }
136 }