camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r908395 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/xml/XPathBuilder.java test/java/org/apache/camel/builder/xml/XPathTest.java
Date Wed, 10 Feb 2010 08:55:46 GMT
Author: davsclaus
Date: Wed Feb 10 08:55:45 2010
New Revision: 908395

URL: http://svn.apache.org/viewvc?rev=908395&view=rev
Log:
CAMEL-2126: XPathBuilder can now be used without using an Exchange so you can use it in a
more generic fashion. However it requires CamelContext to be passed in as parameter.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.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=908395&r1=908394&r2=908395&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 Wed
Feb 10 08:55:45 2010
@@ -22,7 +22,6 @@
 import java.util.Map;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
-
 import javax.xml.namespace.QName;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.xpath.XPath;
@@ -35,12 +34,7 @@
 import javax.xml.xpath.XPathFunctionException;
 import javax.xml.xpath.XPathFunctionResolver;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-import org.xml.sax.InputSource;
-
+import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Message;
@@ -49,12 +43,17 @@
 import org.apache.camel.Service;
 import org.apache.camel.component.bean.BeanInvocation;
 import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.impl.DefaultExchange;
 import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.MessageHelper;
-
+import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
 
 import static org.apache.camel.builder.xml.Namespaces.DEFAULT_NAMESPACE;
 import static org.apache.camel.builder.xml.Namespaces.IN_NAMESPACE;
@@ -127,6 +126,49 @@
         return exchange.getContext().getTypeConverter().convertTo(type, result);
     }
 
+    /**
+     * Matches the given xpath using the provided body.
+     *
+     * @param context the camel context
+     * @param body the body
+     * @return <tt>true</tt> if matches, <tt>false</tt> otherwise
+     */
+    public boolean matches(CamelContext context, Object body) {
+        ObjectHelper.notNull(context, "CamelContext");
+
+        // create a dummy Exchange to use during matching
+        Exchange dummy = new DefaultExchange(context);
+        dummy.getIn().setBody(body);
+
+        boolean answer = matches(dummy);
+
+        // remove the dummy from the thread local after usage
+        exchange.remove();
+        return answer;
+    }
+
+    /**
+     * Evaluates the given xpath using the provided body.
+     *
+     * @param context the camel context
+     * @param body the body
+     * @param type the type to return
+     * @return result of the evaluation
+     */
+    public <T> T evaluate(CamelContext context, Object body, Class<T> type) {
+        ObjectHelper.notNull(context, "CamelContext");
+
+        // create a dummy Exchange to use during evaluation
+        Exchange dummy = new DefaultExchange(context);
+        dummy.getIn().setBody(body);
+
+        T answer = evaluate(dummy, type);
+
+        // remove the dummy from the thread local after usage
+        exchange.remove();
+        return answer;
+    }
+
     // Builder methods
     // -------------------------------------------------------------------------
 

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java?rev=908395&r1=908394&r2=908395&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java Wed Feb
10 08:55:45 2010
@@ -296,4 +296,21 @@
         exchange.getIn().setHeader("name", "James");
         return exchange;
     }
+
+    public void testXPathNotUsingExchangeMatches() throws Exception {
+        assertTrue(XPathBuilder.xpath("/foo/bar/@xyz").matches(context, "<foo><bar
xyz='cheese'/></foo>"));
+        assertFalse(XPathBuilder.xpath("/foo/bar/@xyz").matches(context, "<foo>Hello
World</foo>"));
+    }
+
+    public void testXPathNotUsingExchangeEvaluate() throws Exception {
+        String name = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>cheese</bar></foo>",
String.class);
+        assertEquals("cheese", name);
+
+        Integer number = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>123</bar></foo>",
Integer.class);
+        assertEquals(123, number.intValue());
+
+        Boolean bool = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>true</bar></foo>",
Boolean.class);
+        assertEquals(true, bool.booleanValue());
+    }
+
 }



Mime
View raw message