Author: ningjiang Date: Mon Jan 31 10:05:29 2011 New Revision: 1065540 URL: http://svn.apache.org/viewvc?rev=1065540&view=rev Log: CAMEL-3602 camel xslt component should set the ErrorHandler when it uses the Transformer to transform the message
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java (with props) camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/MyEcho.java (with props) camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java (with props) camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java Added: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java?rev=1065540&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java Mon Jan 31 10:05:29 2011 @@ -0,0 +1,60 @@ +/** + * 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 javax.xml.transform.ErrorListener; +import javax.xml.transform.TransformerException; + +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + + + +public class DefaultTransformErrorHandler implements ErrorHandler, ErrorListener { + private static final transient Log LOG = LogFactory.getLog(DefaultTransformErrorHandler.class); + + public void error(SAXParseException exception) throws SAXException { + throw exception; + } + + public void fatalError(SAXParseException exception) throws SAXException { + throw exception; + } + + public void warning(SAXParseException exception) throws SAXException { + LOG.warn("parser warning", exception); + + } + + public void error(TransformerException exception) throws TransformerException { + throw exception; + } + + public void fatalError(TransformerException exception) throws TransformerException { + throw exception; + } + + public void warning(TransformerException exception) throws TransformerException { + LOG.warn("parser warning", exception); + + } + +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/DefaultTransformErrorHandler.java ------------------------------------------------------------------------------ svn:keywords = Rev Date 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=1065540&r1=1065539&r2=1065540&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 Jan 31 10:05:29 2011 @@ -86,6 +86,7 @@ public class XsltBuilder implements Proc Transformer transformer = getTemplate().newTransformer(); configureTransformer(transformer, exchange); + transformer.setErrorListener(new DefaultTransformErrorHandler()); Source source = getSource(exchange); ResultHandler resultHandler = resultHandlerFactory.createResult(exchange); Result result = resultHandler.getResult(); Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/MyEcho.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/MyEcho.java?rev=1065540&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/MyEcho.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/MyEcho.java Mon Jan 31 10:05:29 2011 @@ -0,0 +1,31 @@ +/** + * 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; + +public final class MyEcho { + private MyEcho() { + //Helper class + } + public static String echoString(String str) { + // throw a runtime exception + if (str == null || str.length() == 0) { + throw new IllegalArgumentException("Cannot take the null String."); + } + return str; + } + +} Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/MyEcho.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/MyEcho.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java?rev=1065540&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java Mon Jan 31 10:05:29 2011 @@ -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 org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +public class XsltTransformingExceptionTest extends ContextTestSupport { + private static final String GOOD_XML_STRING = "<name>Camel</name>"; + private static final String BAD_XML_STRING = "<staff><programmer></programmer></staff>"; + + public void testXsltException() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(0); + try { + template.sendBody("direct:start", BAD_XML_STRING); + fail("Except a camel Execution exception here"); + } catch (CamelExecutionException ex) { + assertTrue(ex.getCause() instanceof javax.xml.transform.TransformerException); + assertTrue(ex.getCause().getCause() instanceof IllegalArgumentException); + } + // we should not get any message from the result endpoint + assertMockEndpointsSatisfied(); + } + + public void testXsltWithoutException() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.message(0).body().contains("Camel"); + template.sendBody("direct:start", GOOD_XML_STRING); + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("xslt:org/apache/camel/component/xslt/transformCallEcho.xsl") + .to("mock:result"); + } + }; + } + +} Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl?rev=1065540&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl Mon Jan 31 10:05:29 2011 @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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 + xmlns:xsl='http://www.w3.org/1999/XSL/Transform' + version='1.0' + xmlns:echo="xalan://org.apache.camel.component.xslt.MyEcho" + exclude-result-prefixes="echo"> + + <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/> + + <xsl:template match="/"> + <transformed> + <cheese> + <xsl:value-of select="echo:echoString(string(name))"></xsl:value-of> + </cheese> + </transformed> + </xsl:template> + +</xsl:stylesheet> Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/transformCallEcho.xsl ------------------------------------------------------------------------------ svn:mime-type = text/xml