KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > nodes > ForNode


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.scriptengine.core.nodes;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.List JavaDoc;
34
35 import com.genimen.djeneric.language.Messages;
36 import com.genimen.djeneric.repository.DjList;
37 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine;
38 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope;
39 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException;
40 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope;
41 import com.genimen.djeneric.tools.scriptengine.core.util.Variable;
42
43 public class ForNode extends SetNode
44 {
45   private boolean _ranged = false;
46
47   public ForNode(int i)
48   {
49     super(i);
50   }
51
52   public ForNode(DjScriptParserEngine p, int i)
53   {
54     super(p, i);
55   }
56
57   public String JavaDoc getName()
58   {
59     return "for";
60   }
61
62   public String JavaDoc toString()
63   {
64     return "for(" + getLoopVariable() + " in ...";
65   }
66
67   public void setLoopVariable(String JavaDoc loopVariable)
68   {
69     setSetName(loopVariable);
70   }
71
72   public String JavaDoc getLoopVariable()
73   {
74     return getSetName();
75   }
76
77   public void setBody(BodyNode body)
78   {
79   }
80
81   public boolean isRanged()
82   {
83     return _ranged;
84   }
85
86   public void setRanged(boolean ranged)
87   {
88     _ranged = ranged;
89   }
90
91   public void execute(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
92   {
93     if (isRanged())
94     {
95       executeRanged(context);
96     }
97     else
98     {
99       executeSetBased(context);
100     }
101   }
102
103   public void executeSetBased(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
104   {
105     String JavaDoc varName = getLoopVariable();
106     ValueExpression set = (ValueExpression) getChild(0);
107
108     Object JavaDoc o = set.getValue(context);
109     if (!(o instanceof DjList)) throw new DjScriptExecutionException(Messages.getString("ForNode.ExpressionNotASet"),
110         this);
111
112     DjList lst = (DjList) o;
113
114     if (lst.size() == 0) return;
115
116     BodyNode body = (BodyNode) getChild(BodyNode.class);
117     FilterNode filter = (FilterNode) getChild(FilterNode.class);
118     OrderByNode orderBy = (OrderByNode) getChild(OrderByNode.class);
119
120     if (body == null) return;
121
122     lst = processFilter(lst, context, filter);
123     processOrderBy(lst, orderBy);
124
125     context.mark();
126
127     Variable loopvar = context.getVariableStack().push(varName, lst.getDjenericObjectAt(0));
128
129     for (int i = 0; i < lst.size(); i++)
130     {
131       loopvar.setObject(lst.getDjenericObjectAt(i));
132       body.execute(context);
133     }
134
135     context.releaseToMark();
136   }
137
138   public void executeRanged(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
139   {
140     context.mark();
141
142     String JavaDoc varName = getLoopVariable();
143     ValueExpression fromE = (ValueExpression) getChild(0);
144     ValueExpression toE = (ValueExpression) getChild(1);
145
146     BodyNode body = (BodyNode) getChild(BodyNode.class);
147
148     if (body == null) return;
149
150     int from = Integer.parseInt(String.valueOf(fromE.getValue(context)));
151     int to = Integer.parseInt(String.valueOf(toE.getValue(context)));
152
153     Variable loopvar = context.getVariableStack().push(varName, new Integer JavaDoc(0));
154
155     if (from < to)
156     {
157       for (int i = from; i <= to; i++)
158       {
159         loopvar.assign(new Integer JavaDoc(i), this);
160         body.execute(context);
161       }
162     }
163     else
164     {
165       for (int i = from; i >= to; i--)
166       {
167         loopvar.assign(new Integer JavaDoc(i), this);
168         body.execute(context);
169       }
170     }
171
172     context.releaseToMark();
173   }
174
175   public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException
176   {
177     ctxt.mark();
178
179     if (isRanged()) ctxt.pushVariable(new Variable(getLoopVariable(), null, Integer JavaDoc.class.getName()));
180     else
181     {
182       ValueExpression set = (ValueExpression) getChild(0);
183       ctxt.pushVariable(new Variable(getLoopVariable(), null, set.getValidatedTypeName(ctxt)));
184     }
185
186     BodyNode body = (BodyNode) getChild(BodyNode.class);
187     body.validateScript(ctxt);
188
189     ctxt.releaseToMark();
190   }
191
192   public void collectVariables(DjScriptCompileTimeScope ctxt, HashMap JavaDoc variables, int stopAtLine)
193       throws DjScriptExecutionException
194   {
195     if (stopAtLine >= 0 && getLine() > stopAtLine) return;
196     variables.put(getLoopVariable(), List JavaDoc.class.getName());
197     super.collectVariables(ctxt, variables, stopAtLine);
198   }
199
200 }
Popular Tags