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

📄 xmldatabinder.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		return (org.w3c.dom.Document) result.getNode();	}		public Databinder bind(Object object) {		objects.add(object);		return this;	}		public Databinder bindAll(Collection collection) {		this.objects.addAll(collection);		return this;	}		public org.w3c.dom.Document toGenericDOM() throws HibernateException {		DOMWriter outputter = new DOMWriter();		try {			return outputter.write( toDocument() );		}		catch (DocumentException jde) {			throw new HibernateException( "Could not transform XML to a DOM", jde );		}	}	public static Templates getOutputStyleSheetTemplates(Properties properties) {		String xsltInputFile = properties.getProperty(Environment.OUTPUT_STYLESHEET);		Templates templates = null;		try {			InputStream stream;			if (xsltInputFile!=null) {				// try getting stylesheet from the classpath first				stream = Environment.class.getResourceAsStream(xsltInputFile);				if (stream == null) {					// not found, so try getting it from the filesystem					stream = new FileInputStream(xsltInputFile);				}			}			else {				stream = Environment.class.getClassLoader().getResourceAsStream("net/sf/hibernate/hibernate-default.xslt");			}			templates = TransformerFactory.newInstance().newTemplates( new StreamSource(stream) );		}		catch (Exception e) {			log.warn("Problem opening output stylesheet - databinding disabled", e);			//throw new HibernateException( "Problem opening output stylesheet: " + e.getMessage() );		}		catch (TransformerFactoryConfigurationError tfce) {			log.warn("no XSLT implementation found - databinding disabled");		}		return templates;			}		private Element renderProperty(String name, Type type, Object value, String componentName, String propertyName, String collectionName, boolean doType) throws HibernateException {		if ( type.isComponentType() ) {			return renderComponentType(name, type, value, componentName, doType);		}		else if ( type.isPersistentCollectionType() ) {			return renderCollectionType(name, type, value, collectionName, doType);		}		else if ( type.isEntityType() ) {			return renderEntityType(name, type, value, propertyName, doType);		}		else {			return renderOtherType(name, type, value, propertyName, doType);		}	}		private Element renderOtherType(String name, Type type, Object value, String propertyName, boolean doType)	throws HibernateException {				Element propertyElem = DocumentHelper.createElement(propertyName);		if ( name!=null) propertyElem.setAttributeValue( "name", name );		//propertyElem.setAttributeValue( "value", types[i].toXML( values[i] ) );		if ( value!=null ) {			String xmlValue = type.toString( value, factory );			if ( type instanceof StringType ) { //TODO: make this polymorphic!!				propertyElem.addCDATA(xmlValue);			}			else {				propertyElem.setText(xmlValue);			}		}		if (doType) propertyElem.setAttributeValue( "type", type.getName() );		return propertyElem;	}	private Element renderEntityType(String name, Type type, Object value, String propertyName, boolean doType)	throws HibernateException {				Element referenceElem = DocumentHelper.createElement(propertyName);		if ( name!=null) referenceElem.setAttributeValue( "name", name );		//propertyElem.setAttributeValue( "value", types[i].toXML( values[i] ) );		if ( ( value = maybeInitializeIfProxy(value, referenceElem) ) != null) {			ClassPersister persister = getPersister( value.getClass() );			if ( persister.hasIdentifierPropertyOrEmbeddedCompositeIdentifier() ) {				Type idType = persister.getIdentifierType();				Object id = persister.getIdentifier(value);				referenceElem.add( renderProperty( persister.getIdentifierPropertyName(), idType, id, "composite-id", "id", null, true) );			}			addClass( referenceElem, value.getClass() );			// avoid duplications (including objects that have a field referencing to themselves)			if ( !processedObjects.contains(value) && !objects.contains(value) ) {				associatedObjects.add(value);			}		}		if (doType) referenceElem.setAttributeValue( "type", type.getName() );		return referenceElem;	}	private Element renderCollectionType(String name, Type type, Object value, String collectionName, boolean doType)	throws HibernateException {				PersistentCollectionType collectiontype = (PersistentCollectionType) type;		String role = collectiontype.getRole();		CollectionPersister persister = factory.getCollectionPersister(role);		Element collectionElem = DocumentHelper.createElement(collectionName);		if (name!=null) collectionElem.setAttributeValue( "name", name );		if ( persister.isArray() ) {			collectionElem.setName("array");		}		else {			if (doType) collectionElem.setAttributeValue( "class", type.getName() );		}		Type elemType = persister.getElementType();		collectionElem.setAttributeValue( "element-type", elemType.getName() );		if (value!=null) {			// arrays can't be lazily initialized			if ( persister.isArray() ) {				collectionElem.setAttributeValue( "index-type", "integer" );				int length = Array.getLength(value);				for ( int i=0; i<length; i++ ) {					Element elemElement = renderProperty(null, elemType, Array.get(value, i), "composite-element", "element", "subcollection", false);					elemElement.setAttributeValue( "index", Integer.toString(i) );					collectionElem.add(elemElement);				}			}			else {				// "real" collections				PersistentCollection persistentCollection = (PersistentCollection) value;				if ( persister.isLazy() && !this.initializeLazy && !persistentCollection.wasInitialized() ) {					collectionElem.setAttributeValue("lazy", "uninitialized");				}				else {					if ( persistentCollection.wasInitialized() ) {						collectionElem.setAttributeValue("lazy", "initialized");					}					else {						collectionElem.setAttributeValue("lazy", "now-initialized");					}					// Try to do this next bit polymorphically, instead of the following:					if ( type instanceof ListType ) {						collectionElem.setAttributeValue( "index-type", "integer" );						Iterator iter = ( (List) value ).iterator();						int i=0;						while ( iter.hasNext() ) {							Element elemElement = renderProperty(null, elemType, iter.next(), "composite-element", "element", "subcollection", false);							elemElement.setAttributeValue( "index", Integer.toString(i++) );							collectionElem.add(elemElement);						}					}					else if ( (type instanceof SetType) || (type instanceof BagType) ) {						Iterator iter = ( (Collection) value ).iterator();						while ( iter.hasNext() ) {							Element elemElement = renderProperty(null, elemType, iter.next(), "composite-element", "element", "subcollection", false);							collectionElem.add(elemElement);						}					}					else if ( type instanceof MapType ) {						Type indexType = persister.getIndexType();						collectionElem.setAttributeValue( "index-type", indexType.getName() );						Iterator iter = ( (Map) value ).entrySet().iterator();						while ( iter.hasNext() ) {							Map.Entry e = (Map.Entry) iter.next();							Object idx = e.getKey();							Element elemElement = renderProperty(null, elemType, e.getValue(), "composite-element", "element", "subcollection", false);							elemElement.setAttributeValue( "index", indexType.toString(idx, factory) ); //index not allowed to be null currently							collectionElem.add(elemElement);						}					}				}			}		}		return collectionElem;	}	private Element renderComponentType(String name, Type type, Object value, String componentName, boolean doType)	throws HibernateException {		AbstractComponentType componenttype = (AbstractComponentType) type;		Element componentElem = DocumentHelper.createElement(componentName);		if ( name!=null) componentElem.setAttributeValue( "name", name );		if (doType) componentElem.setAttributeValue( "class", type.getName() );		if ( value!=null ) {			String[] properties = componenttype.getPropertyNames();			Object[] subvalues = componenttype.getPropertyValues(value, null); //We know that null is okay here .. at least for ComponentType .... TODO: something safer??			Type[] subtypes = componenttype.getSubtypes();			for ( int j=0; j<properties.length; j++ ) {				componentElem.add( renderProperty( properties[j], subtypes[j], subvalues[j], "component", "property", "collection", true ) );			}		}		return componentElem;	}	}

⌨️ 快捷键说明

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