001package org.andromda.schema2xmi; 002 003import org.apache.commons.lang.StringUtils; 004 005/** 006 * Provides formatting functions, when converting SQL names to model names. 007 * 008 * @author Chad Brandon 009 */ 010public class SqlToModelNameFormatter 011{ 012 /** 013 * Converts a table name to an class name. 014 * 015 * @param name the name of the table. 016 * @return the new class name. 017 */ 018 public static String toClassName(String name) 019 { 020 return toCamelCase(name); 021 } 022 023 /** 024 * Converts a column name to an attribute name. 025 * 026 * @param name the name of the column 027 * @return the new attribute name. 028 */ 029 public static String toAttributeName(String name) 030 { 031 return StringUtils.uncapitalize(toClassName(name)); 032 } 033 034 /** 035 * Turns a table name into a model element class name. 036 * 037 * @param name the table name. 038 * @return the new class name. 039 */ 040 public static String toCamelCase(String name) 041 { 042 StringBuilder buffer = new StringBuilder(); 043 String[] tokens = name.split("_|\\s+"); 044 if (tokens != null && tokens.length > 0) 045 { 046 for (int ctr = 0; ctr < tokens.length; ctr++) 047 { 048 buffer.append(StringUtils.capitalize(tokens[ctr].toLowerCase())); 049 } 050 } 051 else 052 { 053 buffer.append(StringUtils.capitalize(name.toLowerCase())); 054 } 055 return buffer.toString(); 056 } 057}