You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/10/16 11:54:19 UTC

svn commit: r585093 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/model/language/ camel-core/src/test/java/org/apache/camel/processor/ components/camel-saxon/src/test/java/org/a...

Author: jstrachan
Date: Tue Oct 16 02:54:06 2007
New Revision: 585093

URL: http://svn.apache.org/viewvc?rev=585093&view=rev
Log:
improved the DSL to allow easier XPath and XQuery expressions using explicit result types or namespaces or both for CAMEL-181

Added:
    activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryWithNamespacesFilterTest.java
      - copied, changed from r584725, activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java
    activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java?rev=585093&r1=585092&r2=585093&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java Tue Oct 16 02:54:06 2007
@@ -17,8 +17,13 @@
  */
 package org.apache.camel.builder;
 
+import java.util.Map;
+
+import org.apache.camel.builder.xml.Namespaces;
 import org.apache.camel.model.ExpressionNode;
 import org.apache.camel.model.language.ExpressionType;
+import org.apache.camel.model.language.XPathExpression;
+import org.apache.camel.model.language.XQueryExpression;
 
 /**
  * Represents an expression clause within the DSL which when the expression is complete
@@ -145,6 +150,79 @@
     }
 
     /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
+     * with the specified result type
+     *
+     * @param text the expression to be evaluated
+     * @param resultType the return type expected by the expressiopn
+     * @return the builder to continue processing the DSL
+     */
+    public T xpath(String text, Class resultType) {
+        XPathExpression expression = new XPathExpression(text);
+        expression.setResultType(resultType);
+        setExpressionType(expression);
+        return result;
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
+     * with the specified result type and set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param resultType the return type expected by the expression
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xpath(String text, Class resultType, Namespaces namespaces) {
+        return xpath(text, resultType, namespaces.getNamespaces());
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
+     * with the specified result type and set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param resultType the return type expected by the expression
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xpath(String text, Class resultType, Map<String,String> namespaces) {
+        XPathExpression expression = new XPathExpression(text);
+        expression.setResultType(resultType);
+        expression.setNamespaces(namespaces);
+        setExpressionType(expression);
+        return result;
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
+     * with the specified set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xpath(String text, Namespaces namespaces) {
+        return xpath(text, namespaces.getNamespaces());
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
+     * with the specified set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xpath(String text, Map<String,String> namespaces) {
+        XPathExpression expression = new XPathExpression(text);
+        expression.setNamespaces(namespaces);
+        setExpressionType(expression);
+        return result;
+    }
+
+
+    /**
      * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
      *
      * @param text the expression to be evaluated
@@ -152,6 +230,78 @@
      */
     public T xquery(String text) {
         return language("xquery", text);
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
+     * with the specified result type
+     *
+     * @param text the expression to be evaluated
+     * @param resultType the return type expected by the expressiopn
+     * @return the builder to continue processing the DSL
+     */
+    public T xquery(String text, Class resultType) {
+        XQueryExpression expression = new XQueryExpression(text);
+        expression.setResultType(resultType);
+        setExpressionType(expression);
+        return result;
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
+     * with the specified result type and set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param resultType the return type expected by the expression
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xquery(String text, Class resultType, Namespaces namespaces) {
+        return xquery(text, resultType, namespaces.getNamespaces());
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
+     * with the specified result type and set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param resultType the return type expected by the expression
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xquery(String text, Class resultType, Map<String,String> namespaces) {
+        XQueryExpression expression = new XQueryExpression(text);
+        expression.setResultType(resultType);
+        expression.setNamespaces(namespaces);
+        setExpressionType(expression);
+        return result;
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
+     * with the specified set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xquery(String text, Namespaces namespaces) {
+        return xquery(text, namespaces.getNamespaces());
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
+     * with the specified set of namespace prefixes and URIs
+     *
+     * @param text the expression to be evaluated
+     * @param namespaces the namespace prefix and URIs to use
+     * @return the builder to continue processing the DSL
+     */
+    public T xquery(String text, Map<String,String> namespaces) {
+        XQueryExpression expression = new XQueryExpression(text);
+        expression.setNamespaces(namespaces);
+        setExpressionType(expression);
+        return result;
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java?rev=585093&r1=585092&r2=585093&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java Tue Oct 16 02:54:06 2007
@@ -32,7 +32,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
-import org.apache.camel.impl.ExpressionSupport;
+import org.apache.camel.builder.ExpressionClause;
 import org.apache.camel.impl.RouteContext;
 import org.apache.camel.spi.Language;
 import org.apache.camel.util.CollectionStringBuffer;
@@ -57,6 +57,8 @@
     private Predicate predicate;
     @XmlTransient
     private Expression expressionValue;
+    @XmlTransient
+    private ExpressionType expressionType;
 
     public static String getLabel(List<ExpressionType> expressions) {
         CollectionStringBuffer buffer = new CollectionStringBuffer();
@@ -116,20 +118,30 @@
 
     public Predicate<Exchange> createPredicate(RouteContext routeContext) {
         if (predicate == null) {
-            CamelContext camelContext = routeContext.getCamelContext();
-            Language language = camelContext.resolveLanguage(getLanguage());
-            predicate = language.createPredicate(getExpression());
-            configurePredicate(routeContext, predicate);
+            if (expressionType != null) {
+                predicate = expressionType.createPredicate(routeContext);
+            }
+            else {
+                CamelContext camelContext = routeContext.getCamelContext();
+                Language language = camelContext.resolveLanguage(getLanguage());
+                predicate = language.createPredicate(getExpression());
+                configurePredicate(routeContext, predicate);
+            }
         }
         return predicate;
     }
 
     public Expression createExpression(RouteContext routeContext) {
         if (expressionValue == null) {
-            CamelContext camelContext = routeContext.getCamelContext();
-            Language language = camelContext.resolveLanguage(getLanguage());
-            expressionValue = language.createExpression(getExpression());
-            configureExpression(routeContext, expressionValue);
+            if (expressionType != null) {
+                expressionValue = expressionType.createExpression(routeContext);
+            }
+            else {
+                CamelContext camelContext = routeContext.getCamelContext();
+                Language language = camelContext.resolveLanguage(getLanguage());
+                expressionValue = language.createExpression(getExpression());
+                configureExpression(routeContext, expressionValue);
+            }
         }
         return expressionValue;
     }
@@ -191,13 +203,22 @@
         return "";
     }
 
+    /**
+     * Allows derived classes to set a lazily created expressionType instance
+     * such as if using the {@link ExpressionClause#
+     *
+     * @param expressionType
+     */
+    protected void setExpressionType(ExpressionType expressionType) {
+        this.expressionType = expressionType;
+    }
+
     protected void configurePredicate(RouteContext routeContext, Predicate predicate) {
     }
 
     protected void configureExpression(RouteContext routeContext, Expression expression) {
     }
 
-
     /**
      * Sets a named property on the object instance using introspection
      */
@@ -209,5 +230,4 @@
             throw new IllegalArgumentException("Failed to set property " + name + " on " + bean + ". Reason: " + e, e);
         }
     }
-
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java?rev=585093&r1=585092&r2=585093&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java Tue Oct 16 02:54:06 2007
@@ -20,10 +20,9 @@
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.xml.Namespaces;
 import org.apache.camel.component.mock.MockEndpoint;
 
-import static org.apache.camel.builder.xml.XPathBuilder.xpath;
-
 /**
  * @version $Revision: 1.1 $
  */
@@ -35,7 +34,7 @@
         resultEndpoint.expectedMessageCount(1);
 
         template.sendBody("direct:start",
-                          "<person xmlns='http://acme.com/cheese' name='James' city='London'/>");
+                "<person xmlns='http://acme.com/cheese' name='James' city='London'/>");
 
         resultEndpoint.assertIsSatisfied();
     }
@@ -44,7 +43,7 @@
         resultEndpoint.expectedMessageCount(0);
 
         template.sendBody("direct:start",
-                          "<person xmlns='http://acme.com/cheese'  name='Hiram' city='Tampa'/>");
+                "<person xmlns='http://acme.com/cheese'  name='Hiram' city='Tampa'/>");
 
         resultEndpoint.assertIsSatisfied();
     }
@@ -61,9 +60,11 @@
         return new RouteBuilder() {
             public void configure() {
                 // START SNIPPET: example
-                from("direct:start").filter(
-                                            xpath("/c:person[@name='James']")
-                                                .namespace("c", "http://acme.com/cheese")).to("mock:result");
+                Namespaces ns = new Namespaces("c", "http://acme.com/cheese");
+
+                from("direct:start").filter().
+                        xpath("/c:person[@name='James']", ns).
+                        to("mock:result");
                 // END SNIPPET: example
             }
         };

Modified: activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java?rev=585093&r1=585092&r2=585093&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java (original)
+++ activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java Tue Oct 16 02:54:06 2007
@@ -20,10 +20,9 @@
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.xml.Namespaces;
 import org.apache.camel.component.mock.MockEndpoint;
 
-import static org.apache.camel.builder.saxon.XQueryBuilder.xquery;
-
 /**
  * @version $Revision: 1.1 $
  */
@@ -34,7 +33,7 @@
     public void testSendMatchingMessage() throws Exception {
         resultEndpoint.expectedMessageCount(1);
 
-        template.sendBody("direct:start", "<person name='James' city='London'/>");
+        template.sendBody("direct:start", "<person xmlns='http://acme.com/cheese' name='James' city='London'/>");
 
         resultEndpoint.assertIsSatisfied();
     }
@@ -42,8 +41,7 @@
     public void testSendNotMatchingMessage() throws Exception {
         resultEndpoint.expectedMessageCount(0);
 
-        template.sendBody("direct:start", "<person name='Hiram' city='Tampa'/>");
-
+        template.sendBody("direct:start", "<person xmlns='http://acme.com/cheese'  name='Hiram' city='Tampa'/>");
 
         resultEndpoint.assertIsSatisfied();
     }
@@ -60,9 +58,11 @@
         return new RouteBuilder() {
             public void configure() {
                 // START SNIPPET: example
+                Namespaces ns = new Namespaces("c", "http://acme.com/cheese");
+
                 from("direct:start").
-                        filter().xquery("/person[@name='James']").
-                to("mock:result");
+                        filter().xquery("/c:person[@name='James']", ns).
+                        to("mock:result");
                 // END SNIPPET: example
             }
         };

Copied: activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryWithNamespacesFilterTest.java (from r584725, activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryWithNamespacesFilterTest.java?p2=activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryWithNamespacesFilterTest.java&p1=activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java&r1=584725&r2=585093&rev=585093&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java (original)
+++ activemq/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryWithNamespacesFilterTest.java Tue Oct 16 02:54:06 2007
@@ -27,7 +27,7 @@
 /**
  * @version $Revision: 1.1 $
  */
-public class XQueryFilterTest extends ContextTestSupport {
+public class XQueryWithNamespacesFilterTest extends ContextTestSupport {
     protected Endpoint<Exchange> startEndpoint;
     protected MockEndpoint resultEndpoint;
 
@@ -67,4 +67,4 @@
             }
         };
     }
-}
+}
\ No newline at end of file