1 30 package com.genimen.djeneric.tools.scriptengine.core.nodes; 31 32 import java.util.HashMap ; 33 import java.util.List ; 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 getName() 58 { 59 return "for"; 60 } 61 62 public String toString() 63 { 64 return "for(" + getLoopVariable() + " in ..."; 65 } 66 67 public void setLoopVariable(String loopVariable) 68 { 69 setSetName(loopVariable); 70 } 71 72 public String 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 varName = getLoopVariable(); 106 ValueExpression set = (ValueExpression) getChild(0); 107 108 Object 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 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 (0)); 154 155 if (from < to) 156 { 157 for (int i = from; i <= to; i++) 158 { 159 loopvar.assign(new Integer (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 (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 .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 variables, int stopAtLine) 193 throws DjScriptExecutionException 194 { 195 if (stopAtLine >= 0 && getLine() > stopAtLine) return; 196 variables.put(getLoopVariable(), List .class.getName()); 197 super.collectVariables(ctxt, variables, stopAtLine); 198 } 199 200 } | Popular Tags |