Search in sources :

Example 1 with XmlHandler

use of org.apache.axiom.core.stream.XmlHandler in project webservices-axiom by apache.

the class MTOMXMLStreamWriterImpl method getOutputStream.

@Override
public OutputStream getOutputStream() throws XMLStreamException {
    OutputStream outputStream;
    XmlHandler handler = getHandler();
    // Remove wrappers that can be safely removed
    while (handler instanceof DocumentElementExtractingFilterHandler || handler instanceof NamespaceRepairingFilterHandler || handler instanceof XsiTypeFilterHandler || handler instanceof XmlDeclarationRewriterHandler || handler instanceof XOPEncodingFilterHandler) {
        handler = ((XmlHandlerWrapper) handler).getParent();
    }
    if (handler instanceof Serializer) {
        try {
            outputStream = ((Serializer) handler).getOutputStream();
        } catch (StreamException ex) {
            throw new XMLStreamException(ex);
        }
    } else {
        outputStream = null;
    }
    if (log.isDebugEnabled()) {
        if (outputStream == null) {
            log.debug("Direct access to the output stream is not available.");
        } else {
            log.debug("Returning access to the output stream: " + outputStream);
        }
    }
    return outputStream;
}
Also used : NamespaceRepairingFilterHandler(org.apache.axiom.core.stream.NamespaceRepairingFilterHandler) XMLStreamException(javax.xml.stream.XMLStreamException) XmlHandler(org.apache.axiom.core.stream.XmlHandler) DocumentElementExtractingFilterHandler(org.apache.axiom.core.stream.DocumentElementExtractingFilterHandler) XsiTypeFilterHandler(org.apache.axiom.om.impl.stream.XsiTypeFilterHandler) OutputStream(java.io.OutputStream) XmlDeclarationRewriterHandler(org.apache.axiom.om.impl.stream.XmlDeclarationRewriterHandler) XOPEncodingFilterHandler(org.apache.axiom.om.impl.stream.xop.XOPEncodingFilterHandler) Serializer(org.apache.axiom.core.stream.serializer.Serializer) StreamException(org.apache.axiom.core.stream.StreamException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with XmlHandler

use of org.apache.axiom.core.stream.XmlHandler 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)

Example 3 with XmlHandler

use of org.apache.axiom.core.stream.XmlHandler in project webservices-axiom by apache.

the class DOMReaderTest method testSimpleDocument.

@Test
public void testSimpleDocument() throws Exception {
    Document document = DOMImplementation.XERCES.newDocument();
    Element root = document.createElementNS("urn:test", "p:root");
    document.appendChild(root);
    root.setTextContent("test");
    XmlHandler handler = mock(XmlHandler.class);
    DOMReader reader = new DOMReader(handler, document, false);
    assertThat(reader.proceed()).isFalse();
    verify(handler).startDocument(null, "1.0", null, false);
    verifyNoMoreInteractions(handler);
    assertThat(reader.proceed()).isFalse();
    verify(handler).startElement("urn:test", "root", "p");
    verify(handler).attributesCompleted();
    verifyNoMoreInteractions(handler);
    assertThat(reader.proceed()).isFalse();
    verify(handler).processCharacterData("test", false);
    verifyNoMoreInteractions(handler);
    assertThat(reader.proceed()).isFalse();
    verify(handler).endElement();
    verifyNoMoreInteractions(handler);
    assertThat(reader.proceed()).isTrue();
    verify(handler).completed();
    verifyNoMoreInteractions(handler);
}
Also used : XmlHandler(org.apache.axiom.core.stream.XmlHandler) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 4 with XmlHandler

use of org.apache.axiom.core.stream.XmlHandler in project webservices-axiom by apache.

the class XMLReaderImpl method parse.

private void parse() throws SAXException {
    XmlHandler handler = new ContentHandlerXmlHandler(contentHandler, lexicalHandler);
    CoreElement contextElement = root.getContextElement();
    if (contextElement != null) {
        handler = new NamespaceContextPreservationFilterHandler(handler, contextElement);
    }
    try {
        root.internalSerialize(handler, cache);
    } catch (CoreModelException ex) {
        throw new SAXException(ex);
    } catch (StreamException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof SAXException) {
            throw (SAXException) cause;
        } else if (cause instanceof Exception) {
            throw new SAXException((Exception) cause);
        } else {
            throw new SAXException(ex);
        }
    }
}
Also used : CoreElement(org.apache.axiom.core.CoreElement) NamespaceContextPreservationFilterHandler(org.apache.axiom.om.impl.stream.NamespaceContextPreservationFilterHandler) ContentHandlerXmlHandler(org.apache.axiom.core.stream.sax.ContentHandlerXmlHandler) XmlHandler(org.apache.axiom.core.stream.XmlHandler) CoreModelException(org.apache.axiom.core.CoreModelException) ContentHandlerXmlHandler(org.apache.axiom.core.stream.sax.ContentHandlerXmlHandler) StreamException(org.apache.axiom.core.stream.StreamException) CoreModelException(org.apache.axiom.core.CoreModelException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) StreamException(org.apache.axiom.core.stream.StreamException)

Aggregations

XmlHandler (org.apache.axiom.core.stream.XmlHandler)4 StreamException (org.apache.axiom.core.stream.StreamException)3 XMLStreamException (javax.xml.stream.XMLStreamException)2 XmlDeclarationRewriterHandler (org.apache.axiom.om.impl.stream.XmlDeclarationRewriterHandler)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 CoreElement (org.apache.axiom.core.CoreElement)1 CoreModelException (org.apache.axiom.core.CoreModelException)1 DocumentElementExtractingFilterHandler (org.apache.axiom.core.stream.DocumentElementExtractingFilterHandler)1 NamespaceRepairingFilterHandler (org.apache.axiom.core.stream.NamespaceRepairingFilterHandler)1 XmlHandlerWrapper (org.apache.axiom.core.stream.XmlHandlerWrapper)1 ContentHandlerXmlHandler (org.apache.axiom.core.stream.sax.ContentHandlerXmlHandler)1 Serializer (org.apache.axiom.core.stream.serializer.Serializer)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 NamespaceContextPreservationFilterHandler (org.apache.axiom.om.impl.stream.NamespaceContextPreservationFilterHandler)1