Search in sources :

Example 1 with XmlHandlerStreamWriter

use of org.apache.axiom.core.stream.stax.push.XmlHandlerStreamWriter in project webservices-axiom by apache.

the class PushOMDataSourceReader method proceed.

@Override
public boolean proceed() throws StreamException {
    // TODO: we might want to unwrap the NamespaceRepairingFilter (and some other filters) here
    XmlHandler handler = this.handler;
    OMOutputFormat format = null;
    XmlHandler current = handler;
    while (current instanceof XmlHandlerWrapper) {
        if (current instanceof XmlDeclarationRewriterHandler) {
            format = ((XmlDeclarationRewriterHandler) current).getFormat();
            break;
        }
        current = ((XmlHandlerWrapper) current).getParent();
    }
    if (format == null) {
        // This is for the OMSourcedElement expansion case
        format = new OMOutputFormat();
        format.setDoOptimize(true);
        handler = new PushOMDataSourceXOPHandler(handler);
    }
    try {
        XMLStreamWriter writer = new XmlHandlerStreamWriter(handler, null, AxiomXMLStreamWriterExtensionFactory.INSTANCE);
        // 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());
            }
        }
        handler.startFragment();
        dataSource.serialize(new MTOMXMLStreamWriterImpl(new PushOMDataSourceStreamWriter(writer), format));
        handler.completed();
    } catch (XMLStreamException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof StreamException) {
            throw (StreamException) cause;
        } else {
            throw new StreamException(ex);
        }
    }
    return true;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) XmlHandlerStreamWriter(org.apache.axiom.core.stream.stax.push.XmlHandlerStreamWriter) StreamException(org.apache.axiom.core.stream.StreamException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamException(javax.xml.stream.XMLStreamException) XmlHandlerWrapper(org.apache.axiom.core.stream.XmlHandlerWrapper) XmlHandler(org.apache.axiom.core.stream.XmlHandler) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) XmlDeclarationRewriterHandler(org.apache.axiom.om.impl.stream.XmlDeclarationRewriterHandler) OMContainer(org.apache.axiom.om.OMContainer)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 StreamException (org.apache.axiom.core.stream.StreamException)1 XmlHandler (org.apache.axiom.core.stream.XmlHandler)1 XmlHandlerWrapper (org.apache.axiom.core.stream.XmlHandlerWrapper)1 XmlHandlerStreamWriter (org.apache.axiom.core.stream.stax.push.XmlHandlerStreamWriter)1 OMContainer (org.apache.axiom.om.OMContainer)1 OMElement (org.apache.axiom.om.OMElement)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)1 XmlDeclarationRewriterHandler (org.apache.axiom.om.impl.stream.XmlDeclarationRewriterHandler)1