camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1145881 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/language/bean/ main/java/org/apache/camel/util/ test/java/org/apache/camel/language/
Date Wed, 13 Jul 2011 06:40:03 GMT
Author: davsclaus
Date: Wed Jul 13 06:40:03 2011
New Revision: 1145881

URL: http://svn.apache.org/viewvc?rev=1145881&view=rev
Log:
CAMEL-4218: Simple languge now supports using chained [] index for list/map.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/OgnlHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=1145881&r1=1145880&r2=1145881&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Wed
Jul 13 06:40:03 2011
@@ -1562,13 +1562,7 @@ public final class ExpressionBuilder {
 
             // Split ognl except when this is not a Map, Array
             // and we would like to keep the dots within the key name
-            List<String> methods;
-            if (ognl.startsWith("[") && ognl.endsWith("]")) {
-                methods = new ArrayList<String>();
-                methods.add(ognl);
-            } else {
-                methods = OgnlHelper.splitOgnl(ognl);
-            }
+            List<String> methods = OgnlHelper.splitOgnl(ognl);
 
             // remove any OGNL operators so we got the pure key name
             String key = OgnlHelper.removeOperators(methods.get(0));

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java?rev=1145881&r1=1145880&r2=1145881&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
Wed Jul 13 06:40:03 2011
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.language.bean;
 
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -184,13 +183,7 @@ public class BeanExpression implements E
 
             // Split ognl except when this is not a Map, Array
             // and we would like to keep the dots within the key name
-            List<String> methods;
-            if (ognl.startsWith("[") && ognl.endsWith("]")) {
-                methods = new ArrayList<String>();
-                methods.add(ognl);
-            } else {
-                methods = OgnlHelper.splitOgnl(ognl);
-            }
+            List<String> methods = OgnlHelper.splitOgnl(ognl);
 
             for (String methodName : methods) {
                 BeanHolder holder = new ConstantBeanHolder(beanToCall, exchange.getContext());

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/OgnlHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/OgnlHelper.java?rev=1145881&r1=1145880&r2=1145881&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/OgnlHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/OgnlHelper.java Wed Jul 13
06:40:03 2011
@@ -166,19 +166,29 @@ public final class OgnlHelper {
      * So use a bit ugly/low-level java code to split the ognl into methods.
      */
     public static List<String> splitOgnl(String ognl) {
-        // TODO: if possible use reg exp to split instead
-
         List<String> methods = new ArrayList<String>();
 
         StringBuilder sb = new StringBuilder();
+
+        int j = 0; // j is used as counter per method
+        boolean squareBracket = false; // special to keep track if we are inside a square
bracket block - (eg [foo])
         for (int i = 0; i < ognl.length(); i++) {
             char ch = ognl.charAt(i);
-            // special for starting
-            if (i == 0 || (i == 1 && ognl.charAt(0) == '?')
-                    || (ch != '.' && ch != '?')) {
+            // special for starting a new method
+            if (j == 0 || (j == 1 && ognl.charAt(i - 1) == '?')
+                    || (ch != '.' && ch != '?' && ch != ']')) {
                 sb.append(ch);
+                // special if we are doing square bracket
+                if (ch == '[') {
+                    squareBracket = true;
+                }
+                j++; // advance
             } else {
-                if (ch == '.') {
+                if (ch == '.' && !squareBracket) {
+                    // only treat dot as a method separator if not inside a square bracket
block
+                    // as dots can be used in key names when accessing maps
+
+                    // a dit denotes end of this method and a new method is to be invoked
                     String s = sb.toString();
 
                     // reset sb
@@ -192,12 +202,40 @@ public final class OgnlHelper {
 
                     // add the method
                     methods.add(s);
+
+                    // reset j to begin a new method
+                    j = 0;
+                } else if (ch == ']') {
+                    // append ending ] to method name
+                    sb.append(ch);
+                    String s = sb.toString();
+
+                    // reset sb
+                    sb.setLength(0);
+
+                    // add the method
+                    methods.add(s);
+
+                    // reset j to begin a new method
+                    j = 0;
+
+                    // no more square bracket
+                    squareBracket = false;
+                }
+
+                // and dont lose the char if its not an ] end marker (as we already added
that)
+                if (ch != ']') {
+                    sb.append(ch);
+                }
+
+                // only advance if already begun on the new method
+                if (j > 0) {
+                    j++;
                 }
-                // and dont lose the char
-                sb.append(ch);
             }
         }
-        // add remainder in buffer
+
+        // add remainder in buffer when reached end of data
         if (sb.length() > 0) {
             methods.add(sb.toString());
         }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java?rev=1145881&r1=1145880&r2=1145881&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java Wed Jul
13 06:40:03 2011
@@ -134,6 +134,43 @@ public class SimpleTest extends Language
         }
     }
     
+    public void testOGNLBodyListAndMap() throws Exception {
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("cool", "Camel rocks");
+        map.put("dude", "Hey dude");
+        map.put("code", 4321);
+
+        List<Map> lines = new ArrayList<Map>();
+        lines.add(map);
+
+        exchange.getIn().setBody(lines);
+
+        assertExpression("${in.body[0][cool]}", "Camel rocks");
+        assertExpression("${body[0][cool]}", "Camel rocks");
+        assertExpression("${in.body[0][code]}", 4321);
+        assertExpression("${body[0][code]}", 4321);
+    }
+
+    public void testOGNLBodyListAndMapAndMethod() throws Exception {
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("camel", new OrderLine(123, "Camel in Action"));
+        map.put("amq", new OrderLine(456, "ActiveMQ in Action"));
+
+        List<Map> lines = new ArrayList<Map>();
+        lines.add(map);
+
+        exchange.getIn().setBody(lines);
+
+        assertExpression("${in.body[0][camel].id}", 123);
+        assertExpression("${in.body[0][camel].name}", "Camel in Action");
+        assertExpression("${in.body[0][camel].getId}", 123);
+        assertExpression("${in.body[0][camel].getName}", "Camel in Action");
+        assertExpression("${body[0][camel].id}", 123);
+        assertExpression("${body[0][camel].name}", "Camel in Action");
+        assertExpression("${body[0][camel].getId}", 123);
+        assertExpression("${body[0][camel].getName}", "Camel in Action");
+    }
+
     public void testOGNLPropertyList() throws Exception {
         List<String> lines = new ArrayList<String>();
         lines.add("Camel in Action");



Mime
View raw message