Author: davsclaus Date: Mon Mar 29 17:29:04 2010 New Revision: 928841 URL: http://svn.apache.org/viewvc?rev=928841&view=rev Log: CAMEL-2589: xslt component now supports output option. Added file based xslt output to stream directly to file to avoid using in memory type.
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandler.java (with props) camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandlerFactory.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltOutputFileTest.java - copied, changed from r928618, camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputBytesTest.java (with props) camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputDOMTest.java (with props) camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java (with props) camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputStringTest.java (with props) camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml (with props) camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml (with props) camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml (with props) camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputStringTest.xml - copied, changed from r928618, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DomResultHandlerFactory.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/ResultHandlerFactory.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandler.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandlerFactory.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandler.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandlerFactory.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Mon Mar 29 17:29:04 2010 @@ -119,6 +119,8 @@ public interface Exchange { String TIMER_PERIOD = "CamelTimerPeriod"; String TIMER_TIME = "CamelTimerTime"; + String XSLT_FILE_NAME = "CamelXsltFileName"; + /** * Returns the {...@link ExchangePattern} (MEP) of this exchange. * Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DomResultHandlerFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DomResultHandlerFactory.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DomResultHandlerFactory.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DomResultHandlerFactory.java Mon Mar 29 17:29:04 2010 @@ -16,6 +16,8 @@ */ package org.apache.camel.builder.xml; +import org.apache.camel.Exchange; + /** * Factory for {...@link DomResultHandler} * @@ -23,7 +25,7 @@ package org.apache.camel.builder.xml; */ public class DomResultHandlerFactory implements ResultHandlerFactory { - public ResultHandler createResult() { + public ResultHandler createResult(Exchange exchange) throws Exception { return new DomResultHandler(); } } Added: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandler.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandler.java?rev=928841&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandler.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandler.java Mon Mar 29 17:29:04 2010 @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.builder.xml; + +import java.io.File; +import javax.xml.transform.Result; +import javax.xml.transform.stream.StreamResult; + +import org.apache.camel.Message; + +/** + * Use file to handle the result of XSLT transformation. + * <p/> + * For example when transforming big files you can stream directly to a file output + * to avoid consuming to much memory. + * + * @version $Revision$ + */ +public class FileResultHandler implements ResultHandler { + + private final File file; + private final StreamResult result; + + public FileResultHandler(File file) { + this.file = file; + this.result = new StreamResult(file); + } + + public Result getResult() { + return result; + } + + public void setBody(Message in) { + in.setBody(file); + } + +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandler.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandlerFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandlerFactory.java?rev=928841&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandlerFactory.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandlerFactory.java Mon Mar 29 17:29:04 2010 @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.builder.xml; + +import java.io.File; + +import org.apache.camel.Exchange; +import org.apache.camel.util.ExchangeHelper; + +/** + * Factory for {...@link javax.xml.transform.stream.StreamResult} which is streamed to file. + * + * @version $Revision$ + */ +public class FileResultHandlerFactory implements ResultHandlerFactory { + + public ResultHandler createResult(Exchange exchange) throws Exception { + String fileName = ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME, String.class); + return new FileResultHandler(new File(fileName)); + } +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandlerFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/FileResultHandlerFactory.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/ResultHandlerFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/ResultHandlerFactory.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/ResultHandlerFactory.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/ResultHandlerFactory.java Mon Mar 29 17:29:04 2010 @@ -16,6 +16,8 @@ */ package org.apache.camel.builder.xml; +import org.apache.camel.Exchange; + /** * Factory for {...@link org.apache.camel.builder.xml.ResultHandler} * @@ -23,5 +25,11 @@ package org.apache.camel.builder.xml; */ public interface ResultHandlerFactory { - ResultHandler createResult(); + /** + * Creates the {...@link ResultHandler} to use + * + * @param exchange the current exchange + * @return the result handler + */ + ResultHandler createResult(Exchange exchange) throws Exception; } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandler.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandler.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandler.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandler.java Mon Mar 29 17:29:04 2010 @@ -29,8 +29,8 @@ import org.apache.camel.Message; * @version $Revision$ */ public class StreamResultHandler implements ResultHandler { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - StreamResult result = new StreamResult(buffer); + private ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + private StreamResult result = new StreamResult(buffer); public Result getResult() { return result; Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandlerFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandlerFactory.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandlerFactory.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StreamResultHandlerFactory.java Mon Mar 29 17:29:04 2010 @@ -16,6 +16,8 @@ */ package org.apache.camel.builder.xml; +import org.apache.camel.Exchange; + /** * Factory for {...@link StreamResultHandler} * @@ -23,7 +25,7 @@ package org.apache.camel.builder.xml; */ public class StreamResultHandlerFactory implements ResultHandlerFactory { - public ResultHandler createResult() { + public ResultHandler createResult(Exchange exchange) throws Exception { return new StreamResultHandler(); } } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandler.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandler.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandler.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandler.java Mon Mar 29 17:29:04 2010 @@ -29,8 +29,8 @@ import org.apache.camel.Message; * @version $Revision$ */ public class StringResultHandler implements ResultHandler { - StringWriter buffer = new StringWriter(); - StreamResult result = new StreamResult(buffer); + private StringWriter buffer = new StringWriter(); + private StreamResult result = new StreamResult(buffer); public Result getResult() { return result; Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandlerFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandlerFactory.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandlerFactory.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/StringResultHandlerFactory.java Mon Mar 29 17:29:04 2010 @@ -16,6 +16,8 @@ */ package org.apache.camel.builder.xml; +import org.apache.camel.Exchange; + /** * Factory for {...@link StringResultHandler} * @@ -23,7 +25,7 @@ package org.apache.camel.builder.xml; */ public class StringResultHandlerFactory implements ResultHandlerFactory { - public ResultHandler createResult() { + public ResultHandler createResult(Exchange exchange) throws Exception { return new StringResultHandler(); } } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java Mon Mar 29 17:29:04 2010 @@ -44,7 +44,10 @@ import static org.apache.camel.util.Obje /** * Creates a <a href="http://camel.apache.org/processor.html">Processor</a> - * which performs an XSLT transformation of the IN message body + * which performs an XSLT transformation of the IN message body. + * <p/> + * Will by defult output the result as a String. You can chose which kind of output + * you want using the <tt>outputXXX</tt> methods. * * @version $Revision$ */ @@ -74,7 +77,7 @@ public class XsltBuilder implements Proc Transformer transformer = getTemplate().newTransformer(); configureTransformer(transformer, exchange); Source source = getSource(exchange); - ResultHandler resultHandler = resultHandlerFactory.createResult(); + ResultHandler resultHandler = resultHandlerFactory.createResult(exchange); Result result = resultHandler.getResult(); // lets copy the headers before we invoke the transform in case they modify them @@ -153,6 +156,15 @@ public class XsltBuilder implements Proc return this; } + /** + * Sets the output as being a File where the filename + * must be provided in the {...@link Exchange#XSLT_FILE_NAME} header. + */ + public XsltBuilder outputFile() { + setResultHandlerFactory(new FileResultHandlerFactory()); + return this; + } + public XsltBuilder parameter(String name, Object value) { parameters.put(name, value); return this; Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Mon Mar 29 17:29:04 2010 @@ -202,9 +202,9 @@ public abstract class ProcessorDefinitio // set the error handler, must be done after init as we can set the error handler as first in the chain if (defn instanceof TryDefinition || defn instanceof CatchDefinition || defn instanceof FinallyDefinition) { // do not use error handler for try .. catch .. finally blocks as it will handle errors itself - } else if (ProcessorDefinitionHelper.isParentOfType(TryDefinition.class, defn, true) || - ProcessorDefinitionHelper.isParentOfType(CatchDefinition.class, defn, true) || - ProcessorDefinitionHelper.isParentOfType(FinallyDefinition.class, defn, true)) { + } else if (ProcessorDefinitionHelper.isParentOfType(TryDefinition.class, defn, true) + || ProcessorDefinitionHelper.isParentOfType(CatchDefinition.class, defn, true) + || ProcessorDefinitionHelper.isParentOfType(FinallyDefinition.class, defn, true)) { // do not use error handler for try .. catch .. finally blocks as it will handle errors itself // by checking that any of our parent(s) is not a try .. catch or finally type } else if (defn instanceof MulticastDefinition || defn instanceof RecipientListDefinition) { Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java Mon Mar 29 17:29:04 2010 @@ -191,6 +191,29 @@ public class XsltBuilderTest extends Con assertEquals("<goodbye>world!</goodbye>", exchange.getOut().getBody(String.class)); } + public void testXsltOutputFile() throws Exception { + // directory must exists + deleteDirectory("target/xslt"); + createDirectory("target/xslt"); + + 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").getAbsoluteFile(); + assertTrue("Output file should exist", file.exists()); + + String body = exchange.getOut().getBody(String.class); + assertTrue(body.endsWith("<goodbye>world!</goodbye>")); + } + public void testXsltSetConverter() throws Exception { URL styleSheet = getClass().getResource("example.xsl"); Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltOutputFileTest.java (from r928618, camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltOutputFileTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltOutputFileTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltTest.java&r1=928618&r2=928841&rev=928841&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XsltOutputFileTest.java Mon Mar 29 17:29:04 2010 @@ -16,9 +16,13 @@ */ package org.apache.camel.builder.xml; +import java.io.File; import java.net.URL; +import org.apache.camel.CamelExecutionException; import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.NoSuchHeaderException; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; @@ -27,15 +31,29 @@ import static org.apache.camel.builder.x /** * @version $Revision$ */ -public class XsltTest extends ContextTestSupport { +public class XsltOutputFileTest extends ContextTestSupport { - public void testXslt() throws Exception { - MockEndpoint resultEndpoint = getMockEndpoint("mock:result"); - resultEndpoint.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>"); + public void testXsltOutputFile() throws Exception { + createDirectory("target/xslt"); - sendBody("direct:start", "<hello>world!</hello>"); + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>"); + mock.expectedFileExists("target/xslt/xsltme.xml"); + mock.message(0).body().isInstanceOf(File.class); - resultEndpoint.assertIsSatisfied(); + template.sendBodyAndHeader("direct:start", "<hello>world!</hello>", Exchange.XSLT_FILE_NAME, "target/xslt/xsltme.xml"); + + mock.assertIsSatisfied(); + } + + public void testXsltOutputFileMissingHeader() throws Exception { + try { + template.sendBody("direct:start", "<hello>world!</hello>"); + fail("Should thrown exception"); + } catch (CamelExecutionException e) { + NoSuchHeaderException nshe = assertIsInstanceOf(NoSuchHeaderException.class, e.getCause()); + assertEquals(Exchange.XSLT_FILE_NAME, nshe.getHeaderName()); + } } @Override @@ -44,8 +62,9 @@ public class XsltTest extends ContextTes public void configure() throws Exception { URL styleSheet = getClass().getResource("example.xsl"); - from("direct:start").process(xslt(styleSheet)).to("mock:result"); + // output xslt as a file + from("direct:start").process(xslt(styleSheet).outputFile()).to("mock:result"); } }; } -} +} \ No newline at end of file Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java?rev=928841&r1=928840&r2=928841&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java (original) +++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java Mon Mar 29 17:29:04 2010 @@ -27,6 +27,7 @@ import org.apache.camel.builder.xml.Xslt import org.apache.camel.component.ResourceBasedComponent; import org.apache.camel.converter.jaxp.XmlConverter; import org.apache.camel.impl.ProcessorEndpoint; +import org.apache.camel.util.ObjectHelper; import org.springframework.core.io.Resource; /** @@ -113,6 +114,8 @@ public class XsltComponent extends Resou if (failOnNullBody != null) { xslt.setFailOnNullBody(failOnNullBody); } + String output = getAndRemoveParameter(parameters, "output", String.class); + configureOutput(xslt, output); xslt.setTransformerInputStream(resource.getInputStream()); configureXslt(xslt, uri, remaining, parameters); @@ -122,4 +125,23 @@ public class XsltComponent extends Resou protected void configureXslt(XsltBuilder xslt, String uri, String remaining, Map<String, Object> parameters) throws Exception { setProperties(xslt, parameters); } + + protected void configureOutput(XsltBuilder xslt, String output) throws Exception { + if (ObjectHelper.isEmpty(output)) { + return; + } + + if ("string".equalsIgnoreCase(output)) { + xslt.outputString(); + } else if ("bytes".equalsIgnoreCase(output)) { + xslt.outputBytes(); + } else if ("DOM".equalsIgnoreCase(output)) { + xslt.outputDOM(); + } else if ("file".equalsIgnoreCase(output)) { + xslt.outputFile(); + } else { + throw new IllegalArgumentException("Unknown output type: " + output); + } + } + } Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputBytesTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputBytesTest.java?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputBytesTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputBytesTest.java Mon Mar 29 17:29:04 2010 @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.xslt; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.component.mock.MockEndpoint; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +/** + * @version $Revision$ + */ +public class XsltOutputBytesTest extends ContextTestSupport { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/component/xslt/XsltOutputBytesTest.xml"); + } + + public void testXsltOutput() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>"); + mock.message(0).body().isInstanceOf(byte[].class); + + template.sendBody("direct:start", "<hello>world!</hello>"); + + assertMockEndpointsSatisfied(); + + + } + +} \ No newline at end of file Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputBytesTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputBytesTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputDOMTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputDOMTest.java?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputDOMTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputDOMTest.java Mon Mar 29 17:29:04 2010 @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.xslt; + +import org.w3c.dom.Document; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.component.mock.MockEndpoint; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +/** + * @version $Revision$ + */ +public class XsltOutputDOMTest extends ContextTestSupport { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/component/xslt/XsltOutputDOMTest.xml"); + } + + public void testXsltOutput() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("<goodbye>world!</goodbye>"); + mock.message(0).body().isInstanceOf(Document.class); + + template.sendBody("direct:start", "<hello>world!</hello>"); + + assertMockEndpointsSatisfied(); + + + } + +} \ No newline at end of file Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputDOMTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputDOMTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java Mon Mar 29 17:29:04 2010 @@ -0,0 +1,62 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.xslt; + +import java.io.File; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.NoSuchHeaderException; +import org.apache.camel.component.mock.MockEndpoint; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +/** + * @version $Revision$ + */ +public class XsltOutputFileTest extends ContextTestSupport { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/component/xslt/XsltOutputFileTest.xml"); + } + + public void testXsltOutput() throws Exception { + createDirectory("target/xslt"); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>"); + mock.expectedFileExists("target/xslt/xsltme.xml"); + mock.message(0).body().isInstanceOf(File.class); + + template.sendBodyAndHeader("direct:start", "<hello>world!</hello>", Exchange.XSLT_FILE_NAME, "target/xslt/xsltme.xml"); + + assertMockEndpointsSatisfied(); + } + + public void testXsltOutputFileMissingHeader() throws Exception { + try { + template.sendBody("direct:start", "<hello>world!</hello>"); + fail("Should thrown exception"); + } catch (CamelExecutionException e) { + NoSuchHeaderException nshe = assertIsInstanceOf(NoSuchHeaderException.class, e.getCause()); + assertEquals(Exchange.XSLT_FILE_NAME, nshe.getHeaderName()); + } + } + +} \ No newline at end of file Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputStringTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputStringTest.java?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputStringTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputStringTest.java Mon Mar 29 17:29:04 2010 @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.xslt; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.component.mock.MockEndpoint; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +/** + * @version $Revision$ + */ +public class XsltOutputStringTest extends ContextTestSupport { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/component/xslt/XsltOutputStringTest.xml"); + } + + public void testXsltOutput() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>"); + mock.message(0).body().isInstanceOf(String.class); + + template.sendBody("direct:start", "<hello>world!</hello>"); + + assertMockEndpointsSatisfied(); + + + } + +} Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputStringTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltOutputStringTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml Mon Mar 29 17:29:04 2010 @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <to uri="xslt:org/apache/camel/component/xslt/example.xsl?output=bytes"/> + <to uri="mock:result"/> + </route> + </camelContext> + +</beans> Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputBytesTest.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml Mon Mar 29 17:29:04 2010 @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <to uri="xslt:org/apache/camel/component/xslt/example.xsl?output=dom"/> + <to uri="mock:result"/> + </route> + </camelContext> + +</beans> Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputDOMTest.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml Mon Mar 29 17:29:04 2010 @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <to uri="xslt:org/apache/camel/component/xslt/example.xsl?output=file"/> + <to uri="mock:result"/> + </route> + </camelContext> + +</beans> Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputFileTest.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputStringTest.xml (from r928618, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputStringTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputStringTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml&r1=928618&r2=928841&rev=928841&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml (original) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltOutputStringTest.xml Mon Mar 29 17:29:04 2010 @@ -22,18 +22,12 @@ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> - <!-- START SNIPPET: example --> - <camelContext xmlns="http://camel.apache.org/schema/spring"> - <route> - <from uri="direct:start"/> - <to uri="xslt:org/apache/camel/component/xslt/transform.xsl"/> - <multicast> - <bean ref="testBean"/> - <to uri="mock:result"/> - </multicast> - </route> - </camelContext> - <!-- END SNIPPET: example --> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <to uri="xslt:org/apache/camel/component/xslt/example.xsl?output=string"/> + <to uri="mock:result"/> + </route> + </camelContext> - <bean id="testBean" class="org.apache.camel.component.xslt.TestBean" scope="singleton"/> </beans> Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl?rev=928841&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl Mon Mar 29 17:29:04 2010 @@ -0,0 +1,26 @@ +<?xml version = "1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> + + <xsl:template match="/"> + <goodbye> + <xsl:value-of select="/hello"/> + </goodbye> + </xsl:template> + +</xsl:stylesheet> Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/example.xsl ------------------------------------------------------------------------------ svn:mime-type = text/xml