You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/05/06 10:25:10 UTC

svn commit: r941608 - /camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java

Author: davsclaus
Date: Thu May  6 08:25:10 2010
New Revision: 941608

URL: http://svn.apache.org/viewvc?rev=941608&view=rev
Log:
Optimize XPath to prefer InputSource for well know types such as file, stream or String

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java?rev=941608&r1=941607&r2=941608&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java Thu May  6 08:25:10 2010
@@ -685,35 +685,49 @@ public class XPathBuilder implements Exp
     }
 
     /**
-     * Strategy method to extract the document from the exchange
+     * Strategy method to extract the document from the exchange.
+     * <p/>
+     * Prefer to return types of {@link InputSource} or {@link DOMSource} which is what the XPath
+     * library works best with. Otherwise you can return types such as {@link Document} or any other kinds.
+     * <p/>
+     * Will try to convert the message body to the same type as of {@link #getDocumentType()}.
      */
     @SuppressWarnings("unchecked")
     protected Object getDocument(Exchange exchange) {
+        Object answer = null;
         Message in = exchange.getIn();
+
+        // prefer to use InputSource for well known types
+        if (in.getBody() instanceof GenericFile) {
+            // special for files so we can work with them out of the box
+            InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, in.getBody());
+            answer = new InputSource(is);
+        } else if (in.getBody() instanceof InputStream) {
+            // special for streams so we can work with them out of the box
+            answer = new InputSource(in.getBody(InputStream.class));
+        } else if (in.getBody() instanceof String) {
+            // special for String so we can work with them out of the box
+            answer = new InputSource(new StringReader(in.getBody(String.class)));
+        }
+
+        // try to type convert to the desired type
         Class type = getDocumentType();
-        Object answer = null;
-        if (type != null) {
+        if (answer == null && type != null) {
             answer = in.getBody(type);
         }
-
+        // fallback to get the body as is
         if (answer == null) {
             answer = in.getBody();
         }
 
-        // lets try coerce some common types into something JAXP can deal with
-        if (answer instanceof GenericFile) {
-            // special for files so we can work with them out of the box
-            InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, answer);
-            answer = new InputSource(is);
-        } else if (answer instanceof BeanInvocation) {
+        // special for bean invocation
+        if (answer instanceof BeanInvocation) {
             // if its a null bean invocation then handle that
             BeanInvocation bi = exchange.getContext().getTypeConverter().convertTo(BeanInvocation.class, answer);
             if (bi.getArgs() != null && bi.getArgs().length == 1 && bi.getArgs()[0] == null) {
                 // its a null argument from the bean invocation so use null as answer
                 answer = null;
             }
-        } else if (answer instanceof String) {
-            answer = new InputSource(new StringReader(answer.toString()));
         }
 
         // call the reset if the in message body is StreamCache