Search in sources :

Example 66 with File

use of java.io.File in project camel by apache.

the class XsltBuilderTest method testXsltTemplates.

public void testXsltTemplates() throws Exception {
    File file = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl");
    Source source = new SAXSource(new InputSource(new FileInputStream(file)));
    XmlConverter converter = new XmlConverter();
    Templates styleSheet = converter.getTransformerFactory().newTemplates(source);
    XsltBuilder builder = XsltBuilder.xslt(styleSheet);
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    builder.process(exchange);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Templates(javax.xml.transform.Templates) File(java.io.File) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) FileInputStream(java.io.FileInputStream) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 67 with File

use of java.io.File in project camel by apache.

the class XsltBuilderTest method testXsltTransformerFile.

public void testXsltTransformerFile() throws Exception {
    File styleSheet = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl");
    XsltBuilder builder = new XsltBuilder();
    builder.setTransformerFile(styleSheet);
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    builder.process(exchange);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) File(java.io.File)

Example 68 with File

use of java.io.File in project camel by apache.

the class XsltBuilderTest method testXsltTransformerInputStream.

public void testXsltTransformerInputStream() throws Exception {
    File styleSheet = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl");
    XsltBuilder builder = new XsltBuilder();
    builder.setTransformerInputStream(new FileInputStream(styleSheet));
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    builder.process(exchange);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 69 with File

use of java.io.File in project camel by apache.

the class XsltBuilderTest method testXsltOutputFile.

public void testXsltOutputFile() throws Exception {
    URL styleSheet = getClass().getResource("example.xsl");
    XsltBuilder builder = XsltBuilder.xslt(styleSheet).outputFile();
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    exchange.getIn().setHeader(Exchange.XSLT_FILE_NAME, "target/xslt/xsltout.xml");
    builder.process(exchange);
    assertIsInstanceOf(File.class, exchange.getOut().getBody());
    File file = new File("target/xslt/xsltout.xml");
    assertTrue("Output file should exist", file.exists());
    String body = exchange.getOut().getBody(String.class);
    assertTrue(body.endsWith("<goodbye>world!</goodbye>"));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) File(java.io.File) URL(java.net.URL)

Example 70 with File

use of java.io.File in project camel by apache.

the class XsltBuilderTest method testXsltOutputFileDelete.

public void testXsltOutputFileDelete() throws Exception {
    URL styleSheet = getClass().getResource("example.xsl");
    XsltBuilder builder = XsltBuilder.xslt(styleSheet).outputFile().deleteOutputFile();
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    exchange.getIn().setHeader(Exchange.XSLT_FILE_NAME, "target/xslt/xsltout.xml");
    builder.process(exchange);
    assertIsInstanceOf(File.class, exchange.getOut().getBody());
    File file = new File("target/xslt/xsltout.xml");
    assertTrue("Output file should exist", file.exists());
    String body = exchange.getOut().getBody(String.class);
    assertTrue(body.endsWith("<goodbye>world!</goodbye>"));
    // now done the exchange
    List<Synchronization> onCompletions = exchange.handoverCompletions();
    UnitOfWorkHelper.doneSynchronizations(exchange, onCompletions, log);
    // the file should be deleted
    assertFalse("Output file should be deleted", file.exists());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Synchronization(org.apache.camel.spi.Synchronization) File(java.io.File) URL(java.net.URL)

Aggregations

File (java.io.File)45560 IOException (java.io.IOException)9550 Test (org.junit.Test)9009 FileOutputStream (java.io.FileOutputStream)3388 ArrayList (java.util.ArrayList)3273 FileInputStream (java.io.FileInputStream)2915 InputStream (java.io.InputStream)1783 FileNotFoundException (java.io.FileNotFoundException)1743 URL (java.net.URL)1649 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1478 Test (org.testng.annotations.Test)1411 HashMap (java.util.HashMap)1404 RandomAccessFile (java.io.RandomAccessFile)1154 FileWriter (java.io.FileWriter)1081 Properties (java.util.Properties)1019 ZipFile (java.util.zip.ZipFile)959 JarFile (java.util.jar.JarFile)866 List (java.util.List)850 BufferedReader (java.io.BufferedReader)840 OutputStream (java.io.OutputStream)811