1 2 package com.openinventions.webappfilter.processor.xpathexpression; 3 4 import com.openinventions.metaframework.*; 5 import java.io.*; 6 import org.apache.commons.logging.*; 7 8 public class XPathExpressionParser implements XPathExpressionParserConstants { 9 private static final Log log = LogFactory.getLog(XPathExpressionParser.class); 10 private State state = null; 11 12 public Object parse(State state) throws Exception { 13 this.state = state; 14 return parseLine(); 15 } 16 17 public double getDouble(Object o) throws Exception { 18 if (o instanceof String ) { 19 String s = (String ) o; 20 if (s.equals("")) { 21 return 0.0; 22 } 23 return Double.parseDouble(s); 24 } else if (o instanceof Long ) { 25 return ((double) ((Long ) o).longValue()); 26 } else if (o instanceof Double ) { 27 return ((Double ) o).doubleValue(); 28 } else { 29 log.error("wasn't expecting class " + o.getClass().getName()); 30 } 31 32 return 0.0; 33 } 34 35 final public Object parseLine() throws ParseException, Exception { 36 Object b; 37 b = expr(); 38 {if (true) return b;} 39 throw new Error ("Missing return statement in function"); 40 } 41 42 final public Object expr() throws ParseException, Exception { 43 Object a = null; 44 Object b = null; 45 a = element(); 46 label_1: 47 while (true) { 48 if (jj_2_1(2)) { 49 ; 50 } else { 51 break label_1; 52 } 53 if (jj_2_2(2)) { 54 jj_consume_token(10); 55 b = expr(); 56 if ((a instanceof Long ) && (b instanceof Long )) { 57 {if (true) return new Long (((Long ) a).longValue() + ((Long ) b).longValue());} 58 } if ((a instanceof Double ) && (b instanceof Double )) { 59 {if (true) return new Double (((Double ) a).doubleValue() + ((Double ) b).doubleValue());} 60 } if ((a instanceof String ) && (b instanceof Long )) { 61 String s = (String ) a; 62 if (s.equals("")) { 63 s = "0"; 64 } 65 {if (true) return new Long (Long.parseLong(s) + ((Long ) b).longValue());} 66 } if ((a instanceof String ) && (b instanceof Double )) { 67 String s = (String ) a; 68 if (s.equals("")) { 69 s = "0.0"; 70 } 71 {if (true) return new Double (Double.parseDouble(s) + ((Double ) b).doubleValue());} 72 } else if ((a instanceof Element) && (b instanceof String )) { 73 Element element = (Element) a; 74 String path = (String ) b; 75 if (!element.isExists(path)) { 76 {if (true) return null;} 77 } else if (element.isElement(path)) { 78 {if (true) return element.getElement(path);} 79 } else { 80 {if (true) return element.getValue(path);} 81 } 82 } else if ((a instanceof String ) && (b instanceof String )) { 83 {if (true) return ((String ) a + (String ) b);} 84 } else { 85 log.error("combination not accepted " + a + "-" + a.getClass().getName() + "-" + b + "-" + b.getClass().getName()); 86 } 87 } else if (jj_2_3(2)) { 88 jj_consume_token(11); 89 b = expr(); 90 if ((a instanceof String ) && (b instanceof Long )) { 91 String s = (String ) a; 92 if (s.equals("")) { 93 s = "0"; 94 } 95 {if (true) return new Long (Long.parseLong(s) - ((Long ) b).longValue());} 96 } if ((a instanceof String ) && (b instanceof Double )) { 97 String s = (String ) a; 98 if (s.equals("")) { 99 s = "0.0"; 100 } 101 {if (true) return new Double (Double.parseDouble(s) - ((Double ) b).doubleValue());} 102 } else { 103 log.error("combination not accepted " + a + "-" + a.getClass().getName() + "-" + b + "-" + b.getClass().getName()); 104 } 105 } else if (jj_2_4(2)) { 106 jj_consume_token(12); 107 b = expr(); 108 {if (true) return new Boolean (getDouble(a) < getDouble(b));} 109 } else if (jj_2_5(2)) { 110 jj_consume_token(13); 111 b = expr(); 112 {if (true) return new Boolean (getDouble(a) <= getDouble(b));} 113 } else if (jj_2_6(2)) { 114 jj_consume_token(14); 115 b = expr(); 116 {if (true) return new Boolean (getDouble(a) > getDouble(b));} 117 } else if (jj_2_7(2)) { 118 jj_consume_token(15); 119 b = expr(); 120 {if (true) return new Boolean (getDouble(a) >= getDouble(b));} 121 } else if (jj_2_8(2)) { 122 jj_consume_token(16); 123 b = expr(); 124 {if (true) return new Boolean (getDouble(a) == getDouble(b));} 125 } else { 126 jj_consume_token(-1); 127 throw new ParseException(); 128 } 129 } 130 {if (true) return a;} 131 throw new Error ("Missing return statement in function"); 132 } 133 134 final public Object element() throws ParseException, Exception { 135 Token t; 136 Object a; 137 if (jj_2_9(2)) { 138 t = jj_consume_token(LONG); 139 {if (true) return new Long (t.toString());} 140 } else if (jj_2_10(2)) { 141 t = jj_consume_token(DOUBLE); 142 {if (true) return new Double (t.toString());} 143 } else if (jj_2_11(2)) { 144 t = jj_consume_token(STRING); 145 String subject = t.toString().trim(); 146 if (subject.length() > 2) { 147 if (subject.startsWith("\"\"")) { 148 subject = subject.substring(2, subject.length() - 2); 149 } else { 150 subject = subject.substring(1, subject.length() - 1); 151 } 152 } else { 153 subject = ""; 154 } 155 {if (true) return subject;} 156 } else if (jj_2_12(2)) { 157 t = jj_consume_token(ELEMENT); 158 String path = t.toString(); 159 String [] pathTokens = t.toString().split("/"); 160 if ((pathTokens != null) && (pathTokens.length > 2)) { 161 Element element = (Element) state.get(pathTokens[1]); 162 String subPath = path.substring(path.indexOf("/", 1), path.length()); 163 if (element.isElement(subPath)) { 164 {if (true) return element.getElement(subPath);} 165 } else { 166 {if (true) return element.getValue(subPath);} 167 } 168 } 169 {if (true) return state.get(pathTokens[1]);} 170 } else if (jj_2_13(2)) { 171 jj_consume_token(17); 172 a = expr(); 173 jj_consume_token(18); 174 {if (true) return a;} 175 } else { 176 jj_consume_token(-1); 177 throw new ParseException(); 178 } 179 throw new Error ("Missing return statement in function"); 180 } 181 182 final private boolean jj_2_1(int xla) { 183 jj_la = xla; jj_lastpos = jj_scanpos = token; 184 try { return !jj_3_1(); } 185 catch(LookaheadSuccess ls) { return true; } 186 finally { jj_save(0, xla); } 187 } 188 189 final private boolean jj_2_2(int xla) { 190 jj_la = xla; jj_lastpos = jj_scanpos = token; 191 try { return !jj_3_2(); } 192 catch(LookaheadSuccess ls) { return true; } 193 finally { jj_save(1, xla); } 194 } 195 196 final private boolean jj_2_3(int xla) { 197 jj_la = xla; jj_lastpos = jj_scanpos = token; 198 try { return !jj_3_3(); } 199 catch(LookaheadSuccess ls) { return true; } 200 finally { jj_save(2, xla); } 201 } 202 203 final private boolean jj_2_4(int xla) { 204 jj_la = xla; jj_lastpos = jj_scanpos = token; 205 try { return !jj_3_4(); } 206 catch(LookaheadSuccess ls) { return true; } 207 finally { jj_save(3, xla); } 208 } 209 210 final private boolean jj_2_5(int xla) { 211 jj_la = xla; jj_lastpos = jj_scanpos = token; 212 try { return !jj_3_5(); } 213 catch(LookaheadSuccess ls) { return true; } 214 finally { jj_save(4, xla); } 215 } 216 217 final private boolean jj_2_6(int xla) { 218 jj_la = xla; jj_lastpos = jj_scanpos = token; 219 try { return !jj_3_6(); } 220 catch(LookaheadSuccess ls) { return true; } 221 finally { jj_save(5, xla); } 222 } 223 224 final private boolean jj_2_7(int xla) { 225 jj_la = xla; jj_lastpos = jj_scanpos = token; 226 try { return !jj_3_7(); } 227 catch(LookaheadSuccess ls) { return true; } 228 finally { jj_save(6, xla); } 229 } 230 231 final private boolean jj_2_8(int xla) { 232 jj_la = xla; jj_lastpos = jj_scanpos = token; 233 try { return !jj_3_8(); } 234 catch(LookaheadSuccess ls) { return true; } 235 finally { jj_save(7, xla); } 236 } 237 238 final private boolean jj_2_9(int xla) { 239 jj_la = xla; jj_lastpos = jj_scanpos = token; 240 try { return !jj_3_9(); } 241 catch(LookaheadSuccess ls) { return true; } 242 finally { jj_save(8, xla); } 243 } 244 245 final private boolean jj_2_10(int xla) { 246 jj_la = xla; jj_lastpos = jj_scanpos = token; 247 try { return !jj_3_10(); } 248 catch(LookaheadSuccess ls) { return true; } 249 finally { jj_save(9, xla); } 250 } 251 252 final private boolean jj_2_11(int xla) { 253 jj_la = xla; jj_lastpos = jj_scanpos = token; 254 try { return !jj_3_11(); } 255 catch(LookaheadSuccess ls) { return true; } 256 finally { jj_save(10, xla); } 257 } 258 259 final private boolean jj_2_12(int xla) { 260 jj_la = xla; jj_lastpos = jj_scanpos = token; 261 try { return !jj_3_12(); } 262 catch(LookaheadSuccess ls) { return true; } 263 finally { jj_save(11, xla); } 264 } 265 266 final private boolean jj_2_13(int xla) { 267 jj_la = xla; jj_lastpos = jj_scanpos = token; 268 try { return !jj_3_13(); } 269 catch(LookaheadSuccess ls) { return true; } 270 finally { jj_save(12, xla); } 271 } 272 273 final private boolean jj_3_13() { 274 if (jj_scan_token(17)) return true; 275 if (jj_3R_2()) return true; 276 return false; 277 } 278 279 final private boolean jj_3_5() { 280 if (jj_scan_token(13)) return true; 281 if (jj_3R_2()) return true; 282 return false; 283 } 284 285 final private boolean jj_3_11() { 286 if (jj_scan_token(STRING)) return true; 287 return false; 288 } 289 290 final private boolean jj_3_4() { 291 if (jj_scan_token(12)) return true; 292 if (jj_3R_2()) return true; 293 return false; 294 } 295 296 final private boolean jj_3_10() { 297 if (jj_scan_token(DOUBLE)) return true; 298 return false; 299 } 300 301 final private boolean jj_3R_3() { 302 Token xsp; 303 xsp = jj_scanpos; 304 if (jj_3_9()) { 305 jj_scanpos = xsp; 306 if (jj_3_10()) { 307 jj_scanpos = xsp; 308 if (jj_3_11()) { 309 jj_scanpos = xsp; 310 if (jj_3_12()) { 311 jj_scanpos = xsp; 312 if (jj_3_13()) return true; 313 } 314 } 315 } 316 } 317 return false; 318 } 319 320 final private boolean jj_3_9() { 321 if (jj_scan_token(LONG)) return true; 322 return false; 323 } 324 325 final private boolean jj_3_1() { 326 Token xsp; 327 xsp = jj_scanpos; 328 if (jj_3_2()) { 329 jj_scanpos = xsp; 330 if (jj_3_3()) { 331 jj_scanpos = xsp; 332 if (jj_3_4()) { 333 jj_scanpos = xsp; 334 if (jj_3_5()) { 335 jj_scanpos = xsp; 336 if (jj_3_6()) { 337 jj_scanpos = xsp; 338 if (jj_3_7()) { 339 jj_scanpos = xsp; 340 if (jj_3_8()) return true; 341 } 342 } 343 } 344 } 345 } 346 } 347 return false; 348 } 349 350 final private boolean jj_3_2() { 351 if (jj_scan_token(10)) return true; 352 if (jj_3R_2()) return true; 353 return false; 354 } 355 356 final private boolean jj_3_12() { 357 if (jj_scan_token(ELEMENT)) return true; 358 return false; 359 } 360 361 final private boolean jj_3R_2() { 362 if (jj_3R_3()) return true; 363 return false; 364 } 365 366 final private boolean jj_3_8() { 367 if (jj_scan_token(16)) return true; 368 if (jj_3R_2()) return true; 369 return false; 370 } 371 372 final private boolean jj_3_7() { 373 if (jj_scan_token(15)) return true; 374 if (jj_3R_2()) return true; 375 return false; 376 } 377 378 final private boolean jj_3_3() { 379 if (jj_scan_token(11)) return true; 380 if (jj_3R_2()) return true; 381 return false; 382 } 383 384 final private boolean jj_3_6() { 385 if (jj_scan_token(14)) return true; 386 if (jj_3R_2()) return true; 387 return false; 388 } 389 390 public XPathExpressionParserTokenManager token_source; 391 SimpleCharStream jj_input_stream; 392 public Token token, jj_nt; 393 private int jj_ntk; 394 private Token jj_scanpos, jj_lastpos; 395 private int jj_la; 396 public boolean lookingAhead = false; 397 private boolean jj_semLA; 398 private int jj_gen; 399 final private int[] jj_la1 = new int[0]; 400 static private int[] jj_la1_0; 401 static { 402 jj_la1_0(); 403 } 404 private static void jj_la1_0() { 405 jj_la1_0 = new int[] {}; 406 } 407 final private JJCalls[] jj_2_rtns = new JJCalls[13]; 408 private boolean jj_rescan = false; 409 private int jj_gc = 0; 410 411 public XPathExpressionParser(java.io.InputStream stream) { 412 jj_input_stream = new SimpleCharStream(stream, 1, 1); 413 token_source = new XPathExpressionParserTokenManager(jj_input_stream); 414 token = new Token(); 415 jj_ntk = -1; 416 jj_gen = 0; 417 for (int i = 0; i < 0; i++) jj_la1[i] = -1; 418 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); 419 } 420 421 public void ReInit(java.io.InputStream stream) { 422 jj_input_stream.ReInit(stream, 1, 1); 423 token_source.ReInit(jj_input_stream); 424 token = new Token(); 425 jj_ntk = -1; 426 jj_gen = 0; 427 for (int i = 0; i < 0; i++) jj_la1[i] = -1; 428 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); 429 } 430 431 public XPathExpressionParser(java.io.Reader stream) { 432 jj_input_stream = new SimpleCharStream(stream, 1, 1); 433 token_source = new XPathExpressionParserTokenManager(jj_input_stream); 434 token = new Token(); 435 jj_ntk = -1; 436 jj_gen = 0; 437 for (int i = 0; i < 0; i++) jj_la1[i] = -1; 438 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); 439 } 440 441 public void ReInit(java.io.Reader stream) { 442 jj_input_stream.ReInit(stream, 1, 1); 443 token_source.ReInit(jj_input_stream); 444 token = new Token(); 445 jj_ntk = -1; 446 jj_gen = 0; 447 for (int i = 0; i < 0; i++) jj_la1[i] = -1; 448 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); 449 } 450 451 public XPathExpressionParser(XPathExpressionParserTokenManager tm) { 452 token_source = tm; 453 token = new Token(); 454 jj_ntk = -1; 455 jj_gen = 0; 456 for (int i = 0; i < 0; i++) jj_la1[i] = -1; 457 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); 458 } 459 460 public void ReInit(XPathExpressionParserTokenManager tm) { 461 token_source = tm; 462 token = new Token(); 463 jj_ntk = -1; 464 jj_gen = 0; 465 for (int i = 0; i < 0; i++) jj_la1[i] = -1; 466 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); 467 } 468 469 final private Token jj_consume_token(int kind) throws ParseException { 470 Token oldToken; 471 if ((oldToken = token).next != null) token = token.next; 472 else token = token.next = token_source.getNextToken(); 473 jj_ntk = -1; 474 if (token.kind == kind) { 475 jj_gen++; 476 if (++jj_gc > 100) { 477 jj_gc = 0; 478 for (int i = 0; i < jj_2_rtns.length; i++) { 479 JJCalls c = jj_2_rtns[i]; 480 while (c != null) { 481 if (c.gen < jj_gen) c.first = null; 482 c = c.next; 483 } 484 } 485 } 486 return token; 487 } 488 token = oldToken; 489 jj_kind = kind; 490 throw generateParseException(); 491 } 492 493 static private final class LookaheadSuccess extends java.lang.Error { } 494 final private LookaheadSuccess jj_ls = new LookaheadSuccess(); 495 final private boolean jj_scan_token(int kind) { 496 if (jj_scanpos == jj_lastpos) { 497 jj_la--; 498 if (jj_scanpos.next == null) { 499 jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken(); 500 } else { 501 jj_lastpos = jj_scanpos = jj_scanpos.next; 502 } 503 } else { 504 jj_scanpos = jj_scanpos.next; 505 } 506 if (jj_rescan) { 507 int i = 0; Token tok = token; 508 while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; } 509 if (tok != null) jj_add_error_token(kind, i); 510 } 511 if (jj_scanpos.kind != kind) return true; 512 if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls; 513 return false; 514 } 515 516 final public Token getNextToken() { 517 if (token.next != null) token = token.next; 518 else token = token.next = token_source.getNextToken(); 519 jj_ntk = -1; 520 jj_gen++; 521 return token; 522 } 523 524 final public Token getToken(int index) { 525 Token t = lookingAhead ? jj_scanpos : token; 526 for (int i = 0; i < index; i++) { 527 if (t.next != null) t = t.next; 528 else t = t.next = token_source.getNextToken(); 529 } 530 return t; 531 } 532 533 final private int jj_ntk() { 534 if ((jj_nt=token.next) == null) 535 return (jj_ntk = (token.next=token_source.getNextToken()).kind); 536 else 537 return (jj_ntk = jj_nt.kind); 538 } 539 540 private java.util.Vector jj_expentries = new java.util.Vector (); 541 private int[] jj_expentry; 542 private int jj_kind = -1; 543 private int[] jj_lasttokens = new int[100]; 544 private int jj_endpos; 545 546 private void jj_add_error_token(int kind, int pos) { 547 if (pos >= 100) return; 548 if (pos == jj_endpos + 1) { 549 jj_lasttokens[jj_endpos++] = kind; 550 } else if (jj_endpos != 0) { 551 jj_expentry = new int[jj_endpos]; 552 for (int i = 0; i < jj_endpos; i++) { 553 jj_expentry[i] = jj_lasttokens[i]; 554 } 555 boolean exists = false; 556 for (java.util.Enumeration e = jj_expentries.elements(); e.hasMoreElements();) { 557 int[] oldentry = (int[])(e.nextElement()); 558 if (oldentry.length == jj_expentry.length) { 559 exists = true; 560 for (int i = 0; i < jj_expentry.length; i++) { 561 if (oldentry[i] != jj_expentry[i]) { 562 exists = false; 563 break; 564 } 565 } 566 if (exists) break; 567 } 568 } 569 if (!exists) jj_expentries.addElement(jj_expentry); 570 if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind; 571 } 572 } 573 574 public ParseException generateParseException() { 575 jj_expentries.removeAllElements(); 576 boolean[] la1tokens = new boolean[19]; 577 for (int i = 0; i < 19; i++) { 578 la1tokens[i] = false; 579 } 580 if (jj_kind >= 0) { 581 la1tokens[jj_kind] = true; 582 jj_kind = -1; 583 } 584 for (int i = 0; i < 0; i++) { 585 if (jj_la1[i] == jj_gen) { 586 for (int j = 0; j < 32; j++) { 587 if ((jj_la1_0[i] & (1<<j)) != 0) { 588 la1tokens[j] = true; 589 } 590 } 591 } 592 } 593 for (int i = 0; i < 19; i++) { 594 if (la1tokens[i]) { 595 jj_expentry = new int[1]; 596 jj_expentry[0] = i; 597 jj_expentries.addElement(jj_expentry); 598 } 599 } 600 jj_endpos = 0; 601 jj_rescan_token(); 602 jj_add_error_token(0, 0); 603 int[][] exptokseq = new int[jj_expentries.size()][]; 604 for (int i = 0; i < jj_expentries.size(); i++) { 605 exptokseq[i] = (int[])jj_expentries.elementAt(i); 606 } 607 return new ParseException(token, exptokseq, tokenImage); 608 } 609 610 final public void enable_tracing() { 611 } 612 613 final public void disable_tracing() { 614 } 615 616 final private void jj_rescan_token() { 617 jj_rescan = true; 618 for (int i = 0; i < 13; i++) { 619 JJCalls p = jj_2_rtns[i]; 620 do { 621 if (p.gen > jj_gen) { 622 jj_la = p.arg; jj_lastpos = jj_scanpos = p.first; 623 switch (i) { 624 case 0: jj_3_1(); break; 625 case 1: jj_3_2(); break; 626 case 2: jj_3_3(); break; 627 case 3: jj_3_4(); break; 628 case 4: jj_3_5(); break; 629 case 5: jj_3_6(); break; 630 case 6: jj_3_7(); break; 631 case 7: jj_3_8(); break; 632 case 8: jj_3_9(); break; 633 case 9: jj_3_10(); break; 634 case 10: jj_3_11(); break; 635 case 11: jj_3_12(); break; 636 case 12: jj_3_13(); break; 637 } 638 } 639 p = p.next; 640 } while (p != null); 641 } 642 jj_rescan = false; 643 } 644 645 final private void jj_save(int index, int xla) { 646 JJCalls p = jj_2_rtns[index]; 647 while (p.gen > jj_gen) { 648 if (p.next == null) { p = p.next = new JJCalls(); break; } 649 p = p.next; 650 } 651 p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla; 652 } 653 654 static final class JJCalls { 655 int gen; 656 Token first; 657 int arg; 658 JJCalls next; 659 } 660 661 } 662 | Popular Tags |