All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.frameworkset.orm.engine.model.JavaNameGenerator Maven / Gradle / Ivy

Go to download

bboss is a j2ee framework include aop/ioc,mvc,persistent,taglib,rpc,event ,bean-xml serializable and so on.http://www.bbossgroups.com

The newest version!
/*
 *  Copyright 2008 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.  
 */
package com.frameworkset.orm.engine.model;

/*
 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;

import com.frameworkset.orm.engine.EngineException;


/**
 * A NameGenerator implementation for Java-esque names.
 *
 * @author Byron Foster
 * @version $Id: JavaNameGenerator.java,v 1.4 2005/02/23 17:32:09 tfischer Exp $
 */
public class JavaNameGenerator implements NameGenerator
{
    /**
     * inputs should consist of two elements, the
     * original name of the database element and the method for
     * generating the name.  There are currently three methods:
     * CONV_METHOD_NOCHANGE - xml names are converted
     * directly to java names without modification.
     * CONV_METHOD_UNDERSCORE will capitalize the first
     * letter, remove underscores, and capitalize each letter before
     * an underscore.  All other letters are lowercased. "javaname"
     * works the same as the CONV_METHOD_JAVANAME method
     * but will not lowercase any characters.
     *
     * @param inputs list expected to contain two parameters, element
     * 0 contains name to convert, element 1 contains method for conversion.
     * @return The generated name.
     * @see com.frameworkset.orm.engine.model.NameGenerator
     */
    public String generateName(List inputs)
    {
        String schemaName = (String) inputs.get(0);
        String method = (String) inputs.get(1);
        String javaName = null;

        if (CONV_METHOD_UNDERSCORE.equals(method))
        {
            javaName = underscoreMethod(schemaName,false);
        }
        else if (CONV_METHOD_UNDERSCORE_OMIT_SCHEMA.equals(method))
        {
            javaName = underscoreOmitSchemaMethod(schemaName,  false);
        }
        else if (CONV_METHOD_JAVANAME.equals(method))
        {
            javaName = javanameMethod(schemaName,false);
        }
        else if (CONV_METHOD_NOCHANGE.equals(method))
        {
            javaName = nochangeMethod(schemaName,false);
        }
        else if (CONV_METHOD_JAVAFIELDNAME.equals(method))
        {
            javaName = this.javanameField(schemaName,  false);
        }
        
        else
        {
            // if for some reason nothing is defined then we default
            // to the traditional method.
            javaName = underscoreMethod(schemaName,false);
        }

        return javaName;
    }
    public String generateName(List inputs,boolean IGNORE_FIRST_TOKEN) throws EngineException
    {
    	String schemaName = (String) inputs.get(0);
        String method = (String) inputs.get(1);
        String javaName = null;

        if (CONV_METHOD_UNDERSCORE.equals(method))
        {
            javaName = underscoreMethod(schemaName,IGNORE_FIRST_TOKEN);
        }
        else if (CONV_METHOD_UNDERSCORE_OMIT_SCHEMA.equals(method))
        {
            javaName = underscoreOmitSchemaMethod(schemaName,IGNORE_FIRST_TOKEN);
        }
        else if (CONV_METHOD_JAVANAME.equals(method))
        {
            javaName = javanameMethod(schemaName,IGNORE_FIRST_TOKEN);
        }
        else if (CONV_METHOD_NOCHANGE.equals(method))
        {
            javaName = nochangeMethod(schemaName,IGNORE_FIRST_TOKEN);
        }
        else if (CONV_METHOD_JAVAFIELDNAME.equals(method))
        {
            javaName = this.javanameField(schemaName,IGNORE_FIRST_TOKEN);
        }
        
        else
        {
            // if for some reason nothing is defined then we default
            // to the traditional method.
            javaName = underscoreMethod(schemaName,IGNORE_FIRST_TOKEN);
        }

        return javaName;
    }
    

    /**
     * Converts a database schema name to java object name.  Removes
     * STD_SEPARATOR_CHAR and SCHEMA_SEPARATOR_CHAR,
     * capitilizes first letter of name and each letter after the 
     * STD_SEPERATOR and SCHEMA_SEPARATOR_CHAR,
     * converts the rest of the letters to lowercase.
     *
     * @param schemaName name to be converted.
     * @return converted name.
     * @see com.frameworkset.orm.engine.model.NameGenerator
     * @see #underscoreMethod(String)
     */
    protected String underscoreMethod(String schemaName,boolean IGNORE_FIRST_TOKEN)
    {
        StringBuilder name = new StringBuilder();
        
        // remove the STD_SEPARATOR_CHARs and capitalize 
        // the tokens
        StringTokenizer tok = new StringTokenizer
            (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
        while (tok.hasMoreTokens())
        {
            String namePart = ((String) tok.nextElement()).toLowerCase();
            name.append(StringUtils.capitalize(namePart));
        }
        
        // remove the SCHEMA_SEPARATOR_CHARs and capitalize 
        // the tokens
        schemaName = name.toString();
        name = new StringBuilder();
        tok = new StringTokenizer
            (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR));
        while (tok.hasMoreTokens())
        {
            String namePart = (String) tok.nextElement();
            name.append(StringUtils.capitalize(namePart));
        }
        return name.toString();
    }

