Search in sources :

Example 91 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class SerializationTest method serializeObject.

private String serializeObject(DataBinding dataBinding, TestBean1 bean) throws XMLStreamException {
    DataWriter<XMLStreamWriter> writer = dataBinding.createWriter(XMLStreamWriter.class);
    StringWriter stringWriter = new StringWriter();
    XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(stringWriter);
    writer.write(bean, xmlStreamWriter);
    xmlStreamWriter.flush();
    xmlStreamWriter.close();
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 92 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class JSONProvider method marshal.

protected void marshal(Marshaller ms, Object actualObject, Class<?> actualClass, Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception {
    OutputStream actualOs = os;
    MessageContext mc = getContext();
    if (mc != null && PropertyUtils.isTrue(mc.get(Marshaller.JAXB_FORMATTED_OUTPUT))) {
        actualOs = new CachedOutputStream();
    }
    XMLStreamWriter writer = createWriter(actualObject, actualClass, genericType, enc, actualOs, isCollection);
    if (namespaceMap.size() > 1 || namespaceMap.size() == 1 && !namespaceMap.containsKey(JSONUtils.XSI_URI)) {
        setNamespaceMapper(ms, namespaceMap);
    }
    org.apache.cxf.common.jaxb.JAXBUtils.setNoEscapeHandler(ms);
    ms.marshal(actualObject, writer);
    writer.close();
    if (os != actualOs) {
        StringIndenter formatter = new StringIndenter(IOUtils.newStringFromBytes(((CachedOutputStream) actualOs).getBytes()));
        Writer outWriter = new OutputStreamWriter(os, enc);
        IOUtils.copy(new StringReader(formatter.result()), outWriter, 2048);
        outWriter.close();
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) StringIndenter(org.codehaus.jettison.util.StringIndenter) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 93 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class StaxSerializer method deserialize.

private Node deserialize(Node ctx, XMLStreamReader reader, boolean wrapped) throws XMLEncryptionException {
    Document contextDocument = null;
    if (Node.DOCUMENT_NODE == ctx.getNodeType()) {
        contextDocument = (Document) ctx;
    } else {
        contextDocument = ctx.getOwnerDocument();
    }
    XMLStreamWriter writer = null;
    try {
        if (ctx instanceof SOAPElement) {
            SOAPElement el = (SOAPElement) ctx;
            while (el != null && !(el instanceof SOAPEnvelope)) {
                el = el.getParentElement();
            }
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new SAAJStreamWriter((SOAPEnvelope) el, element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        if (DOMUtils.isJava9SAAJ()) {
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new OverlayW3CDOMStreamWriter(ctx.getOwnerDocument(), element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        // Import to a dummy fragment
        DocumentFragment dummyFragment = contextDocument.createDocumentFragment();
        writer = StaxUtils.createXMLStreamWriter(new DOMResult(dummyFragment));
        StaxUtils.copy(reader, writer);
        if (wrapped) {
            DocumentFragment result = contextDocument.createDocumentFragment();
            Node child = dummyFragment.getFirstChild().getFirstChild();
            if (child != null && child.getNextSibling() == null) {
                return child;
            }
            while (child != null) {
                Node nextChild = child.getNextSibling();
                result.appendChild(child);
                child = nextChild;
            }
            dummyFragment = result;
        }
        return dummyFragment;
    } catch (XMLStreamException ex) {
        throw new XMLEncryptionException(ex);
    }
}
Also used : OverlayW3CDOMStreamWriter(org.apache.cxf.staxutils.OverlayW3CDOMStreamWriter) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment) SAAJStreamWriter(org.apache.cxf.binding.soap.saaj.SAAJStreamWriter) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException)

Example 94 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class AbstractSecurityTest method getMessageBytes.

protected byte[] getMessageBytes(Document doc) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLStreamWriter byteArrayWriter = StaxUtils.createXMLStreamWriter(outputStream);
    StaxUtils.writeDocument(doc, byteArrayWriter, false);
    byteArrayWriter.flush();
    return outputStream.toByteArray();
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 95 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class HWSAXSourcePayloadProvider method getSOAPBodyFile.

private File getSOAPBodyFile(Document doc) throws Exception {
    File file = FileUtils.createTempFile("cxf-systest", "xml");
    try (FileOutputStream out = new FileOutputStream(file)) {
        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
        StaxUtils.writeDocument(doc, writer, true);
        writer.close();
        return file;
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

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