CAMEL-6661 Supporting the href is empty in camel-xslt component with thanks to Rene
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1c060896 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1c060896 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1c060896 Branch: refs/heads/camel-2.11.x Commit: 1c060896dea2f5c668704df881a988ab69ee8dc3 Parents: cf2c97b Author: Willem Jiang <willem.ji...@gmail.com> Authored: Tue Dec 31 15:44:51 2013 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Tue Dec 31 15:53:06 2013 +0800 ---------------------------------------------------------------------- .../camel/builder/xml/XsltUriResolver.java | 4 ++ .../xslt/SaxonXslIncludetEmptyHrefTest.java | 49 ++++++++++++++++++++ .../component/xslt/transform_includes_data.xsl | 29 ++++++++++++ 3 files changed, 82 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1c060896/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java index 3927205..dfbcc63 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java +++ b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java @@ -62,6 +62,10 @@ public class XsltUriResolver implements URIResolver { } public Source resolve(String href, String base) throws TransformerException { + // supports the empty href + if (ObjectHelper.isEmpty(href)) { + href = location; + } if (ObjectHelper.isEmpty(href)) { throw new TransformerException("include href is empty"); } http://git-wip-us.apache.org/repos/asf/camel/blob/1c060896/components/camel-saxon/src/test/java/org/apache/camel/component/xslt/SaxonXslIncludetEmptyHrefTest.java ---------------------------------------------------------------------- diff --git a/components/camel-saxon/src/test/java/org/apache/camel/component/xslt/SaxonXslIncludetEmptyHrefTest.java b/components/camel-saxon/src/test/java/org/apache/camel/component/xslt/SaxonXslIncludetEmptyHrefTest.java new file mode 100644 index 0000000..ad7e14f --- /dev/null +++ b/components/camel-saxon/src/test/java/org/apache/camel/component/xslt/SaxonXslIncludetEmptyHrefTest.java @@ -0,0 +1,49 @@ +/** + * 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.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class SaxonXslIncludetEmptyHrefTest extends CamelTestSupport { + + @Test + public void testXsltOutput() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + + mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><MyDate>February</MyDate>"); + mock.message(0).body().isInstanceOf(String.class); + + template.sendBody("direct:start", "<root>1</root>"); + + 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/transform_includes_data.xsl") + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1c060896/components/camel-saxon/src/test/resources/org/apache/camel/component/xslt/transform_includes_data.xsl ---------------------------------------------------------------------- diff --git a/components/camel-saxon/src/test/resources/org/apache/camel/component/xslt/transform_includes_data.xsl b/components/camel-saxon/src/test/resources/org/apache/camel/component/xslt/transform_includes_data.xsl new file mode 100644 index 0000000..610ab3a --- /dev/null +++ b/components/camel-saxon/src/test/resources/org/apache/camel/component/xslt/transform_includes_data.xsl @@ -0,0 +1,29 @@ +<?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:date="http://exslt.org/dates-and-times" version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> + <date:months> + <date:month length="31" abbr="Jan">January</date:month> + <date:month length="28" abbr="Feb">February</date:month> + </date:months> + <xsl:template match="root"> + <xsl:variable name="month-node" select="document('')/*/date:months/date:month[number(2)]"/> + <xsl:element name="MyDate"> + <xsl:value-of select="$month-node"/> + </xsl:element> + </xsl:template> +</xsl:stylesheet>