[Snmap-developer] SNMAP/src/net/sf/snmap/cfg SnmapBinder.java,1.1,1.2
Status: Planning
Brought to you by:
arden
|
From: arden l. <ar...@us...> - 2006-01-13 03:23:53
|
Update of /cvsroot/snmap/SNMAP/src/net/sf/snmap/cfg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7683/src/net/sf/snmap/cfg Modified Files: SnmapBinder.java Log Message: Index: SnmapBinder.java =================================================================== RCS file: /cvsroot/snmap/SNMAP/src/net/sf/snmap/cfg/SnmapBinder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SnmapBinder.java 19 Dec 2005 03:05:58 -0000 1.1 --- SnmapBinder.java 13 Jan 2006 03:23:44 -0000 1.2 *************** *** 1,8 **** --- 1,16 ---- package net.sf.snmap.cfg; + import java.util.ArrayList; + import java.util.HashSet; import java.util.Iterator; import java.util.Properties; import net.sf.snmap.MappingException; + import net.sf.snmap.mapping.ObjectType; + import net.sf.snmap.mapping.PersistentClass; + import net.sf.snmap.mapping.RootClass; + import net.sf.snmap.mapping.Property; + import net.sf.snmap.mapping.Value; + import net.sf.snmap.mapping.SimpleValue; import org.apache.commons.logging.Log; *************** *** 11,14 **** --- 19,23 ---- import org.dom4j.Document; import org.dom4j.Element; + import org.snmp4j.smi.OID; /** *************** *** 16,179 **** * metamodel (the classes in the <tt>mapping</tt> package) */ ! public final class SnmapBinder { ! private static final Log log = LogFactory.getLog(SnmapBinder.class); ! /** ! * Private constructor to disallow instantiation. ! */ ! private SnmapBinder() { ! } ! /** ! * The main contract into the hbm.xml-based binder. Performs necessary ! * binding operations represented by the given DOM. ! * ! * @param doc ! * The DOM to be parsed and bound. ! * @param mappings ! * Current bind state. ! * @param inheritedMetas ! * Any inherited meta-tag information. ! * @throws MappingException ! */ ! public static void bindRoot(Document doc, Mappings mappings, ! java.util.Map inheritedMetas) throws MappingException { ! java.util.List names = HbmBinder.getExtendsNeeded(doc, mappings); ! if (!names.isEmpty()) { ! // classes mentioned in extends not available - so put it in queue ! for (Iterator iter = names.iterator(); iter.hasNext();) { ! String className = (String) iter.next(); ! mappings.addToExtendsQueue(className, doc); ! } ! return; ! } ! Element hmNode = doc.getRootElement(); ! inheritedMetas = getMetas(hmNode, inheritedMetas, true); // get ! // meta's ! // from ! // <hibernate-mapping> ! extractRootAttributes(hmNode, mappings); ! Iterator filterDefs = hmNode.elementIterator("filter-def"); ! while (filterDefs.hasNext()) { ! parseFilterDef((Element) filterDefs.next(), mappings); ! } ! Iterator typeDefs = hmNode.elementIterator("typedef"); ! while (typeDefs.hasNext()) { ! Element typeDef = (Element) typeDefs.next(); ! String typeClass = typeDef.attributeValue("class"); ! String typeName = typeDef.attributeValue("name"); ! Iterator paramIter = typeDef.elementIterator("param"); ! Properties parameters = new Properties(); ! while (paramIter.hasNext()) { ! Element param = (Element) paramIter.next(); ! parameters.setProperty(param.attributeValue("name"), param ! .getTextTrim()); ! } ! mappings.addTypeDef(typeName, typeClass, parameters); ! } ! Iterator nodes = hmNode.elementIterator("class"); ! while (nodes.hasNext()) { ! Element n = (Element) nodes.next(); ! // RootClass rootclass = new RootClass(); ! // bindRootClass( n, rootclass, mappings, inheritedMetas ); ! // mappings.addClass( rootclass ); ! } ! Iterator subclassnodes = hmNode.elementIterator("subclass"); ! while (subclassnodes.hasNext()) { ! Element subnode = (Element) subclassnodes.next(); ! // PersistentClass superModel = getSuperclass( mappings, subnode ); ! // handleSubclass( superModel, mappings, subnode, inheritedMetas ); ! } ! Iterator joinedsubclassnodes = hmNode ! .elementIterator("joined-subclass"); ! while (joinedsubclassnodes.hasNext()) { ! Element subnode = (Element) joinedsubclassnodes.next(); ! // PersistentClass superModel = getSuperclass( mappings, subnode ); ! // handleJoinedSubclass( superModel, mappings, subnode, ! // inheritedMetas ); ! } ! Iterator unionsubclassnodes = hmNode.elementIterator("union-subclass"); ! while (unionsubclassnodes.hasNext()) { ! Element subnode = (Element) unionsubclassnodes.next(); ! // PersistentClass superModel = getSuperclass( mappings, subnode ); ! // handleUnionSubclass( superModel, mappings, subnode, ! // inheritedMetas ); ! } ! nodes = hmNode.elementIterator("query"); ! while (nodes.hasNext()) { ! bindNamedQuery((Element) nodes.next(), null, mappings); ! } ! nodes = hmNode.elementIterator("sql-query"); ! while (nodes.hasNext()) { ! bindNamedSQLQuery((Element) nodes.next(), null, mappings); ! } ! nodes = hmNode.elementIterator("resultset"); ! while (nodes.hasNext()) { ! bindResultSetMappingDefinition((Element) nodes.next(), null, ! mappings); ! } ! nodes = hmNode.elementIterator("import"); ! while (nodes.hasNext()) { ! Element n = (Element) nodes.next(); ! String className = getClassName(n.attribute("class"), mappings); ! Attribute renameNode = n.attribute("rename"); ! String rename = (renameNode == null) ? StringHelper ! .unqualify(className) : renameNode.getValue(); ! log.debug("Import: " + rename + " -> " + className); ! mappings.addImport(className, rename); ! } ! nodes = hmNode.elementIterator("database-object"); ! while (nodes.hasNext()) { ! Element auxDbObjectNode = (Element) nodes.next(); ! AuxiliaryDatabaseObject auxDbObject = null; ! Element definitionNode = auxDbObjectNode.element("definition"); ! if (definitionNode != null) { ! try { ! auxDbObject = (AuxiliaryDatabaseObject) ReflectHelper ! .classForName( ! definitionNode.attributeValue("class")) ! .newInstance(); ! } catch (ClassNotFoundException e) { ! throw new MappingException( ! "could not locate custom database object class [" ! + definitionNode.attributeValue("class") ! + "]"); ! } catch (Throwable t) { ! throw new MappingException( ! "could not instantiate custom database object class [" ! + definitionNode.attributeValue("class") ! + "]"); ! } ! } else { ! auxDbObject = new SimpleAuxiliaryDatabaseObject(auxDbObjectNode ! .elementTextTrim("create"), auxDbObjectNode ! .elementTextTrim("drop")); ! } ! Iterator dialectScopings = auxDbObjectNode ! .elementIterator("dialect-scope"); ! while (dialectScopings.hasNext()) { ! Element dialectScoping = (Element) dialectScopings.next(); ! auxDbObject.addDialectScope(dialectScoping ! .attributeValue("name")); ! } ! mappings.addAuxiliaryDatabaseObject(auxDbObject); ! } ! } ! } \ No newline at end of file --- 25,238 ---- * metamodel (the classes in the <tt>mapping</tt> package) */ ! public final class SnmapBinder ! { ! private static final Log log = LogFactory.getLog( SnmapBinder.class ); ! /** ! * Private constructor to disallow instantiation. ! */ ! private SnmapBinder() ! {} ! /** ! * The main contract into the hbm.xml-based binder. Performs necessary ! * binding operations represented by the given DOM. ! * ! * @param doc ! * The DOM to be parsed and bound. ! * @param mappings ! * Current bind state. ! * @param inheritedMetas ! * Any inherited meta-tag information. ! * @throws MappingException ! */ ! public static void bindRoot( Document doc, Mappings mappings, java.util.Map inheritedMetas ) ! throws MappingException ! { ! // Get extend information (package) ! SnmapBinder.getExtendsNeeded( doc, mappings ); ! Element hmNode = doc.getRootElement(); ! Iterator nodes = hmNode.elementIterator( "class" ); ! while( nodes.hasNext() ) ! { ! Element n = (Element) nodes.next(); ! RootClass rootclass = new RootClass(); ! bindRootClass( n, rootclass, mappings, inheritedMetas ); ! mappings.addClass( rootclass ); ! } ! } ! /** ! * Responsible for perfoming the bind operation related to an <class/> ! * mapping element. ! * ! * @param node ! * The DOM Element for the <class/> element. ! * @param rootClass ! * The mapping instance to which to bind the information. ! * @param mappings ! * The current bind state. ! * @param inheritedMetas ! * Any inherited meta-tag information. ! * @throws MappingException ! */ ! public static void bindRootClass( Element node, RootClass rootClass, Mappings mappings, ! java.util.Map inheritedMetas ) throws MappingException ! { ! bindClass( node, rootClass, mappings, inheritedMetas ); ! bindRootPersistentClassCommonValues( node, inheritedMetas, mappings, rootClass ); ! } ! private static void bindRootPersistentClassCommonValues( Element node, ! java.util.Map inheritedMetas, Mappings mappings, RootClass entity ) throws MappingException ! { ! Iterator subnodes = node.elementIterator(); ! while( subnodes.hasNext() ) ! { ! Element subnode = (Element) subnodes.next(); ! String name = subnode.getName(); ! // TODO : check Table? ! if ("table".equals(name)) { ! } ! } ! createClassProperties( node, entity, mappings, inheritedMetas ); ! } ! public static void getExtendsNeeded( Document doc, Mappings mappings ) ! { ! final Element hmNode = doc.getRootElement(); ! Attribute packNode = hmNode.attribute( "package" ); ! if( packNode != null ) ! mappings.setDefaultPackage( packNode.getValue() ); ! } ! public static void bindClass( Element node, PersistentClass persistentClass, Mappings mappings, ! java.util.Map inheritedMetas ) throws MappingException ! { ! bindPojoRepresentation( node, persistentClass, mappings, inheritedMetas ); ! } ! protected static void createClassProperties( Element node, PersistentClass persistentClass, ! Mappings mappings, java.util.Map inheritedMetas ) throws MappingException ! { ! createClassProperties( node, persistentClass, mappings, inheritedMetas, true, true ); ! } ! protected static void createClassProperties( Element node, PersistentClass persistentClass, ! Mappings mappings, java.util.Map inheritedMetas, boolean mutable, boolean nullable ) ! throws MappingException ! { ! Iterator iter = node.elementIterator(); ! while( iter.hasNext() ) ! { ! Element subnode = (Element) iter.next(); ! String name = subnode.getName(); ! String propertyName = subnode.attributeValue( "name" ); ! ! Value value = null; ! ! if( "property".equals( name ) ) ! { ! value = new SimpleValue(); ! bindSimpleValue( subnode, (SimpleValue) value, nullable, propertyName, mappings ); ! } ! if( value != null ) ! { ! Property property = createProperty( value, propertyName, persistentClass ! .getClassName(), subnode, mappings, inheritedMetas ); ! persistentClass.addProperty( property ); ! } ! } ! } ! private static void bindPojoRepresentation( Element node, PersistentClass entity, ! Mappings mappings, java.util.Map metaTags ) ! { ! ! String className = getClassName( node.attribute( "name" ), mappings ); ! entity.setClassName( className ); ! } ! ! private static String getClassName( Attribute att, Mappings model ) ! { ! if( att == null ) ! return null; ! return getClassName( att.getValue(), model ); ! } ! ! private static String getClassName( String unqualifiedName, Mappings model ) ! { ! if( unqualifiedName == null ) ! return null; ! if( unqualifiedName.indexOf( '.' ) < 0 && model.getDefaultPackage() != null ) ! { ! return model.getDefaultPackage() + '.' + unqualifiedName; ! } ! return unqualifiedName; ! } ! ! private static Property createProperty( final Value value, final String propertyName, ! final String className, final Element subnode, final Mappings mappings, ! java.util.Map inheritedMetas ) throws MappingException ! { ! value.setTypeUsingReflection( className, propertyName ); ! ! Property prop = new Property(); ! prop.setValue( value ); ! bindProperty( subnode, prop, mappings, inheritedMetas ); ! return prop; ! } ! ! ! // automatically makes a ?? with OID ! public static void bindSimpleValue(Element node, SimpleValue simpleValue, boolean isNullable, ! String path, Mappings mappings) throws MappingException { ! bindOid( node, simpleValue, isNullable, path, mappings ); ! } ! ! ! public static void bindProperty( Element node, Property property, Mappings mappings, ! java.util.Map inheritedMetas ) throws MappingException ! { ! String propName = node.attributeValue( "name" ); ! property.setName( propName ); ! } ! ! public static void bindOid(final Element node, final SimpleValue simpleValue, ! final boolean isNullable, final String propertyPath, ! final Mappings mappings) throws MappingException { ! // OID ! Attribute oidAttribute = node.attribute( "oid" ); ! if ( oidAttribute == null ) { ! throw new MappingException( ! "oid attribute should not be null." ); ! } ! else { ! ObjectType objectType = new ObjectType(); ! objectType.setValue( simpleValue ); ! bindObjectType( node, objectType, isNullable ); ! ! simpleValue.setObjectType(objectType); ! ! } ! ! } ! public static void bindObjectType(Element node, ObjectType objectType, boolean isNullable) { ! Attribute oidNode = node.attribute( "oid" ); ! if ( oidNode != null ) ! { ! OID oid = new OID(oidNode.getValue()); ! objectType.setOid(oid); ! } ! } ! } |