ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1729234 - in /webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl: common/builder/ common/serializer/push/ intf/ mixin/ stream/
Date Mon, 08 Feb 2016 19:43:34 GMT
Author: veithen
Date: Mon Feb  8 19:43:33 2016
New Revision: 1729234

URL: http://svn.apache.org/viewvc?rev=1729234&view=rev
Log:
Rip out the XMLStreamReader to XmlHandler conversion logic from SerializerImpl and reuse the
code from StAXOMBuilder instead.

Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/StAXHelper.java
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/intf/Serializer.java
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/StreamException.java

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/StAXHelper.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/StAXHelper.java?rev=1729234&r1=1729233&r2=1729234&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/StAXHelper.java
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/StAXHelper.java
Mon Feb  8 19:43:33 2016
@@ -107,7 +107,7 @@ public class StAXHelper {
      */
     private Exception parserException;
     
-    private int lookAheadToken = XMLStreamReader.START_DOCUMENT;
+    private int lookAheadToken;
     
     public StAXHelper(XMLStreamReader parser, XmlHandler handler, BuilderHandler builderHandler,
             Closeable closeable, boolean autoClose, PayloadSelector payloadSelector) {
@@ -118,13 +118,18 @@ public class StAXHelper {
         this.autoClose = autoClose;
         this.payloadSelector = payloadSelector;
         dataHandlerReader = XMLStreamReaderUtils.getDataHandlerReader(parser);
+        lookAheadToken = parser.getEventType();
+    }
+    
+    public StAXHelper(XMLStreamReader parser, XmlHandler handler) {
+        this(parser, handler, null, null, false, PayloadSelector.DEFAULT);
     }
 
     private static String normalize(String s) {
         return s == null ? "" : s;
     }
     
-    private void createOMText(int textType) throws StreamException {
+    private void processText(int textType) throws StreamException {
         if (textType == XMLStreamConstants.CHARACTERS && dataHandlerReader != null
&& dataHandlerReader.isBinary()) {
             TextContent data;
             if (dataHandlerReader.isDeferred()) {
@@ -232,11 +237,13 @@ public class StAXHelper {
      * @throws OMException
      */
     public int next() throws OMException {
-        if (!builderHandler.cache) {
-            throw new IllegalStateException("Can't process next node because caching is disabled");
-        }
-        if (builderHandler.done) {
-            throw new OMException();
+        if (builderHandler != null) {
+            if (!builderHandler.cache) {
+                throw new IllegalStateException("Can't process next node because caching
is disabled");
+            }
+            if (builderHandler.done) {
+                throw new OMException();
+            }
         }
         int token = parserNext();
         
@@ -248,13 +255,13 @@ public class StAXHelper {
                     handler.startDocument(parser.getEncoding(), parser.getVersion(), parser.getCharacterEncodingScheme(),
parser.isStandalone());
                     break;
                 case XMLStreamConstants.START_ELEMENT: {
-                    createNextOMElement();
+                    processElement();
                     break;
                 }
                 case XMLStreamConstants.CHARACTERS:
                 case XMLStreamConstants.CDATA:
                 case XMLStreamConstants.SPACE:
-                    createOMText(token);
+                    processText(token);
                     break;
                 case XMLStreamConstants.END_ELEMENT:
                     handler.endElement();
@@ -266,7 +273,7 @@ public class StAXHelper {
                     handler.processComment(parser.getText());
                     break;
                 case XMLStreamConstants.DTD:
-                    createDTD();
+                    processDTD();
                     break;
                 case XMLStreamConstants.PROCESSING_INSTRUCTION:
                     handler.processProcessingInstruction(parser.getPITarget(), parser.getPIData());
@@ -275,14 +282,14 @@ public class StAXHelper {
                     handler.processEntityReference(parser.getLocalName(), parser.getText());
                     break;
                 default :
-                    throw new OMException();
+                    throw new IllegalStateException();
             }
         } catch (StreamException ex) {
             throw new OMException(ex);
         }
         
         // TODO: this will fail if there is whitespace before the document element
-        if (token != XMLStreamConstants.START_DOCUMENT && builderHandler.target ==
null && !builderHandler.done) {
+        if (builderHandler != null && token != XMLStreamConstants.START_DOCUMENT
&& builderHandler.target == null && !builderHandler.done) {
             // We get here if the document has been discarded (by getDocumentElement(true)
             // or because the builder is linked to an OMSourcedElement) and
             // we just processed the END_ELEMENT event for the root element. In this case,
we consume
@@ -304,24 +311,21 @@ public class StAXHelper {
         return token;
     }
     
-    /**
-     * Creates a new OMElement using either a CustomBuilder or 
-     * the default Builder mechanism.
-     * @throws StreamException 
-     */
-    private void createNextOMElement() throws StreamException {
+    private void processElement() throws StreamException {
         String namespaceURI = normalize(parser.getNamespaceURI());
         String localName = parser.getLocalName();
         String prefix = normalize(parser.getPrefix());
-        if (customBuilderForPayload != null && payloadSelector.isPayload(builderHandler.elementLevel+1,
builderHandler.target)
-                && createWithCustomBuilder(customBuilderForPayload)) {
-            return;
-        }
-        if (customBuilders != null && builderHandler.elementLevel < this.maxDepthForCustomBuilders)
{
-            CustomBuilder customBuilder = customBuilders.get(namespaceURI, localName);
-            if (customBuilder != null && createWithCustomBuilder(customBuilder))
{
+        if (builderHandler != null) {
+            if (customBuilderForPayload != null && payloadSelector.isPayload(builderHandler.elementLevel+1,
builderHandler.target)
+                    && processWithCustomBuilder(customBuilderForPayload)) {
                 return;
             }
+            if (customBuilders != null && builderHandler.elementLevel < this.maxDepthForCustomBuilders)
{
+                CustomBuilder customBuilder = customBuilders.get(namespaceURI, localName);
+                if (customBuilder != null && processWithCustomBuilder(customBuilder))
{
+                    return;
+                }
+            }
         }
         handler.startElement(namespaceURI, localName, prefix);
         for (int i = 0, count = parser.getNamespaceCount(); i < count; i++) {
@@ -341,7 +345,7 @@ public class StAXHelper {
         handler.attributesCompleted();
     }
     
-    private boolean createWithCustomBuilder(CustomBuilder customBuilder) throws StreamException
{
+    private boolean processWithCustomBuilder(CustomBuilder customBuilder) throws StreamException
{
         
         String namespace = parser.getNamespaceURI();
         if (namespace == null) {
@@ -372,7 +376,7 @@ public class StAXHelper {
         }
     }
     
-    private void createDTD() throws StreamException {
+    private void processDTD() throws StreamException {
         DTDReader dtdReader;
         try {
             dtdReader = (DTDReader)parser.getProperty(DTDReader.PROPERTY);
@@ -380,7 +384,7 @@ public class StAXHelper {
             dtdReader = null;
         }
         if (dtdReader == null) {
-            throw new OMException("Cannot create OMDocType because the XMLStreamReader doesn't
support the DTDReader extension");
+            throw new StreamException("Cannot process DTD events because the XMLStreamReader
doesn't support the DTDReader extension");
         }
         String internalSubset = getDTDText();
         // Woodstox returns an empty string if there is no internal subset
@@ -455,7 +459,7 @@ public class StAXHelper {
                     parserException = ex;
                     throw ex;
                 }
-                if (event == XMLStreamConstants.END_DOCUMENT) {
+                if (builderHandler != null && event == XMLStreamConstants.END_DOCUMENT)
{
                     if (builderHandler.cache && builderHandler.elementLevel != 0)
{
                         throw new OMException("Unexpected END_DOCUMENT event");
                     }

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java?rev=1729234&r1=1729233&r2=1729234&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java
Mon Feb  8 19:43:33 2016
@@ -28,9 +28,7 @@ import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.axiom.core.CoreAttribute;
-import org.apache.axiom.ext.stax.DTDReader;
 import org.apache.axiom.ext.stax.datahandler.DataHandlerProvider;
-import org.apache.axiom.ext.stax.datahandler.DataHandlerReader;
 import org.apache.axiom.om.DeferredParsingException;
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMContainer;
@@ -39,12 +37,12 @@ import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMSerializable;
+import org.apache.axiom.om.impl.common.builder.StAXHelper;
 import org.apache.axiom.om.impl.common.util.OMDataSourceUtil;
 import org.apache.axiom.om.impl.intf.Serializer;
 import org.apache.axiom.om.impl.intf.TextContent;
 import org.apache.axiom.om.impl.stream.StreamException;
 import org.apache.axiom.om.impl.stream.XmlHandler;
-import org.apache.axiom.util.stax.XMLStreamReaderUtils;
 
 public abstract class SerializerImpl implements Serializer {
     private final OMSerializable root;
@@ -125,78 +123,6 @@ public abstract class SerializerImpl imp
         attributesCompleted();
     }
     
-    public final void copyEvent(XMLStreamReader reader, DataHandlerReader dataHandlerReader)
throws StreamException {
-        try {
-            int eventType = reader.getEventType();
-            switch (eventType) {
-                case XMLStreamReader.DTD:
-                    DTDReader dtdReader;
-                    try {
-                        dtdReader = (DTDReader)reader.getProperty(DTDReader.PROPERTY);
-                    } catch (IllegalArgumentException ex) {
-                        dtdReader = null;
-                    }
-                    if (dtdReader == null) {
-                        throw new XMLStreamException("Cannot serialize the DTD because the
XMLStreamReader doesn't support the DTDReader extension");
-                    }
-                    processDocumentTypeDeclaration(dtdReader.getRootName(), dtdReader.getPublicId(),
dtdReader.getSystemId(), reader.getText());
-                    break;
-                case XMLStreamReader.START_ELEMENT:
-                    handler.startElement(normalize(reader.getNamespaceURI()), reader.getLocalName(),
normalize(reader.getPrefix()));
-                    for (int i=0, count=reader.getNamespaceCount(); i<count; i++) {
-                        handler.processNamespaceDeclaration(normalize(reader.getNamespacePrefix(i)),
normalize(reader.getNamespaceURI(i)));
-                    }
-                    for (int i=0, count=reader.getAttributeCount(); i<count; i++) {
-                        handler.processAttribute(
-                                normalize(reader.getAttributeNamespace(i)),
-                                reader.getAttributeLocalName(i),
-                                normalize(reader.getAttributePrefix(i)),
-                                reader.getAttributeValue(i),
-                                reader.getAttributeType(i),
-                                reader.isAttributeSpecified(i));
-                    }
-                    attributesCompleted();
-                    break;
-                case XMLStreamReader.END_ELEMENT:
-                    endElement();
-                    break;
-                case XMLStreamReader.CHARACTERS:
-                    if (dataHandlerReader != null && dataHandlerReader.isBinary())
{
-                        if (dataHandlerReader.isDeferred()) {
-                            writeDataHandler(dataHandlerReader.getDataHandlerProvider(),
-                                    dataHandlerReader.getContentID(), dataHandlerReader.isOptimized());
-                        } else {
-                            writeDataHandler(dataHandlerReader.getDataHandler(),
-                                    dataHandlerReader.getContentID(), dataHandlerReader.isOptimized());
-                        }
-                        break;
-                    }
-                    // Fall through
-                case XMLStreamReader.SPACE:
-                case XMLStreamReader.CDATA:
-                    writeText(eventType, reader.getText());
-                    break;
-                case XMLStreamReader.PROCESSING_INSTRUCTION:
-                    processProcessingInstruction(reader.getPITarget(), reader.getPIData());
-                    break;
-                case XMLStreamReader.COMMENT:
-                    processComment(reader.getText());
-                    break;
-                case XMLStreamReader.ENTITY_REFERENCE:
-                    processEntityReference(reader.getLocalName(), reader.getText());
-                    break;
-                default:
-                    throw new IllegalStateException();
-            }
-        } catch (XMLStreamException ex) {
-            throw new DeferredParsingException(ex);
-        }
-    }
-    
-    private static String normalize(String s) {
-        return s == null ? "" : s;
-    }
-    
     /**
      * Serialize the given data source.
      * 
@@ -213,21 +139,17 @@ public abstract class SerializerImpl imp
         if (OMDataSourceUtil.isPullDataSource(dataSource)) {
             try {
                 XMLStreamReader reader = dataSource.getReader();
-                DataHandlerReader dataHandlerReader = XMLStreamReaderUtils.getDataHandlerReader(reader);
+                StAXHelper helper = new StAXHelper(reader, this);
+                while (helper.lookahead() != XMLStreamReader.START_ELEMENT) {
+                    helper.parserNext();
+                }
                 int depth = 0;
-                int eventType;
-                // Note: the loop is constructed in such a way that we skip both START_DOCUMENT
and END_DOCUMENT
-                while ((eventType = reader.next()) != XMLStreamReader.END_DOCUMENT) {
-                    if (eventType == XMLStreamReader.START_ELEMENT) {
-                        depth++;
-                    }
-                    if (depth > 0) {
-                        copyEvent(reader, dataHandlerReader);
+                do {
+                    switch (helper.next()) {
+                        case XMLStreamReader.START_ELEMENT: depth++; break;
+                        case XMLStreamReader.END_ELEMENT: depth--; break;
                     }
-                    if (eventType == XMLStreamReader.END_ELEMENT) {
-                        depth--;
-                    }
-                }
+                } while (depth > 0);
                 reader.close();
             } catch (XMLStreamException ex) {
                 // XMLStreamExceptions occurring while _writing_ are wrapped in an OutputException.

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/intf/Serializer.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/intf/Serializer.java?rev=1729234&r1=1729233&r2=1729234&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/intf/Serializer.java
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/intf/Serializer.java
Mon Feb  8 19:43:33 2016
@@ -18,14 +18,10 @@
  */
 package org.apache.axiom.om.impl.intf;
 
-import javax.xml.stream.XMLStreamReader;
-
-import org.apache.axiom.ext.stax.datahandler.DataHandlerReader;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.impl.stream.StreamException;
 import org.apache.axiom.om.impl.stream.XmlHandler;
 
 public interface Serializer extends XmlHandler {
     void serializeStartpart(OMElement element) throws StreamException;
-    void copyEvent(XMLStreamReader reader, DataHandlerReader dataHandlerReader) throws StreamException;
 }

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj?rev=1729234&r1=1729233&r2=1729234&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
Mon Feb  8 19:43:33 2016
@@ -34,8 +34,6 @@ import org.apache.axiom.core.CoreNSAware
 import org.apache.axiom.core.CoreNode;
 import org.apache.axiom.core.ElementMatcher;
 import org.apache.axiom.core.Mapper;
-import org.apache.axiom.ext.stax.datahandler.DataHandlerReader;
-import org.apache.axiom.om.DeferredParsingException;
 import org.apache.axiom.om.NodeUnavailableException;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
@@ -54,6 +52,7 @@ import org.apache.axiom.om.impl.common.N
 import org.apache.axiom.om.impl.common.OMChildrenQNameIterator;
 import org.apache.axiom.om.impl.common.OMContentHandler;
 import org.apache.axiom.om.impl.common.SAXResultContentHandler;
+import org.apache.axiom.om.impl.common.builder.StAXHelper;
 import org.apache.axiom.om.impl.common.serializer.pull.OMXMLStreamReaderExAdapter;
 import org.apache.axiom.om.impl.common.serializer.pull.PullSerializer;
 import org.apache.axiom.om.impl.common.serializer.push.sax.XMLReaderImpl;
@@ -65,7 +64,6 @@ import org.apache.axiom.om.impl.intf.Ser
 import org.apache.axiom.om.impl.stream.StreamException;
 import org.apache.axiom.om.util.OMXMLStreamReaderValidator;
 import org.apache.axiom.om.util.StAXUtils;
-import org.apache.axiom.util.stax.XMLStreamReaderUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.xml.sax.InputSource;
@@ -377,23 +375,10 @@ public aspect AxiomContainerSupport {
             // events from the underlying XMLStreamReader.
             if (!isComplete() && getBuilder() != null) {
                 Builder builder = (Builder)getBuilder();
-                XMLStreamReader reader = builder.disableCaching();
-                DataHandlerReader dataHandlerReader = XMLStreamReaderUtils.getDataHandlerReader(reader);
-                boolean first = true;
+                StAXHelper helper = new StAXHelper(builder.disableCaching(), serializer);
                 int depth = 0;
                 loop: while (true) {
-                    int event;
-                    if (first) {
-                        event = reader.getEventType();
-                        first = false;
-                    } else {
-                        try {
-                            event = reader.next();
-                        } catch (XMLStreamException ex) {
-                            throw new DeferredParsingException(ex);
-                        }
-                    }
-                    switch (event) {
+                    switch (helper.lookahead()) {
                         case XMLStreamReader.START_ELEMENT:
                             depth++;
                             break;
@@ -414,7 +399,7 @@ public aspect AxiomContainerSupport {
                     }
                     // Note that we don't copy the final END_ELEMENT/END_DOCUMENT event for
                     // the container. This is the responsibility of the caller.
-                    serializer.copyEvent(reader, dataHandlerReader);
+                    helper.next();
                 }
                 builder.reenableCaching(this);
             }

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/StreamException.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/StreamException.java?rev=1729234&r1=1729233&r2=1729234&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/StreamException.java
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/StreamException.java
Mon Feb  8 19:43:33 2016
@@ -18,12 +18,17 @@
  */
 package org.apache.axiom.om.impl.stream;
 
+// TODO: update Javadoc
 /**
  * Wraps an exception thrown by the underlying API (StAX, SAX, etc.) while writing data.
  */
 public class StreamException extends Exception {
     private static final long serialVersionUID = 7173617216602466028L;
 
+    public StreamException(String message) {
+        super(message);
+    }
+
     public StreamException(String message, Throwable cause) {
         super(message, cause);
     }



Mime
View raw message