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 2011/11/06 18:56:10 UTC

svn commit: r1198521 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/ axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/ axiom-testsuite/src/main/java/org/apache/axiom/ts/ axiom-t...

Author: veithen
Date: Sun Nov  6 17:56:10 2011
New Revision: 1198521

URL: http://svn.apache.org/viewvc?rev=1198521&view=rev
Log:
Allow usage of createStAXOMBuilder to process fragments from an XMLStreamReader.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderFromFragment.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderIncorrectState.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java
    webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java?rev=1198521&r1=1198520&r2=1198521&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java Sun Nov  6 17:56:10 2011
@@ -23,6 +23,7 @@ import java.io.Reader;
 
 import javax.mail.internet.ContentType;
 import javax.mail.internet.ParseException;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.Source;
 
@@ -43,10 +44,28 @@ public class OMXMLBuilderFactory {
     
     /**
      * Create an object model builder for plain XML that pulls events from a StAX stream reader.
+     * <p>
+     * The reader must be positioned on a {@link XMLStreamConstants#START_DOCUMENT} or
+     * {@link XMLStreamConstants#START_ELEMENT} event. If the current event is
+     * {@link XMLStreamConstants#START_DOCUMENT} then the builder will consume events up to the
+     * {@link XMLStreamConstants#END_DOCUMENT} event. If the current event is
+     * {@link XMLStreamConstants#START_ELEMENT}, then the builder will consume events up to the
+     * corresponding {@link XMLStreamConstants#END_ELEMENT}. After the object model is completely
+     * built, the stream reader will be positioned on the event immediately following this
+     * {@link XMLStreamConstants#END_ELEMENT} event. This means that this method can be used in a
+     * well defined way to build an object model from a fragment (corresponding to a single element)
+     * of the document represented by the stream reader.
+     * <p>
+     * The returned builder also performs namespace repairing, i.e. it adds appropriate namespace
+     * declarations if undeclared namespaces appear in the StAX stream.
      * 
      * @param parser
      *            the stream reader to read the XML data from
      * @return the builder
+     * @throws OMException
+     *             if the stream reader is positioned on an event other than
+     *             {@link XMLStreamConstants#START_DOCUMENT} or
+     *             {@link XMLStreamConstants#START_ELEMENT}
      */
     public static OMXMLParserWrapper createStAXOMBuilder(XMLStreamReader parser) {
         OMMetaFactory metaFactory = OMAbstractFactory.getMetaFactory();
@@ -56,6 +75,9 @@ public class OMXMLBuilderFactory {
     /**
      * Create an object model builder that pulls events from a StAX stream reader using a specified
      * object model factory.
+     * <p>
+     * See {@link #createStAXOMBuilder(XMLStreamReader)} for more information about the behavior of
+     * the returned builder.
      * 
      * @param omFactory
      *            the object model factory to use
@@ -334,6 +356,9 @@ public class OMXMLBuilderFactory {
      * Create an object model builder for SOAP that pulls events from a StAX stream reader.
      * The method will select the appropriate {@link SOAPFactory}
      * based on the namespace URI of the SOAP envelope.
+     * <p>
+     * See {@link #createStAXOMBuilder(XMLStreamReader)} for more information about the behavior of
+     * the returned builder.
      * 
      * @param parser
      *            the stream reader to read the XML data from

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java?rev=1198521&r1=1198520&r2=1198521&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java Sun Nov  6 17:56:10 2011
@@ -36,6 +36,8 @@ import org.apache.axiom.soap.SOAPFactory
 import org.apache.axiom.soap.SOAPModelBuilder;
 import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
 import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
+import org.apache.axiom.util.stax.XMLEventUtils;
+import org.apache.axiom.util.stax.XMLFragmentStreamReader;
 import org.apache.axiom.util.stax.xop.MimePartProvider;
 import org.xml.sax.InputSource;
 
@@ -44,7 +46,7 @@ import org.xml.sax.InputSource;
  * ({@link org.apache.axiom.om.impl.builder.StAXOMBuilder} and its subclasses).
  */
 public abstract class AbstractOMMetaFactory implements OMMetaFactory {
-    private XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, InputSource is) {
+    private static XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, InputSource is) {
         try {
             if (is.getByteStream() != null) {
                 String encoding = is.getEncoding();
@@ -63,7 +65,20 @@ public abstract class AbstractOMMetaFact
         }
     }
     
-    public OMXMLParserWrapper createStAXOMBuilder(OMFactory omFactory, XMLStreamReader parser) {
+    private static XMLStreamReader getXMLStreamReader(XMLStreamReader originalReader) {
+        int eventType = originalReader.getEventType();
+        switch (eventType) {
+            case XMLStreamReader.START_DOCUMENT:
+                return originalReader;
+            case XMLStreamReader.START_ELEMENT:
+                return new XMLFragmentStreamReader(originalReader);
+            default:
+                throw new OMException("The supplied XMLStreamReader is in an unexpected state ("
+                        + XMLEventUtils.getEventTypeString(eventType) + ")");
+        }
+    }
+    
+    private static OMXMLParserWrapper internalCreateStAXOMBuilder(OMFactory omFactory, XMLStreamReader parser) {
         StAXOMBuilder builder = new StAXOMBuilder(omFactory, parser);
         // StAXOMBuilder defaults to the "legacy" behavior, which is to keep a reference to the
         // parser after the builder has been closed. Since releasing this reference is a good idea
@@ -73,8 +88,12 @@ public abstract class AbstractOMMetaFact
         return builder;
     }
 
+    public OMXMLParserWrapper createStAXOMBuilder(OMFactory omFactory, XMLStreamReader parser) {
+        return internalCreateStAXOMBuilder(omFactory, getXMLStreamReader(parser));
+    }
+
     public OMXMLParserWrapper createOMBuilder(OMFactory omFactory, StAXParserConfiguration configuration, InputSource is) {
-        return createStAXOMBuilder(omFactory, createXMLStreamReader(configuration, is));
+        return internalCreateStAXOMBuilder(omFactory, createXMLStreamReader(configuration, is));
     }
     
     public OMXMLParserWrapper createOMBuilder(OMFactory omFactory, Source source) {
@@ -97,14 +116,18 @@ public abstract class AbstractOMMetaFact
         return builder;
     }
 
-    public SOAPModelBuilder createStAXSOAPModelBuilder(XMLStreamReader parser) {
+    private SOAPModelBuilder internalCreateStAXSOAPModelBuilder(XMLStreamReader parser) {
         StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(this, parser);
         builder.releaseParserOnClose(true);
         return builder;
     }
 
+    public SOAPModelBuilder createStAXSOAPModelBuilder(XMLStreamReader parser) {
+        return internalCreateStAXSOAPModelBuilder(getXMLStreamReader(parser));
+    }
+
     public SOAPModelBuilder createSOAPModelBuilder(StAXParserConfiguration configuration, InputSource is) {
-        return createStAXSOAPModelBuilder(createXMLStreamReader(configuration, is));
+        return internalCreateStAXSOAPModelBuilder(createXMLStreamReader(configuration, is));
     }
 
     public SOAPModelBuilder createSOAPModelBuilder(StAXParserConfiguration configuration,

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java?rev=1198521&r1=1198520&r2=1198521&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java Sun Nov  6 17:56:10 2011
@@ -62,6 +62,8 @@ public class OMTestSuiteBuilder extends 
             addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMSource(metaFactory, conformanceFiles[i]));
             addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromSAXSource(metaFactory, conformanceFiles[i]));
         }
+        addTest(new org.apache.axiom.ts.om.builder.TestCreateStAXOMBuilderFromFragment(metaFactory));
+        addTest(new org.apache.axiom.ts.om.builder.TestCreateStAXOMBuilderIncorrectState(metaFactory));
         addTest(new org.apache.axiom.ts.om.builder.TestCreateStAXOMBuilderNamespaceRepairing(metaFactory));
         addTest(new org.apache.axiom.ts.om.builder.TestGetDocumentElement(metaFactory));
         addTest(new org.apache.axiom.ts.om.builder.TestGetDocumentElementWithDiscardDocument(metaFactory));

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderFromFragment.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderFromFragment.java?rev=1198521&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderFromFragment.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderFromFragment.java Sun Nov  6 17:56:10 2011
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.om.builder;
+
+import java.io.StringReader;
+
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests the behavior of {@link OMXMLBuilderFactory#createStAXOMBuilder(OMFactory, XMLStreamReader)}
+ * if the supplied {@link XMLStreamReader} is positioned on a
+ * {@link XMLStreamConstants#START_ELEMENT} event.
+ */
+public class TestCreateStAXOMBuilderFromFragment extends AxiomTestCase {
+    public TestCreateStAXOMBuilderFromFragment(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a><b>text</b></a>"));
+        // Position the reader on the event for <b>
+        while (reader.getEventType() != XMLStreamReader.START_ELEMENT || !reader.getLocalName().equals("b")) {
+            reader.next();
+        }
+        // Check that the builder only builds the part of the document corresponding to <b>text</b>
+        OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader).getDocumentElement();
+        assertEquals("b", element.getLocalName());
+        OMNode child = element.getFirstOMChild();
+        assertTrue(child instanceof OMText);
+        assertNull(element.getNextOMSibling());
+        // Check that the original reader is now positioned on the event just following </b>
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.getEventType());
+        assertEquals("a", reader.getLocalName());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderFromFragment.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderIncorrectState.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderIncorrectState.java?rev=1198521&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderIncorrectState.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderIncorrectState.java Sun Nov  6 17:56:10 2011
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.om.builder;
+
+import java.io.StringReader;
+
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that {@link OMXMLBuilderFactory#createStAXOMBuilder(OMFactory, XMLStreamReader)} throws an
+ * exception if the supplied {@link XMLStreamReader} is not positioned on a
+ * {@link XMLStreamConstants#START_DOCUMENT} or {@link XMLStreamConstants#END_DOCUMENT} event.
+ */
+public class TestCreateStAXOMBuilderIncorrectState extends AxiomTestCase {
+    public TestCreateStAXOMBuilderIncorrectState(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<root>text</root>"));
+        // Position the reader on a CHARACTERS event
+        while (reader.getEventType() != XMLStreamReader.CHARACTERS) {
+            reader.next();
+        }
+        try {
+            OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader);
+            fail("Expected OMException");
+        } catch (OMException ex) {
+            // Expected
+        }
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateStAXOMBuilderIncorrectState.java
------------------------------------------------------------------------------
    svn:eol-style = native