    /**
     * Converts a database schema name to java object name. 
     * First, it removes all characters before the last occurence of 
     * .SCHEMA_SEPARATOR_CHAR. Then, in a second step, removes
     * STD_SEPARATOR_CHAR, capitilizes first letter of
     * name and each letter after the STD_SEPERATOR,
     * and converts the rest of the letters to lowercase.
     *
     * @param schemaName name to be converted.
     * @return converted name.
     * @see com.frameworkset.orm.engine.model.NameGenerator
     * @see #underscoreOmitSchemaMethod(String)
     */
    protected String underscoreOmitSchemaMethod(String schemaName,boolean IGNORE_FIRST_TOKEN)
    {
        // take only part after last dot
        int lastDotPos = schemaName.lastIndexOf(SCHEMA_SEPARATOR_CHAR);
        if (lastDotPos != -1) {
            schemaName = schemaName.substring(lastDotPos + 1);
        }
        StringBuilder name = new StringBuilder();
        StringTokenizer tok = new StringTokenizer
            (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
        while (tok.hasMoreTokens())
        {
            String namePart = ((String) tok.nextElement()).toLowerCase();
            name.append(StringUtils.capitalize(namePart));
        }
        return name.toString();
    }

    /**
     * Converts a database schema name to java object name.  Operates
     * same as underscoreMethod but does not convert anything to
     * lowercase.
     *
     * @param schemaName name to be converted.
     * @return converted name.
     * @see com.frameworkset.orm.engine.model.NameGenerator
     * @see #underscoreMethod(String)
     */
    protected String javanameMethod(String schemaName,boolean IGNORE_FIRST_TOKEN)
    {
        StringBuilder name = new StringBuilder();
        StringTokenizer tok = new StringTokenizer
            (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
        boolean first = true;
        String firstNamepart = null;
        while (tok.hasMoreTokens())
        {        
        	
        	if(first && IGNORE_FIRST_TOKEN)
        	{
        		firstNamepart = (String) tok.nextElement();
        		first =false;
        		
        		continue;
        	}
            String namePart = (String) tok.nextElement();
            
            name.append(StringUtils.capitalize(namePart));
             
        }

        // remove the SCHEMA_SEPARATOR_CHARs and capitalize 
        // the tokens
        if(name.length() > 0 )
        	schemaName = name.toString();
        else if(IGNORE_FIRST_TOKEN && firstNamepart != null)
        	schemaName = firstNamepart;
        name = new StringBuilder();
        
        tok = new StringTokenizer
            (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR));
        while (tok.hasMoreTokens())
        {
            String namePart = (String) tok.nextElement();
            name.append(StringUtils.capitalize(namePart));
        }
        return name.toString();
    }
    
    /**
     * Converts a database schema name to java object name.  Operates
     * same as underscoreMethod but does not convert anything to
     * lowercase.
     *
     * @param schemaName name to be converted.
     * @return converted name.
     * @see com.frameworkset.orm.engine.model.NameGenerator
     * @see #underscoreMethod(String)
     */
    protected String javanameField(String schemaName,boolean IGNORE_FIRST_TOKEN)
    {
        StringBuilder name = new StringBuilder();
        StringTokenizer tok = new StringTokenizer
            (schemaName, String.valueOf(STD_SEPARATOR_CHAR));
        boolean first = true;
        while (tok.hasMoreTokens())
        {
        	
            String namePart = (String) tok.nextElement();
            if(!first)
            {
            	name.append(StringUtils.capitalize(namePart));
            }
            else
            {
            	first = false;
            	name.append(namePart);
            }
        }

        // remove the SCHEMA_SEPARATOR_CHARs and capitalize 
        // the tokens
        schemaName = name.toString();
        name = new StringBuilder();
        
        tok = new StringTokenizer
            (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR));
        first = true;
        while (tok.hasMoreTokens())
        {
            String namePart = (String) tok.nextElement();
            if(!first)
            {
            	name.append(StringUtils.capitalize(namePart));
            }
            else
            {
            	first = false;
            	name.append(namePart);
            }
        }
        return name.toString();
    }

    /**
     * Converts a database schema name to java object name.  In this
     * case no conversion is made.
     *
     * @param name name to be converted.
     * @return The name parameter, unchanged.
     */
    protected final String nochangeMethod(String name,boolean IGNORE_FIRST_TOKEN)
    {
        return name;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy