Author: nbubna
Date: Wed Mar 11 22:34:40 2009
New Revision: 752661
URL: http://svn.apache.org/viewvc?rev=752661&view=rev
Log:
remove $velocityCount and $velocityHasNext now that $foreach provides them
Modified:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/RuntimeConstants.java
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ForeachTestCase.java
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/IndexTestCase.java
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/TemplateTestCase.java
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/issues/Velocity532TestCase.java
velocity/engine/branches/2.0_Exp/test/templates/foreach-array.vm
velocity/engine/branches/2.0_Exp/test/templates/foreach-null-list.vm
velocity/engine/branches/2.0_Exp/test/templates/foreach-variable.vm
Modified:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/RuntimeConstants.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=752661&r1=752660&r2=752661&view=diff
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/RuntimeConstants.java
(original)
+++
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/RuntimeConstants.java
Wed Mar 11 22:34:40 2009
@@ -76,18 +76,6 @@
* ----------------------------------------------------------------------
*/
- /** Counter reference name in #foreach directives. */
- String COUNTER_NAME = "directive.foreach.counter.name";
-
- /**
- * Iterator.hasNext() reference name in #foreach directives.
- * @since 1.6
- */
- String HAS_NEXT_NAME = "directive.foreach.iterator.name";
-
- /** Initial counter value in #foreach directives. */
- String COUNTER_INITIAL_VALUE = "directive.foreach.counter.initial.value";
-
/** Maximum allowed number of loops. */
String MAX_NUMBER_LOOPS = "directive.foreach.maxloops";
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=752661&r1=752660&r2=752661&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 Mar 11 22:34:40 2009
@@ -25,9 +25,6 @@
import java.util.Iterator;
import org.apache.velocity.context.InternalContextAdapter;
-import org.apache.velocity.exception.MethodInvocationException;
-import org.apache.velocity.exception.ParseErrorException;
-import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.TemplateInitException;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeConstants;
@@ -71,25 +68,6 @@
}
/**
- * The name of the variable to use when placing
- * the counter value into the context. Right
- * now the default is $velocityCount.
- */
- protected String counterName;
-
- /**
- * The name of the variable to use when placing
- * iterator hasNext() value into the context.Right
- * now the defailt is $velocityHasNext
- */
- private String hasNextName;
-
- /**
- * What value to start the loop counter at.
- */
- protected int counterInitialValue;
-
- /**
* The maximum number of times we're allowed to loop.
*/
private int maxNbrLoops;
@@ -129,9 +107,6 @@
{
super.init(rs, context, node);
- counterName = rsvc.getString(RuntimeConstants.COUNTER_NAME);
- hasNextName = rsvc.getString(RuntimeConstants.HAS_NEXT_NAME);
- counterInitialValue =
rsvc.getInt(RuntimeConstants.COUNTER_INITIAL_VALUE);
maxNbrLoops = rsvc.getInt(RuntimeConstants.MAX_NUMBER_LOOPS,
Integer.MAX_VALUE);
if (maxNbrLoops < 1)
@@ -187,80 +162,72 @@
}
/**
- * renders the #foreach() block
- * @param context
- * @param writer
- * @param node
- * @return True if the directive rendered successfully.
- * @throws IOException
- * @throws MethodInvocationException
- * @throws ResourceNotFoundException
- * @throws ParseErrorException
- */
- public boolean render(InternalContextAdapter context,
- Writer writer, Node node)
- throws IOException, MethodInvocationException,
ResourceNotFoundException,
- ParseErrorException
+ * Retrieve the contextual iterator.
+ */
+ protected Iterator getIterator(InternalContextAdapter context, Node node)
{
- /*
- * do our introspection to see what our collection is
- */
-
- Object listObject = node.jjtGetChild(2).value(context);
-
- if (listObject == null)
- return false;
-
Iterator i = null;
-
- try
- {
- i = rsvc.getUberspect().getIterator(listObject, uberInfo);
- }
- /**
- * pass through application level runtime exceptions
+ /*
+ * do our introspection to see what our collection is
*/
- catch( RuntimeException e )
- {
- throw e;
- }
- catch(Exception ee)
+ Object iterable = node.value(context);
+ if (iterable != null)
{
- String msg = "Error getting iterator for #foreach at "+uberInfo;
- rsvc.getLog().error(msg, ee);
- throw new VelocityException(msg, ee);
- }
-
- if (i == null)
- {
- if (skipInvalidIterator)
+ try
+ {
+ i = rsvc.getUberspect().getIterator(iterable, uberInfo);
+ }
+ /*
+ * pass through application level runtime exceptions
+ */
+ catch (RuntimeException e)
{
- return false;
+ throw e;
}
- else
+ catch (Exception ee)
{
- Node pnode = node.jjtGetChild(2);
- String msg = "#foreach parameter " + pnode.literal() + " at "
- + Log.formatFileString(pnode)
- + " is of type " + listObject.getClass().getName()
- + " and is either of wrong type or cannot be iterated.";
+ String msg = "Error getting iterator for #foreach parameter "
+ + node.literal() + " at " + Log.formatFileString(node);
+ rsvc.getLog().error(msg, ee);
+ throw new VelocityException(msg, ee);
+ }
+
+ if (i == null && !skipInvalidIterator)
+ {
+ String msg = "#foreach parameter " + node.literal() + " at "
+ + Log.formatFileString(node) + " is of type " +
iterable.getClass().getName()
+ + " and cannot be iterated by " +
rsvc.getUberspect().getClass().getName();
rsvc.getLog().error(msg);
throw new VelocityException(msg);
}
}
+ return i;
+ }
- int counter = counterInitialValue;
- boolean maxNbrLoopsExceeded = false;
+ /**
+ * renders the #foreach() block
+ * @param context
+ * @param writer
+ * @param node
+ * @return True if the directive rendered successfully.
+ * @throws IOException
+ */
+ public boolean render(InternalContextAdapter context, Writer writer, Node
node)
+ throws IOException
+ {
+ Iterator i = getIterator(context, node.jjtGetChild(2));
+ if (i == null)
+ {
+ return false;
+ }
// Get the block ast tree which is always the last child
Node block = node.jjtGetChild(node.jjtGetNumChildren()-1);
/*
- * save the element key if there is one, and the loop counter
+ * save the element key if there is one
*/
Object o = context.get(elementKey);
- Object savedCounter = context.get(counterName);
- Object nextFlag = context.get(hasNextName);
/*
* roll our own scope class instead of using preRender(ctx)'s
@@ -273,23 +240,22 @@
context.put(name, foreach);
}
- while (!maxNbrLoopsExceeded && i.hasNext())
+ int count = 1;
+ while (count <= maxNbrLoops && i.hasNext())
{
+ count++;
+
+ put(context, elementKey, i.next());
if (isScopeProvided())
{
// update the scope control
foreach.index++;
foreach.hasNext = i.hasNext();
}
- put(context, counterName , Integer.valueOf(counter));
- Object value = i.next();
- put(context, hasNextName, Boolean.valueOf(i.hasNext()));
- put(context, elementKey, value);
-
try
{
- block.render(context, writer);
+ renderBlock(context, writer, block);
}
catch (StopCommand stop)
{
@@ -300,23 +266,22 @@
else
{
// clean up first
- clean(context, o, savedCounter, nextFlag);
+ clean(context, o);
throw stop;
}
}
-
- counter++;
-
- // Determine whether we're allowed to continue looping.
- // ASSUMPTION: counterInitialValue is not negative!
- maxNbrLoopsExceeded = (counter - counterInitialValue) >=
maxNbrLoops;
}
- clean(context, o, savedCounter, nextFlag);
+ clean(context, o);
return true;
}
- protected void clean(InternalContextAdapter context,
- Object o, Object savedCounter, Object nextFlag)
+ protected void renderBlock(InternalContextAdapter context, Writer writer,
Node block)
+ throws IOException
+ {
+ block.render(context, writer);
+ }
+
+ protected void clean(InternalContextAdapter context, Object o)
{
/*
* restores element key if exists
@@ -331,31 +296,6 @@
context.remove(elementKey);
}
- /*
- * restores the loop counter (if we were nested)
- * if we have one, else just removes
- */
- if (savedCounter != null)
- {
- context.put(counterName, savedCounter);
- }
- else
- {
- context.remove(counterName);
- }
-
- /*
- * restores the "hasNext" boolean flag if it exists
- */
- if (nextFlag != null)
- {
- context.put(hasNextName, nextFlag);
- }
- else
- {
- context.remove(hasNextName);
- }
-
// clean up after the ForeachScope
postRender(context);
}
Modified:
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=752661&r1=752660&r2=752661&view=diff
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java
(original)
+++
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/contrib/For.java
Wed Mar 11 22:34:40 2009
@@ -1,5 +1,7 @@
package org.apache.velocity.runtime.directive.contrib;
+import java.io.IOException;
+import java.io.Writer;
import java.util.ArrayList;
import org.apache.velocity.context.InternalContextAdapter;
@@ -13,7 +15,6 @@
import org.apache.velocity.runtime.parser.node.ASTReference;
import org.apache.velocity.runtime.parser.node.Node;
-
/**
* The #for directive provides the behavior of the #foreach directive but also
* provides an 'index' keyword that allows the user to define an optional
index variable
@@ -24,6 +25,8 @@
*/
public class For extends Foreach
{
+ protected String counterName;
+ protected int counterInitialValue;
public String getName()
{
@@ -49,7 +52,42 @@
counterInitialValue = 0;
}
}
-
+
+ @Override
+ public boolean render(InternalContextAdapter context, Writer writer, Node
node)
+ throws IOException
+ {
+ Object c = context.get(counterName);
+ context.put(counterName, counterInitialValue);
+ try
+ {
+ return super.render(context, writer, node);
+ }
+ finally
+ {
+ if (c != null)
+ {
+ context.put(counterName, c);
+ }
+ else
+ {
+ context.remove(counterName);
+ }
+ }
+ }
+
+ @Override
+ protected void renderBlock(InternalContextAdapter context, Writer writer,
Node node)
+ throws IOException
+ {
+ Object count = context.get(counterName);
+ if (count instanceof Number)
+ {
+ context.put(counterName, ((Number)count).intValue() + 1);
+ }
+ super.renderBlock(context, writer, node);
+ }
+
/**
* 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).
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=752661&r1=752660&r2=752661&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 Mar 11 22:34:40 2009
@@ -103,7 +103,7 @@
list.add("test2");
list.add("test3");
context.put("list", list);
- assertEvalEquals("test1 SEPARATOR test2 SEPARATOR test3 ", "#foreach
($value in $list)$value #if( $velocityHasNext )SEPARATOR #end#end");
+ assertEvalEquals("test1 SEPARATOR test2 SEPARATOR test3 ", "#foreach
($value in $list)$value #if( $foreach.hasNext )SEPARATOR #end#end");
}
public void testNestedVelocityHasNextProperty()
@@ -121,7 +121,7 @@
list2.add("a3");
context.put("list2", list2);
- assertEvalEquals("test1 (a1;a2;a3)-test2 (a1;a2;a3)-test3
(a1;a2;a3)-test4 (a1;a2;a3)", "#foreach ($value in $list)$value (#foreach ($val
in $list2)$val#if( $velocityHasNext );#end#end)#if( $velocityHasNext
)-#end#end");
+ assertEvalEquals("test1 (a1;a2;a3)-test2 (a1;a2;a3)-test3
(a1;a2;a3)-test4 (a1;a2;a3)", "#foreach ($value in $list)$value (#foreach ($val
in $list2)$val#if( $foreach.hasNext );#end#end)#if( $foreach.hasNext
)-#end#end");
}
public static class MyIterable
Modified:
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/IndexTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/IndexTestCase.java?rev=752661&r1=752660&r2=752661&view=diff
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/IndexTestCase.java
(original)
+++
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/IndexTestCase.java
Wed Mar 11 22:34:40 2009
@@ -36,7 +36,6 @@
{
super.setUp();
engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT,
Boolean.TRUE);
- engine.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, new
Integer(0));
context.put("NULL", null);
context.put("red", "blue");
@@ -81,7 +80,7 @@
assertEvalEquals("2", "$foo.getBar()[1]");
assertEvalEquals("2", "$foo.getBar()[3][1]");
- assertEvalEquals(" a ab abc ", "#foreach($i in $foo.bar[3])
$str[$velocityCount] #end");
+ assertEvalEquals(" a ab abc ", "#foreach($i in $foo.bar[3])
$str[$foreach.index] #end");
assertEvalEquals("apple", "#set($hash = {'a':'apple',
'b':'orange'})$hash['a']");
Modified:
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/TemplateTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/TemplateTestCase.java?rev=752661&r1=752660&r2=752661&view=diff
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/TemplateTestCase.java
(original)
+++
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/TemplateTestCase.java
Wed Mar 11 22:34:40 2009
@@ -104,7 +104,6 @@
throws Exception
{
super.setUp();
- engine.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, 1);
provider = new TestProvider();
al = provider.getCustomers();
Modified:
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/issues/Velocity532TestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/issues/Velocity532TestCase.java?rev=752661&r1=752660&r2=752661&view=diff
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/issues/Velocity532TestCase.java
(original)
+++
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/issues/Velocity532TestCase.java
Wed Mar 11 22:34:40 2009
@@ -33,7 +33,7 @@
public void test532()
{
- String template = "#macro( test )$velocityCount#end"+
+ String template = "#macro( test )$foreach.count#end"+
"#foreach( $i in [1..5] )#test()#end";
assertEvalEquals("12345", template);
}
@@ -43,7 +43,7 @@
// try something a little more like Matt's example
String template = "#macro( test $baz )"+
"#if( $foo == $null )"+
- "#if( $velocityCount == 3 )bar#end"+
+ "#if( $foreach.count == 3 )bar#end"+
"#end#end"+
"#foreach( $i in [1..5] )#test($i)#end";
assertEvalEquals("bar", template);
Modified: velocity/engine/branches/2.0_Exp/test/templates/foreach-array.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/test/templates/foreach-array.vm?rev=752661&r1=752660&r2=752661&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/test/templates/foreach-array.vm (original)
+++ velocity/engine/branches/2.0_Exp/test/templates/foreach-array.vm Wed Mar 11
22:34:40 2009
@@ -16,7 +16,7 @@
<table>
#foreach ($element in $stringarray)
<tr>
- <td>This is $element and it is the $velocityCount item</td>
+ <td>This is $element and it is the $foreach.count item</td>
</tr>
#end
</table>
@@ -26,7 +26,7 @@
<table>
#foreach ($element in $woogiefoogie)
<tr>
- <td>This is $element and it is the $velocityCount item</td>
+ <td>This is $element and it is the $foreach.count item</td>
</tr>
#end
</table>
Modified: velocity/engine/branches/2.0_Exp/test/templates/foreach-null-list.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/test/templates/foreach-null-list.vm?rev=752661&r1=752660&r2=752661&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/test/templates/foreach-null-list.vm
(original)
+++ velocity/engine/branches/2.0_Exp/test/templates/foreach-null-list.vm Wed
Mar 11 22:34:40 2009
@@ -13,5 +13,5 @@
#foreach ($element in $nullList)
This is $element.
- $velocityCount
+ $foreach.count
#end
Modified: velocity/engine/branches/2.0_Exp/test/templates/foreach-variable.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/test/templates/foreach-variable.vm?rev=752661&r1=752660&r2=752661&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/test/templates/foreach-variable.vm
(original)
+++ velocity/engine/branches/2.0_Exp/test/templates/foreach-variable.vm Wed Mar
11 22:34:40 2009
@@ -13,20 +13,20 @@
#foreach ($element in $list)
This is $element.
- $velocityCount
+ $foreach.count
#end
#foreach ($element in $list)
-- inner foreach --
#foreach ($element in $list)
This is $element.
- $velocityCount
+ $foreach.count
#end
-- inner foreach --
-- outer foreach --
This is $element.
- $velocityCount
+ $foreach.count
-- outer foreach --
#end