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/02/07 17:26:45 UTC

svn commit: r1728983 - in /webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl: common/builder/ stream/stax/

Author: veithen
Date: Sun Feb  7 16:26:45 2016
New Revision: 1728983

URL: http://svn.apache.org/viewvc?rev=1728983&view=rev
Log:
Decouple API conversion logic from builder logic.

Added:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMDataSourceStreamWriter.java
      - copied, changed from r1728960, webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/XmlHandlerStreamWriter.java
      - copied, changed from r1728960, webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java
Removed:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java
Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMBuilder.java

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMBuilder.java?rev=1728983&r1=1728982&r2=1728983&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMBuilder.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMBuilder.java Sun Feb  7 16:26:45 2016
@@ -18,11 +18,18 @@
  */
 package org.apache.axiom.om.impl.common.builder;
 
+import java.util.Iterator;
+
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.axiom.om.DeferredParsingException;
+import org.apache.axiom.om.OMContainer;
 import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.impl.intf.AxiomSourcedElement;
+import org.apache.axiom.om.impl.stream.stax.XmlHandlerStreamWriter;
 
 public final class PushOMBuilder extends AbstractPushBuilder {
     private final AxiomSourcedElement root;
@@ -38,7 +45,16 @@ public final class PushOMBuilder extends
     
     public int next() {
         try {
-            dataSource.serialize(new BuilderHandlerXMLStreamWriter(handler, root));
+            XMLStreamWriter writer = new XmlHandlerStreamWriter(handler);
+            // Seed the namespace context with the namespace context from the parent
+            OMContainer parent = root.getParent();
+            if (parent instanceof OMElement) {
+                for (Iterator<OMNamespace> it = ((OMElement)parent).getNamespacesInScope(); it.hasNext(); ) {
+                    OMNamespace ns = it.next();
+                    writer.setPrefix(ns.getPrefix(), ns.getNamespaceURI());
+                }
+            }
+            dataSource.serialize(new PushOMDataSourceStreamWriter(writer));
         } catch (XMLStreamException ex) {
             throw new DeferredParsingException(ex);
         }

Copied: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMDataSourceStreamWriter.java (from r1728960, webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java)
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMDataSourceStreamWriter.java?p2=webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMDataSourceStreamWriter.java&p1=webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java&r1=1728960&r2=1728983&rev=1728983&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/PushOMDataSourceStreamWriter.java Sun Feb  7 16:26:45 2016
@@ -18,149 +18,49 @@
  */
 package org.apache.axiom.om.impl.common.builder;
 
-import java.io.IOException;
-import java.util.Iterator;
-
-import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.util.stax.wrapper.XMLStreamWriterWrapper;
 
-import org.apache.axiom.ext.stax.datahandler.DataHandlerProvider;
-import org.apache.axiom.ext.stax.datahandler.DataHandlerWriter;
-import org.apache.axiom.om.OMConstants;
-import org.apache.axiom.om.OMContainer;
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNamespace;
-import org.apache.axiom.om.impl.intf.AxiomSourcedElement;
-import org.apache.axiom.om.impl.intf.TextContent;
-import org.apache.axiom.util.stax.AbstractXMLStreamWriter;
-
-public class BuilderHandlerXMLStreamWriter extends AbstractXMLStreamWriter implements DataHandlerWriter {
-    private final BuilderHandler handler;
-    private boolean inStartElement;
-
-    public BuilderHandlerXMLStreamWriter(BuilderHandler handler, AxiomSourcedElement root) throws XMLStreamException {
-        this.handler = handler;
-        // Seed the namespace context with the namespace context from the parent
-        OMContainer parent = root.getParent();
-        if (parent instanceof OMElement) {
-            for (Iterator<OMNamespace> it = ((OMElement)parent).getNamespacesInScope(); it.hasNext(); ) {
-                OMNamespace ns = it.next();
-                setPrefix(ns.getPrefix(), ns.getNamespaceURI());
-            }
-        }
-    }
-    
-    private static String normalize(String s) {
-        return s == null ? "" : s;
-    }
-    
-    public Object getProperty(String name) throws IllegalArgumentException {
-        if (DataHandlerWriter.PROPERTY.equals(name)) {
-            return this;
-        } else {
-            throw new IllegalArgumentException("Unsupported property " + name);
-        }
+/**
+ * {@link XMLStreamWriter} wrapper that throws exceptions for invocations of methods that
+ * {@link OMDataSource#serialize(XMLStreamWriter)} is not allowed to call.
+ */
+public final class PushOMDataSourceStreamWriter extends XMLStreamWriterWrapper {
+    public PushOMDataSourceStreamWriter(XMLStreamWriter parent) {
+        super(parent);
     }
 
-    protected void doWriteStartDocument() {
+    public void writeStartDocument() {
         throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartDocument()");
     }
 
-    protected void doWriteStartDocument(String encoding, String version) {
+    public void writeStartDocument(String encoding, String version) {
         throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartDocument(String, String)");
     }
 
-    protected void doWriteStartDocument(String version) {
+    public void writeStartDocument(String version) {
         throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartDocument(String)");
     }
 
-    protected void doWriteEndDocument() {
+    public void writeEndDocument() {
         throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeEndDocument()");
     }
 
-    protected void doWriteDTD(String dtd) throws XMLStreamException {
+    public void writeDTD(String dtd) throws XMLStreamException {
         throw new XMLStreamException("A DTD must not appear in element content");
     }
 
-    protected void doWriteStartElement(String prefix, String localName, String namespaceURI) {
-        handler.startElement(normalize(namespaceURI), localName, normalize(prefix));
-        inStartElement = true;
-    }
-
-    protected void doWriteStartElement(String localName) throws XMLStreamException {
+    public void writeStartElement(String localName) throws XMLStreamException {
         throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartElement(String)");
     }
 
-    protected void doWriteEndElement() {
-        handler.endElement();
-    }
-
-    private void finishStartElement() {
-        if (inStartElement) {
-            handler.attributesCompleted();
-            inStartElement = false;
-        }
-    }
-    
-    protected void doWriteEmptyElement(String prefix, String localName, String namespaceURI) {
-        doWriteStartElement(prefix, localName, namespaceURI);
-        finishStartElement();
-        doWriteEndElement();
-    }
-
-    protected void doWriteEmptyElement(String localName) throws XMLStreamException {
+    public void writeEmptyElement(String localName) throws XMLStreamException {
         throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeEmptyElement(String)");
     }
 
-    protected void doWriteAttribute(String prefix, String namespaceURI, String localName, String value) {
-        handler.processAttribute(normalize(namespaceURI), localName, normalize(prefix), value, OMConstants.XMLATTRTYPE_CDATA, true);
-    }
-
-    protected void doWriteAttribute(String localName, String value) throws XMLStreamException {
-        doWriteAttribute(null, null, localName, value);
-    }
-
-    protected void doWriteNamespace(String prefix, String namespaceURI) {
-        handler.processNamespaceDeclaration(normalize(prefix), normalize(namespaceURI));
-    }
-
-    protected void doWriteDefaultNamespace(String namespaceURI) {
-        doWriteNamespace(null, namespaceURI);
-    }
-
-    protected void doWriteCharacters(char[] text, int start, int len) {
-        doWriteCharacters(new String(text, start, len));
-    }
-
-    protected void doWriteCharacters(String text) {
-        finishStartElement();
-        handler.processCharacterData(text, false);
-    }
-
-    protected void doWriteCData(String data) {
-        finishStartElement();
-        handler.processCDATASection(data);
-    }
-
-    protected void doWriteComment(String data) {
-        finishStartElement();
-        handler.processComment(data);
-    }
-
-    protected void doWriteEntityRef(String name) throws XMLStreamException {
-        finishStartElement();
-        handler.processEntityReference(name, null);
-    }
-
-    protected void doWriteProcessingInstruction(String piTarget, String data) {
-        finishStartElement();
-        handler.processProcessingInstruction(piTarget, data);
-    }
-
-    protected void doWriteProcessingInstruction(String target) {
-        doWriteProcessingInstruction(target, "");
-    }
-
     public void flush() throws XMLStreamException {
         // Do nothing
     }
@@ -168,16 +68,4 @@ public class BuilderHandlerXMLStreamWrit
     public void close() throws XMLStreamException {
         throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT call XMLStreamWriter#close()");
     }
-
-    public void writeDataHandler(DataHandler dataHandler, String contentID, boolean optimize)
-            throws IOException, XMLStreamException {
-        finishStartElement();
-        handler.processCharacterData(new TextContent(contentID, dataHandler, optimize), false);
-    }
-
-    public void writeDataHandler(DataHandlerProvider dataHandlerProvider, String contentID,
-            boolean optimize) throws IOException, XMLStreamException {
-        finishStartElement();
-        handler.processCharacterData(new TextContent(contentID, dataHandlerProvider, optimize), false);
-    }
 }

Copied: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/XmlHandlerStreamWriter.java (from r1728960, webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java)
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/XmlHandlerStreamWriter.java?p2=webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/XmlHandlerStreamWriter.java&p1=webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java&r1=1728960&r2=1728983&rev=1728983&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/builder/BuilderHandlerXMLStreamWriter.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/XmlHandlerStreamWriter.java Sun Feb  7 16:26:45 2016
@@ -16,10 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.axiom.om.impl.common.builder;
+package org.apache.axiom.om.impl.stream.stax;
 
 import java.io.IOException;
-import java.util.Iterator;
 
 import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamException;
@@ -27,33 +26,32 @@ import javax.xml.stream.XMLStreamExcepti
 import org.apache.axiom.ext.stax.datahandler.DataHandlerProvider;
 import org.apache.axiom.ext.stax.datahandler.DataHandlerWriter;
 import org.apache.axiom.om.OMConstants;
-import org.apache.axiom.om.OMContainer;
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNamespace;
-import org.apache.axiom.om.impl.intf.AxiomSourcedElement;
 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.AbstractXMLStreamWriter;
 
-public class BuilderHandlerXMLStreamWriter extends AbstractXMLStreamWriter implements DataHandlerWriter {
-    private final BuilderHandler handler;
+public class XmlHandlerStreamWriter extends AbstractXMLStreamWriter implements DataHandlerWriter {
+    private final XmlHandler handler;
     private boolean inStartElement;
 
-    public BuilderHandlerXMLStreamWriter(BuilderHandler handler, AxiomSourcedElement root) throws XMLStreamException {
+    public XmlHandlerStreamWriter(XmlHandler handler) {
         this.handler = handler;
-        // Seed the namespace context with the namespace context from the parent
-        OMContainer parent = root.getParent();
-        if (parent instanceof OMElement) {
-            for (Iterator<OMNamespace> it = ((OMElement)parent).getNamespacesInScope(); it.hasNext(); ) {
-                OMNamespace ns = it.next();
-                setPrefix(ns.getPrefix(), ns.getNamespaceURI());
-            }
-        }
     }
     
     private static String normalize(String s) {
         return s == null ? "" : s;
     }
     
+    private static XMLStreamException toXMLStreamException(StreamException ex) {
+        Throwable cause = ex.getCause();
+        if (cause instanceof XMLStreamException) {
+            return (XMLStreamException)cause;
+        } else {
+            return new XMLStreamException(ex);
+        }
+    }
+    
     public Object getProperty(String name) throws IllegalArgumentException {
         if (DataHandlerWriter.PROPERTY.equals(name)) {
             return this;
@@ -62,122 +60,186 @@ public class BuilderHandlerXMLStreamWrit
         }
     }
 
-    protected void doWriteStartDocument() {
-        throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartDocument()");
+    protected void doWriteStartDocument() throws XMLStreamException {
+        try {
+            handler.startDocument(null, "1.0", null, true);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteStartDocument(String encoding, String version) {
-        throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartDocument(String, String)");
+    protected void doWriteStartDocument(String encoding, String version) throws XMLStreamException {
+        try {
+            handler.startDocument(null, version, encoding, true);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteStartDocument(String version) {
-        throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartDocument(String)");
+    protected void doWriteStartDocument(String version) throws XMLStreamException {
+        try {
+            handler.startDocument(null, version, null, true);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteEndDocument() {
-        throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeEndDocument()");
+    protected void doWriteEndDocument() throws XMLStreamException {
+        try {
+            handler.endDocument();
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
     protected void doWriteDTD(String dtd) throws XMLStreamException {
-        throw new XMLStreamException("A DTD must not appear in element content");
+        throw new UnsupportedOperationException();
     }
 
-    protected void doWriteStartElement(String prefix, String localName, String namespaceURI) {
-        handler.startElement(normalize(namespaceURI), localName, normalize(prefix));
+    protected void doWriteStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
+        try {
+            handler.startElement(normalize(namespaceURI), localName, normalize(prefix));
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
         inStartElement = true;
     }
 
     protected void doWriteStartElement(String localName) throws XMLStreamException {
-        throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeStartElement(String)");
+        throw new UnsupportedOperationException();
     }
 
-    protected void doWriteEndElement() {
-        handler.endElement();
+    protected void doWriteEndElement() throws XMLStreamException {
+        try {
+            handler.endElement();
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    private void finishStartElement() {
+    private void finishStartElement() throws XMLStreamException {
         if (inStartElement) {
-            handler.attributesCompleted();
+            try {
+                handler.attributesCompleted();
+            } catch (StreamException ex) {
+                throw toXMLStreamException(ex);
+            }
             inStartElement = false;
         }
     }
     
-    protected void doWriteEmptyElement(String prefix, String localName, String namespaceURI) {
+    protected void doWriteEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
         doWriteStartElement(prefix, localName, namespaceURI);
         finishStartElement();
         doWriteEndElement();
     }
 
     protected void doWriteEmptyElement(String localName) throws XMLStreamException {
-        throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT use XMLStreamWriter#writeEmptyElement(String)");
+        throw new UnsupportedOperationException();
     }
 
-    protected void doWriteAttribute(String prefix, String namespaceURI, String localName, String value) {
-        handler.processAttribute(normalize(namespaceURI), localName, normalize(prefix), value, OMConstants.XMLATTRTYPE_CDATA, true);
+    protected void doWriteAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
+        try {
+            handler.processAttribute(normalize(namespaceURI), localName, normalize(prefix), value, OMConstants.XMLATTRTYPE_CDATA, true);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
     protected void doWriteAttribute(String localName, String value) throws XMLStreamException {
         doWriteAttribute(null, null, localName, value);
     }
 
-    protected void doWriteNamespace(String prefix, String namespaceURI) {
-        handler.processNamespaceDeclaration(normalize(prefix), normalize(namespaceURI));
+    protected void doWriteNamespace(String prefix, String namespaceURI) throws XMLStreamException {
+        try {
+            handler.processNamespaceDeclaration(normalize(prefix), normalize(namespaceURI));
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteDefaultNamespace(String namespaceURI) {
+    protected void doWriteDefaultNamespace(String namespaceURI) throws XMLStreamException {
         doWriteNamespace(null, namespaceURI);
     }
 
-    protected void doWriteCharacters(char[] text, int start, int len) {
+    protected void doWriteCharacters(char[] text, int start, int len) throws XMLStreamException {
         doWriteCharacters(new String(text, start, len));
     }
 
-    protected void doWriteCharacters(String text) {
+    protected void doWriteCharacters(String text) throws XMLStreamException {
         finishStartElement();
-        handler.processCharacterData(text, false);
+        try {
+            handler.processCharacterData(text, false);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteCData(String data) {
+    protected void doWriteCData(String data) throws XMLStreamException {
         finishStartElement();
-        handler.processCDATASection(data);
+        try {
+            handler.processCDATASection(data);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteComment(String data) {
+    protected void doWriteComment(String data) throws XMLStreamException {
         finishStartElement();
-        handler.processComment(data);
+        try {
+            handler.processComment(data);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
     protected void doWriteEntityRef(String name) throws XMLStreamException {
         finishStartElement();
-        handler.processEntityReference(name, null);
+        try {
+            handler.processEntityReference(name, null);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteProcessingInstruction(String piTarget, String data) {
+    protected void doWriteProcessingInstruction(String piTarget, String data) throws XMLStreamException {
         finishStartElement();
-        handler.processProcessingInstruction(piTarget, data);
+        try {
+            handler.processProcessingInstruction(piTarget, data);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
-    protected void doWriteProcessingInstruction(String target) {
+    protected void doWriteProcessingInstruction(String target) throws XMLStreamException {
         doWriteProcessingInstruction(target, "");
     }
 
-    public void flush() throws XMLStreamException {
+    public void flush() {
         // Do nothing
     }
 
     public void close() throws XMLStreamException {
-        throw new UnsupportedOperationException("OMDataSource#serialize(XMLStreamWriter) MUST NOT call XMLStreamWriter#close()");
+        // Do nothing
     }
 
     public void writeDataHandler(DataHandler dataHandler, String contentID, boolean optimize)
             throws IOException, XMLStreamException {
         finishStartElement();
-        handler.processCharacterData(new TextContent(contentID, dataHandler, optimize), false);
+        try {
+            handler.processCharacterData(new TextContent(contentID, dataHandler, optimize), false);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 
     public void writeDataHandler(DataHandlerProvider dataHandlerProvider, String contentID,
             boolean optimize) throws IOException, XMLStreamException {
         finishStartElement();
-        handler.processCharacterData(new TextContent(contentID, dataHandlerProvider, optimize), false);
+        try {
+            handler.processCharacterData(new TextContent(contentID, dataHandlerProvider, optimize), false);
+        } catch (StreamException ex) {
+            throw toXMLStreamException(ex);
+        }
     }
 }