Search in sources :

Example 56 with StreamResult

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

the class SaxonUriResolverTest method test.

@Test
public void test() throws Exception {
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Source xsl = fromClasspath(XSL_PATH);
    xsl.setSystemId("classpath:/" + XSL_PATH);
    Source xml = fromString(XML_DATA);
    TransformerFactory factory = new TransformerFactoryImpl();
    Transformer transformer = factory.newTransformer(xsl);
    transformer.setURIResolver(new XsltUriResolver(context(), XSL_PATH));
    transformer.transform(xml, result);
    Assert.assertEquals(XML_RESP, writer.toString());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerFactoryImpl(net.sf.saxon.TransformerFactoryImpl) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) XsltUriResolver(org.apache.camel.builder.xml.XsltUriResolver) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 57 with StreamResult

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

the class EipDocumentationEnricherMojo method saveToFile.

private void saveToFile(Document document, File outputFile, Transformer transformer) throws FileNotFoundException, TransformerException {
    StreamResult result = new StreamResult(new FileOutputStream(outputFile));
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream)

Example 58 with StreamResult

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

the class SpringBootStarterMojo method writeXmlFormatted.

private void writeXmlFormatted(Document pom, File destination) throws Exception {
    XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//text()[normalize-space(.) = '']");
    NodeList emptyNodes = (NodeList) xpath.evaluate(pom, XPathConstants.NODESET);
    // Remove empty text nodes
    for (int i = 0; i < emptyNodes.getLength(); i++) {
        Node emptyNode = emptyNodes.item(i);
        emptyNode.getParentNode().removeChild(emptyNode);
    }
    pom.setXmlStandalone(true);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    DOMSource source = new DOMSource(pom);
    String content;
    try (StringWriter out = new StringWriter()) {
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
        content = out.toString();
    }
    // Fix header formatting problem
    content = content.replaceFirst("-->", "-->\n").replaceFirst("\\?><!--", "\\?>\n<!--");
    writeIfChanged(content, destination);
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DependencyNode(org.apache.maven.shared.dependency.tree.DependencyNode)

Example 59 with StreamResult

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

the class ConsumerEndpointMappingByBeanNameRouteTest method testBeanName.

@Test
public void testBeanName() throws Exception {
    StreamSource source = new StreamSource(new StringReader(xmlRequestForGoogleStockQuote));
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    assertNotNull(result);
    TestUtil.assertEqualsIgnoreNewLinesSymbol(expectedResponse, sw.toString());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 60 with StreamResult

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

the class ConsumerEndpointMappingResponseHandlingRouteTest method testRootQName.

@Test
public void testRootQName() throws Exception {
    StreamSource source = new StreamSource(new StringReader(xmlRequestForGoogleStockQuote));
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    assertNotNull(result);
    TestUtil.assertEqualsIgnoreNewLinesSymbol(expectedResponse, sw.toString());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)448 Transformer (javax.xml.transform.Transformer)267 DOMSource (javax.xml.transform.dom.DOMSource)234 StringWriter (java.io.StringWriter)206 TransformerFactory (javax.xml.transform.TransformerFactory)138 TransformerException (javax.xml.transform.TransformerException)125 Document (org.w3c.dom.Document)103 IOException (java.io.IOException)94 StreamSource (javax.xml.transform.stream.StreamSource)88 Source (javax.xml.transform.Source)74 Test (org.junit.Test)73 DocumentBuilder (javax.xml.parsers.DocumentBuilder)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 Element (org.w3c.dom.Element)59 File (java.io.File)58 Result (javax.xml.transform.Result)57 StringReader (java.io.StringReader)56 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)50 ByteArrayInputStream (java.io.ByteArrayInputStream)44