velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r667320 - in /velocity/tools/trunk/src: main/java/org/apache/velocity/tools/generic/LoopTool.java test/java/org/apache/velocity/tools/LoopToolTests.java
Date Fri, 13 Jun 2008 03:14:03 GMT
Author: nbubna
Date: Thu Jun 12 20:14:03 2008
New Revision: 667320

URL: http://svn.apache.org/viewvc?rev=667320&view=rev
Log:
add short syntax for count/index/first/last properties of outer loops (e.g. $loop.count_foo
eq $loop.getCount('foo'))

Modified:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java
    velocity/tools/trunk/src/test/java/org/apache/velocity/tools/LoopToolTests.java

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java?rev=667320&r1=667319&r2=667320&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java Thu
Jun 12 20:14:03 2008
@@ -372,11 +372,21 @@
     }
 
     /**
-     * Searches all the loops being managed for one with a sync'ed
-     * Iterator under the specified name and returns the current value
-     * for that sync'ed iterator, if any.
+     * <p>This serves two purposes:
+     * <ul><li>Getting the current value of a sync'ed iterator</li>
+     * <li>Abbreviate syntax for properties of outer loops</li></ul></p>
+     * <p>First, it searches all the loops being managed for one
+     * with a sync'ed Iterator under the specified name and
+     * returns the current value for that sync'ed iterator,
+     * if any. If there is no sync'ed iterators or none with
+     * that name, then this will check if the specified key
+     * is requesting a "property" of an outer loop (e.g.
+     * {@code $loop.foo_count} or {@code $loop.foo_first}).
+     * This syntax is shorter and clearer than {@code $loop.getCount('foo')}.
+     * If the key starts with a property name and ends with an outer loop
+     * name, then the value of that property for that loop is returned.
      */
-    public Object get(String synced)
+    public Object get(String key)
     {
         // search all iterators in reverse
         // (so nested ones take priority)
@@ -384,11 +394,32 @@
         for (int i=iterators.size() - 1; i >= 0; i--)
         {
             ManagedIterator iterator = iterators.get(i);
-            if (iterator.isSyncedWith(synced))
+            if (iterator.isSyncedWith(key))
             {
-                return iterator.get(synced);
+                return iterator.get(key);
             }
         }
+        // shortest key would be "last_X" where X is the loop name
+        if (key == null || key.length() < 6)
+        {
+            return null;
+        }
+        if (key.startsWith("last_"))
+        {
+            return isLast(key.substring(5, key.length()));
+        }
+        if (key.startsWith("count_"))
+        {
+            return getCount(key.substring(6, key.length()));
+        }
+        if (key.startsWith("index_"))
+        {
+            return getIndex(key.substring(6, key.length()));
+        }
+        if (key.startsWith("first_"))
+        {
+            return isFirst(key.substring(6, key.length()));
+        }
         return null;
     }
 

Modified: velocity/tools/trunk/src/test/java/org/apache/velocity/tools/LoopToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/java/org/apache/velocity/tools/LoopToolTests.java?rev=667320&r1=667319&r2=667320&view=diff
==============================================================================
--- velocity/tools/trunk/src/test/java/org/apache/velocity/tools/LoopToolTests.java (original)
+++ velocity/tools/trunk/src/test/java/org/apache/velocity/tools/LoopToolTests.java Thu Jun
12 20:14:03 2008
@@ -214,13 +214,19 @@
         assertTrue(loop.isFirst());
         assertTrue(loop.isFirst("i"));
         assertTrue(loop.isFirst("j"));
+        // check short syntax too
+        assertTrue((Boolean)loop.get("first_i"));
+        assertTrue((Boolean)loop.get("first_j"));
         i.next();
         assertFalse(loop.isFirst("i"));
+        assertFalse((Boolean)loop.get("first_i"));
         j.next();
         assertTrue(loop.isFirst());
         assertTrue(loop.isFirst("j"));
+        assertTrue((Boolean)loop.get("first_j"));
         j.next();
         assertFalse(loop.isFirst("j"));
+        assertFalse((Boolean)loop.get("first_j"));
     }
 
     public @Test void methodIsLast() throws Exception
@@ -249,11 +255,15 @@
         assertFalse(loop.isLast());
         assertTrue(loop.isLast("i"));
         assertFalse(loop.isLast("j"));
+        // check short syntax too
+        assertTrue((Boolean)loop.get("last_i"));
+        assertFalse((Boolean)loop.get("last_j"));
         j.next();
         j.next();
         j.next();
         assertTrue(loop.isLast());
         assertTrue(loop.isLast("j"));
+        assertTrue((Boolean)loop.get("last_j"));
     }
 
     public @Test void methodGet() throws Exception
@@ -347,6 +357,11 @@
         assertEquals(1, loop.getIndex("j"));
         assertEquals(1, loop.getCount("i"));
         assertEquals(0, loop.getIndex("i"));
+        // check short syntax too
+        assertEquals(2, loop.get("count_j"));
+        assertEquals(1, loop.get("index_j"));
+        assertEquals(1, loop.get("count_i"));
+        assertEquals(0, loop.get("index_i"));
     }
 
     public @Test void aliasMethods() throws Exception



Mime
View raw message