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/07/20 09:35:59 UTC

svn commit: r965744 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/xml/ test/java/org/apache/camel/builder/xml/ test/resources/org/apache/camel/builder/xml/

Author: davsclaus
Date: Tue Jul 20 07:35:58 2010
New Revision: 965744

URL: http://svn.apache.org/viewvc?rev=965744&view=rev
Log:
CAMEL-2967: Added two new functions to xpath builder to use properties component and simple language.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java   (contents, props changed)
      - copied, changed from r965720, camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathHeaderTest.java
    camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/Namespaces.java
    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/Namespaces.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/Namespaces.java?rev=965744&r1=965743&r2=965744&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/Namespaces.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/Namespaces.java Tue Jul 20 07:35:58 2010
@@ -38,6 +38,7 @@ public class Namespaces {
     public static final String DEFAULT_NAMESPACE = "http://camel.apache.org/schema/spring";
     public static final String IN_NAMESPACE = "http://camel.apache.org/xml/in/";
     public static final String OUT_NAMESPACE = "http://camel.apache.org/xml/out/";
+    public static final String FUNCTION_NAMESPACE = "http://camel.apache.org/xml/function/";
     public static final String SYSTEM_PROPERTIES_NAMESPACE = "http://camel.apache.org/xml/variables/system-properties";
     public static final String ENVIRONMENT_VARIABLES = "http://camel.apache.org/xml/variables/environment-variables";
     public static final String EXCHANGE_PROPERTY = "http://camel.apache.org/xml/variables/exchange-property";

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=965744&r1=965743&r2=965744&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 Tue Jul 20 07:35:58 2010
@@ -50,6 +50,7 @@ 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.Language;
 import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.MessageHelper;
@@ -58,6 +59,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import static org.apache.camel.builder.xml.Namespaces.DEFAULT_NAMESPACE;
+import static org.apache.camel.builder.xml.Namespaces.FUNCTION_NAMESPACE;
 import static org.apache.camel.builder.xml.Namespaces.IN_NAMESPACE;
 import static org.apache.camel.builder.xml.Namespaces.OUT_NAMESPACE;
 import static org.apache.camel.builder.xml.Namespaces.isMatchingNamespaceOrEmptyNamespace;
@@ -98,6 +100,8 @@ public class XPathBuilder implements Exp
     private XPathFunction headerFunction;
     private XPathFunction outBodyFunction;
     private XPathFunction outHeaderFunction;
+    private XPathFunction propertiesFunction;
+    private XPathFunction simpleFunction;
 
     public XPathBuilder(String text) {
         this.text = text;
@@ -464,7 +468,8 @@ public class XPathBuilder implements Exp
                     if (exchange != null && !list.isEmpty()) {
                         Object value = list.get(0);
                         if (value != null) {
-                            return exchange.get().getIn().getHeader(value.toString());
+                            String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value);
+                            return exchange.get().getIn().getHeader(text);
                         }
                     }
                     return null;
@@ -503,7 +508,8 @@ public class XPathBuilder implements Exp
                     if (exchange.get() != null && !list.isEmpty()) {
                         Object value = list.get(0);
                         if (value != null) {
-                            return exchange.get().getOut().getHeader(value.toString());
+                            String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value);
+                            return exchange.get().getOut().getHeader(text);
                         }
                     }
                     return null;
@@ -517,6 +523,59 @@ public class XPathBuilder implements Exp
         this.outHeaderFunction = outHeaderFunction;
     }
 
+    public XPathFunction getPropertiesFunction() {
+        if (propertiesFunction == null) {
+            propertiesFunction = new XPathFunction() {
+                public Object evaluate(List list) throws XPathFunctionException {
+                    if (exchange != null && !list.isEmpty()) {
+                        Object value = list.get(0);
+                        if (value != null) {
+                            String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value);
+                            try {
+                                // use the property placeholder resolver to lookup the property for us
+                                Object answer = exchange.get().getContext().resolvePropertyPlaceholders("{{" + text + "}}");
+                                return answer;
+                            } catch (Exception e) {
+                                throw new XPathFunctionException(e);
+                            }
+                        }
+                    }
+                    return null;
+                }
+            };
+        }
+        return propertiesFunction;
+    }
+
+    public void setPropertiesFunction(XPathFunction propertiesFunction) {
+        this.propertiesFunction = propertiesFunction;
+    }
+
+    public XPathFunction getSimpleFunction() {
+        if (simpleFunction == null) {
+            simpleFunction = new XPathFunction() {
+                public Object evaluate(List list) throws XPathFunctionException {
+                    if (exchange != null && !list.isEmpty()) {
+                        Object value = list.get(0);
+                        if (value != null) {
+                            String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value);
+                            Language simple = exchange.get().getContext().resolveLanguage("simple");
+                            Expression exp = simple.createExpression(text);
+                            Object answer = exp.evaluate(exchange.get(), Object.class);
+                            return answer;
+                        }
+                    }
+                    return null;
+                }
+            };
+        }
+        return simpleFunction;
+    }
+
+    public void setSimpleFunction(XPathFunction simpleFunction) {
+        this.simpleFunction = simpleFunction;
+    }
+
     public Class<?> getResultType() {
         return resultType;
     }
