⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binder.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			if (persistentClass==null) throw new MappingException(				"Association references unmapped class: " + assocClass.getName()			);			oneToMany.setAssociatedClass(persistentClass);			model.setCollectionTable( persistentClass.getTable() );			log.info("Mapping collection: " + model.getRole() + " -> " + model.getCollectionTable().getName() );		}		Iterator iter = node.elementIterator();		while( iter.hasNext() ) {			Element subnode = (Element) iter.next();			String name = subnode.getName();			if ( "key".equals(name) ) {				SimpleValue key = new SimpleValue( model.getCollectionTable() );				bindSimpleValue(subnode, key, model.isOneToMany(), Collection.DEFAULT_KEY_COLUMN_NAME);				key.setType( model.getOwner().getIdentifier().getType() );				if ( key.getType().getReturnedClass().isArray() ) throw new MappingException(					"illegal use of an array as an identifier (arrays don't reimplement equals)"				);				model.setKey(key);			}			else if ( "element".equals(name) ) {				SimpleValue elt = new SimpleValue( model.getCollectionTable() );				model.setElement(elt);				bindSimpleValue(subnode, elt, true, Collection.DEFAULT_ELEMENT_COLUMN_NAME);			}			else if ( "many-to-many".equals(name) ) {				ManyToOne element = new ManyToOne( model.getCollectionTable() );				model.setElement(element);				bindManyToOne(subnode, element, Collection.DEFAULT_ELEMENT_COLUMN_NAME, false, mappings);			}			else if ( "composite-element".equals(name) ) {				Component element = new Component( model.getCollectionTable() );				model.setElement(element);				bindComponent(subnode, element, null, model.getRole() + ".element", true, mappings);			}			else if ( "many-to-any".equals(name) ) {				Any element = new Any( model.getCollectionTable() );				model.setElement(element);				bindAny(subnode, element, true);			}			else if ( "jcs-cache".equals(name) || "cache".equals(name) ) {				CacheConcurrencyStrategy cache = CacheFactory.createCache( 					subnode, model.getRole(), model.getOwner().isMutable()				);				mappings.addCache( model.getRole(), cache );				model.setCache(cache);			}		}		if ( !model.isInverse() ) {			if ( !model.isOneToMany() ) { // no foreign key for a one-to-many				model.getElement().createForeignKey();			}			model.getKey().createForeignKeyOfClass( model.getOwner().getMappedClass() );		}		model.getCollectionTable().createIndex( model.getKey().getColumnIterator(), true );	}	public static void bindRoot(Document doc, Mappings model) throws MappingException {		Element hmNode = doc.getRootElement();		Attribute schemaNode = hmNode.attribute("schema");		model.setSchemaName( (schemaNode==null) ? null : schemaNode.getValue() );		Attribute dcNode = hmNode.attribute("default-cascade");		model.setDefaultCascade( (dcNode==null) ? "none" : dcNode.getValue() );		Attribute aiNode = hmNode.attribute("auto-import");		model.setAutoImport( (aiNode==null) ? true : "true".equals( aiNode.getValue() ) );		Iterator nodes = hmNode.elementIterator("class");		while ( nodes.hasNext() ) {			Element n = (Element) nodes.next();			RootClass rootclass = new RootClass();			Binder.bindRootClass(n, rootclass, model);			model.addClass(rootclass);		}				Iterator subclassnodes = hmNode.elementIterator("subclass");			while ( subclassnodes.hasNext() ) {				Element subnode = (Element) subclassnodes.next();				PersistentClass superModel = getSuperclass(model, subnode);				handleSubclass(superModel, model, subnode);			}				Iterator joinedsubclassnodes = hmNode.elementIterator("joined-subclass");			while ( joinedsubclassnodes.hasNext() ) {				Element subnode = (Element) joinedsubclassnodes.next();				PersistentClass superModel = getSuperclass(model, subnode);				handleJoinedSubclass(superModel, model, subnode);			}						nodes = hmNode.elementIterator("query");		while ( nodes.hasNext() ) {			Element n = (Element) nodes.next();			String qname = n.attributeValue("name");			String query = n.getText();			log.debug("Named query: " + qname + " -> " + query);			model.addQuery(qname, query);		}		nodes = hmNode.elementIterator("sql-query");		while ( nodes.hasNext() ) {			Element queryElem = (Element) nodes.next();						Attribute queryName = queryElem.attribute("name");			if ( queryName != null && !queryName.getText().trim().equals( "" ) ) {				Iterator returns = queryElem.elementIterator("return");								Class[] classes = null;				String[] aliases = null;				int i = 0;				classes = new Class[ queryElem.elements("return").size() ];				aliases = new String[ queryElem.elements("return").size() ];										while ( returns.hasNext() ) {					Element qReturn = (Element) returns.next();					try {						classes[ i ] = Class.forName(qReturn.attribute("class").getText() );						aliases[ i ] = qReturn.attribute("alias").getText();					}						// should we throw an exception or just log and continue?						catch ( ClassNotFoundException cnfe ) {							throw new MappingException( "Class Not ( " +									qReturn.attribute( "class" ).getText() +									" ) found in named sql query " + queryName.getText() );						}					i++;    				}				String qName = queryElem.getText();				NamedSQLQuery nq = new NamedSQLQuery(qName, aliases, classes);								log.debug( "Named sql query: " + queryName.getText() + " -> " + nq.getQueryString() );				model.addSQLQuery(queryName.getText(), nq);			}		}				nodes = hmNode.elementIterator("import");		while ( nodes.hasNext() ) {			Element n = (Element) nodes.next();			String className = n.attributeValue("class");			Attribute renameNode = n.attribute("rename");			String rename = (renameNode==null) ? StringHelper.unqualify(className) : renameNode.getValue();			log.debug("Import: " + rename + " -> " + className);			model.addImport(className, rename);		}	}	private static PersistentClass getSuperclass(Mappings model, Element subnode) throws MappingException {		String extendsValue = subnode.attributeValue("extends");		Class superclass;		try {			superclass = ReflectHelper.classForName(extendsValue);		} 		catch (ClassNotFoundException e) {			throw new MappingException("extends class " + extendsValue + " not found.", e);		}		PersistentClass superModel = model.getClass(superclass);					if (superModel==null) {			throw new MappingException( "Cannot extend unmapped class " + superclass.getName() );		}		return superModel;	}	private static String getPropertyName(Element node) {		Attribute propertyNameNode = node.attribute("name");		return (propertyNameNode==null) ? null : propertyNameNode.getValue();	}	abstract static class SecondPass {		Element node;		Mappings mappings;		Collection collection;		SecondPass(Element node, Mappings mappings, Collection collection) {			this.node = node;			this.collection = collection;			this.mappings = mappings;		}		final void doSecondPass(java.util.Map persistentClasses) throws MappingException {			if ( log.isDebugEnabled() ) log.debug("Second pass for collection: " + collection.getRole() );			secondPass(persistentClasses);			if ( log.isDebugEnabled() ) {				String msg = "Mapped collection key: " + columns( collection.getKey() );				if ( collection.isIndexed() ) msg+= ", index: " + columns( ( (IndexedCollection) collection ).getIndex() );				if ( collection.isOneToMany() ) {					msg += ", one-to-many: " + collection.getElement().getType().getName();				}				else {					msg += ", element: " + columns( collection.getElement() );					msg += ", type: " + collection.getElement().getType().getName();				}				log.debug(msg);			}		}		abstract void secondPass(java.util.Map persistentClasses) throws MappingException;	}	static class CollectionSecondPass extends SecondPass {		CollectionSecondPass(Element node, Mappings mappings, Collection collection) {			super(node, mappings, collection);		}		void secondPass(java.util.Map persistentClasses) throws MappingException {			Binder.bindCollectionSecondPass( node, collection, persistentClasses, mappings );		}	}	static class IdentifierCollectionSecondPass extends SecondPass {		IdentifierCollectionSecondPass(Element node, Mappings mappings, IdentifierCollection collection) {			super(node, mappings, collection);		}		void secondPass(java.util.Map persistentClasses) throws MappingException {			Binder.bindIdentifierCollectionSecondPass( node, (IdentifierCollection) collection, persistentClasses, mappings );		}	}	static class MapSecondPass extends SecondPass {		MapSecondPass(Element node, Mappings mappings, Map collection) {			super(node, mappings, collection);		}		void secondPass(java.util.Map persistentClasses) throws MappingException {			Binder.bindMapSecondPass( node, (Map) collection, persistentClasses, mappings );		}	}	static class SetSecondPass extends SecondPass {		SetSecondPass(Element node, Mappings mappings, Set collection) {			super(node, mappings, collection);		}		void secondPass(java.util.Map persistentClasses) throws MappingException {			Binder.bindSetSecondPass( node, (Set) collection, persistentClasses, mappings );		}	}	static class ListSecondPass extends SecondPass {		ListSecondPass(Element node, Mappings mappings, List collection) {			super(node, mappings, collection);		}		void secondPass(java.util.Map persistentClasses) throws MappingException {			Binder.bindListSecondPass( node, (List) collection, persistentClasses, mappings );		}	}	//This inner class implements a case statement....perhaps im being a bit over-clever here	abstract static class CollectionType {		private String xmlTag;		public abstract Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException;		CollectionType(String xmlTag) {			this.xmlTag = xmlTag;		}		public String toString() {			return xmlTag;		}		private static final CollectionType MAP = new CollectionType("map") {			public Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException {				Map map = new Map(owner);				Binder.bindCollection(node, map, prefix, mappings);				return map;			}		};		private static final CollectionType SET = new CollectionType("set") {			public Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException {				Set set = new Set(owner);				Binder.bindCollection(node, set, prefix, mappings);				return set;			}		};		private static final CollectionType LIST = new CollectionType("list") {			public Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException {				List list = new List(owner);				Binder.bindCollection(node, list, prefix, mappings);				return list;			}		};		private static final CollectionType BAG = new CollectionType("bag") {			public Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException {				Bag bag = new Bag(owner);				Binder.bindCollection(node, bag, prefix, mappings);				return bag;			}		};		private static final CollectionType IDBAG = new CollectionType("idbag") {			public Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException {				IdentifierBag bag = new IdentifierBag(owner);				Binder.bindCollection(node, bag, prefix, mappings);				return bag;			}		};		private static final CollectionType ARRAY = new CollectionType("array") {			public Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException {				Array array = new Array(owner);				Binder.bindArray(node, array, prefix, mappings);				return array;			}		};		private static final CollectionType PRIMITIVE_ARRAY = new CollectionType("primitive-array") {			public Collection create(Element node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException {				PrimitiveArray array = new PrimitiveArray(owner);				Binder.bindArray(node, array, prefix, mappings);				return array;			}		};		private static final HashMap INSTANCES = new HashMap();		static {			INSTANCES.put(MAP.toString(), MAP);			INSTANCES.put(BAG.toString(), BAG);			INSTANCES.put(IDBAG.toString(), IDBAG);			INSTANCES.put(SET.toString(), SET);			INSTANCES.put(LIST.toString(), LIST);			INSTANCES.put(ARRAY.toString(), ARRAY);			INSTANCES.put(PRIMITIVE_ARRAY.toString(), PRIMITIVE_ARRAY);		}		public static CollectionType collectionTypeFromString(String xmlTagName) {			return (CollectionType) INSTANCES.get(xmlTagName);		}	}	private static int getOptimisticLockMode(Attribute olAtt) throws MappingException {				if (olAtt==null) return Versioning.OPTIMISTIC_LOCK_VERSION;		String olMode = olAtt.getValue();		if ( olMode==null || "version".equals(olMode) ) {			return Versioning.OPTIMISTIC_LOCK_VERSION;		}		else if ( "dirty".equals(olMode) ) {			return Versioning.OPTIMISTIC_LOCK_DIRTY;		}		else if ( "all".equals(olMode) ) {			return Versioning.OPTIMISTIC_LOCK_ALL;		}		else if ( "none".equals(olMode) ) {			return Versioning.OPTIMISTIC_LOCK_NONE;		}		else {			throw new MappingException("Unsupported optimistic-lock style: " + olMode);		}	}		private static final java.util.Map getMetas(Element node) {		java.util.Map map = new HashMap();		Iterator iter = node.elementIterator("meta");		while ( iter.hasNext() ) {			Element metaNode = (Element) iter.next();			String name = metaNode.attributeValue("attribute");			MetaAttribute meta = (MetaAttribute) map.get(name);			if (meta==null) {				meta = new MetaAttribute();				map.put(name, meta);			}			meta.addValue( metaNode.getText() );		}		return map;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -