Author: byron
Date: Wed Feb 18 04:39:39 2009
New Revision: 745376

URL: http://svn.apache.org/viewvc?rev=745376&view=rev
Log:
VELOCITY-696 Moved index keyword functionality to seperate For directive

Added:
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java
Modified:
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
    
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ForeachTestCase.java

Modified: 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java?rev=745376&r1=745375&r2=745376&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
 Wed Feb 18 04:39:39 2009
@@ -75,7 +75,7 @@
      * the counter value into the context. Right
      * now the default is $velocityCount.
      */
-    private String counterName;
+    protected String counterName;
 
     /**
      * The name of the variable to use when placing
@@ -87,7 +87,7 @@
     /**
      * What value to start the loop counter at.
      */
-    private int counterInitialValue;
+    protected int counterInitialValue;
 
     /**
      * The maximum number of times we're allowed to loop.
@@ -167,16 +167,6 @@
             elementKey = sn.getFirstToken().image.substring(1);
         }
         
-        // If we have more then 3 argument then the user has specified an
-        // index value, i.e.; #foreach($a in $b index $c)
-        if (node.jjtGetNumChildren() > 4)
-        {
-            // The index variable name is at position 4
-            counterName = ((ASTReference) node.jjtGetChild(4)).getRootString();
-            // The count value always starts at 0 when using an index.
-            counterInitialValue = 0;
-        }
-
         /*
          * make an uberinfo - saves new's later on
          */
@@ -368,20 +358,5 @@
             throw new MacroParseException("Argument 3 of #foreach is of the 
wrong type",
                 templateName, t);
         }
-        
-        // If #foreach is defining an index variable make sure it has the 
'index $var' combo.
-        if (argtypes.size() > 3)
-        {
-            if (argtypes.get(3) != ParserTreeConstants.JJTWORD)
-            {
-                throw new MacroParseException("Expected word 'index' at 
argument position 4 in #foreach",
-                    templateName, t);
-            }
-            else if (argtypes.size() == 4 || argtypes.get(4) != 
ParserTreeConstants.JJTREFERENCE)
-            {
-                throw new MacroParseException("Expected a reference after 
'index' in #foreach",
-                  templateName, t);          
-            }
-        }                
     }        
 }

Added: 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java?rev=745376&view=auto
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java
 (added)
+++ 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java
 Wed Feb 18 04:39:39 2009
@@ -0,0 +1,70 @@
+package org.apache.velocity.runtime.directive.contrib;
+
+import java.util.ArrayList;
+
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.exception.TemplateInitException;
+import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.directive.Foreach;
+import org.apache.velocity.runtime.directive.MacroParseException;
+import org.apache.velocity.runtime.parser.ParseException;
+import org.apache.velocity.runtime.parser.ParserTreeConstants;
+import org.apache.velocity.runtime.parser.Token;
+import org.apache.velocity.runtime.parser.node.ASTReference;
+import org.apache.velocity.runtime.parser.node.Node;
+
+public class For extends Foreach
+{
+
+  public String getName()
+  {
+    return "for";
+  }
+
+  public int getType()
+  {
+    return BLOCK;
+  }
+
+  public void init(RuntimeServices rs, InternalContextAdapter context, Node 
node)
+      throws TemplateInitException
+  {
+    super.init(rs, context, node);    
+    // If we have more then 3 argument then the user has specified an
+    // index value, i.e.; #foreach($a in $b index $c)
+    if (node.jjtGetNumChildren() > 4)
+    {
+        // The index variable name is at position 4
+        counterName = ((ASTReference) node.jjtGetChild(4)).getRootString();
+        // The count value always starts at 0 when using an index.
+        counterInitialValue = 0;
+    }
+  }
+    
+  /**
+   * We do not allow a word token in any other arg position except for the 2nd
+   * since we are looking for the pattern #foreach($foo in $bar).
+   */
+  public void checkArgs(ArrayList<Integer> argtypes, Token t,
+      String templateName) throws ParseException
+  {
+    super.checkArgs(argtypes, t, templateName);
+    
+    // If #foreach is defining an index variable make sure it has the 'index
+    // $var' combo.
+    if (argtypes.size() > 3)
+    {
+      if (argtypes.get(3) != ParserTreeConstants.JJTWORD)
+      {
+        throw new MacroParseException(
+            "Expected word 'index' at argument position 4 in #foreach",
+            templateName, t);
+      } 
+      else if (argtypes.size() == 4 || argtypes.get(4) != 
ParserTreeConstants.JJTREFERENCE)
+      {
+        throw new MacroParseException(
+            "Expected a reference after 'index' in #foreach", templateName, t);
+      }
+    }
+  }
+}

Modified: 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ForeachTestCase.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ForeachTestCase.java?rev=745376&r1=745375&r2=745376&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ForeachTestCase.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ForeachTestCase.java
 Wed Feb 18 04:39:39 2009
@@ -58,19 +58,6 @@
         assertEvalEquals("1 2 3 ", "#foreach ($item in [1..10])$item #end");   
          
     }
     
-    public void testIndexKeyword()
-    {
-        assertEvalEquals("041526", "#set($b = [4, 5, 6])#foreach($a in $b 
index $c)$c$a#end");
-        assertEvalEquals("444546545556646566", "#set($b = [4, 5, 
6])#foreach($a in $b index $c)#foreach($x in $b index $c)$a$x#end#end");
-    }
-    
-    public void testIndexErrors()
-    {
-      assertEvalException("#foreach($a in b)#end");
-      assertEvalException("#foreach($a in $b index)#end");
-      assertEvalException("#foreach($a in $b index blaa)#end");
-    }
-
     /**
      * Tests proper method execution during a Foreach loop over a Collection
      * with items of varying classes.


Reply via email to