1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.apache.commons.jexl.parser;
18  
19  import java.util.Iterator;
20  
21  import org.apache.commons.jexl.JexlContext;
22  import org.apache.commons.jexl.util.Introspector;
23  import org.apache.commons.jexl.util.introspection.Info;
24  
25  /***
26   * ForEach statement. Syntax: foreach (var in iterable) Statement()
27   * 
28   * @author Dion Gillard
29   * @since 1.1
30   */
31  public class ASTForeachStatement extends SimpleNode {
32      /*** dummy velocity info. */
33      private static final Info DUMMY = new Info("", 1, 1);
34      /*** index of the loop variable. */
35      private static final int VAR_INDEX = 0;
36      /*** index of the items. */
37      private static final int ITEMS_INDEX = 1;
38      /*** index of the code to execute. */
39      private static final int STATEMENT_INDEX = 2;
40  
41  
42      /***
43       * Create the node given an id.
44       * 
45       * @param id node id.
46       */
47      public ASTForeachStatement(int id) {
48          super(id);
49      }
50  
51      /***
52       * Create a node with the given parser and id.
53       * 
54       * @param p a parser.
55       * @param id node id.
56       */
57      public ASTForeachStatement(Parser p, int id) {
58          super(p, id);
59      }
60  
61      /*** {@inheritDoc} */
62      public Object jjtAccept(ParserVisitor visitor, Object data) {
63          return visitor.visit(this, data);
64      }
65  
66      /*** {@inheritDoc} */
67      public Object value(JexlContext jc) throws Exception {
68          Object result = null;
69          
70          ASTReference loopVariable = (ASTReference) jjtGetChild(VAR_INDEX);
71          
72          SimpleNode iterable = (SimpleNode) jjtGetChild(ITEMS_INDEX);
73          Object iterableValue = iterable.value(jc);
74          
75          if (iterableValue != null && jjtGetNumChildren() >= (STATEMENT_INDEX + 1)) {
76              
77              SimpleNode statement = (SimpleNode) jjtGetChild(2);
78              
79              
80              Iterator itemsIterator = Introspector.getUberspect().getIterator(
81                      iterableValue, DUMMY);
82              while (itemsIterator.hasNext()) {
83                  
84                  Object value = itemsIterator.next();
85                  jc.getVars().put(loopVariable.getRootString(), value);
86                  
87                  result = statement.value(jc);
88              }
89          }
90          return result;
91      }
92  }