org.apache.velocity.runtime.directive

Class Foreach

Implemented Interfaces:
Cloneable, DirectiveConstants

public class Foreach
extends Directive

Foreach directive used for moving through arrays, or objects that provide an Iterator.

Version:
$Id: Foreach.java,v 1.40 2001/10/24 03:06:17 geirm Exp $

Authors:
Jason van Zyl
Geir Magnusson Jr.

Nested Class Summary

Field Summary

private static int
INFO_ARRAY
Flag to indicate that the list object being used in an array.
private static int
INFO_COLLECTION
Flag to indicate that the list object being used is a Collection.
private static int
INFO_ENUMERATION
Flag to indicate that the list object being used is an Enumeration
private static int
INFO_ITERATOR
Flag to indicate that the list object being used provides an Iterator.
private static int
INFO_MAP
Flag to indicate that the list object being used is a Map.
private static int
UNKNOWN
private int
counterInitialValue
What value to start the loop counter at.
private String
counterName
The name of the variable to use when placing the counter value into the context.
private String
elementKey
The reference name used to access each of the elements in the list object.

Fields inherited from class org.apache.velocity.runtime.directive.Directive

column, line, rsvc

Fields inherited from interface org.apache.velocity.runtime.directive.DirectiveConstants

BLOCK, LINE

Method Summary

private Iterator
getIterator(InternalContextAdapter context, Node node)
returns an Iterator to the collection in the #foreach()
String
getName()
Return name of this directive.
int
getType()
Return type of this directive.
void
init(RuntimeServices rs, InternalContextAdapter context, Node node)
simple init - init the tree and get the elementKey from the AST
boolean
render(InternalContextAdapter context, Writer writer, Node node)
renders the #foreach() block

Methods inherited from class org.apache.velocity.runtime.directive.Directive

getColumn, getLine, getName, getType, init, render, setLocation

Field Details

INFO_ARRAY

private static final int INFO_ARRAY
Flag to indicate that the list object being used in an array.

Field Value:
1


INFO_COLLECTION

private static final int INFO_COLLECTION
Flag to indicate that the list object being used is a Collection.

Field Value:
4


INFO_ENUMERATION

private static final int INFO_ENUMERATION
Flag to indicate that the list object being used is an Enumeration

Field Value:
5


INFO_ITERATOR

private static final int INFO_ITERATOR
Flag to indicate that the list object being used provides an Iterator.

Field Value:
2


INFO_MAP

private static final int INFO_MAP
Flag to indicate that the list object being used is a Map.

Field Value:
3


UNKNOWN

private static final int UNKNOWN

Field Value:
-1


counterInitialValue

private int counterInitialValue
What value to start the loop counter at.


counterName

private String counterName
The name of the variable to use when placing the counter value into the context. Right now the default is $velocityCount.


elementKey

private String elementKey
The reference name used to access each of the elements in the list object. It is the $item in the following: #foreach ($item in $list) This can be used class wide because it is immutable.

Method Details

getIterator

private Iterator getIterator(InternalContextAdapter context,
                             Node node)
            throws MethodInvocationException
returns an Iterator to the collection in the #foreach()

Parameters:
context - current context
node - AST node

Returns:
Iterator to do the dataset


getName

public String getName()
Return name of this directive.
Overrides:
getName in interface Directive


getType

public int getType()
Return type of this directive.
Overrides:
getType in interface Directive


init

public void init(RuntimeServices rs,
                 InternalContextAdapter context,
                 Node node)
            throws Exception
simple init - init the tree and get the elementKey from the AST
Overrides:
init in interface Directive


render

public boolean render(InternalContextAdapter context,
                      Writer writer,
                      Node node)
            throws IOException,
                   MethodInvocationException,
                   ResourceNotFoundException,
                   ParseErrorException
renders the #foreach() block
Overrides:
render in interface Directive


Copyright © 2003 Apache Software Foundation. All Rights Reserved.