camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: CAMEL-9897: Add an Option to the XSLT Component to support custom EntityResolver
Date Thu, 26 May 2016 17:10:27 GMT
Repository: camel
Updated Branches:
  refs/heads/master f15176341 -> cefe7f332


CAMEL-9897: Add an Option to the XSLT Component to support custom EntityResolver


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a38d0fff
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a38d0fff
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a38d0fff

Branch: refs/heads/master
Commit: a38d0fff3c80aea60272055092689aa108e245be
Parents: f151763
Author: Sami Nurminen <snurmine@gmail.com>
Authored: Thu May 26 18:13:07 2016 +0300
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu May 26 18:52:35 2016 +0200

----------------------------------------------------------------------
 .../apache/camel/builder/xml/XsltBuilder.java   | 16 +++-
 .../camel/component/xslt/XsltEndpoint.java      | 15 ++++
 camel-core/src/test/data/xml_with_entity.xml    |  3 +
 .../xslt/XsltCustomizeEntityResolverTest.java   | 78 ++++++++++++++++++++
 4 files changed, 111 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a38d0fff/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
index d5e73da..2770d10 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
@@ -42,6 +42,8 @@ import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Node;
 
+import org.xml.sax.EntityResolver;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.ExpectedBodyTypeException;
 import org.apache.camel.Message;
@@ -84,6 +86,7 @@ public class XsltBuilder implements Processor {
     private boolean deleteOutputFile;
     private ErrorListener errorListener;
     private boolean allowStAX = true;
+    private EntityResolver entityResolver;
 
     public XsltBuilder() {
     }
@@ -399,6 +402,10 @@ public class XsltBuilder implements Processor {
         this.uriResolver = uriResolver;
     }
 
+    public void setEntityResolver(EntityResolver entityResolver) {
+        this.entityResolver = entityResolver;
+    }
+
     public boolean isDeleteOutputFile() {
         return deleteOutputFile;
     }
@@ -497,6 +504,7 @@ public class XsltBuilder implements Processor {
             if (source == null) {
                 // then try SAX
                 source = exchange.getContext().getTypeConverter().tryConvertTo(SAXSource.class,
exchange, body);
+                tryAddEntityResolver((SAXSource)source);
             }
             if (source == null) {
                 // then try stream
@@ -528,7 +536,13 @@ public class XsltBuilder implements Processor {
         }
         return source;
     }
-   
+
+    private void tryAddEntityResolver(SAXSource source) {
+        //expecting source to have not null XMLReader
+        if (this.entityResolver != null && source != null) {
+            source.getXMLReader().setEntityResolver(this.entityResolver);
+        }
+    }
 
     /**
      * Configures the transformer with exchange specific parameters

http://git-wip-us.apache.org/repos/asf/camel/blob/a38d0fff/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
index 86e6ab5..c7ec0a2 100644
--- a/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
@@ -25,6 +25,7 @@ import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.URIResolver;
+import org.xml.sax.EntityResolver;
 
 import org.apache.camel.Component;
 import org.apache.camel.Exchange;
@@ -90,6 +91,8 @@ public class XsltEndpoint extends ProcessorEndpoint {
     private boolean allowStAX = true;
     @UriParam
     private boolean deleteOutputFile;
+    @UriParam(label = "advanced")
+    private EntityResolver entityResolver;
 
     @Deprecated
     public XsltEndpoint(String endpointUri, Component component, XsltBuilder xslt, String
resourceUri,
@@ -340,6 +343,17 @@ public class XsltEndpoint extends ProcessorEndpoint {
         this.deleteOutputFile = deleteOutputFile;
     }
 
+    public EntityResolver getEntityResolver() {
+        return entityResolver;
+    }
+
+    /**
+     * To use a custom org.xml.sax.EntityResolver with javax.xml.transform.sax.SAXSource.
+     */
+    public void setEntityResolver(EntityResolver entityResolver) {
+        this.entityResolver = entityResolver;
+    }
+
     public Map<String, Object> getParameters() {
         return parameters;
     }
@@ -417,6 +431,7 @@ public class XsltEndpoint extends ProcessorEndpoint {
         xslt.setFailOnNullBody(failOnNullBody);
         xslt.transformerCacheSize(transformerCacheSize);
         xslt.setUriResolver(uriResolver);
+        xslt.setEntityResolver(entityResolver);
         xslt.setAllowStAX(allowStAX);
         xslt.setDeleteOutputFile(deleteOutputFile);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a38d0fff/camel-core/src/test/data/xml_with_entity.xml
----------------------------------------------------------------------
diff --git a/camel-core/src/test/data/xml_with_entity.xml b/camel-core/src/test/data/xml_with_entity.xml
new file mode 100644
index 0000000..2e0fb45
--- /dev/null
+++ b/camel-core/src/test/data/xml_with_entity.xml
@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE A SYSTEM  "dtd.dtd">
+<A>1</A>

http://git-wip-us.apache.org/repos/asf/camel/blob/a38d0fff/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeEntityResolverTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeEntityResolverTest.java
b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeEntityResolverTest.java
new file mode 100644
index 0000000..91c4c3d
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeEntityResolverTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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.IOException;
+import java.io.StringReader;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ *
+ */
+public class XsltCustomizeEntityResolverTest extends ContextTestSupport {
+
+    private static final String EXPECTED_XML_CONSTANT = "<A>1</A>";
+
+    public void testXsltCustomURIResolverDirectInRouteUri() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:resultURIResolverDirect");
+        mock.expectedMessageCount(1);
+
+        mock.message(0).body().contains(EXPECTED_XML_CONSTANT);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:src/test/data/?fileName=xml_with_entity.xml&noop=true").
+                to("xslt:xslt/common/copy.xsl?allowStAX=false&output=string&entityResolver=#customEntityResolver").
+                    to("mock:resultURIResolverDirect");
+            }
+        };
+    }
+
+    private EntityResolver getCustomEntityResolver() {
+        return new EntityResolver() {
+            @Override
+            public InputSource resolveEntity(String publicId, String systemId) throws SAXException,
IOException {
+
+                System.out.println("publicId:" + publicId + ",systeMdi:" + systemId);
+                return new InputSource(new StringReader("<!ELEMENT A (#PCDATA)>"));
+
+            }
+        };
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+        EntityResolver customEntityResolver = getCustomEntityResolver();
+        registry.bind("customEntityResolver", customEntityResolver);
+        return registry;
+    }
+}


Mime
View raw message