@@ -640,6 +699,7 @@ public class XPathBuilder implements Exp
         setNamespaceIfNotPresent(context, "out", OUT_NAMESPACE);
         setNamespaceIfNotPresent(context, "env", Namespaces.ENVIRONMENT_VARIABLES);
         setNamespaceIfNotPresent(context, "system", Namespaces.SYSTEM_PROPERTIES_NAMESPACE);
+        setNamespaceIfNotPresent(context, "function", Namespaces.FUNCTION_NAMESPACE);
     }
 
     protected void setNamespaceIfNotPresent(DefaultNamespaceContext context, String prefix, String uri) {
@@ -678,6 +738,15 @@ public class XPathBuilder implements Exp
                             return getOutHeaderFunction();
                         }
                     }
+                    if (isMatchingNamespaceOrEmptyNamespace(qName.getNamespaceURI(), FUNCTION_NAMESPACE)) {
+                        String localPart = qName.getLocalPart();
+                        if (localPart.equals("properties") && argumentCount == 1) {
+                            return getPropertiesFunction();
+                        }
+                        if (localPart.equals("simple") && argumentCount == 1) {
+                            return getSimpleFunction();
+                        }
+                    }
                 }
                 return answer;
             }

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java (from r965720, camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathHeaderTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathHeaderTest.java&r1=965720&r2=965744&rev=965744&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathHeaderTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java Tue Jul 20 07:35:58 2010
@@ -19,13 +19,14 @@ package org.apache.camel.builder.xml;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.properties.PropertiesComponent;
 
 /**
  * XPath with and without header test.
  */
-public class XPathHeaderTest extends ContextTestSupport {
+public class XPathFunctionsTest extends ContextTestSupport {
 
-    public void testChoiceWithHeaderSelectCamel() throws Exception {
+    public void testChoiceWithHeaderAndPropertiesSelectCamel() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:camel");
         mock.expectedBodiesReceived("<name>King</name>");
         mock.expectedHeaderReceived("type", "Camel");
@@ -35,16 +36,16 @@ public class XPathHeaderTest extends Con
         mock.assertIsSatisfied();
     }
 
-    public void testChoiceWithNoHeaderSelectDonkey() throws Exception {
+    public void testChoiceWithNoHeaderAndPropertiesSelectDonkey() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:donkey");
-        mock.expectedBodiesReceived("<name>Kong</name>");
+        mock.expectedBodiesReceived("<name>Donkey Kong</name>");
 
-        template.sendBody("direct:in", "<name>Kong</name>");
+        template.sendBody("direct:in", "<name>Donkey Kong</name>");
 
         mock.assertIsSatisfied();
     }
 
-    public void testChoiceWithNoHeaderSelectOther() throws Exception {
+    public void testChoiceWithNoHeaderAndPropertiesSelectOther() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:other");
         mock.expectedBodiesReceived("<name>Other</name>");
 
@@ -56,19 +57,31 @@ public class XPathHeaderTest extends Con
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                // START SNIPPET: e1
+                // START SNIPPET: ex
+                // setup properties component
+                PropertiesComponent properties = new PropertiesComponent();
+                properties.setLocation("classpath:org/apache/camel/builder/xml/myprop.properties");
+                context.addComponent("properties", properties);
+
+                // myprop.properties contains the following properties
+                // foo=Camel
+                // bar=Kong
+
                 from("direct:in").choice()
-                    // using $headerName is special notation in Camel to get the header key
-                    .when().xpath("$type = 'Camel'")
+                    // $type is a variable for the header with key type
+                    // here we use the properties function to lookup foo from the properties files
+                    // which at runtime will be evaluted to 'Camel'
+                    .when().xpath("$type = function:properties('foo')")
                         .to("mock:camel")
-                    // here we test for the body name tag
-                    .when().xpath("//name = 'Kong'")
+                    // here we use the simple language to evaluate the expression
+                    // which at runtime will be evaluated to 'Donkey Kong'
+                    .when().xpath("//name = function:simple('Donkey ${properties:bar}')")
                         .to("mock:donkey")
                     .otherwise()
                         .to("mock:other")
                     .end();
-                // END SNIPPET: e1
+                // END SNIPPET: ex
             }
         };
     }
-}
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFunctionsTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties?rev=965744&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties (added)
+++ camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties Tue Jul 20 07:35:58 2010
@@ -0,0 +1,19 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+foo=Camel
+bar=Kong
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/camel-core/src/test/resources/org/apache/camel/builder/xml/myprop.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain