|
From: <one...@us...> - 2003-02-09 06:28:20
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/xml
In directory sc8-pr-cvs1:/tmp/cvs-serv20713/hibernate/xml
Modified Files:
XMLDatabinder.java
Log Message:
standardised on dom4j
fixed bugs in collection caching (sometimes an exception occurred)
allowed null discriminators
set autocommit to true in SchemaUpdate
collections now deserialize correctly
Index: XMLDatabinder.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/xml/XMLDatabinder.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** XMLDatabinder.java 14 Jan 2003 13:42:19 -0000 1.5
--- XMLDatabinder.java 9 Feb 2003 06:28:17 -0000 1.6
***************
*** 2,6 ****
package net.sf.hibernate.xml;
! import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Array;
--- 2,6 ----
package net.sf.hibernate.xml;
! import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Array;
***************
*** 21,25 ****
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
- import javax.xml.transform.stream.StreamSource;
import net.sf.hibernate.Databinder;
--- 21,24 ----
***************
*** 45,53 ****
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
! import org.jdom.Document;
! import org.jdom.Element;
! import org.jdom.JDOMException;
! import org.jdom.output.DOMOutputter;
! import org.jdom.output.XMLOutputter;
/**
--- 44,55 ----
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
! import org.dom4j.Document;
! import org.dom4j.DocumentException;
! import org.dom4j.DocumentHelper;
! import org.dom4j.Element;
! import org.dom4j.io.DOMWriter;
! import org.dom4j.io.DocumentSource;
! import org.dom4j.io.OutputFormat;
! import org.dom4j.io.XMLWriter;
/**
***************
*** 78,88 ****
}
! private Document toJDOMDocument() throws HibernateException {
this.associatedObjects = new HashSet();
this.processedObjects = new HashSet();
! Element root = new Element("hibernate-generic");
! root.setAttribute("datetime", Hibernate.TIMESTAMP.toXML( new Date(), factory) );
! Document doc = new Document(root);
Iterator iter = null;
--- 80,90 ----
}
! private Document toDocument() throws HibernateException {
this.associatedObjects = new HashSet();
this.processedObjects = new HashSet();
! Document doc = DocumentHelper.createDocument();
! Element root = doc.addElement("hibernate-generic");
! root.setAttributeValue("datetime", Hibernate.TIMESTAMP.toXML( new Date(), factory) );
Iterator iter = null;
***************
*** 92,102 ****
while ( iter.hasNext() ) {
Object object = iter.next();
! Element objectElem = new Element("object");
! root.addContent(objectElem);
if ((object = maybeInitializeIfProxy(object, objectElem)) != null) {
String className = object.getClass().getName();
! objectElem.setAttribute( "class", StringHelper.unqualify(className) );
! objectElem.setAttribute( "package", StringHelper.qualifier(className) );
ClassPersister persister = getPersister( object.getClass() );
//ID
--- 94,103 ----
while ( iter.hasNext() ) {
Object object = iter.next();
! Element objectElem = root.addElement("object");
if ((object = maybeInitializeIfProxy(object, objectElem)) != null) {
String className = object.getClass().getName();
! objectElem.setAttributeValue( "class", StringHelper.unqualify(className) );
! objectElem.setAttributeValue( "package", StringHelper.qualifier(className) );
ClassPersister persister = getPersister( object.getClass() );
//ID
***************
*** 105,113 ****
String idValue = idType.toXML( persister.getIdentifier(object), factory );
//objectElem.setAttribute("uid", idValue);
! Element idElem = new Element("uid");
! idElem.setAttribute( "name", persister.getIdentifierPropertyName() );
! idElem.setAttribute( "type", idType.getName() );
! idElem.addContent(idValue);
! objectElem.addContent(idElem);
}
//PROPERTIES
--- 106,113 ----
String idValue = idType.toXML( persister.getIdentifier(object), factory );
//objectElem.setAttribute("uid", idValue);
! Element idElem = objectElem.addElement("uid");
! idElem.setAttributeValue( "name", persister.getIdentifierPropertyName() );
! idElem.setAttributeValue( "type", idType.getName() );
! idElem.setText(idValue);
}
//PROPERTIES
***************
*** 117,121 ****
//This approach wont work for components + collections
for ( int i=0; i<types.length; i++ ) {
! objectElem.addContent( renderProperty( names[i], types[i], values[i], "component", "property", "collection", true ) );
}
}
--- 117,121 ----
//This approach wont work for components + collections
for ( int i=0; i<types.length; i++ ) {
! objectElem.add( renderProperty( names[i], types[i], values[i], "component", "property", "collection", true ) );
}
}
***************
*** 140,153 ****
if (li.isUninitialized() && !this.initializeLazy) {
! element.setAttribute("proxy", "uninitialized");
! element.setAttribute( "uid", li.getIdentifier().toString() );
return null;
}
if (li.isUninitialized()) {
! element.setAttribute("proxy", "now-initialized");
}
else {
! element.setAttribute("proxy", "initialized");
}
--- 140,153 ----
if (li.isUninitialized() && !this.initializeLazy) {
! element.setAttributeValue("proxy", "uninitialized");
! element.setAttributeValue( "uid", li.getIdentifier().toString() );
return null;
}
if (li.isUninitialized()) {
! element.setAttributeValue("proxy", "now-initialized");
}
else {
! element.setAttributeValue("proxy", "initialized");
}
***************
*** 156,163 ****
public String toGenericXML() throws HibernateException {
! XMLOutputter outputter = new XMLOutputter();
! outputter.setNewlines(true);
! outputter.setIndent(true);
! return outputter.outputString( toJDOMDocument() );
}
--- 156,169 ----
public String toGenericXML() throws HibernateException {
! StringWriter writer = new StringWriter();
! XMLWriter outputter = new XMLWriter( writer, OutputFormat.createPrettyPrint() );
! //outputter.setNewlines(true);
! try {
! outputter.write( toDocument() );
! }
! catch (IOException ioe) {
! throw new HibernateException("could not XML to String", ioe);
! }
! return writer.toString();
}
***************
*** 165,171 ****
if ( type.isComponentType() ) {
AbstractComponentType componenttype = (AbstractComponentType) type;
! Element componentElem = new Element(componentName);
! if ( name!=null) componentElem.setAttribute( "name", name );
! if (doType) componentElem.setAttribute( "class", type.getName() );
if ( value!=null ) {
String[] properties = componenttype.getPropertyNames();
--- 171,177 ----
if ( type.isComponentType() ) {
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();
***************
*** 173,177 ****
Type[] subtypes = componenttype.getSubtypes();
for ( int j=0; j<properties.length; j++ ) {
! componentElem.addContent( renderProperty( properties[j], subtypes[j], subvalues[j], "component", "property", "collection", true ) );
}
}
--- 179,183 ----
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 ) );
}
}
***************
*** 182,205 ****
String role = collectiontype.getRole();
CollectionPersister persister = factory.getCollectionPersister(role);
! Element collectionElem = new Element(collectionName);
! if (name!=null) collectionElem.setAttribute( "name", name );
if ( persister.isArray() ) {
collectionElem.setName("array");
}
else {
! if (doType) collectionElem.setAttribute( "class", type.getName() );
}
Type elemType = persister.getElementType();
! collectionElem.setAttribute( "element-type", elemType.getName() );
if (value!=null) {
// arrays can't be lazily initialized
if ( persister.isArray() ) {
! collectionElem.setAttribute( "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.setAttribute( "index", Integer.toString(i) );
! collectionElem.addContent(elemElement);
}
}
--- 188,211 ----
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);
}
}
***************
*** 209,231 ****
if ( persister.isLazy() && !this.initializeLazy && !persistentCollection.wasInitialized() ) {
! collectionElem.setAttribute("lazy", "uninitialized");
}
else {
if ( persistentCollection.wasInitialized() ) {
! collectionElem.setAttribute("lazy", "initialized");
}
else {
! collectionElem.setAttribute("lazy", "now-initialized");
}
// Try to do this next bit polymorphically, instead of the following:
if ( type instanceof ListType ) {
! collectionElem.setAttribute( "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.setAttribute( "index", Integer.toString(i++) );
! collectionElem.addContent(elemElement);
}
}
--- 215,237 ----
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);
}
}
***************
*** 234,243 ****
while ( iter.hasNext() ) {
Element elemElement = renderProperty(null, elemType, iter.next(), "composite-element", "element", "subcollection", false);
! collectionElem.addContent(elemElement);
}
}
else if ( type instanceof MapType ) {
Type indexType = persister.getIndexType();
! collectionElem.setAttribute( "index-type", indexType.getName() );
Iterator iter = ( (Map) value ).entrySet().iterator();
while ( iter.hasNext() ) {
--- 240,249 ----
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() ) {
***************
*** 245,250 ****
Object idx = e.getKey();
Element elemElement = renderProperty(null, elemType, e.getValue(), "composite-element", "element", "subcollection", false);
! elemElement.setAttribute( "index", indexType.toXML(idx, factory) ); //index not allowed to be null currently
! collectionElem.addContent(elemElement);
}
}
--- 251,256 ----
Object idx = e.getKey();
Element elemElement = renderProperty(null, elemType, e.getValue(), "composite-element", "element", "subcollection", false);
! elemElement.setAttributeValue( "index", indexType.toXML(idx, factory) ); //index not allowed to be null currently
! collectionElem.add(elemElement);
}
}
***************
*** 255,268 ****
}
else if ( type.isEntityType() ) {
! Element referenceElem = new Element(propertyName);
! if ( name!=null) referenceElem.setAttribute( "name", name );
! //propertyElem.setAttribute( "value", types[i].toXML( values[i] ) );
if ( ( value = maybeInitializeIfProxy(value, referenceElem) ) != null) {
if ( getPersister( value.getClass() ).hasIdentifierProperty() )
! referenceElem.setAttribute( "uid", type.toXML( value, factory ) );
String className = value.getClass().getName();
! referenceElem.setAttribute( "class", StringHelper.unqualify(className) );
! referenceElem.setAttribute( "package", StringHelper.qualifier(className) );
// avoid duplications (including objects that have a field referencing to themselves)
--- 261,274 ----
}
else if ( type.isEntityType() ) {
! 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) {
if ( getPersister( value.getClass() ).hasIdentifierProperty() )
! referenceElem.setAttributeValue( "uid", type.toXML( value, factory ) );
String className = value.getClass().getName();
! referenceElem.setAttributeValue( "class", StringHelper.unqualify(className) );
! referenceElem.setAttributeValue( "package", StringHelper.qualifier(className) );
// avoid duplications (including objects that have a field referencing to themselves)
***************
*** 271,283 ****
}
}
! if (doType) referenceElem.setAttribute( "type", type.getName() );
return referenceElem;
}
else {
! Element propertyElem = new Element(propertyName);
! if ( name!=null) propertyElem.setAttribute( "name", name );
! //propertyElem.setAttribute( "value", types[i].toXML( values[i] ) );
! if ( value!=null ) propertyElem.addContent( type.toXML( value, factory ) );
! if (doType) propertyElem.setAttribute( "type", type.getName() );
return propertyElem;
}
--- 277,289 ----
}
}
! if (doType) referenceElem.setAttributeValue( "type", type.getName() );
return referenceElem;
}
else {
! Element propertyElem = DocumentHelper.createElement(propertyName);
! if ( name!=null) propertyElem.setAttributeValue( "name", name );
! //propertyElem.setAttributeValue( "value", types[i].toXML( values[i] ) );
! if ( value!=null ) propertyElem.setText( type.toXML( value, factory ) );
! if (doType) propertyElem.setAttributeValue( "type", type.getName() );
return propertyElem;
}
***************
*** 285,292 ****
public String toXML() throws HibernateException, TransformerException {
! // According to this link it is faster to pass Xalan a stream than
! // a DOM tree:
! // http://www-106.ibm.com/developerworks/xml/library/x-tipjdom.html
! Source source = new StreamSource( new StringReader( toGenericXML() ) );
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
--- 291,296 ----
public String toXML() throws HibernateException, TransformerException {
!
! Source source = new DocumentSource( toDocument() );
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
***************
*** 314,318 ****
public org.w3c.dom.Document toDOM() throws HibernateException, TransformerException {
! Source source = new StreamSource( new StringReader( toGenericXML() ) );
DOMResult result = new DOMResult();
transform.transform(source, result);
--- 318,322 ----
public org.w3c.dom.Document toDOM() throws HibernateException, TransformerException {
! Source source = new DocumentSource( toDocument() );
DOMResult result = new DOMResult();
transform.transform(source, result);
***************
*** 331,340 ****
public org.w3c.dom.Document toGenericDOM() throws HibernateException {
! DOMOutputter outputter = new DOMOutputter();
try {
! return outputter.output( toJDOMDocument() );
}
! catch (JDOMException jde) {
! throw new HibernateException( "JDOM Exception", jde );
}
}
--- 335,344 ----
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 );
}
}
|