From: <max...@us...> - 2003-03-25 07:08:40
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java In directory sc8-pr-cvs1:/tmp/cvs-serv18147/src/net/sf/hibernate/tool/hbm2java Modified Files: BasicRenderer.java Added Files: AbstractRenderer.java Log Message: Small refactoring of Renderers. Now there is an AbstractRenderer with "basics" supportmethods. Done to support the multiple files per entity strategy, for e.g. Finders and Constants files. --- NEW FILE: AbstractRenderer.java --- /* * Created on 25-03-2003 * * To change this generated comment go to * Window>Preferences>Java>Code Generation>Code Template */ package net.sf.hibernate.tool.hbm2java; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TreeSet; import org.apache.commons.lang.StringUtils; import net.sf.hibernate.util.StringHelper; /** * @author max */ public abstract class AbstractRenderer implements Renderer { /** * Returns the true name for the given class name. By true name is * that it will return the Proxy for the class name if the class was * defined with a proxy attribute. * @param field class name that we use to serach in class2classmap * @param class2classmap a map from classname to classmappings * @return String return either name or the proxy name of the classmap */ static protected String getTrueTypeName(Field field, Map class2classmap) { String name = (field.getClassType() != null) ? field.getClassType().getFullyQualifiedName() : field.getType(); ClassMapping cmap = (ClassMapping) class2classmap.get(name); if (cmap != null) { if (cmap.getProxy() != null) { return cmap.getProxy(); } } return name; } static protected String getTrueTypeName(ClassName cn, Map class2classmap) { String name = cn.getFullyQualifiedName(); ClassMapping cmap = (ClassMapping) class2classmap.get(name); if (cmap != null) { if (cmap.getProxy() != null) { return cmap.getProxy(); } } return name; } /** * Returns the last part of type if it is in the set of imports. * e.g. java.util.Date would become Date, if imports contains * java.util.Date. * * @param type * @param imports * @return String */ protected String shortenType(String type, TreeSet imports) { if( imports.contains(type) ) { return type.substring( type.lastIndexOf(StringHelper.DOT)+1 ); } else { if( type.endsWith("[]") ) { return shortenType( type.substring(0, type.length()-2), imports ) + "[]"; } else { return type; } } } /** * Convert string into something that can be rendered nicely into a javadoc * comment. * Prefix each line with a star ('*'). * @param string */ protected String toJavaDoc(String string, int indent) { StringBuffer result = new StringBuffer(); if(string!=null) { String[] lines = StringUtils.split(string, "\n\r\f"); for (int i = 0; i < lines.length; i++) { String docline = " * " + lines[i] + "\n"; result.append(StringUtils.leftPad(docline, docline.length() + indent)); } } return result.toString(); } public String getFieldScope(Field field, String localScopeName, String defaultScope) { if (defaultScope==null) defaultScope = "private"; return ( field.getMeta(localScopeName)==null )? defaultScope : field.getMetaAsString(localScopeName); } } Index: BasicRenderer.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/BasicRenderer.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** BasicRenderer.java 17 Feb 2003 18:22:42 -0000 1.11 --- BasicRenderer.java 25 Mar 2003 07:08:37 -0000 1.12 *************** *** 17,21 **** import org.apache.commons.logging.LogFactory; ! public class BasicRenderer implements Renderer { static final protected int ORDINARY = 0; --- 17,21 ---- import org.apache.commons.logging.LogFactory; ! public class BasicRenderer extends AbstractRenderer { static final protected int ORDINARY = 0; *************** *** 25,76 **** static private Log log = LogFactory.getLog(BasicRenderer.class); ! /** ! * Returns the true name for the given class name. By true name is ! * that it will return the Proxy for the class name if the class was ! * defined with a proxy attribute. ! * @param field class name that we use to serach in class2classmap ! * @param class2classmap a map from classname to classmappings ! * @return String return either name or the proxy name of the classmap ! */ ! String getTrueTypeName(Field field, Map class2classmap) { ! String name = ( field.getClassType()!=null ) ? ! field.getClassType().getFullyQualifiedName(): ! field.getType(); ! ClassMapping cmap = (ClassMapping) class2classmap.get(name); ! ! if(cmap!=null) { ! if (cmap.getProxy()!=null) { ! return cmap.getProxy(); ! } ! } ! return name; ! } ! ! private String makeSupportField(String fieldName, List fieldList) { ! String suffix=""; ! boolean needSuffix=false; ! for( Iterator fields = fieldList.iterator(); fields.hasNext();) { ! String name = ((Field)fields.next()).getName(); ! if(name.equals(fieldName)) needSuffix=true; ! suffix+=name; ! } ! return needSuffix ? fieldName+"_"+suffix : fieldName; ! } ! ! String getTrueTypeName(ClassName cn, Map class2classmap) { ! String name = cn.getFullyQualifiedName(); ! ClassMapping cmap = (ClassMapping) class2classmap.get(name); ! ! ! if(cmap!=null) { ! if (cmap.getProxy()!=null) { ! return cmap.getProxy(); ! } ! } ! return name; ! } ! ! ! public void render(String packageName, ClassMapping classMapping, Map class2classmap, PrintWriter mainwriter) throws Exception { if ( classMapping.getGeneratedPackageName()!=null ) { mainwriter.println("package " + classMapping.getGeneratedPackageName() + ";"); --- 25,29 ---- static private Log log = LogFactory.getLog(BasicRenderer.class); ! public void render(ClassMapping classMapping, Map class2classmap, PrintWriter mainwriter) throws Exception { if ( classMapping.getGeneratedPackageName()!=null ) { mainwriter.println("package " + classMapping.getGeneratedPackageName() + ";"); *************** *** 404,418 **** } ! private String getFieldAsObject(boolean prependThis, Field field) { ! ClassName type = field.getClassType(); ! if(type != null && type.isPrimitive() && !type.isArray()) { ! String typeName = (String) primitiveToObject.get(type.getName()); ! typeName = "new "+typeName+"( "; ! typeName += prependThis ? "this." : ""; ! return typeName+field.getName()+" )"; ! } ! return field.getName(); ! } ! public int doFieldAccessors(ClassMapping classMapping, Map class2classmap, --- 357,361 ---- } ! public int doFieldAccessors(ClassMapping classMapping, Map class2classmap, *************** *** 523,573 **** } ! public String getFieldScope(Field field, String localScopeName, String defaultScope) { ! if (defaultScope==null) defaultScope = "private"; ! return ( field.getMeta(localScopeName)==null )? defaultScope : field.getMetaAsString(localScopeName); ! } ! ! /** ! * Convert string into something that can be rendered nicely into a javadoc ! * comment. ! * Prefix each line with a star ('*'). ! * @param string ! */ ! private String toJavaDoc(String string, int indent) { ! StringBuffer result = new StringBuffer(); ! ! if(string!=null) { ! String[] lines = StringUtils.split(string, "\n\r\f"); ! for (int i = 0; i < lines.length; i++) { ! String docline = " * " + lines[i] + "\n"; ! result.append(StringUtils.leftPad(docline, docline.length() + indent)); ! } ! } ! ! return result.toString(); ! } ! ! ! /** ! * Returns the last part of type if it is in the set of imports. ! * e.g. java.util.Date would become Date, if imports contains ! * java.util.Date. ! * ! * @param type ! * @param imports ! * @return String ! */ ! private String shortenType(String type, TreeSet imports) { ! if( imports.contains(type) ) { ! return type.substring( type.lastIndexOf(StringHelper.DOT)+1 ); ! } ! else { ! if( type.endsWith("[]") ) { ! return shortenType( type.substring(0, type.length()-2), imports ) + "[]"; ! } ! else { ! return type; ! } ! } ! } } --- 466,491 ---- } ! protected String makeSupportField(String fieldName, List fieldList) { ! String suffix = ""; ! boolean needSuffix = false; ! for (Iterator fields = fieldList.iterator(); fields.hasNext();) { ! String name = ((Field) fields.next()).getName(); ! if (name.equals(fieldName)) ! needSuffix = true; ! suffix += name; ! } ! return needSuffix ? fieldName + "_" + suffix : fieldName; ! } ! ! private String getFieldAsObject(boolean prependThis, Field field) { ! ClassName type = field.getClassType(); ! if(type != null && type.isPrimitive() && !type.isArray()) { ! String typeName = (String) primitiveToObject.get(type.getName()); ! typeName = "new "+typeName+"( "; ! typeName += prependThis ? "this." : ""; ! return typeName+field.getName()+" )"; ! } ! return field.getName(); ! } ! } |