You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2012/07/18 16:53:33 UTC

svn commit: r1362971 - in /webservices/axiom/branches/AXIOM-435/modules: axiom-api/src/main/java/org/apache/axiom/om/ axiom-api/src/test/java/org/apache/axiom/om/ axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/ axiom-testsuite/...

Author: veithen
Date: Wed Jul 18 14:53:33 2012
New Revision: 1362971

URL: http://svn.apache.org/viewvc?rev=1362971&view=rev
Log:
More unfinished code.

Modified:
    webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java
    webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java
    webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/test/java/org/apache/axiom/om/AbstractTestCase.java
    webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java
    webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/DOMXMLStreamReader.java
    webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
    webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMSource.java

Modified: webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java?rev=1362971&r1=1362970&r2=1362971&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java (original)
+++ webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java Wed Jul 18 14:53:33 2012
@@ -21,11 +21,13 @@ package org.apache.axiom.om;
 
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
 
 import org.apache.axiom.om.util.StAXParserConfiguration;
 import org.apache.axiom.soap.SOAPFactory;
 import org.apache.axiom.soap.SOAPModelBuilder;
 import org.apache.axiom.util.stax.xop.MimePartProvider;
+import org.w3c.dom.EntityReference;
 import org.xml.sax.InputSource;
 
 /**
@@ -112,6 +114,31 @@ public interface OMMetaFactory {
     OMXMLParserWrapper createOMBuilder(OMFactory omFactory, Source source);
     
     /**
+     * Create an object model builder for plain XML that gets its input from a {@link DOMSource}.
+     * 
+     * @param omFactory
+     *            The object model factory to use. This factory must be obtained from the same
+     *            {@link OMMetaFactory} instance as the one used to invoke this method. In general
+     *            the factory will be retrieved from {@link #getOMFactory()}), but in some cases it
+     *            may be necessary to pass a {@link SOAPFactory} instance, although this method will
+     *            never produce a SOAP infoset.
+     * @param expandEntityReferences
+     *            Determines how {@link EntityReference} nodes are handled:
+     *            <ul>
+     *            <li>If the parameter is <code>false</code> then a single {@link OMEntityReference}
+     *            will be created for each {@link EntityReference}. The child nodes of
+     *            {@link EntityReference} nodes are not taken into account.
+     *            <li>If the parameter is <code>true</code> then no {@link OMEntityReference} nodes
+     *            are created and the children of {@link EntityReference} nodes are converted and
+     *            inserted into the Axiom tree.
+     *            </ul>
+     * @param source
+     *            the source of the XML document
+     * @return the builder
+     */
+    OMXMLParserWrapper createOMBuilder(OMFactory omFactory, DOMSource source, boolean expandEntityReferences);
+    
+    /**
      * Create an XOP aware object model builder.
      * 
      * @param configuration

Modified: webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java?rev=1362971&r1=1362970&r2=1362971&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java (original)
+++ webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java Wed Jul 18 14:53:33 2012
@@ -26,12 +26,14 @@ import javax.mail.internet.ParseExceptio
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
 
 import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.impl.builder.OMAttachmentAccessorMimePartProvider;
 import org.apache.axiom.om.util.StAXParserConfiguration;
 import org.apache.axiom.soap.SOAPFactory;
 import org.apache.axiom.soap.SOAPModelBuilder;
+import org.w3c.dom.EntityReference;
 import org.xml.sax.InputSource;
 
 /**
@@ -293,6 +295,29 @@ public class OMXMLBuilderFactory {
     }
     
     /**
+     * Create an object model builder that reads a plain XML document from the provided
+     * {@link DOMSource}.
+     * 
+     * @param source
+     *            the source of the XML document
+     * @param expandEntityReferences
+     *            Determines how {@link EntityReference} nodes are handled:
+     *            <ul>
+     *            <li>If the parameter is <code>false</code> then a single {@link OMEntityReference}
+     *            will be created for each {@link EntityReference}. The child nodes of
+     *            {@link EntityReference} nodes are not taken into account.
+     *            <li>If the parameter is <code>true</code> then no {@link OMEntityReference} nodes
+     *            are created and the children of {@link EntityReference} nodes are converted and
+     *            inserted into the Axiom tree.
+     *            </ul>
+     * @return the builder
+     */
+    public static OMXMLParserWrapper createOMBuilder(DOMSource source, boolean expandEntityReferences) {
+        OMMetaFactory metaFactory = OMAbstractFactory.getMetaFactory();
+        return metaFactory.createOMBuilder(metaFactory.getOMFactory(), source, expandEntityReferences);
+    }
+    
+    /**
      * Create an object model builder that reads an XML document from the provided {@link Source}
      * using a specified object model factory.
      * 
@@ -303,11 +328,36 @@ public class OMXMLBuilderFactory {
      * @return the builder
      */
     // TODO: if the source is a SAXSource or DOMSource and the document has a DTD, then Axiom will remove the DTD (or even fail); fix this and/or specify the behavior
