Search in sources :

Example 31 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project webservices-axiom by apache.

the class MTOMLogSample method logMessage.

// START SNIPPET: variant2
private void logMessage(SOAPEnvelope env) throws XMLStreamException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter writer = new LogWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(sw));
    env.serialize(writer);
    writer.flush();
    log.info("Message: " + sw.toString());
}
Also used : StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 32 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project webservices-axiom by apache.

the class TestSerialize method runTest.

@Override
protected void runTest() throws Throwable {
    OMText text = metaFactory.getOMFactory().createOMText("test", type);
    XMLStreamWriter writer = mock(XMLStreamWriter.class);
    text.serialize(writer);
    if (type == OMNode.CDATA_SECTION_NODE) {
        verify(writer).writeCData(text.getText());
    } else {
        verify(writer).writeCharacters(text.getText());
    }
    verify(writer, atMost(1)).flush();
    verifyNoMoreInteractions(writer);
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OMText(org.apache.axiom.om.OMText)

Example 33 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project webservices-axiom by apache.

the class TestSerialize method runTest.

@Override
protected void runTest() throws Throwable {
    OMDocType doctype = metaFactory.getOMFactory().createOMDocType(null, "root", null, "my.dtd", null);
    XMLStreamWriter writer = mock(XMLStreamWriter.class);
    doctype.serialize(writer);
    verify(writer).writeDTD("<!DOCTYPE root SYSTEM \"my.dtd\">");
    verify(writer, atMost(1)).flush();
    verifyNoMoreInteractions(writer);
}
Also used : OMDocType(org.apache.axiom.om.OMDocType) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 34 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter 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 35 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project webservices-axiom by apache.

the class TestSerialize method runTest.

@Override
protected void runTest() throws Throwable {
    OMEntityReference entref = metaFactory.getOMFactory().createOMEntityReference(null, "testref");
    XMLStreamWriter writer = mock(XMLStreamWriter.class);
    entref.serialize(writer);
    verify(writer).writeEntityRef(entref.getName());
    verify(writer, atMost(1)).flush();
    verifyNoMoreInteractions(writer);
}
Also used : OMEntityReference(org.apache.axiom.om.OMEntityReference) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)209 XMLStreamException (javax.xml.stream.XMLStreamException)84 StringWriter (java.io.StringWriter)47 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)32 QName (javax.xml.namespace.QName)26 Document (org.w3c.dom.Document)26 Fault (org.apache.cxf.interceptor.Fault)25 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StreamSource (javax.xml.transform.stream.StreamSource)17 StringReader (java.io.StringReader)16 JAXBException (javax.xml.bind.JAXBException)14 DOMSource (javax.xml.transform.dom.DOMSource)14 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)14 Message (org.apache.cxf.message.Message)13 Element (org.w3c.dom.Element)11