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 2016/01/19 22:08:53 UTC

svn commit: r1725612 - in /webservices/axiom/trunk: aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/ axiom-api/src/main/java/org/apache/axiom/om/ axiom-api/src/main/java/org/apache/axiom/om/impl/builder/ testing/axiom-testsuite/src/mai...

Author: veithen
Date: Tue Jan 19 21:08:53 2016
New Revision: 1725612

URL: http://svn.apache.org/viewvc?rev=1725612&view=rev
Log:
AXIOM-478:
* Allow OMElement#getTextAsStream to use the feature implemented in AXIOM-288 so that caching will be reenabled after the stream is closed.
* Update the Javadoc to document the requirement to close the Reader.
* Throw a meaningful exception when the next() method is invoked on a builder with caching disabled.

Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/AxiomElementSupport.aj
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetTextAsStreamWithoutCaching.java

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/AxiomElementSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/AxiomElementSupport.aj?rev=1725612&r1=1725611&r2=1725612&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/AxiomElementSupport.aj (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/AxiomElementSupport.aj Tue Jan 19 21:08:53 2016
@@ -20,6 +20,7 @@ package org.apache.axiom.om.impl.common;
 
 import static org.apache.axiom.util.xml.NSUtils.generatePrefix;
 
+import java.io.FilterReader;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
@@ -56,6 +57,7 @@ import org.apache.axiom.om.impl.intf.Axi
 import org.apache.axiom.om.impl.intf.AxiomElement;
 import org.apache.axiom.om.impl.intf.AxiomNamespaceDeclaration;
 import org.apache.axiom.util.namespace.MapBasedNamespaceContext;
+import org.apache.axiom.util.stax.XMLStreamIOException;
 import org.apache.axiom.util.stax.XMLStreamReaderUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -161,11 +163,25 @@ public aspect AxiomElementSupport {
         }
         // In all other cases, extract the data from the XMLStreamReader
         try {
-            XMLStreamReader reader = getXMLStreamReader(cache);
+            final XMLStreamReader reader = getXMLStreamReader(cache);
             if (reader.getEventType() == XMLStreamReader.START_DOCUMENT) {
                 reader.next();
             }
-            return XMLStreamReaderUtils.getElementTextAsStream(reader, true);
+            Reader stream = XMLStreamReaderUtils.getElementTextAsStream(reader, true);
+            if (!cache) {
+                // If caching is disabled, we need to close the XMLStreamReader to reenable it
+                stream = new FilterReader(stream) {
+                    @Override
+                    public void close() throws IOException {
+                        try {
+                            reader.close();
+                        } catch (XMLStreamException ex) {
+                            throw new XMLStreamIOException(ex);
+                        }
+                    }
+                };
+            }
+            return stream;
         } catch (XMLStreamException ex) {
             throw new OMException(ex);
         }

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java?rev=1725612&r1=1725611&r2=1725612&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java Tue Jan 19 21:08:53 2016
@@ -453,6 +453,9 @@ public interface OMElement extends OMNod
      * in Axiom and it may be necessary to configure the parser with
      * {@link StAXParserConfiguration#NON_COALESCING}.
      * </ol>
+     * <p>
+     * When this method is used with {@code cache} set to {@code false} the caller must close the
+     * returned stream before attempting to access other nodes in the tree.
      * 
      * @param cache
      *            whether to enable caching when accessing the element

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=1725612&r1=1725611&r2=1725612&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java Tue Jan 19 21:08:53 2016
@@ -172,6 +172,9 @@ public class StAXOMBuilder extends StAXB
      * @throws OMException
      */
     public int next() throws OMException {
+        if (!cache) {
+            throw new IllegalStateException("Can't process next node because caching is disabled");
+        }
         // We need a loop here because we may decide to skip an event
         while (true) {
             if (done) {

Modified: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetTextAsStreamWithoutCaching.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetTextAsStreamWithoutCaching.java?rev=1725612&r1=1725611&r2=1725612&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetTextAsStreamWithoutCaching.java (original)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetTextAsStreamWithoutCaching.java Tue Jan 19 21:08:53 2016
@@ -18,11 +18,14 @@
  */
 package org.apache.axiom.ts.om.element;
 
+import static com.google.common.truth.Truth.assertThat;
+
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.SequenceInputStream;
+import java.nio.charset.Charset;
 import java.util.Vector;
 
 import javax.activation.DataSource;
@@ -42,16 +45,22 @@ public class TestGetTextAsStreamWithoutC
     }
 
     protected void runTest() throws Throwable {
+        Charset charset = Charset.forName("ascii");
         OMFactory factory = metaFactory.getOMFactory();
         DataSource ds = new RandomDataSource(654321, 64, 128, 20000000);
         Vector<InputStream> v = new Vector<InputStream>();
-        v.add(new ByteArrayInputStream("<a>".getBytes("ascii")));
+        v.add(new ByteArrayInputStream("<root><a>".getBytes(charset)));
         v.add(ds.getInputStream());
-        v.add(new ByteArrayInputStream("</a>".getBytes("ascii")));
-        OMElement element = OMXMLBuilderFactory.createOMBuilder(factory,
+        v.add(new ByteArrayInputStream("</a><b/></root>".getBytes(charset)));
+        OMElement root = OMXMLBuilderFactory.createOMBuilder(factory,
                 StAXParserConfiguration.NON_COALESCING,
                 new SequenceInputStream(v.elements()), "ascii").getDocumentElement();
-        Reader in = element.getTextAsStream(false);
-        IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), "ascii"), "expected", in, "actual");
+        OMElement child = (OMElement)root.getFirstOMChild();
+        Reader in = child.getTextAsStream(false);
+        IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), charset), "expected", in, "actual");
+        in.close();
+        // No try to access subsequent nodes
+        child = (OMElement)child.getNextOMSibling();
+        assertThat(child.getLocalName()).isEqualTo("b");
     }
 }