camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1188647 - in /camel/branches/camel-2.8.x: ./ camel-core/src/main/java/org/apache/camel/builder/xml/ components/camel-spring/src/test/java/org/apache/camel/component/xslt/ components/camel-spring/src/test/resources/org/apache/camel/componen...
Date Tue, 25 Oct 2011 13:23:23 GMT
Author: davsclaus
Date: Tue Oct 25 13:23:22 2011
New Revision: 1188647

URL: http://svn.apache.org/viewvc?rev=1188647&view=rev
Log:
CAMEL-4579: Added option allowStAX to XSLT component.

Added:
    camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.java
      - copied, changed from r1188642, camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteTest.java
    camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.xml
      - copied, changed from r1188642, camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml
Modified:
    camel/branches/camel-2.8.x/   (props changed)
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Oct 25 13:23:22 2011
@@ -1 +1 @@
-/camel/trunk:1186106,1186625,1186772,1187221,1187485,1187882,1187893,1188070-1188085
+/camel/trunk:1186106,1186625,1186772,1187221,1187485,1187882,1187893,1188070-1188085,1188642

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java?rev=1188647&r1=1188646&r2=1188647&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
(original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltBuilder.java
Tue Oct 25 13:23:22 2011
@@ -32,6 +32,8 @@ import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamSource;
 
@@ -46,6 +48,8 @@ import org.apache.camel.impl.Synchroniza
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.apache.camel.util.ObjectHelper.notNull;
 
@@ -59,6 +63,7 @@ import static org.apache.camel.util.Obje
  * @version 
  */
 public class XsltBuilder implements Processor {
+    private final static Logger LOG = LoggerFactory.getLogger(XsltBuilder.class);
     private Map<String, Object> parameters = new HashMap<String, Object>();
     private XmlConverter converter = new XmlConverter();
     private Templates template;
@@ -67,6 +72,7 @@ public class XsltBuilder implements Proc
     private URIResolver uriResolver;
     private boolean deleteOutputFile;
     private ErrorListener errorListener = new XsltErrorListener();
+    private boolean allowStAX;
 
     public XsltBuilder() {
     }
@@ -104,7 +110,9 @@ public class XsltBuilder implements Proc
         try {
             is = exchange.getIn().getBody(InputStream.class);
             Source source = getSource(exchange, is);
+            LOG.trace("Using {} as source", source);
             transformer.transform(source, result);
+            LOG.trace("Transform complete with result {}", result);
             resultHandler.setBody(out);
         } finally {
             IOHelper.close(is);
@@ -211,6 +219,16 @@ public class XsltBuilder implements Proc
         return this;
     }
 
+    /**
+     * Enables to allow using StAX.
+     * <p/>
+     * When enabled StAX is preferred as the first choice as {@link Source}.
+     */
+    public XsltBuilder allowStAX() {
+        setAllowStAX(true);
+        return this;
+    }
+
     // Properties
     // -------------------------------------------------------------------------
 
@@ -246,6 +264,14 @@ public class XsltBuilder implements Proc
         this.resultHandlerFactory = resultHandlerFactory;
     }
 
+    public boolean isAllowStAX() {
+        return allowStAX;
+    }
+
+    public void setAllowStAX(boolean allowStAX) {
+        this.allowStAX = allowStAX;
+    }
+
     /**
      * Sets the XSLT transformer from a Source
      *
@@ -333,16 +359,32 @@ public class XsltBuilder implements Proc
     /**
      * Converts the inbound stream to a {@link Source}.
      * <p/>
-     * This implementation will prefer StAX first, and fallback to other kinds of Source
types.
+     * This implementation will prefer to source in the following order:
+     * <ul>
+     *   <li>StAX - Is StAX is allowed</li>
+     *   <li>SAX - SAX as 2nd choice</li>
+     *   <li>Stream - Stream as 3rd choice</li>
+     *   <li>DOM - DOM as 4th choice</li>
+     * </ul>
      */
     protected Source getSource(Exchange exchange, InputStream is) {
-        // try StAX first
-        Source source = exchange.getContext().getTypeConverter().convertTo(StAXSource.class,
exchange, is);
-        if (source == null) {
-            // fallback and try other kind of source
+        Source source = null;
+        if (isAllowStAX()) {
             source = exchange.getContext().getTypeConverter().convertTo(StAXSource.class,
exchange, is);
         }
         if (source == null) {
+            // then try SAX
+            source = exchange.getContext().getTypeConverter().convertTo(SAXSource.class,
exchange, is);
+        }
+        if (source == null) {
+            // then try stream
+            source = exchange.getContext().getTypeConverter().convertTo(StreamSource.class,
exchange, is);
+        }
+        if (source == null) {
+            // and fallback to DOM
+            source = exchange.getContext().getTypeConverter().convertTo(DOMSource.class,
exchange, is);
+        }
+        if (source == null) {
             if (isFailOnNullBody()) {
                 throw new ExpectedBodyTypeException(exchange, Source.class);
             } else {
@@ -383,6 +425,7 @@ public class XsltBuilder implements Proc
             String key = entry.getKey();
             Object value = entry.getValue();
             if (value != null) {
+                LOG.trace("Transformer set parameter {} -> {}", key, value);
                 transformer.setParameter(key, value);
             }
         }

Copied: camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.java
(from r1188642, camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.java?p2=camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.java&p1=camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteTest.java&r1=1188642&r2=1188647&rev=1188647&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteTest.java
(original)
+++ camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.java
Tue Oct 25 13:23:22 2011
@@ -27,7 +27,7 @@ import org.springframework.context.suppo
 /**
  * @version 
  */
-public class XsltRouteTest extends SpringTestSupport {
+public class XsltRouteAllowStAXTest extends SpringTestSupport {
 
     public void testSendMessageAndHaveItTransformed() throws Exception {
         MockEndpoint endpoint = getMockEndpoint("mock:result");
@@ -58,6 +58,6 @@ public class XsltRouteTest extends Sprin
     }
 
     protected AbstractXmlApplicationContext createApplicationContext() {
-        return new ClassPathXmlApplicationContext("org/apache/camel/component/xslt/camelContext.xml");
+        return new ClassPathXmlApplicationContext("org/apache/camel/component/xslt/XsltRouteAllowStAXTest.xml");
     }
 }

Copied: camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.xml
(from r1188642, camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml)
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.xml?p2=camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.xml&p1=camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml&r1=1188642&r2=1188647&rev=1188647&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/camelContext.xml
(original)
+++ camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/xslt/XsltRouteAllowStAXTest.xml
Tue Oct 25 13:23:22 2011
@@ -26,7 +26,7 @@
   <camelContext xmlns="http://camel.apache.org/schema/spring">
     <route>
       <from uri="direct:start"/>
-      <to uri="xslt:org/apache/camel/component/xslt/transform.xsl"/>
+      <to uri="xslt:org/apache/camel/component/xslt/transform.xsl?allowStAX=true"/>
       <multicast>
         <bean ref="testBean"/>
         <to uri="mock:result"/>



Mime
View raw message