Hi,

I had some problems using a custom DocumentFactory (extending the
DOMDocumentFactory) and DOM4J. The problem was that DOM4J wasn't realy using
my DocumentFactory, but the default factory (in this case the
DOMDocumentFactory) or didn't use a factory at all! I have fixed this with
the 2 following patches (one for DOMElement and one for DOMDocument, both in
the org.dom4j.dom package). I have also included these patches as
attachment.

Index: DOMElement.java
===================================================================
RCS file: /cvsroot/dom4j/dom4j/src/java/org/dom4j/dom/DOMElement.java,v
retrieving revision 1.13
diff -u -w -r1.13 DOMElement.java
--- DOMElement.java 2001/08/14 13:21:10 1.13
+++ DOMElement.java 2001/12/20 23:06:36
@@ -299,7 +299,9 @@
     // Implementation methods

//-------------------------------------------------------------------------
     protected DocumentFactory getDocumentFactory() {
-        return DOCUMENT_FACTORY;
+        DocumentFactory factory = getQName().getDocumentFactory();
+        return ( factory != null ) ? factory : DOCUMENT_FACTORY;
+
     }

     protected Attribute attribute(org.w3c.dom.Attr attr) {

Index: DOMDocument.java
===================================================================
RCS file: /cvsroot/dom4j/dom4j/src/java/org/dom4j/dom/DOMDocument.java,v
retrieving revision 1.6
diff -u -w -r1.6 DOMDocument.java
--- DOMDocument.java 2001/06/20 18:59:23 1.6
+++ DOMDocument.java 2001/12/20 23:13:08

@@ -35,7 +36,7 @@
 public class DOMDocument extends DefaultDocument implements
org.w3c.dom.Document {

     /** The <code>DocumentFactory</code> instance used by default */
-    private static final DocumentFactory DOCUMENT_FACTORY =
DOMDocumentFactory.getInstance();
+    private static final DOMDocumentFactory DOCUMENT_FACTORY =
(DOMDocumentFactory) DOMDocumentFactory.getInstance();


     public DOMDocument() {

@@ -192,7 +193,12 @@
     }

     public org.w3c.dom.DOMImplementation getImplementation() {
-        return DOMDocumentFactory.singleton;
+        if (getDocumentFactory() instanceof org.w3c.dom.DOMImplementation)
{
+            return (org.w3c.dom.DOMImplementation) getDocumentFactory();
+        } else {
+            return DOCUMENT_FACTORY;
+        }
+
     }

     public org.w3c.dom.Element getDocumentElement() {

@@ -200,7 +206,7 @@
     }

     public org.w3c.dom.Element createElement(String tagName) throws
DOMException {
-        return new DOMElement(tagName);
+        return (org.w3c.dom.Element)
getDocumentFactory().createElement(tagName);
     }

     public org.w3c.dom.DocumentFragment createDocumentFragment() {

@@ -209,29 +215,30 @@
     }

     public org.w3c.dom.Text createTextNode(String data) {
-        return new DOMText(data);
+        return (org.w3c.dom.Text) getDocumentFactory().createText(data);
     }

     public org.w3c.dom.Comment createComment(String data) {
-        return new DOMComment(data);
+        return (org.w3c.dom.Comment)
getDocumentFactory().createComment(data);
     }

     public org.w3c.dom.CDATASection createCDATASection(String data) throws
DOMException {
-        return new DOMCDATA(data);
+        return (org.w3c.dom.CDATASection)
getDocumentFactory().createCDATA(data);
     }

     public org.w3c.dom.ProcessingInstruction createProcessingInstruction(
         String target, String data
     ) throws DOMException {
-        return new DOMProcessingInstruction(target, data);
+        return (org.w3c.dom.ProcessingInstruction)
getDocumentFactory().createProcessingInstruction(target, data);
     }

     public org.w3c.dom.Attr createAttribute(String name) throws
DOMException {
-        return new DOMAttribute( DOCUMENT_FACTORY.createQName(name) );
+        QName qname = getDocumentFactory().createQName(name);
+        return (org.w3c.dom.Attr)
getDocumentFactory().createAttribute(null, qname, null);
     }

     public org.w3c.dom.EntityReference createEntityReference(String name)
throws DOMException {
-        return new DOMEntityReference(name);
+        return (org.w3c.dom.EntityReference) ((DOMDocumentFactory)
getDocumentFactory()).createEntity(name);
     }

     public org.w3c.dom.Node importNode(

@@ -244,15 +251,15 @@
     public org.w3c.dom.Element createElementNS(
         String namespaceURI, String qualifiedName
     ) throws DOMException {
-        QName qname = DOCUMENT_FACTORY.createQName( qualifiedName,
namespaceURI );
-        return new DOMElement( qname );
+        QName qname = getDocumentFactory().createQName( qualifiedName,
namespaceURI );
+        return (org.w3c.dom.Element)
getDocumentFactory().createElement(qname);
     }

     public org.w3c.dom.Attr createAttributeNS(
         String namespaceURI, String qualifiedName
     ) throws DOMException {
-        QName qname = DOCUMENT_FACTORY.createQName( qualifiedName,
namespaceURI );
-        return new DOMAttribute( qname );
+        QName qname = getDocumentFactory().createQName( qualifiedName,
namespaceURI );
+        return (org.w3c.dom.Attr)
getDocumentFactory().createAttribute(null, qname, null);
     }



@@ -261,13 +268,15 @@
     }


-
     // Implementation methods

//-------------------------------------------------------------------------
     protected DocumentFactory getDocumentFactory() {
+        if (super.getDocumentFactory() == null) {
         return DOCUMENT_FACTORY;
+        } else {
+            return super.getDocumentFactory();
         }
-
+    }
 }


Regards,
Maarten
Index: DOMElement.java
===================================================================
RCS file: /cvsroot/dom4j/dom4j/src/java/org/dom4j/dom/DOMElement.java,v
retrieving revision 1.13
diff -u -w -r1.13 DOMElement.java
--- DOMElement.java     2001/08/14 13:21:10     1.13
+++ DOMElement.java     2001/12/20 23:06:36
@@ -299,7 +299,9 @@
     // Implementation methods

     //-------------------------------------------------------------------------       
     

     protected DocumentFactory getDocumentFactory() {

-        return DOCUMENT_FACTORY;

+        DocumentFactory factory = getQName().getDocumentFactory();
+        return ( factory != null ) ? factory : DOCUMENT_FACTORY;
+
     }

     

     protected Attribute attribute(org.w3c.dom.Attr attr) {



Index: DOMDocument.java
===================================================================
RCS file: /cvsroot/dom4j/dom4j/src/java/org/dom4j/dom/DOMDocument.java,v
retrieving revision 1.6
diff -u -w -r1.6 DOMDocument.java
--- DOMDocument.java    2001/06/20 18:59:23     1.6
+++ DOMDocument.java    2001/12/20 23:13:08

@@ -35,7 +36,7 @@
 public class DOMDocument extends DefaultDocument implements org.w3c.dom.Document {
 
     /** The <code>DocumentFactory</code> instance used by default */
-    private static final DocumentFactory DOCUMENT_FACTORY = 
DOMDocumentFactory.getInstance();
+    private static final DOMDocumentFactory DOCUMENT_FACTORY = (DOMDocumentFactory) 
+DOMDocumentFactory.getInstance();
    
 
     public DOMDocument() { 

@@ -192,7 +193,12 @@
     }
 
     public org.w3c.dom.DOMImplementation getImplementation() {
-        return DOMDocumentFactory.singleton;
+        if (getDocumentFactory() instanceof org.w3c.dom.DOMImplementation) {
+            return (org.w3c.dom.DOMImplementation) getDocumentFactory();
+        } else {
+            return DOCUMENT_FACTORY;
+        }
+
     }
 
     public org.w3c.dom.Element getDocumentElement() {

@@ -200,7 +206,7 @@
     }
 
     public org.w3c.dom.Element createElement(String tagName) throws DOMException {
-        return new DOMElement(tagName);
+        return (org.w3c.dom.Element) getDocumentFactory().createElement(tagName);
     }
 
     public org.w3c.dom.DocumentFragment createDocumentFragment() {

@@ -209,29 +215,30 @@
     }
 
     public org.w3c.dom.Text createTextNode(String data) {
-        return new DOMText(data);
+        return (org.w3c.dom.Text) getDocumentFactory().createText(data);
     }
 
     public org.w3c.dom.Comment createComment(String data) {
-        return new DOMComment(data);
+        return (org.w3c.dom.Comment) getDocumentFactory().createComment(data);
     }
 
     public org.w3c.dom.CDATASection createCDATASection(String data) throws 
DOMException {
-        return new DOMCDATA(data);
+        return (org.w3c.dom.CDATASection) getDocumentFactory().createCDATA(data);
     }

     public org.w3c.dom.ProcessingInstruction createProcessingInstruction(
         String target, String data
     ) throws DOMException {
-        return new DOMProcessingInstruction(target, data);
+        return (org.w3c.dom.ProcessingInstruction) 
+getDocumentFactory().createProcessingInstruction(target, data);
     }
 
     public org.w3c.dom.Attr createAttribute(String name) throws DOMException {
-        return new DOMAttribute( DOCUMENT_FACTORY.createQName(name) );
+        QName qname = getDocumentFactory().createQName(name);
+        return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null, qname, 
+null);
     }
     
     public org.w3c.dom.EntityReference createEntityReference(String name) throws 
DOMException {
-        return new DOMEntityReference(name);
+        return (org.w3c.dom.EntityReference) ((DOMDocumentFactory) 
+getDocumentFactory()).createEntity(name);
     }
 
     public org.w3c.dom.Node importNode(

@@ -244,15 +251,15 @@
     public org.w3c.dom.Element createElementNS(
         String namespaceURI, String qualifiedName
     ) throws DOMException {
-        QName qname = DOCUMENT_FACTORY.createQName( qualifiedName, namespaceURI );
-        return new DOMElement( qname );
+        QName qname = getDocumentFactory().createQName( qualifiedName, namespaceURI );
+        return (org.w3c.dom.Element) getDocumentFactory().createElement(qname);
     }

     public org.w3c.dom.Attr createAttributeNS(
         String namespaceURI, String qualifiedName
     ) throws DOMException {
-        QName qname = DOCUMENT_FACTORY.createQName( qualifiedName, namespaceURI );
-        return new DOMAttribute( qname );
+        QName qname = getDocumentFactory().createQName( qualifiedName, namespaceURI );
+        return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null, qname, 
+null);
     }



@@ -261,13 +268,15 @@
     }
   
     
-    
     // Implementation methods
     //-------------------------------------------------------------------------       
     
     protected DocumentFactory getDocumentFactory() {
+        if (super.getDocumentFactory() == null) {
         return DOCUMENT_FACTORY;
+        } else {
+            return super.getDocumentFactory();
         }
-    
+    }
 }

Reply via email to