Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java In directory sc8-pr-cvs1:/tmp/cvs-serv1918/src/net/sf/hibernate/tool/hbm2java Modified Files: BasicRenderer.java MetaAttributeHelper.java ClassMapping.java CodeGenerator.java Field.java Log Message: Fixed bug regarding "abstract" class generation. small refactorings of metaattributehelper (unified access to them) implemented the inherit keyword for <meta> Index: BasicRenderer.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/BasicRenderer.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** BasicRenderer.java 12 Apr 2003 14:47:36 -0000 1.15 --- BasicRenderer.java 4 May 2003 10:32:02 -0000 1.16 *************** *** 51,55 **** classScope = classMapping.getMetaAsString("scope-class").trim(); } ! if(classMapping.shouldBeAbstract()) { writer.print( "abstract " + classScope + " class " + savedToClass ); } else { --- 51,55 ---- classScope = classMapping.getMetaAsString("scope-class").trim(); } ! if(classMapping.shouldBeAbstract() && (classScope.indexOf("abstract")==-1)) { writer.print( "abstract " + classScope + " class " + savedToClass ); } else { *************** *** 77,86 **** if(classMapping.getMeta("implements")!=null) { ! Collection implementz = classMapping.getMeta("implements"); ! for (Iterator iter = implementz.iterator(); iter.hasNext();) { ! String iface = (String) iter.next(); ! writer.print(StringHelper.COMMA_SPACE); ! writer.print(iface); ! } } --- 77,81 ---- if(classMapping.getMeta("implements")!=null) { ! writer.print(MetaAttributeHelper.getMetaAsString(classMapping.getMeta("implements"),", ")); } Index: MetaAttributeHelper.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/MetaAttributeHelper.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MetaAttributeHelper.java 16 Jan 2003 20:54:55 -0000 1.1 --- MetaAttributeHelper.java 4 May 2003 10:32:02 -0000 1.2 *************** *** 2,8 **** --- 2,11 ---- import java.util.ArrayList; + import java.util.Collection; import java.util.Iterator; import java.util.List; + import net.sf.hibernate.util.StringHelper; + import org.apache.commons.collections.MultiHashMap; import org.apache.commons.collections.MultiMap; *************** *** 20,83 **** public class MetaAttributeHelper { ! static private Log log = LogFactory.getLog(MetaAttributeHelper.class); ! ! /** ! * Load meta attributes from jdom element into a MultiMap. ! * ! * @param element ! * @return MultiMap ! */ ! static protected MultiMap loadMetaMap(Element element) { ! MultiMap result = new MultiHashMap(); ! List metaAttributeList = new ArrayList(); ! metaAttributeList.addAll(element.getChildren("meta")); ! for (Iterator iter = metaAttributeList.iterator(); iter.hasNext();) { ! Element metaAttrib = (Element) iter.next(); ! // does not use getTextNormalize() or getTextTrim() as that would remove the formatting in new lines in items like description for javadocs. ! result.put(metaAttrib.getAttribute("attribute").getValue(), metaAttrib.getText()); ! } ! return result; ! } ! /** ! * Merges a Multimap with inherited maps. ! * Values specified always overrules/replaces the inherited values. ! * ! * @param local ! * @param inherited ! * @return a MultiMap with all values from local and extra values ! * from inherited ! */ ! static public MultiMap mergeMetaMaps(MultiMap local, MultiMap inherited) { ! MultiHashMap result = new MultiHashMap(); ! ! if (inherited != null) { ! result.putAll(local); ! for (Iterator iter = inherited.keySet().iterator(); iter.hasNext();) { ! String key = (String) iter.next(); ! if (!local.containsKey(key)) { ! // inheriting a meta attribute ! result.put(key, inherited.get(key)); ! } ! } ! } ! ! ! return result; ! ! } ! /** ! * Method loadAndMergeMetaMap. ! * @param classElement ! * @param inheritedMeta ! * @return MultiMap ! */ ! public static MultiMap loadAndMergeMetaMap(Element classElement, MultiMap inheritedMeta) { ! return mergeMetaMaps(loadMetaMap(classElement), inheritedMeta); ! } } --- 23,151 ---- public class MetaAttributeHelper { ! static private Log log = LogFactory.getLog(MetaAttributeHelper.class); ! static class MetaAttribute { ! String value; ! boolean inheritable = true; ! MetaAttribute(String value, boolean inherit) { ! this.value = value; ! this.inheritable = inherit; ! } ! public String toString() { ! return value; ! } ! } ! /** ! * Load meta attributes from jdom element into a MultiMap. ! * ! * @param element ! * @return MultiMap ! */ ! static protected MultiMap loadMetaMap(Element element) { ! MultiMap result = new MultiHashMap(); ! List metaAttributeList = new ArrayList(); ! metaAttributeList.addAll(element.getChildren("meta")); ! for (Iterator iter = metaAttributeList.iterator(); iter.hasNext();) { ! Element metaAttrib = (Element) iter.next(); ! // does not use getTextNormalize() or getTextTrim() as that would remove the formatting in new lines in items like description for javadocs. ! String attribute = metaAttrib.getAttribute("attribute").getValue(); ! String value = metaAttrib.getText(); ! boolean inherit = ! Boolean ! .valueOf(metaAttrib.getAttributeValue("inherit")) ! .booleanValue(); ! MetaAttribute ma = new MetaAttribute(value, inherit); ! result.put(attribute, ma); ! } ! return result; ! ! } ! ! /** ! * Merges a Multimap with inherited maps. ! * Values specified always overrules/replaces the inherited values. ! * ! * @param local ! * @param inherited ! * @return a MultiMap with all values from local and extra values ! * from inherited ! */ ! static public MultiMap mergeMetaMaps(MultiMap local, MultiMap inherited) { ! MultiHashMap result = new MultiHashMap(); ! ! if (inherited != null) { ! result.putAll(local); ! for (Iterator iter = inherited.keySet().iterator(); ! iter.hasNext(); ! ) { ! String key = (String) iter.next(); ! ! if (!local.containsKey(key)) { ! // inheriting a meta attribute only if it is inheritable ! ArrayList ml = (ArrayList) inherited.get(key); ! for (Iterator iterator = ml.iterator(); ! iterator.hasNext(); ! ) { ! MetaAttribute element = (MetaAttribute) iterator.next(); ! if (element.inheritable) { ! result.put(key, element); ! } ! } ! } ! } ! } ! ! return result; ! ! } ! /** ! * Method loadAndMergeMetaMap. ! * @param classElement ! * @param inheritedMeta ! * @return MultiMap ! */ ! public static MultiMap loadAndMergeMetaMap( ! Element classElement, ! MultiMap inheritedMeta) { ! return mergeMetaMaps(loadMetaMap(classElement), inheritedMeta); ! } ! ! /** ! * @param collection ! * @param string ! */ ! public static String getMetaAsString(Collection meta, String seperator) { ! StringBuffer buf = new StringBuffer(); ! for (Iterator iter = meta.iterator(); iter.hasNext();) { ! buf.append(seperator); ! buf.append(iter.next()); ! } ! return buf.toString(); ! } ! ! static boolean getMetaAsBool(Collection c, boolean defaultValue) { ! if(c==null || c.isEmpty()) { ! return defaultValue; ! } else { ! return Boolean.valueOf(c.iterator().next().toString()).booleanValue(); ! } ! } ! ! static String getMetaAsString(Collection c) { ! if(c==null || c.isEmpty()) { ! return StringHelper.EMPTY_STRING; ! } else { ! StringBuffer sb = new StringBuffer(); ! for (Iterator iter = c.iterator(); iter.hasNext();) { ! Object element = (Object) iter.next(); ! sb.append(element.toString()); ! } ! return sb.toString(); ! } ! } } Index: ClassMapping.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/ClassMapping.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** ClassMapping.java 20 Apr 2003 11:52:47 -0000 1.15 --- ClassMapping.java 4 May 2003 10:32:02 -0000 1.16 *************** *** 515,522 **** ClassName foreignClass = null; Set foreignKeys = null; ! if(collection.getChildren("one-to-many").size()!=0) { // Collect bidirectional data foreignClass = new ClassName(); foreignClass.setFullyQualifiedName(collection.getChild("one-to-many").getAttributeValue("class")); // Collect the keys foreignKeys = new HashSet(); --- 515,530 ---- ClassName foreignClass = null; Set foreignKeys = null; ! // Collect bidirectional data ! if(collection.getChildren("one-to-many").size()!=0) { foreignClass = new ClassName(); foreignClass.setFullyQualifiedName(collection.getChild("one-to-many").getAttributeValue("class")); + } else if (collection.getChildren("many-to-many").size()!=0) { + foreignClass = new ClassName(); + foreignClass.setFullyQualifiedName(collection.getChild("many-to-many").getAttributeValue("class")); + } + + // Do the foreign keys and import + if (foreignClass != null) { // Collect the keys foreignKeys = new HashSet(); *************** *** 681,695 **** Collection c= getMeta(attribute); ! if(c==null) { ! return StringHelper.EMPTY_STRING; ! } ! else { ! StringBuffer sb = new StringBuffer(); ! for (Iterator iter = c.iterator(); iter.hasNext();) { ! Object element = (Object) iter.next(); ! sb.append(element.toString()); ! } ! return sb.toString(); ! } } /** --- 689,693 ---- Collection c= getMeta(attribute); ! return MetaAttributeHelper.getMetaAsString(c); } /** *************** *** 707,712 **** log.warn("Warning: meta attribute extends='" + getMetaAsString("extends") + "' will be ignored for subclass " + name); } ! ! } --- 705,709 ---- log.warn("Warning: meta attribute extends='" + getMetaAsString("extends") + "' will be ignored for subclass " + name); } ! } Index: CodeGenerator.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/CodeGenerator.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CodeGenerator.java 16 Jan 2003 20:52:38 -0000 1.4 --- CodeGenerator.java 4 May 2003 10:32:02 -0000 1.5 *************** *** 89,94 **** // parse the mapping file Document document = builder.build( new File( (String) iter.next() ) ); ! Iterator classElements = document.getRootElement().getChildren("class").iterator(); ! MultiMap mm = MetaAttributeHelper.loadAndMergeMetaMap(document.getRootElement(), globalMetas); while ( classElements.hasNext() ) { ClassMapping cmap = new ClassMapping( (Element) classElements.next(), mm); --- 89,96 ---- // parse the mapping file Document document = builder.build( new File( (String) iter.next() ) ); ! ! Element rootElement = document.getRootElement(); ! Iterator classElements = rootElement.getChildren("class").iterator(); ! MultiMap mm = MetaAttributeHelper.loadAndMergeMetaMap(rootElement, globalMetas); while ( classElements.hasNext() ) { ClassMapping cmap = new ClassMapping( (Element) classElements.next(), mm); Index: Field.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/Field.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Field.java 17 Feb 2003 18:22:42 -0000 1.7 --- Field.java 4 May 2003 10:32:02 -0000 1.8 *************** *** 3,10 **** import java.util.Collection; - import java.util.Iterator; import java.util.Set; - import net.sf.hibernate.util.StringHelper; import org.apache.commons.collections.MultiMap; --- 3,8 ---- *************** *** 125,140 **** Collection c= getMeta(attribute); ! if(c==null || c.isEmpty()) { ! return StringHelper.EMPTY_STRING; ! } else { ! StringBuffer sb = new StringBuffer(); ! for (Iterator iter = c.iterator(); iter.hasNext();) { ! Object element = (Object) iter.next(); ! sb.append(element.toString()); ! } ! return sb.toString(); ! } } ! public boolean getMetaAsBool(String attribute) { return getMetaAsBool(attribute,false); --- 123,129 ---- Collection c= getMeta(attribute); ! return MetaAttributeHelper.getMetaAsString(c); } ! public boolean getMetaAsBool(String attribute) { return getMetaAsBool(attribute,false); *************** *** 144,154 **** Collection c= getMeta(attribute); ! if(c==null || c.isEmpty()) { ! return defaultValue; ! } else { ! return Boolean.valueOf(c.iterator().next().toString()).booleanValue(); ! } } ! /** * Returns the foreignClass. --- 133,139 ---- Collection c= getMeta(attribute); ! return MetaAttributeHelper.getMetaAsBool(c,defaultValue); } ! /** * Returns the foreignClass. |