+    // TODO: update Javadoc to explain relationship with the methods that take a DOMSource or SAXSource
     public static OMXMLParserWrapper createOMBuilder(OMFactory omFactory, Source source) {
         return omFactory.getMetaFactory().createOMBuilder(omFactory, source);
     }
     
     /**
+     * Create an object model builder that reads an XML document from the provided {@link DOMSource}
+     * using a specified object model factory.
+     * 
+     * @param omFactory
+     *            the object model factory to use
+     * @param source
+     *            the source of the XML document
+     * @param expandEntityReferences
+     *            Determines how {@link EntityReference} nodes are handled:
+     *            <ul>
+     *            <li>If the parameter is <code>false</code> then a single {@link OMEntityReference}
+     *            will be created for each {@link EntityReference}. The child nodes of
+     *            {@link EntityReference} nodes are not taken into account.
+     *            <li>If the parameter is <code>true</code> then no {@link OMEntityReference} nodes
+     *            are created and the children of {@link EntityReference} nodes are converted and
+     *            inserted into the Axiom tree.
+     *            </ul>
+     * @return the builder
+     */
+    public static OMXMLParserWrapper createOMBuilder(OMFactory omFactory, DOMSource source, boolean expandEntityReferences) {
+        return omFactory.getMetaFactory().createOMBuilder(omFactory, source, expandEntityReferences);
+    }
+    
+    /**
      * Create an XOP aware model builder from the provided {@link Attachments} object and with a
      * given parser configuration.
      * 

Modified: webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/test/java/org/apache/axiom/om/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/test/java/org/apache/axiom/om/AbstractTestCase.java?rev=1362971&r1=1362970&r2=1362971&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/test/java/org/apache/axiom/om/AbstractTestCase.java (original)
+++ webservices/axiom/branches/AXIOM-435/modules/axiom-api/src/test/java/org/apache/axiom/om/AbstractTestCase.java Wed Jul 18 14:53:33 2012
@@ -88,9 +88,13 @@ public abstract class AbstractTestCase
     }
     
     public static Document toDocumentWithoutDTD(InputStream in) throws Exception {
+        return toDocumentWithoutDTD(in, false);
+    }
+    
+    public static Document toDocumentWithoutDTD(InputStream in, boolean replaceEntityReferences) throws Exception {
         // We use StAX to parse the document because in contrast to DOM, it allows references to undeclared entities.
         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
-        inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
+        inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.valueOf(replaceEntityReferences));
         XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
         XMLEventReader reader = inputFactory.createXMLEventReader(in);
         Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

Modified: webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java?rev=1362971&r1=1362970&r2=1362971&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java (original)
+++ webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java Wed Jul 18 14:53:33 2012
@@ -101,7 +101,7 @@ public abstract class AbstractOMMetaFact
         if (source instanceof SAXSource) {
             return new SAXOMXMLParserWrapper(omFactory, (SAXSource)source);
         } else if (source instanceof DOMSource) {
-            return new StAXOMBuilder(omFactory, new DOMXMLStreamReader(((DOMSource)source).getNode()));
+            return createOMBuilder(omFactory, (DOMSource)source, true);
         } else {
             try {
                 return new StAXOMBuilder(omFactory, StAXUtils.getXMLInputFactory().createXMLStreamReader(source));
@@ -111,6 +111,11 @@ public abstract class AbstractOMMetaFact
         }
     }
 
+    public OMXMLParserWrapper createOMBuilder(OMFactory omFactory, DOMSource source,
+            boolean expandEntityReferences) {
+        return new StAXOMBuilder(omFactory, new DOMXMLStreamReader(((DOMSource)source).getNode(), expandEntityReferences));
+    }
+
     public OMXMLParserWrapper createOMBuilder(StAXParserConfiguration configuration,
             OMFactory omFactory, InputSource rootPart, MimePartProvider mimePartProvider) {
         XOPAwareStAXOMBuilder builder = new XOPAwareStAXOMBuilder(omFactory, createXMLStreamReader(

Modified: webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/DOMXMLStreamReader.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/DOMXMLStreamReader.java?rev=1362971&r1=1362970&r2=1362971&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/DOMXMLStreamReader.java (original)
+++ webservices/axiom/branches/AXIOM-435/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/DOMXMLStreamReader.java Wed Jul 18 14:53:33 2012
@@ -32,6 +32,7 @@ import org.w3c.dom.ProcessingInstruction
 
 class DOMXMLStreamReader extends AbstractXMLStreamReader {
     private final Node root;
+    private final boolean expandEntityReferences;
     private Node node;
     private int event;
     private boolean attributesLoaded;
@@ -40,9 +41,10 @@ class DOMXMLStreamReader extends Abstrac
     private int namespaceCount;
     private Attr[] namespaces = new Attr[8];
 
-    public DOMXMLStreamReader(Node node) {
+    public DOMXMLStreamReader(Node node, boolean expandEntityReferences) {
         root = node;
         this.node = node;
+        this.expandEntityReferences = expandEntityReferences;
         event = START_DOCUMENT;
     }
 
@@ -51,9 +53,10 @@ class DOMXMLStreamReader extends Abstrac
     }
 
     public int next() throws XMLStreamException {
+        boolean forceTraverse = false;
         while (true) {
             boolean visited;
-            if (event == START_DOCUMENT || event == START_ELEMENT) {
+            if (event == START_DOCUMENT || event == START_ELEMENT || forceTraverse) {
                 Node firstChild = node.getFirstChild();
                 if (firstChild == null) {
                     visited = true;
@@ -61,6 +64,7 @@ class DOMXMLStreamReader extends Abstrac
                     node = firstChild;
                     visited = false;
                 }
+                forceTraverse = false;
             } else {
                 Node nextSibling = node.getNextSibling();
                 if (nextSibling == null) {
@@ -100,7 +104,14 @@ class DOMXMLStreamReader extends Abstrac
                     event = PROCESSING_INSTRUCTION;
                     break;
                 case Node.ENTITY_REFERENCE_NODE:
-                    event = ENTITY_REFERENCE;
+                    if (expandEntityReferences) {
+                        if (!visited) {
+                            forceTraverse = true;
+                        }
+                        continue;
+                    } else {
+                        event = ENTITY_REFERENCE;
+                    }
                     break;
                 default:
                     throw new IllegalStateException("Unexpected node type " + node.getNodeType());

Modified: webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1362971&r1=1362970&r2=1362971&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java (original)
+++ webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java Wed Jul 18 14:53:33 2012
@@ -75,8 +75,14 @@ public class OMTestSuiteBuilder extends 
         addTest(new org.apache.axiom.ts.om.builder.TestCloseWithReader(metaFactory));
         addTest(new org.apache.axiom.ts.om.builder.TestCloseWithXMLStreamReader(metaFactory));
         for (int i=0; i<conformanceFiles.length; i++) {
-            addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMSource(metaFactory, conformanceFiles[i]));
-            addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromSAXSource(metaFactory, conformanceFiles[i]));
+            ConformanceTestFile file = conformanceFiles[i];
+            if (file.hasEntityReferences()) {
+                addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMSource(metaFactory, file, Boolean.TRUE));
+                addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMSource(metaFactory, file, Boolean.FALSE));
+            } else {
+                addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMSource(metaFactory, file, null));
+            }
+            addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromSAXSource(metaFactory, file));
         }
         addTest(new org.apache.axiom.ts.om.builder.TestCreateStAXOMBuilderFromFragment(metaFactory));
         addTest(new org.apache.axiom.ts.om.builder.TestCreateStAXOMBuilderIncorrectState(metaFactory));

Modified: webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMSource.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMSource.java?rev=1362971&r1=1362970&r2=1362971&view=diff
==============================================================================
--- webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMSource.java (original)
+++ webservices/axiom/branches/AXIOM-435/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMSource.java Wed Jul 18 14:53:33 2012
@@ -36,23 +36,39 @@ import org.custommonkey.xmlunit.XMLAsser
 import org.custommonkey.xmlunit.XMLUnit;
 
 public class TestCreateOMBuilderFromDOMSource extends ConformanceTestCase {
-    public TestCreateOMBuilderFromDOMSource(OMMetaFactory metaFactory, ConformanceTestFile file) {
+    private final Boolean expandEntityReferences;
+    
+    public TestCreateOMBuilderFromDOMSource(OMMetaFactory metaFactory, ConformanceTestFile file,
+            Boolean expandEntityReferences) {
         super(metaFactory, file);
+        this.expandEntityReferences = expandEntityReferences;
+        if (expandEntityReferences != null) {
+            addTestProperty("expandEntityReferences", expandEntityReferences.toString());
+        }
     }
 
     protected void runTest() throws Throwable {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
+        // We never expand entity references during parsing, but we may do this later when
+        // converting DOM to OM.
         factory.setExpandEntityReferences(false);
         DocumentBuilder documentBuilder = factory.newDocumentBuilder();
         InputStream in = getFileAsStream();
         try {
             DOMSource source = new DOMSource(documentBuilder.parse(in));
-            OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source);
+            OMXMLParserWrapper builder;
+            if (expandEntityReferences == null) {
+                builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source);
+            } else {
+                builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source,
+                        expandEntityReferences.booleanValue());
+            }
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             builder.getDocument().serialize(baos);
             XMLAssert.assertXMLIdentical(XMLUnit.compareXML(
-                    AbstractTestCase.toDocumentWithoutDTD(getFileAsStream()),
+                    AbstractTestCase.toDocumentWithoutDTD(getFileAsStream(),
+                            expandEntityReferences == null ? true : expandEntityReferences.booleanValue()),
                     AbstractTestCase.toDocumentWithoutDTD(new ByteArrayInputStream(baos.toByteArray()))), true);
         } finally {
             in.close();