%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jexl.parser.ASTForeachStatement |
|
|
1 | /* |
|
2 | * Copyright 2002-2006 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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 | 1 | 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 | 0 | super(id); |
49 | 0 | } |
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 | 10 | super(p, id); |
59 | 10 | } |
60 | ||
61 | /** {@inheritDoc} */ |
|
62 | public Object jjtAccept(ParserVisitor visitor, Object data) { |
|
63 | 0 | return visitor.visit(this, data); |
64 | } |
|
65 | ||
66 | /** {@inheritDoc} */ |
|
67 | public Object value(JexlContext jc) throws Exception { |
|
68 | 10 | Object result = null; |
69 | /* first child is the loop variable */ |
|
70 | 10 | ASTReference loopVariable = (ASTReference) jjtGetChild(VAR_INDEX); |
71 | /* second child is the variable to iterate */ |
|
72 | 10 | SimpleNode iterable = (SimpleNode) jjtGetChild(ITEMS_INDEX); |
73 | 10 | Object iterableValue = iterable.value(jc); |
74 | // make sure there is a value to iterate on and a statement to execute |
|
75 | 10 | if (iterableValue != null && jjtGetNumChildren() >= (STATEMENT_INDEX + 1)) { |
76 | /* third child is the statement to execute */ |
|
77 | 8 | SimpleNode statement = (SimpleNode) jjtGetChild(2); |
78 | // get an iterator for the collection/array etc via the |
|
79 | // introspector. |
|
80 | 8 | Iterator itemsIterator = Introspector.getUberspect().getIterator( |
81 | iterableValue, DUMMY); |
|
82 | 123 | while (itemsIterator.hasNext()) { |
83 | // set loopVariable to value of iterator |
|
84 | 107 | Object value = itemsIterator.next(); |
85 | 107 | jc.getVars().put(loopVariable.getRootString(), value); |
86 | // execute statement |
|
87 | 107 | result = statement.value(jc); |
88 | } |
|
89 | } |
|
90 | 10 | return result; |
91 | } |
|
92 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |