Search in sources :

Example 51 with XMLStreamWriter

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

the class OutTransformWriterTest method testReadWithComplexTransformationNamespace2.

@Test
public void testReadWithComplexTransformationNamespace2() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put("{http://testbeans.com/double}*", "{http://testbeans.com/double/v2}*");
    map.put("{http://testbeans.com}*", "{http://testbeans.com/v3}*");
    // the namespaces are prefixed in the input
    XMLStreamReader reader = TransformTestUtils.createOutTransformedStreamReader("../resources/doubleBeanIn1.xml", map, null, null, null, false, null);
    XMLStreamReader reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
    TransformTestUtils.verifyReaders(reader2, reader, true, false);
    // the child elements with the default namespace that is declared in the elements
    reader = TransformTestUtils.createOutTransformedStreamReader("../resources/doubleBeanIn2.xml", map, null, null, null, false, null);
    reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
    TransformTestUtils.verifyReaders(reader2, reader, true, false);
    // the child elements with the default namespace that is declared in their parent element
    reader = TransformTestUtils.createOutTransformedStreamReader("../resources/doubleBeanIn3.xml", map, null, null, null, false, null);
    reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
    TransformTestUtils.verifyReaders(reader2, reader, true, false);
    // writing each child separately (as the soap header children are serialized)
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XMLStreamWriter writer = new OutTransformWriter(StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name()), map, null, null, null, false, null);
    boolean nsset = "ns3".equals(writer.getNamespaceContext().getPrefix("http://testbeans.com/double"));
    writer.writeStartElement("ns3", "testDoubleBean", "http://testbeans.com/double");
    if (!nsset) {
        writer.writeNamespace("ns3", "http://testbeans.com/double");
    }
    nsset = "".equals(writer.getNamespaceContext().getPrefix("http://testbeans.com"));
    writer.writeStartElement("", "bean", "http://testbeans.com");
    if (!nsset) {
        writer.writeNamespace("", "http://testbeans.com");
    }
    writer.writeEndElement();
    nsset = "".equals(writer.getNamespaceContext().getPrefix("http://testbeans.com"));
    writer.writeStartElement("", "beanNext", "http://testbeans.com");
    if (!nsset) {
        writer.writeNamespace("", "http://testbeans.com");
    }
    writer.writeEndElement();
    writer.writeEndElement();
    writer.flush();
    reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(os.toByteArray()));
    reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
    TransformTestUtils.verifyReaders(reader2, reader, true, false);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 52 with XMLStreamWriter

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

the class OutTransformWriterTest method testDefaultNamespace.

@Test
public void testDefaultNamespace() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name());
    Map<String, String> outMap = new HashMap<>();
    outMap.put("{http://testbeans.com}*", "{http://testbeans.com/v2}*");
    OutTransformWriter transformWriter = new OutTransformWriter(writer, outMap, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), false, "http://testbeans.com/v2");
    JAXBContext context = JAXBContext.newInstance(TestBean.class);
    Marshaller m = context.createMarshaller();
    m.marshal(new TestBean(), transformWriter);
    String expected = "<?xml version='1.0' encoding='UTF-8'?>" + "<testBean xmlns=\"http://testbeans.com/v2\"><bean/></testBean>";
    assertEquals(expected, os.toString());
}
Also used : Marshaller(javax.xml.bind.Marshaller) HashMap(java.util.HashMap) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 53 with XMLStreamWriter

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

the class OutTransformWriterTest method testReplaceSimpleElement.

// additional test cases
@Test
public void testReplaceSimpleElement() throws Exception {
    InputStream is = new ByteArrayInputStream("<ns:test xmlns:ns=\"http://bar\"><ns:a>1</ns:a></ns:test>".getBytes());
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XMLStreamWriter writer = new OutTransformWriter(StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name()), null, Collections.singletonMap("{http://bar}a", "{http://bar}a=1 2 3"), null, null, false, null);
    StaxUtils.copy(new StreamSource(is), writer);
    writer.flush();
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(os.toByteArray()));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StaxUtils.copy(reader, bos);
    String value = bos.toString();
    assertEquals("<ns:test xmlns:ns=\"http://bar\"><ns:a>1 2 3</ns:a></ns:test>", value);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 54 with XMLStreamWriter

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

the class AbstractLoggingInterceptor method writePrettyPayload.

protected void writePrettyPayload(StringBuilder builder, StringWriter stringWriter, String contentType) throws Exception {
    // Just transform the XML message when the cos has content
    StringWriter swriter = new StringWriter();
    XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter);
    xwriter = new PrettyPrintXMLStreamWriter(xwriter, 2);
    StaxUtils.copy(new StreamSource(new StringReader(stringWriter.getBuffer().toString())), xwriter);
    xwriter.close();
    String result = swriter.toString();
    if (result.length() < limit || limit == -1) {
        builder.append(swriter.toString());
    } else {
        builder.append(swriter.toString().substring(0, limit));
    }
}
Also used : PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Example 55 with XMLStreamWriter

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

the class AbstractOutDatabindingInterceptor method writeParts.

protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, List<MessagePartInfo> parts) {
    OutputStream out = message.getContent(OutputStream.class);
    XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);
    Service service = exchange.getService();
    XMLStreamWriter xmlWriter = origXmlWriter;
    CachingXmlEventWriter cache = null;
    // configure endpoint and operation level schema validation
    setOperationSchemaValidation(message);
    // need to cache the events in case validation fails or buffering is enabled
    if (shouldBuffer(message)) {
        if (!(xmlWriter instanceof CachingXmlEventWriter)) {
            cache = new CachingXmlEventWriter();
            try {
                cache.setNamespaceContext(origXmlWriter.getNamespaceContext());
            } catch (XMLStreamException e) {
            // ignorable, will just get extra namespace decls
            }
            xmlWriter = cache;
        }
        out = null;
    }
    if (out != null && writeToOutputStream(message, operation.getBinding(), service) && !MessageUtils.getContextualBoolean(message, DISABLE_OUTPUTSTREAM_OPTIMIZATION, false)) {
        if (xmlWriter != null) {
            try {
                xmlWriter.writeCharacters("");
                xmlWriter.flush();
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
        }
        DataWriter<OutputStream> osWriter = getDataWriter(message, service, OutputStream.class);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                Object o = objs.get(part);
                osWriter.write(o, part, out);
            }
        }
    } else {
        DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message, service, XMLStreamWriter.class);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                NamespaceContext c = null;
                if (!part.isElement() && xmlWriter instanceof CachingXmlEventWriter) {
                    try {
                        c = xmlWriter.getNamespaceContext();
                        xmlWriter.setNamespaceContext(new CachingXmlEventWriter.NSContext(null));
                    } catch (XMLStreamException e) {
                    // ignore
                    }
                }
                Object o = objs.get(part);
                dataWriter.write(o, part, xmlWriter);
                if (c != null) {
                    try {
                        xmlWriter.setNamespaceContext(c);
                    } catch (XMLStreamException e) {
                    // ignore
                    }
                }
            }
        }
    }
    if (cache != null) {
        try {
            for (XMLEvent event : cache.getEvents()) {
                StaxUtils.writeEvent(event, origXmlWriter);
            }
        } catch (XMLStreamException e) {
            throw new Fault(e);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Service(org.apache.cxf.service.Service) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) NamespaceContext(javax.xml.namespace.NamespaceContext) XMLEvent(javax.xml.stream.events.XMLEvent)

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