Search in sources :

Example 11 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class XmlConverterTest method testToStreamSourceByStAXSource.

public void testToStreamSourceByStAXSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    StAXSource source = conv.toStAXSource("<foo>bar</foo>", null);
    StreamSource out = conv.toStreamSource(source, null);
    assertNotSame(source, out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource)

Example 12 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class XmlConverterTest method testToStreamSourceByInputStream.

public void testToStreamSourceByInputStream() throws Exception {
    XmlConverter conv = new XmlConverter();
    InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
    StreamSource out = conv.toStreamSource(is);
    assertNotNull(out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource)

Example 13 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class StAX2SAXSourceTest method testDefaultPrefixInRootElementWithCopyTransformer.

public void testDefaultPrefixInRootElementWithCopyTransformer() throws Exception {
    TransformerFactory trf = TransformerFactory.newInstance();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLStreamReader reader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, new StringReader(TEST_XML));
    // ensure UTF-8 encoding
    Exchange exchange = new DefaultExchange(context);
    exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
    XMLStreamWriter writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, baos);
    StAX2SAXSource staxSource = new StAX2SAXSource(reader);
    StreamSource templateSource = new StreamSource(getClass().getResourceAsStream("/xslt/common/copy.xsl"));
    Transformer transformer = trf.newTransformer(templateSource);
    log.info("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(TEST_XML));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) TransformerFactory(javax.xml.transform.TransformerFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class XmlConverterTest method testToStreamSourceByStreamSource.

public void testToStreamSourceByStreamSource() throws Exception {
    Exchange exchange = new DefaultExchange(context);
    XmlConverter conv = new XmlConverter();
    StreamSource source = conv.toStreamSource("<foo>bar</foo>".getBytes(), exchange);
    StreamSource out = conv.toStreamSource(source, null);
    assertSame(source, out);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) StreamSource(javax.xml.transform.stream.StreamSource)

Example 15 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class XmlConverterTest method testToStreamSourceByByteBuffer.

public void testToStreamSourceByByteBuffer() throws Exception {
    Exchange exchange = new DefaultExchange(context);
    XmlConverter conv = new XmlConverter();
    ByteBuffer bytes = context.getTypeConverter().convertTo(ByteBuffer.class, "<foo>bar</foo>");
    StreamSource out = conv.toStreamSource(bytes, exchange);
    assertNotNull(out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) StreamSource(javax.xml.transform.stream.StreamSource) ByteBuffer(java.nio.ByteBuffer)

Aggregations

StreamSource (javax.xml.transform.stream.StreamSource)338 Source (javax.xml.transform.Source)115 StringReader (java.io.StringReader)101 StreamResult (javax.xml.transform.stream.StreamResult)85 Transformer (javax.xml.transform.Transformer)74 Test (org.junit.Test)73 InputStream (java.io.InputStream)68 TransformerFactory (javax.xml.transform.TransformerFactory)58 ByteArrayInputStream (java.io.ByteArrayInputStream)56 IOException (java.io.IOException)52 DOMSource (javax.xml.transform.dom.DOMSource)49 TransformerException (javax.xml.transform.TransformerException)48 StringWriter (java.io.StringWriter)45 SchemaFactory (javax.xml.validation.SchemaFactory)44 InputSource (org.xml.sax.InputSource)44 Schema (javax.xml.validation.Schema)43 SAXException (org.xml.sax.SAXException)42 SAXSource (javax.xml.transform.sax.SAXSource)33 Validator (javax.xml.validation.Validator)31 File (java.io.File)27