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