Search in sources :

Example 46 with XMLStreamWriter

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

the class StaxUtilsTest method testDefaultPrefixInRootElementWithJDKInternalCopyTransformer.

@Test
public void testDefaultPrefixInRootElementWithJDKInternalCopyTransformer() throws Exception {
    TransformerFactory trf = null;
    try {
        trf = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null);
        trf.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String xml = "<root xmlns=\"urn:org.apache.cxf:test\">Text</root>";
        StringReader stringReader = new StringReader(xml);
        StreamSource source = new StreamSource(stringReader);
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(source);
        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
        StaxSource staxSource = new StaxSource(reader);
        Document doc = StaxUtils.read(getTestStream("./resources/copy.xsl"));
        Transformer transformer = trf.newTransformer(new DOMSource(doc));
        // System.out.println("Used transformer: " + transformer.getClass().getName());
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(staxSource, new StreamResult(baos));
        writer.flush();
        baos.flush();
        assertThat(new String(baos.toByteArray()), equalTo(xml));
    } catch (Throwable throwable) {
        // ignore on non Sun/Oracle JDK
        return;
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 47 with XMLStreamWriter

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

the class StaxUtilsTest method testCXF3193.

@Test
public void testCXF3193() throws Exception {
    String testString = "<a:elem1 xmlns:a=\"test\" xmlns:b=\"test\" a:attr1=\"value\"/>";
    CachingXmlEventWriter writer = new CachingXmlEventWriter();
    StaxUtils.copy(StaxUtils.createXMLStreamReader(new StringReader(testString)), writer);
    StringWriter swriter = new StringWriter();
    XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter);
    for (XMLEvent event : writer.getEvents()) {
        StaxUtils.writeEvent(event, xwriter);
    }
    xwriter.flush();
    String s = swriter.toString();
    int idx = s.indexOf("xmlns:a");
    idx = s.indexOf("xmlns:a", idx + 1);
    assertEquals(-1, idx);
}
Also used : StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) Test(org.junit.Test)

Example 48 with XMLStreamWriter

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

the class StaxUtilsTest method testCopy.

@Test
public void testCopy() throws Exception {
    // do the stream copying
    String soapMessage = "./resources/headerSoapReq.xml";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(getTestStream(soapMessage));
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
    StaxUtils.copy(reader, writer);
    writer.flush();
    baos.flush();
    // write output to a string
    String output = baos.toString();
    baos.close();
    // re-read the input xml doc to a string
    InputStreamReader inputStreamReader = new InputStreamReader(getTestStream(soapMessage));
    StringWriter stringWriter = new StringWriter();
    char[] buffer = new char[4096];
    int n = 0;
    n = inputStreamReader.read(buffer);
    while (n > 0) {
        stringWriter.write(buffer, 0, n);
        n = inputStreamReader.read(buffer);
    }
    String input = stringWriter.toString();
    stringWriter.close();
    // seach for the first begin of "<soap:Envelope" to escape the apache licenses header
    int beginIndex = input.indexOf("<soap:Envelope");
    input = input.substring(beginIndex);
    beginIndex = output.indexOf("<soap:Envelope");
    output = output.substring(beginIndex);
    output = output.replaceAll("\r\n", "\n");
    input = input.replaceAll("\r\n", "\n");
    // compare the input and output string
    assertEquals(input, output);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 49 with XMLStreamWriter

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

the class StaxUtilsTest method testDefaultPrefixInRootElementWithXalanCopyTransformer.

@Test
public void testDefaultPrefixInRootElementWithXalanCopyTransformer() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String xml = "<root xmlns=\"urn:org.apache.cxf:test\">Text</root>";
    StringReader stringReader = new StringReader(xml);
    // StreamSource source = new StreamSource(stringReader);
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(stringReader);
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
    StaxSource staxSource = new StaxSource(reader);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    Document doc = StaxUtils.read(getTestStream("./resources/copy.xsl"));
    Transformer transformer = transformerFactory.newTransformer(new DOMSource(doc));
    // System.out.println("Used transformer: " + transformer.getClass().getName());
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(staxSource, new StreamResult(baos));
    writer.flush();
    baos.flush();
    assertThat(new String(baos.toByteArray()), equalTo(xml));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 50 with XMLStreamWriter

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

the class StaxUtilsTest method testNonNamespaceAwareParser.

@Test
public void testNonNamespaceAwareParser() throws Exception {
    String xml = "<blah xmlns=\"http://blah.org/\" xmlns:snarf=\"http://snarf.org\">" + "<foo snarf:blop=\"blop\">foo</foo></blah>";
    StringReader reader = new StringReader(xml);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(false);
    dbf.setValidating(false);
    Document doc = dbf.newDocumentBuilder().parse(new InputSource(reader));
    Source source = new DOMSource(doc);
    dbf.setNamespaceAware(true);
    reader = new StringReader(xml);
    Document docNs = dbf.newDocumentBuilder().parse(new InputSource(reader));
    Source sourceNs = new DOMSource(docNs);
    XMLStreamReader sreader = StaxUtils.createXMLStreamReader(source);
    StringWriter sw = new StringWriter();
    XMLStreamWriter swriter = StaxUtils.createXMLStreamWriter(sw);
    // should not throw an exception
    StaxUtils.copy(sreader, swriter);
    swriter.flush();
    swriter.close();
    String output = sw.toString();
    assertTrue(output.contains("blah"));
    assertTrue(output.contains("foo"));
    assertTrue(output.contains("snarf"));
    assertTrue(output.contains("blop"));
    sreader = StaxUtils.createXMLStreamReader(sourceNs);
    sw = new StringWriter();
    swriter = StaxUtils.createXMLStreamWriter(sw);
    // should not throw an exception
    StaxUtils.copy(sreader, swriter);
    swriter.flush();
    swriter.close();
    output = sw.toString();
    assertTrue(output.contains("blah"));
    assertTrue(output.contains("foo"));
    assertTrue(output.contains("snarf"));
    assertTrue(output.contains("blop"));
    sreader = StaxUtils.createXMLStreamReader(source);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    swriter = StaxUtils.createXMLStreamWriter(bout);
    StaxUtils.copy(sreader, swriter);
    swriter.flush();
    swriter.close();
    output = bout.toString();
    assertTrue(output.contains("blah"));
    assertTrue(output.contains("foo"));
    assertTrue(output.contains("snarf"));
    assertTrue(output.contains("blop"));
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) Test(org.junit.Test)

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