1 2 3 package com.genimen.djeneric.repository.oql.core; 4 5 import java.math.BigDecimal ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 9 import com.genimen.djeneric.repository.DjObject; 10 import com.genimen.djeneric.repository.exceptions.DjenericException; 11 import com.genimen.djeneric.repository.oql.core.nodes.AndOrNode; 12 import com.genimen.djeneric.repository.oql.core.nodes.ArgumentsNode; 13 import com.genimen.djeneric.repository.oql.core.nodes.BoolNode; 14 import com.genimen.djeneric.repository.oql.core.nodes.BracketNode; 15 import com.genimen.djeneric.repository.oql.core.nodes.CalculatedExpressionNode; 16 import com.genimen.djeneric.repository.oql.core.nodes.CharNode; 17 import com.genimen.djeneric.repository.oql.core.nodes.ExpressionNode; 18 import com.genimen.djeneric.repository.oql.core.nodes.FloatNode; 19 import com.genimen.djeneric.repository.oql.core.nodes.IntegerNode; 20 import com.genimen.djeneric.repository.oql.core.nodes.MatchingContext; 21 import com.genimen.djeneric.repository.oql.core.nodes.NullNode; 22 import com.genimen.djeneric.repository.oql.core.nodes.OperatorNode; 23 import com.genimen.djeneric.repository.oql.core.nodes.ParameterNode; 24 import com.genimen.djeneric.repository.oql.core.nodes.PropertyNode; 25 import com.genimen.djeneric.repository.oql.core.nodes.PropertyOrFunctionNode; 26 import com.genimen.djeneric.repository.oql.core.nodes.StringNode; 27 import com.genimen.djeneric.repository.oql.core.nodes.SubExpressionNode; 28 import com.genimen.djeneric.util.DjLogger; 29 30 public class SimpleNode implements Node 31 { 32 protected Node parent; 33 protected Node[] children; 34 protected int id; 35 protected DjOqlParserEngine parser; 36 public int beginLine = 0, beginColumn = 0; 37 int bracketOpenCount = 0; 38 int bracketCloseCount = 0; 39 40 protected String name = null; 41 42 public SimpleNode(int i) 43 { 44 id = i; 45 } 46 47 public SimpleNode(DjOqlParserEngine p, int i) 48 { 49 this(i); 50 parser = p; 51 } 52 53 public String getName() 54 { 55 return "SimpleNode"; 56 } 57 58 public void setLineInfo(Token token) 59 { 60 beginLine = token.beginLine; 61 beginColumn = token.beginColumn; 62 } 63 64 public void setLineInfo(SimpleNode node) 65 { 66 beginLine = node.beginLine; 67 beginColumn = node.beginColumn; 68 } 69 70 public void setLineInfo(int line, int column) 71 { 72 beginLine = line; 73 beginColumn = column; 74 } 75 76 public static Node jjtCreate(int id) 77 { 78 SimpleNode newNode; 79 80 if (id == DjOqlParserEngineTreeConstants.JJTEXPRESSION) newNode = new ExpressionNode(id); 81 else if (id == DjOqlParserEngineTreeConstants.JJTPARAMETER) newNode = new ParameterNode(id); 82 else if (id == DjOqlParserEngineTreeConstants.JJTBRACKETOPEN) newNode = new BracketNode(id); 83 else if (id == DjOqlParserEngineTreeConstants.JJTBRACKETCLOSE) newNode = new BracketNode(id); 84 else if (id == DjOqlParserEngineTreeConstants.JJTSUBEXPRESSION) newNode = new SubExpressionNode(id); 85 else if (id == DjOqlParserEngineTreeConstants.JJTANDOR) newNode = new AndOrNode(id); 86 else if (id == DjOqlParserEngineTreeConstants.JJTCALCULATEDEXPRESSION) newNode = new CalculatedExpressionNode(id); 87 else if (id == DjOqlParserEngineTreeConstants.JJTOPERATOR) newNode = new OperatorNode(id); 88 else if (id == DjOqlParserEngineTreeConstants.JJTOBJECTPATH) newNode = new PropertyNode(id); 89 else if (id == DjOqlParserEngineTreeConstants.JJTPROPERTYORFUNCTION) newNode = new PropertyOrFunctionNode(id); 90 else if (id == DjOqlParserEngineTreeConstants.JJTFUNCTIONCALL) newNode = new PropertyOrFunctionNode(id); 91 else if (id == DjOqlParserEngineTreeConstants.JJTBOOLLITERAL) newNode = new BoolNode(id); 92 else if (id == DjOqlParserEngineTreeConstants.JJTCHARLITERAL) newNode = new CharNode(id); 93 else if (id == DjOqlParserEngineTreeConstants.JJTFLOATLITERAL) newNode = new FloatNode(id); 94 else if (id == DjOqlParserEngineTreeConstants.JJTARGUMENTS) newNode = new ArgumentsNode(id); 95 else if (id == DjOqlParserEngineTreeConstants.JJTINTEGERLITERAL) newNode = new IntegerNode(id); 96 else if (id == DjOqlParserEngineTreeConstants.JJTNULLLITERAL) newNode = new NullNode(id); 97 else if (id == DjOqlParserEngineTreeConstants.JJTSTRINGLITERAL) newNode = new StringNode(id); 98 else newNode = new SimpleNode(id); 99 100 return newNode; 101 } 102 103 public static Node jjtCreate(DjOqlParserEngine p, int id) 104 { 105 return new SimpleNode(p, id); 106 } 107 108 public void jjtOpen() 109 { 110 } 111 112 public void jjtClose() 113 { 114 } 115 116 public void setParent(SimpleNode n) 117 { 118 parent = n; 119 } 120 121 public void jjtSetParent(Node n) 122 { 123 parent = n; 124 } 125 126 public SimpleNode getParent() 127 { 128 return (SimpleNode) parent; 129 } 130 131 public Node jjtGetParent() 132 { 133 return parent; 134 } 135 136 public void addChild(SimpleNode n) 137 { 138 jjtAddChild(n); 139 n.setParent(this); 140 } 141 142 public void addChild(SimpleNode n, int i) 143 { 144 jjtAddChild(n, i); 145 n.setParent(this); 146 } 147 148 public void jjtAddChild(Node n) 149 { 150 int idx = 0; 151 if (children != null) idx = children.length; 152 jjtAddChild(n, idx); 153 } 154 155 public void jjtAddChild(Node n, int i) 156 { 157 if (children == null) 158 { 159 children = new Node[i + 1]; 160 } 161 else if (i >= children.length) 162 { 163 Node c[] = new Node[i + 1]; 164 System.arraycopy(children, 0, c, 0, children.length); 165 children = c; 166 } 167 children[i] = n; 168 } 169 170 public void replaceChild(int i, SimpleNode n) 171 { 172 children[i] = n; 173 n.setParent(this); 174 } 175 176 public void replaceChild(SimpleNode node, SimpleNode newNode) 177 { 178 children[indexOf(node)] = newNode; 179 newNode.setParent(this); 180 } 181 182 public int indexOf(SimpleNode node) 183 { 184 for (int i = 0; i < children.length; i++) 185 { 186 if (children[i] == node) return i; 187 } 188 return -1; 189 } 190 191 public SimpleNode getChild(int i) 192 { 193 return (SimpleNode) children[i]; 194 } 195 196 public Node jjtGetChild(int i) 197 { 198 return children[i]; 199 } 200 201 public void removeChild(int at) 202 { 203 jjtRemoveChild(at); 204 } 205 206 public void jjtRemoveChild(int at) 207 { 208 Node c[] = new Node[children.length - 1]; 209 int idx = 0; 210 for (int i = 0; i < children.length; i++) 211 { 212 if (i != at) c[idx++] = children[i]; 213 } 214 children = c; 215 } 216 217 public void removeChild(Node node) 218 { 219 jjtRemoveChild(node); 220 } 221 222 public void jjtRemoveChild(Node node) 223 { 224 int at = -1; 225 for (int i = 0; i < children.length; i++) 226 { 227 if (children[i] == node) 228 { 229 at = i; 230 break; 231 } 232 } 233 if (at == -1) return; 234 jjtRemoveChild(at); 235 } 236 237 public int getChildCount() 238 { 239 return jjtGetNumChildren(); 240 } 241 242 public int jjtGetNumChildren() 243 { 244 return (children == null) ? 0 : children.length; 245 } 246 247 254 public String toString() 255 { 256 if (name != null) return name; 257 return DjOqlParserEngineTreeConstants.jjtNodeName[id]; 258 } 259 260 public void setName(String nm) 261 { 262 name = nm; 263 } 264 265 public String toString(String prefix) 266 { 267 return prefix + toString(); 268 } 269 270 274 public void dump(String prefix) 275 { 276 DjLogger.log(toString(prefix)); 277 if (children != null) 278 { 279 for (int i = 0; i < children.length; ++i) 280 { 281 SimpleNode n = (SimpleNode) children[i]; 282 if (n != null) 283 { 284 n.dump(prefix + " "); 285 } 286 } 287 } 288 } 289 290 public String classOnly(Object o) 291 { 292 if (o == null) return "null"; 293 String full = o.getClass().getName(); 294 int idx = full.lastIndexOf("."); 295 if (idx == -1) return full; 296 return full.substring(idx + 1); 297 } 298 299 public SimpleNode getChild(Class clazz) 300 { 301 for (int i = 0; i < getChildCount(); i++) 302 { 303 if (getChild(i).getClass() == clazz) return getChild(i); 304 } 305 return null; 306 } 307 308 public ArrayList getChildren(Class clazz) 309 { 310 ArrayList lst = new ArrayList (); 311 312 for (int i = 0; i < getChildCount(); i++) 313 { 314 if (getChild(i).getClass() == clazz) lst.add(getChild(i)); 315 } 316 return lst; 317 } 318 319 public void collectNodes(Class clazz, ArrayList result) 320 { 321 if (this.getClass() == clazz) result.add(this); 322 323 for (int i = 0; i < getChildCount(); i++) 324 { 325 getChild(i).collectNodes(clazz, result); 326 } 327 } 328 329 public SimpleNode getParent(Class clazz) 330 { 331 SimpleNode p = getParent(); 332 while (p != null && p.getClass() != clazz) 333 { 334 p = p.getParent(); 335 } 336 return p; 337 } 338 339 public int getColumn() 340 { 341 return beginColumn; 342 } 343 344 public int getLine() 345 { 346 return beginLine; 347 } 348 349 public String getNodeTitle() 350 { 351 return toString(); 352 } 353 354 public void addPaths(ArrayList allPaths) 355 { 356 for (int i = 0; i < getChildCount(); i++) 357 { 358 getChild(i).addPaths(allPaths); 359 } 360 } 361 362 public void translate(StringBuffer result, HashMap path2AliasMapping) throws ParseException 363 { 364 for (int i = 0; i < getChildCount(); i++) 365 { 366 getChild(i).translate(result, path2AliasMapping); 367 } 368 } 369 370 public boolean shouldUseBrackets() 371 { 372 boolean result = true; 373 374 for (int i = 0; i < getChildCount() && result; i++) 375 { 376 result &= getChild(i).shouldUseBrackets(); 377 378 } 379 return result; 380 } 381 382 public void collectParameters(ArrayList parameters) 383 { 384 for (int i = 0; i < getChildCount(); i++) 385 { 386 getChild(i).collectParameters(parameters); 387 } 388 } 389 390 public BigDecimal toBigDecimal(Object o) 391 { 392 if (o instanceof BigDecimal ) return (BigDecimal ) o; 393 return new BigDecimal (o.toString()); 394 } 395 396 public Float toFloat(Object o) 397 { 398 if (o instanceof Float ) return (Float ) o; 399 return new Float (o.toString()); 400 } 401 402 public Long toLong(Object o) throws MatchException 403 { 404 if (o instanceof DjObject) 405 { 406 DjObject djo = (DjObject) o; 407 try 408 { 409 return new Long (djo.getObjectId()); 410 } 411 catch (DjenericException e) 412 { 413 DjLogger.log(e); 414 throw new MatchException(e.getMessage()); 415 } 416 } 417 if (o instanceof Long ) return (Long ) o; 418 if (o instanceof Float ) return new Long (((Float ) o).longValue()); 419 420 return new Long (o.toString()); 422 } 423 424 public boolean match(MatchingContext context) throws MatchException 425 { 426 boolean result = true; 427 for (int i = 0; i < getChildCount() && result; i++) 428 { 429 result &= getChild(i).match(context); 430 } 431 return result; 432 } 433 434 public int getBracketCloseCount() 435 { 436 return bracketCloseCount; 437 } 438 439 public int getBracketOpenCount() 440 { 441 return bracketOpenCount; 442 } 443 444 public void setBracketCloseCount(int i) 445 { 446 bracketCloseCount = i; 447 } 448 449 public void setBracketOpenCount(int i) 450 { 451 bracketOpenCount = i; 452 } 453 454 public void increaseOpenBracket(int delta) 455 { 456 bracketOpenCount += delta; 457 } 458 459 public void increaseCloseBracket(int delta) 460 { 461 bracketCloseCount += delta; 462 } 463 464 public void appendOpenBrackets(StringBuffer sb) 465 { 466 for (int i = 0; i < bracketOpenCount; i++) 467 sb.append("("); 468 } 469 470 public void appendCloseBrackets(StringBuffer sb) 471 { 472 for (int i = 0; i < bracketOpenCount; i++) 473 sb.append(")"); 474 } 475 } | Popular Tags |