1 2 3 package com.genimen.djeneric.tools.scriptengine.core; 4 5 import java.math.BigDecimal ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 9 import com.genimen.djeneric.tools.scriptengine.core.nodes.ActionListNode; 10 import com.genimen.djeneric.tools.scriptengine.core.nodes.ActionNode; 11 import com.genimen.djeneric.tools.scriptengine.core.nodes.AndOrNode; 12 import com.genimen.djeneric.tools.scriptengine.core.nodes.ArgumentsNode; 13 import com.genimen.djeneric.tools.scriptengine.core.nodes.AssignmentStatementNode; 14 import com.genimen.djeneric.tools.scriptengine.core.nodes.BodyNode; 15 import com.genimen.djeneric.tools.scriptengine.core.nodes.BoolNode; 16 import com.genimen.djeneric.tools.scriptengine.core.nodes.CalculatedExpressionNode; 17 import com.genimen.djeneric.tools.scriptengine.core.nodes.CharNode; 18 import com.genimen.djeneric.tools.scriptengine.core.nodes.ChooseNode; 19 import com.genimen.djeneric.tools.scriptengine.core.nodes.ConstructorNode; 20 import com.genimen.djeneric.tools.scriptengine.core.nodes.ControllerNode; 21 import com.genimen.djeneric.tools.scriptengine.core.nodes.CustomPanelStatementNode; 22 import com.genimen.djeneric.tools.scriptengine.core.nodes.DeclarationNode; 23 import com.genimen.djeneric.tools.scriptengine.core.nodes.DeleteStatementNode; 24 import com.genimen.djeneric.tools.scriptengine.core.nodes.DispatchNode; 25 import com.genimen.djeneric.tools.scriptengine.core.nodes.EditStatementNode; 26 import com.genimen.djeneric.tools.scriptengine.core.nodes.ElseNode; 27 import com.genimen.djeneric.tools.scriptengine.core.nodes.EventDefinitionNode; 28 import com.genimen.djeneric.tools.scriptengine.core.nodes.EventMappingNode; 29 import com.genimen.djeneric.tools.scriptengine.core.nodes.ExpressionNode; 30 import com.genimen.djeneric.tools.scriptengine.core.nodes.FailStatementNode; 31 import com.genimen.djeneric.tools.scriptengine.core.nodes.FilterNode; 32 import com.genimen.djeneric.tools.scriptengine.core.nodes.FloatNode; 33 import com.genimen.djeneric.tools.scriptengine.core.nodes.ForNode; 34 import com.genimen.djeneric.tools.scriptengine.core.nodes.IfNode; 35 import com.genimen.djeneric.tools.scriptengine.core.nodes.IntegerNode; 36 import com.genimen.djeneric.tools.scriptengine.core.nodes.JavaStatementNode; 37 import com.genimen.djeneric.tools.scriptengine.core.nodes.NewNode; 38 import com.genimen.djeneric.tools.scriptengine.core.nodes.NullNode; 39 import com.genimen.djeneric.tools.scriptengine.core.nodes.OperatorNode; 40 import com.genimen.djeneric.tools.scriptengine.core.nodes.OqlFilterNode; 41 import com.genimen.djeneric.tools.scriptengine.core.nodes.OrderByNode; 42 import com.genimen.djeneric.tools.scriptengine.core.nodes.PropertyOrFunctionNode; 43 import com.genimen.djeneric.tools.scriptengine.core.nodes.PropertyPathNode; 44 import com.genimen.djeneric.tools.scriptengine.core.nodes.ScriptNode; 45 import com.genimen.djeneric.tools.scriptengine.core.nodes.SetNode; 46 import com.genimen.djeneric.tools.scriptengine.core.nodes.StringNode; 47 import com.genimen.djeneric.tools.scriptengine.core.nodes.SubExpressionNode; 48 import com.genimen.djeneric.tools.scriptengine.core.nodes.SubScriptStatementNode; 49 import com.genimen.djeneric.tools.scriptengine.core.nodes.TerminateStatementNode; 50 import com.genimen.djeneric.tools.scriptengine.core.nodes.TraceStatementNode; 51 import com.genimen.djeneric.tools.scriptengine.core.nodes.TransactionalStatementNode; 52 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope; 53 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException; 54 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope; 55 56 public class SimpleNode implements Node 57 { 58 protected Node parent; 59 protected Node[] children; 60 protected int id; 61 protected DjScriptParserEngine parser; 62 private int beginLine = 0, beginColumn = 0; 63 64 protected String name = null; 65 66 public SimpleNode(int i) 67 { 68 id = i; 69 } 70 71 public SimpleNode(DjScriptParserEngine p, int i) 72 { 73 this(i); 74 parser = p; 75 } 76 77 public String getName() 78 { 79 return "SimpleNode"; 80 } 81 82 public void setLineInfo(Token token) 83 { 84 beginLine = token.beginLine; 85 beginColumn = token.beginColumn; 86 } 87 88 public void setLineInfo(SimpleNode node) 89 { 90 beginLine = node.beginLine; 91 beginColumn = node.beginColumn; 92 } 93 94 public void setLineInfo(int line, int column) 95 { 96 beginLine = line; 97 beginColumn = column; 98 } 99 100 public static Node jjtCreate(int id) 101 { 102 SimpleNode newNode; 103 104 if (id == DjScriptParserEngineTreeConstants.JJTACTION) newNode = new ActionNode(id); 105 else if (id == DjScriptParserEngineTreeConstants.JJTEVENTDEFINITION) newNode = new EventDefinitionNode(id); 106 else if (id == DjScriptParserEngineTreeConstants.JJTDELETESTATEMENT) newNode = new DeleteStatementNode(id); 107 else if (id == DjScriptParserEngineTreeConstants.JJTTERMINATESTATEMENT) newNode = new TerminateStatementNode(id); 108 else if (id == DjScriptParserEngineTreeConstants.JJTDISPATCH) newNode = new DispatchNode(id); 109 else if (id == DjScriptParserEngineTreeConstants.JJTFAILSTATEMENT) newNode = new FailStatementNode(id); 110 else if (id == DjScriptParserEngineTreeConstants.JJTTRANSACTIONALSTATEMENT) newNode = new TransactionalStatementNode( 111 id); 112 else if (id == DjScriptParserEngineTreeConstants.JJTTRACESTATEMENT) newNode = new TraceStatementNode(id); 113 else if (id == DjScriptParserEngineTreeConstants.JJTCHOOSE) newNode = new ChooseNode(id); 114 else if (id == DjScriptParserEngineTreeConstants.JJTASSIGNMENTSTATEMENT) newNode = new AssignmentStatementNode(id); 115 else if (id == DjScriptParserEngineTreeConstants.JJTCONTROLLER) newNode = new ControllerNode(id); 116 else if (id == DjScriptParserEngineTreeConstants.JJTACTIONLIST) newNode = new ActionListNode(id); 117 else if (id == DjScriptParserEngineTreeConstants.JJTSUBSCRIPTSTATEMENT) newNode = new SubScriptStatementNode(id); 118 else if (id == DjScriptParserEngineTreeConstants.JJTEDITSTATEMENT) newNode = new EditStatementNode(id); 119 else if (id == DjScriptParserEngineTreeConstants.JJTCUSTOMPANELSTATEMENT) newNode = new CustomPanelStatementNode(id); 120 else if (id == DjScriptParserEngineTreeConstants.JJTOQLFILTERNODE) newNode = new OqlFilterNode(id); 121 else if (id == DjScriptParserEngineTreeConstants.JJTCONSTRUCTOR) newNode = new ConstructorNode(id); 122 else if (id == DjScriptParserEngineTreeConstants.JJTEVENTMAPPING) newNode = new EventMappingNode(id); 123 else if (id == DjScriptParserEngineTreeConstants.JJTBUILDTREE) newNode = new ScriptNode(id); 124 else if (id == DjScriptParserEngineTreeConstants.JJTBUILDEMBEDDEDTREE) newNode = new ScriptNode(id); 125 else if (id == DjScriptParserEngineTreeConstants.JJTSETBASEDFORSTATEMENT) newNode = new ForNode(id); 126 else if (id == DjScriptParserEngineTreeConstants.JJTRANGEDBASEDFORSTATEMENT) newNode = new ForNode(id); 127 else if (id == DjScriptParserEngineTreeConstants.JJTSETSTATEMENT) newNode = new SetNode(id); 128 else if (id == DjScriptParserEngineTreeConstants.JJTIFSTATEMENT) newNode = new IfNode(id); 129 else if (id == DjScriptParserEngineTreeConstants.JJTBODY) newNode = new BodyNode(id); 130 else if (id == DjScriptParserEngineTreeConstants.JJTELSESTATEMENT) newNode = new ElseNode(id); 131 else if (id == DjScriptParserEngineTreeConstants.JJTEXPRESSION) newNode = new ExpressionNode(id); 132 else if (id == DjScriptParserEngineTreeConstants.JJTSUBEXPRESSION) newNode = new SubExpressionNode(id); 133 else if (id == DjScriptParserEngineTreeConstants.JJTANDOR) newNode = new AndOrNode(id); 134 else if (id == DjScriptParserEngineTreeConstants.JJTCALCULATEDEXPRESSION) newNode = new CalculatedExpressionNode(id); 135 else if (id == DjScriptParserEngineTreeConstants.JJTOPERATOR) newNode = new OperatorNode(id); 136 else if (id == DjScriptParserEngineTreeConstants.JJTOBJECTPATH) newNode = new PropertyPathNode(id); 137 else if (id == DjScriptParserEngineTreeConstants.JJTPROPERTYORFUNCTION) newNode = new PropertyOrFunctionNode(id); 138 else if (id == DjScriptParserEngineTreeConstants.JJTFUNCTIONCALL) newNode = new PropertyOrFunctionNode(id); 139 else if (id == DjScriptParserEngineTreeConstants.JJTBOOLLITERAL) newNode = new BoolNode(id); 140 else if (id == DjScriptParserEngineTreeConstants.JJTCHARLITERAL) newNode = new CharNode(id); 141 else if (id == DjScriptParserEngineTreeConstants.JJTFLOATLITERAL) newNode = new FloatNode(id); 142 else if (id == DjScriptParserEngineTreeConstants.JJTDECLARATION) newNode = new DeclarationNode(id); 143 else if (id == DjScriptParserEngineTreeConstants.JJTARGUMENTS) newNode = new ArgumentsNode(id); 144 else if (id == DjScriptParserEngineTreeConstants.JJTNEW) newNode = new NewNode(id); 145 else if (id == DjScriptParserEngineTreeConstants.JJTINTEGERLITERAL) newNode = new IntegerNode(id); 146 else if (id == DjScriptParserEngineTreeConstants.JJTNULLLITERAL) newNode = new NullNode(id); 147 else if (id == DjScriptParserEngineTreeConstants.JJTSTRINGLITERAL) newNode = new StringNode(id); 148 else if (id == DjScriptParserEngineTreeConstants.JJTFILTER) newNode = new FilterNode(id); 149 else if (id == DjScriptParserEngineTreeConstants.JJTORDERBY) newNode = new OrderByNode(id); 150 else if (id == DjScriptParserEngineTreeConstants.JJTJAVASTATEMENT) newNode = new JavaStatementNode(id); 151 else if (id == DjScriptParserEngineTreeConstants.JJTPARSEDECLARATIONONLY) newNode = new ScriptNode(id); 152 else newNode = new SimpleNode(id); 153 154 return newNode; 155 } 156 157 public static Node jjtCreate(DjScriptParserEngine p, int id) 158 { 159 return new SimpleNode(p, id); 160 } 161 162 public void jjtOpen() 163 { 164 } 165 166 public void jjtClose() 167 { 168 } 169 170 public void setParent(SimpleNode n) 171 { 172 parent = n; 173 } 174 175 public void jjtSetParent(Node n) 176 { 177 parent = n; 178 } 179 180 public SimpleNode getParent() 181 { 182 return (SimpleNode) parent; 183 } 184 185 public Node jjtGetParent() 186 { 187 return parent; 188 } 189 190 public void addChild(SimpleNode n) 191 { 192 jjtAddChild(n); 193 n.setParent(this); 194 } 195 196 public void addChild(SimpleNode n, int i) 197 { 198 jjtAddChild(n, i); 199 n.setParent(this); 200 } 201 202 public void jjtAddChild(Node n) 203 { 204 int idx = 0; 205 if (children != null) idx = children.length; 206 jjtAddChild(n, idx); 207 } 208 209 public void jjtAddChild(Node n, int i) 210 { 211 if (children == null) 212 { 213 children = new Node[i + 1]; 214 } 215 else if (i >= children.length) 216 { 217 Node c[] = new Node[i + 1]; 218 System.arraycopy(children, 0, c, 0, children.length); 219 children = c; 220 } 221 children[i] = n; 222 } 223 224 public void replaceChild(int i, SimpleNode n) 225 { 226 children[i] = n; 227 n.setParent(this); 228 } 229 230 public void replaceChild(SimpleNode node, SimpleNode newNode) 231 { 232 children[indexOf(node)] = newNode; 233 newNode.setParent(this); 234 } 235 236 public int indexOf(SimpleNode node) 237 { 238 for (int i = 0; i < children.length; i++) 239 { 240 if (children[i] == node) return i; 241 } 242 return -1; 243 } 244 245 public SimpleNode getChild(int i) 246 { 247 return (SimpleNode) children[i]; 248 } 249 250 public Node jjtGetChild(int i) 251 { 252 return children[i]; 253 } 254 255 public void removeChild(int at) 256 { 257 jjtRemoveChild(at); 258 } 259 260 public void jjtRemoveChild(int at) 261 { 262 Node c[] = new Node[children.length - 1]; 263 int idx = 0; 264 for (int i = 0; i < children.length; i++) 265 { 266 if (i != at) c[idx++] = children[i]; 267 } 268 children = c; 269 } 270 271 public void removeChild(Node node) 272 { 273 jjtRemoveChild(node); 274 } 275 276 public void jjtRemoveChild(Node node) 277 { 278 int at = -1; 279 for (int i = 0; i < children.length; i++) 280 { 281 if (children[i] == node) 282 { 283 at = i; 284 break; 285 } 286 } 287 if (at == -1) return; 288 jjtRemoveChild(at); 289 } 290 291 public int getChildCount() 292 { 293 return jjtGetNumChildren(); 294 } 295 296 public int jjtGetNumChildren() 297 { 298 return (children == null) ? 0 : children.length; 299 } 300 301 306 public String toString() 307 { 308 if (name != null) return name; 309 return DjScriptParserEngineTreeConstants.jjtNodeName[id]; 310 } 311 312 public void setName(String nm) 313 { 314 name = nm; 315 } 316 317 public String toString(String prefix) 318 { 319 return prefix + toString(); 320 } 321 322 325 public void dump(String prefix) 326 { 327 System.out.println(toString(prefix)); 328 if (children != null) 329 { 330 for (int i = 0; i < children.length; ++i) 331 { 332 SimpleNode n = (SimpleNode) children[i]; 333 if (n != null) 334 { 335 n.dump(prefix + " "); 336 } 337 } 338 } 339 } 340 341 public void execute(DjScriptExecutionTimeScope ctxt) throws DjScriptExecutionException 342 { 343 throw new DjScriptExecutionException("Can not execute a " + getClass().getName()); 344 } 345 346 public void translateOql(DjScriptExecutionTimeScope ctxt, StringBuffer result, HashMap parameters) 347 throws ParseException 348 { 349 for (int i = 0; i < getChildCount(); i++) 350 { 351 getChild(i).translateOql(ctxt, result, parameters); 352 } 353 } 354 355 public BigDecimal toBigDecimal(Object o) 356 { 357 if (o instanceof BigDecimal ) return (BigDecimal ) o; 358 return new BigDecimal (o.toString()); 359 } 360 361 public Float toFloat(Object o) 362 { 363 if (o instanceof Float ) return (Float ) o; 364 return new Float (o.toString()); 365 } 366 367 public Long toLong(Object o) 368 { 369 if (o instanceof Long ) return (Long ) o; 370 if (o instanceof Float ) return new Long (((Float ) o).longValue()); 371 372 return new Long (o.toString()); 373 } 374 375 public String classOnly(Object o) 376 { 377 if (o == null) return "null"; 378 String full = o.getClass().getName(); 379 int idx = full.lastIndexOf("."); 380 if (idx == -1) return full; 381 return full.substring(idx + 1); 382 } 383 384 public SimpleNode getChild(Class clazz) 385 { 386 for (int i = 0; i < getChildCount(); i++) 387 { 388 if (getChild(i).getClass() == clazz) return getChild(i); 389 } 390 return null; 391 } 392 393 public ArrayList getChildren(Class clazz) 394 { 395 ArrayList lst = new ArrayList (); 396 397 for (int i = 0; i < getChildCount(); i++) 398 { 399 if (getChild(i).getClass() == clazz) lst.add(getChild(i)); 400 } 401 return lst; 402 } 403 404 public void collectNodes(Class clazz, ArrayList result) 405 { 406 if (this.getClass() == clazz) result.add(this); 407 408 for (int i = 0; i < getChildCount(); i++) 409 { 410 getChild(i).collectNodes(clazz, result); 411 } 412 } 413 414 public void collectVariables(DjScriptCompileTimeScope ctxt, HashMap variables, int stopAtLine) 415 throws DjScriptExecutionException 416 { 417 if (stopAtLine >= 0 && getLine() > stopAtLine) return; 418 for (int i = 0; i < getChildCount(); i++) 419 { 420 getChild(i).collectVariables(ctxt, variables, stopAtLine); 421 } 422 } 423 424 public void collectEvents(DjScriptCompileTimeScope ctxt, ArrayList ev) throws DjScriptExecutionException 425 { 426 for (int i = 0; i < getChildCount(); i++) 427 { 428 getChild(i).collectEvents(ctxt, ev); 429 } 430 } 431 432 protected boolean doCollectEvents(DjScriptCompileTimeScope ctxt, ArrayList ev) throws DjScriptExecutionException 433 { 434 ActionNode a = (ActionNode) getParent(ActionNode.class); 435 EventDefinitionNode events[] = (EventDefinitionNode[]) getChildren(EventDefinitionNode.class) 436 .toArray(new EventDefinitionNode[0]); 437 438 for (int i = 0; i < events.length; i++) 439 { 440 ev.add(a.getActionName() + "." + events[i].getEventName()); 441 } 442 return events.length != 0; 443 } 444 445 public SimpleNode getParent(Class clazz) 446 { 447 SimpleNode p = getParent(); 448 while (p != null && p.getClass() != clazz) 449 { 450 p = p.getParent(); 451 } 452 return p; 453 } 454 455 public int getColumn() 456 { 457 if (beginColumn != 0) return beginColumn; 458 if (getParent() != null) return getParent().getColumn(); 459 return 0; 460 } 461 462 public int getLine() 463 { 464 if (beginLine != 0) return beginLine; 465 if (getParent() != null) return getParent().getLine(); 466 return 0; 467 } 468 469 public String getNodeTitle() 470 { 471 return toString(); 472 } 473 474 public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException 475 { 476 for (int i = 0; i < getChildCount(); i++) 477 { 478 getChild(i).validateScript(ctxt); 479 } 480 } 481 482 public String getValidatedTypeName(DjScriptCompileTimeScope context) throws DjScriptExecutionException 483 { 484 return "[unknown for " + this.getClass().getName() + "]"; 485 } 486 487 } | Popular Tags |