Author: davsclaus
Date: Sun Mar 14 16:14:18 2010
New Revision: 922885

URL: http://svn.apache.org/viewvc?rev=922885&view=rev
Log:
CAMEL-2546: Camel OGNL added special last keyword to get last from List.

Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.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/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=922885&r1=922884&r2=922885&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
 Sun Mar 14 16:14:18 2010
@@ -219,19 +219,40 @@ public class BeanExpression implements E
         }
 
         private Object lookupResult(Exchange exchange, String key, Object 
result, boolean elvis, String ognlPath, Object bean) {
+            // trim key
+            key = key.trim();
+
             // try map first
             Map map = 
exchange.getContext().getTypeConverter().convertTo(Map.class, result);
             if (map != null) {
                 return map.get(key);
             }
 
+            // special for list is last keyword
             Integer num = 
exchange.getContext().getTypeConverter().convertTo(Integer.class, key);
-            if (num != null) {
+            boolean checkList = key.startsWith("last") || num != null;
+
+            if (checkList) {
                 List list = 
exchange.getContext().getTypeConverter().convertTo(List.class, result);
                 if (list != null) {
-                    if (list.size() > num - 1) {
+                    if (key.startsWith("last")) {
+                        num = list.size() - 1;
+
+                        // maybe its an expression to subtract a number after 
last
+                        String after = ObjectHelper.after(key, "-");
+                        if (after != null) {
+                            Integer redux = 
exchange.getContext().getTypeConverter().convertTo(Integer.class, after.trim());
+                            if (redux != null) {
+                                num -= redux;
+                            } else {
+                                throw new 
ExpressionIllegalSyntaxException(key);
+                            }
+                        }
+                    }
+                    if (num != null && num >= 0 && list.size() > num - 1) {
                         return list.get(num);
-                    } else if (!elvis) {
+                    }
+                    if (!elvis) {
                         // not elvis then its mandatory so thrown out of 
bounds exception
                         throw new IndexOutOfBoundsException("Index: " + num + 
", Size: " + list.size()
                                 + " out of bounds with List from bean: " + 
bean + "using OGNL path [" + ognlPath + "]");

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=922885&r1=922884&r2=922885&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 
Sun Mar 14 16:14:18 2010
@@ -329,6 +329,12 @@ public class SimpleTest extends Language
 
         assertExpression("${in.body.getLines[1].getId}", 456);
         assertExpression("${in.body.getLines[1].getName}", "ActiveMQ in 
Action");
+
+        assertExpression("${in.body.getLines[last].getId}", 456);
+        assertExpression("${in.body.getLines[last].getName}", "ActiveMQ in 
Action");
+
+        assertExpression("${in.body.getLines[last-1].getId}", 123);
+        assertExpression("${in.body.getLines[last-1].getName}", "Camel in 
Action");
     }
 
     public void testBodyOGNLOrderListShorthand() throws Exception {
@@ -344,6 +350,12 @@ public class SimpleTest extends Language
 
         assertExpression("${in.body.lines[1].id}", 456);
         assertExpression("${in.body.lines[1].name}", "ActiveMQ in Action");
+
+        assertExpression("${in.body.lines[last].id}", 456);
+        assertExpression("${in.body.lines[last].name}", "ActiveMQ in Action");
+
+        assertExpression("${in.body.lines[last-1].id}", 123);
+        assertExpression("${in.body.lines[last-1].name}", "Camel in Action");
     }
 
     public void testBodyOGNLList() throws Exception {
@@ -415,9 +427,24 @@ public class SimpleTest extends Language
             fail("Should have thrown an exception");
         } catch (RuntimeBeanExpressionException e) {
             IndexOutOfBoundsException cause = 
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
-
             assertTrue(cause.getMessage().startsWith("Index: 3, Size: 2 out of 
bounds with List from bean"));
         }
+
+        try {
+            assertExpression("${in.body.getLines[last-2].getId}", 123);
+            fail("Should have thrown an exception");
+        } catch (RuntimeBeanExpressionException e) {
+            IndexOutOfBoundsException cause = 
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
+            assertTrue(cause.getMessage().startsWith("Index: -1, Size: 2 out 
of bounds with List from bean"));
+        }
+
+        try {
+            assertExpression("${in.body.getLines[last - XXX].getId}", 123);
+            fail("Should have thrown an exception");
+        } catch (RuntimeBeanExpressionException e) {
+            ExpressionIllegalSyntaxException cause = 
assertIsInstanceOf(ExpressionIllegalSyntaxException.class, e.getCause());
+            assertEquals("last - XXX", cause.getExpression());
+        }
     }
 
     public void testBodyOGNLOrderListOutOfBoundsShorthand() throws Exception {
@@ -433,9 +460,24 @@ public class SimpleTest extends Language
             fail("Should have thrown an exception");
         } catch (RuntimeBeanExpressionException e) {
             IndexOutOfBoundsException cause = 
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
-
             assertTrue(cause.getMessage().startsWith("Index: 3, Size: 2 out of 
bounds with List from bean"));
         }
+
+        try {
+            assertExpression("${in.body.lines[last - 2].id}", 123);
+            fail("Should have thrown an exception");
+        } catch (RuntimeBeanExpressionException e) {
+            IndexOutOfBoundsException cause = 
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
+            assertTrue(cause.getMessage().startsWith("Index: -1, Size: 2 out 
of bounds with List from bean"));
+        }
+
+        try {
+            assertExpression("${in.body.lines[last - XXX].id}", 123);
+            fail("Should have thrown an exception");
+        } catch (RuntimeBeanExpressionException e) {
+            ExpressionIllegalSyntaxException cause = 
assertIsInstanceOf(ExpressionIllegalSyntaxException.class, e.getCause());
+            assertEquals("last - XXX", cause.getExpression());
+        }
     }
 
     public void testBodyOGNLOrderListOutOfBoundsWithElvis() throws Exception {


Reply via email to