1 16 package org.apache.cocoon.components.xpointer.parser; 17 18 import java.util.HashMap ; 19 20 import org.apache.cocoon.components.xpointer.ElementPathPart; 21 import org.apache.cocoon.components.xpointer.ShorthandPart; 22 import org.apache.cocoon.components.xpointer.UnsupportedPart; 23 import org.apache.cocoon.components.xpointer.XPointer; 24 import org.apache.cocoon.components.xpointer.XPointerPart; 25 import org.apache.cocoon.components.xpointer.XmlnsPart; 26 27 public class XPointerFrameworkParser 28 implements XPointerFrameworkParserConstants { 29 private XPointer xpointer = new XPointer(); 30 private HashMap namespaces = new HashMap (); 31 32 public static void main(String [] args) throws Exception { 33 System.out.println("will parse this: " + args[0]); 34 XPointerFrameworkParser xfp = 35 new XPointerFrameworkParser(new java.io.StringReader (args[0])); 36 xfp.pointer(); 37 } 38 39 public static XPointer parse(String xpointer) throws ParseException { 40 XPointerFrameworkParser xfp = 41 new XPointerFrameworkParser(new java.io.StringReader (xpointer)); 42 try { 43 xfp.pointer(); 44 } catch (TokenMgrError e) { 45 throw new ParseException(e.getMessage()); 48 } 49 return xfp.getXPointer(); 50 } 51 52 public XPointer getXPointer() { 53 return xpointer; 54 } 55 56 private String unescape(String data) throws ParseException { 57 StringBuffer result = new StringBuffer (data.length()); 58 boolean inCircumflex = false; 59 for (int i = 0; i < data.length(); i++) { 60 char c = data.charAt(i); 61 if (inCircumflex) { 62 switch (c) { 63 case '^' : 64 case '(' : 65 case ')' : 66 result.append(c); 67 inCircumflex = false; 68 break; 69 default : 70 throw new ParseException( 71 "Incorrect use of circumflex character at position " 72 + i 73 + " in the string " 74 + data); 75 } 76 } else if (c == '^') { 77 inCircumflex = true; 78 } else { 79 result.append(c); 80 } 81 } 82 return result.toString(); 83 } 84 85 final public void pointer() throws ParseException { 86 if (jj_2_1(2)) { 87 schemeBased(); 88 } else { 89 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { 90 case NCName : 91 shortHand(); 92 break; 93 default : 94 jj_la1[0] = jj_gen; 95 jj_consume_token(-1); 96 throw new ParseException(); 97 } 98 } 99 } 100 101 final public void shortHand() throws ParseException { 102 Token x; 103 x = jj_consume_token(NCName); 104 xpointer.addPart(new ShorthandPart(x.image)); 105 } 106 107 final public void schemeBased() throws ParseException { 108 pointerPart(); 109 label_1 : while (true) { 110 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { 111 case NCName : 112 case WS : 113 case QName : 114 break; 115 default : 116 jj_la1[1] = jj_gen; 117 break label_1; 118 } 119 label_2 : while (true) { 120 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { 121 case WS : 122 break; 123 default : 124 jj_la1[2] = jj_gen; 125 break label_2; 126 } 127 jj_consume_token(WS); 128 } 129 pointerPart(); 130 } 131 } 132 133 final public void pointerPart() throws ParseException { 134 Token x; 135 String schemeName; 136 String schemeData; 137 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { 138 case NCName : 139 x = jj_consume_token(NCName); 140 break; 141 case QName : 142 x = jj_consume_token(QName); 143 break; 144 default : 145 jj_la1[3] = jj_gen; 146 jj_consume_token(-1); 147 throw new ParseException(); 148 } 149 jj_consume_token(LBRACE); 150 token_source.switchTo(IN_SCHEME); 152 153 schemeName = x.image; 155 schemeData = schemeData(); 156 jj_consume_token(RBRACE); 157 token_source.switchTo(DEFAULT); 159 160 String schemeNamespace = null, schemeLocalName = null; 162 int colonPos = schemeName.indexOf(':'); 163 if (colonPos != -1) { 164 String schemePrefix = schemeName.substring(0, colonPos); 165 schemeNamespace = (String ) namespaces.get(schemePrefix); 166 schemeLocalName = schemeName.substring(colonPos + 1); 167 } else { 168 schemeLocalName = schemeName; 169 } 170 171 if (schemeNamespace == null && schemeLocalName.equals("xmlns")) { 173 int eqPos = schemeData.indexOf("="); 174 if (eqPos == -1) { 175 if (true) 176 throw new ParseException("xmlns scheme data should contain an equals sign"); 177 } 178 179 String prefix = schemeData.substring(0, eqPos).trim(); 182 String namespace = 183 schemeData.substring(eqPos + 1, schemeData.length()).trim(); 184 xpointer.addPart(new XmlnsPart(prefix, namespace)); 185 namespaces.put(prefix, namespace); 186 } else if ( 187 schemeNamespace == null && schemeLocalName.equals("xpointer")) { 188 xpointer.addPart(new XPointerPart(schemeData)); 189 } else if ( 190 "http://apache.org/cocoon/xpointer".equals(schemeNamespace) 191 && schemeLocalName.equals("elementpath")) { 192 xpointer.addPart(new ElementPathPart(schemeData)); 193 } else { 194 xpointer.addPart(new UnsupportedPart(schemeName)); 195 } 196 } 197 198 final public String schemeData() throws ParseException { 199 String temp; 200 StringBuffer schemeData = new StringBuffer (); 201 label_3 : while (true) { 202 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { 203 case LBRACE : 204 case CIRC_LBRACE : 205 case CIRC_RBRACE : 206 case DOUBLE_CIRC : 207 case NormalChar : 208 break; 209 default : 210 jj_la1[4] = jj_gen; 211 break label_3; 212 } 213 temp = escapedData(); 214 schemeData.append(temp); 215 } 216 { 217 if (true) 218 return unescape(schemeData.toString()); 219 } 220 throw new Error ("Missing return statement in function"); 221 } 222 223 final public String escapedData() throws ParseException { 224 Token x; 225 String temp; 226 StringBuffer data = new StringBuffer (); 227 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { 228 case NormalChar : 229 x = jj_consume_token(NormalChar); 230 data.append(x.image); 231 break; 232 case CIRC_LBRACE : 233 x = jj_consume_token(CIRC_LBRACE); 234 data.append(x.image); 235 break; 236 case CIRC_RBRACE : 237 x = jj_consume_token(CIRC_RBRACE); 238 data.append(x.image); 239 break; 240 case DOUBLE_CIRC : 241 x = jj_consume_token(DOUBLE_CIRC); 242 data.append(x.image); 243 break; 244 case LBRACE : 245 x = jj_consume_token(LBRACE); 246 data.append(x.image); 247 temp = schemeData(); 248 data.append(temp); 249 x = jj_consume_token(RBRACE); 250 data.append(x.image); 251 break; 252 default : 253 jj_la1[5] = jj_gen; 254 jj_consume_token(-1); 255 throw new ParseException(); 256 } 257 { 258 if (true) 259 return data.toString(); 260 } 261 throw new Error ("Missing return statement in function"); 262 } 263 264 final private boolean jj_2_1(int xla) { 265 jj_la = xla; 266 jj_lastpos = jj_scanpos = token; 267 boolean retval = !jj_3_1(); 268 jj_save(0, xla); 269 return retval; 270 } 271 272 final private boolean jj_3R_6() { 273 if (jj_scan_token(NCName)) 274 return true; 275 if (jj_la == 0 && jj_scanpos == jj_lastpos) 276 return false; 277 return false; 278 } 279 280 final private boolean jj_3R_4() { 281 if (jj_3R_5()) 282 return true; 283 if (jj_la == 0 && jj_scanpos == jj_lastpos) 284 return false; 285 return false; 286 } 287 288 final private boolean jj_3R_5() { 289 Token xsp; 290 xsp = jj_scanpos; 291 if (jj_3R_6()) { 292 jj_scanpos = xsp; 293 if (jj_3R_7()) 294 return true; 295 if (jj_la == 0 && jj_scanpos == jj_lastpos) 296 return false; 297 } else if (jj_la == 0 && jj_scanpos == jj_lastpos) 298 return false; 299 if (jj_scan_token(LBRACE)) 300 return true; 301 if (jj_la == 0 && jj_scanpos == jj_lastpos) 302 return false; 303 return false; 304 } 305 306 final private boolean jj_3R_7() { 307 if (jj_scan_token(QName)) 308 return true; 309 if (jj_la == 0 && jj_scanpos == jj_lastpos) 310 return false; 311 return false; 312 } 313 314 final private boolean jj_3_1() { 315 if (jj_3R_4()) 316 return true; 317 if (jj_la == 0 && jj_scanpos == jj_lastpos) 318 return false; 319 return false; 320 } 321 322 public XPointerFrameworkParserTokenManager token_source; 323 SimpleCharStream jj_input_stream; 324 public Token token, jj_nt; 325 private int jj_ntk; 326 private Token jj_scanpos, jj_lastpos; 327 private int jj_la; 328 public boolean lookingAhead = false; 329 private int jj_gen; 330 final private int[] jj_la1 = new int[6]; 331 static private int[] jj_la1_0; 332 static { 333 jj_la1_0(); 334 } 335 private static void jj_la1_0() { 336 jj_la1_0 = new int[] { 0x80, 0x380, 0x100, 0x280, 0xf400, 0xf400, }; 337 } 338 final private JJCalls[] jj_2_rtns = new JJCalls[1]; 339 private boolean jj_rescan = false; 340 private int jj_gc = 0; 341 342 public XPointerFrameworkParser(java.io.InputStream stream) { 343 jj_input_stream = new SimpleCharStream(stream, 1, 1); 344 token_source = new XPointerFrameworkParserTokenManager(jj_input_stream); 345 token = new Token(); 346 jj_ntk = -1; 347 jj_gen = 0; 348 for (int i = 0; i < 6; i++) 349 jj_la1[i] = -1; 350 for (int i = 0; i < jj_2_rtns.length; i++) 351 jj_2_rtns[i] = new JJCalls(); 352 } 353 354 public void reInit(java.io.InputStream stream) { 355 jj_input_stream.reInit(stream, 1, 1); 356 token_source.reInit(jj_input_stream); 357 token = new Token(); 358 jj_ntk = -1; 359 jj_gen = 0; 360 for (int i = 0; i < 6; i++) 361 jj_la1[i] = -1; 362 for (int i = 0; i < jj_2_rtns.length; i++) 363 jj_2_rtns[i] = new JJCalls(); 364 } 365 366 public XPointerFrameworkParser(java.io.Reader stream) { 367 jj_input_stream = new SimpleCharStream(stream, 1, 1); 368 token_source = new XPointerFrameworkParserTokenManager(jj_input_stream); 369 token = new Token(); 370 jj_ntk = -1; 371 jj_gen = 0; 372 for (int i = 0; i < 6; i++) 373 jj_la1[i] = -1; 374 for (int i = 0; i < jj_2_rtns.length; i++) 375 jj_2_rtns[i] = new JJCalls(); 376 } 377 378 public void reInit(java.io.Reader stream) { 379 jj_input_stream.reInit(stream, 1, 1); 380 token_source.reInit(jj_input_stream); 381 token = new Token(); 382 jj_ntk = -1; 383 jj_gen = 0; 384 for (int i = 0; i < 6; i++) 385 jj_la1[i] = -1; 386 for (int i = 0; i < jj_2_rtns.length; i++) 387 jj_2_rtns[i] = new JJCalls(); 388 } 389 390 public XPointerFrameworkParser(XPointerFrameworkParserTokenManager tm) { 391 token_source = tm; 392 token = new Token(); 393 jj_ntk = -1; 394 jj_gen = 0; 395 for (int i = 0; i < 6; i++) 396 jj_la1[i] = -1; 397 for (int i = 0; i < jj_2_rtns.length; i++) 398 jj_2_rtns[i] = new JJCalls(); 399 } 400 401 public void reInit(XPointerFrameworkParserTokenManager tm) { 402 token_source = tm; 403 token = new Token(); 404 jj_ntk = -1; 405 jj_gen = 0; 406 for (int i = 0; i < 6; i++) 407 jj_la1[i] = -1; 408 for (int i = 0; i < jj_2_rtns.length; i++) 409 jj_2_rtns[i] = new JJCalls(); 410 } 411 412 final private Token jj_consume_token(int kind) throws ParseException { 413 Token oldToken; 414 if ((oldToken = token).next != null) 415 token = token.next; 416 else 417 token = token.next = token_source.getNextToken(); 418 jj_ntk = -1; 419 if (token.kind == kind) { 420 jj_gen++; 421 if (++jj_gc > 100) { 422 jj_gc = 0; 423 for (int i = 0; i < jj_2_rtns.length; i++) { 424 JJCalls c = jj_2_rtns[i]; 425 while (c != null) { 426 if (c.gen < jj_gen) 427 c.first = null; 428 c = c.next; 429 } 430 } 431 } 432 return token; 433 } 434 token = oldToken; 435 jj_kind = kind; 436 throw generateParseException(); 437 } 438 439 final private boolean jj_scan_token(int kind) { 440 if (jj_scanpos == jj_lastpos) { 441 jj_la--; 442 if (jj_scanpos.next == null) { 443 jj_lastpos = 444 jj_scanpos = jj_scanpos.next = token_source.getNextToken(); 445 } else { 446 jj_lastpos = jj_scanpos = jj_scanpos.next; 447 } 448 } else { 449 jj_scanpos = jj_scanpos.next; 450 } 451 if (jj_rescan) { 452 int i = 0; 453 Token tok = token; 454 while (tok != null && tok != jj_scanpos) { 455 i++; 456 tok = tok.next; 457 } 458 if (tok != null) 459 jj_add_error_token(kind, i); 460 } 461 return (jj_scanpos.kind != kind); 462 } 463 464 final public Token getNextToken() { 465 if (token.next != null) 466 token = token.next; 467 else 468 token = token.next = token_source.getNextToken(); 469 jj_ntk = -1; 470 jj_gen++; 471 return token; 472 } 473 474 final public Token getToken(int index) { 475 Token t = lookingAhead ? jj_scanpos : token; 476 for (int i = 0; i < index; i++) { 477 if (t.next != null) 478 t = t.next; 479 else 480 t = t.next = token_source.getNextToken(); 481 } 482 return t; 483 } 484 485 final private int jj_ntk() { 486 if ((jj_nt = token.next) == null) 487 return (jj_ntk = (token.next = token_source.getNextToken()).kind); 488 else 489 return (jj_ntk = jj_nt.kind); 490 } 491 492 private java.util.Vector jj_expentries = new java.util.Vector (); 493 private int[] jj_expentry; 494 private int jj_kind = -1; 495 private int[] jj_lasttokens = new int[100]; 496 private int jj_endpos; 497 498 private void jj_add_error_token(int kind, int pos) { 499 if (pos >= 100) 500 return; 501 if (pos == jj_endpos + 1) { 502 jj_lasttokens[jj_endpos++] = kind; 503 } else if (jj_endpos != 0) { 504 jj_expentry = new int[jj_endpos]; 505 for (int i = 0; i < jj_endpos; i++) { 506 jj_expentry[i] = jj_lasttokens[i]; 507 } 508 boolean exists = false; 509 for (java.util.Enumeration enumeration = jj_expentries.elements(); 510 enumeration.hasMoreElements(); 511 ) { 512 int[] oldentry = (int[]) (enumeration.nextElement()); 513 if (oldentry.length == jj_expentry.length) { 514 exists = true; 515 for (int i = 0; i < jj_expentry.length; i++) { 516 if (oldentry[i] != jj_expentry[i]) { 517 exists = false; 518 break; 519 } 520 } 521 if (exists) 522 break; 523 } 524 } 525 if (!exists) 526 jj_expentries.addElement(jj_expentry); 527 if (pos != 0) 528 jj_lasttokens[(jj_endpos = pos) - 1] = kind; 529 } 530 } 531 532 public ParseException generateParseException() { 533 jj_expentries.removeAllElements(); 534 boolean[] la1tokens = new boolean[16]; 535 for (int i = 0; i < 16; i++) { 536 la1tokens[i] = false; 537 } 538 if (jj_kind >= 0) { 539 la1tokens[jj_kind] = true; 540 jj_kind = -1; 541 } 542 for (int i = 0; i < 6; i++) { 543 if (jj_la1[i] == jj_gen) { 544 for (int j = 0; j < 32; j++) { 545 if ((jj_la1_0[i] & (1 << j)) != 0) { 546 la1tokens[j] = true; 547 } 548 } 549 } 550 } 551 for (int i = 0; i < 16; i++) { 552 if (la1tokens[i]) { 553 jj_expentry = new int[1]; 554 jj_expentry[0] = i; 555 jj_expentries.addElement(jj_expentry); 556 } 557 } 558 jj_endpos = 0; 559 jj_rescan_token(); 560 jj_add_error_token(0, 0); 561 int[][] exptokseq = new int[jj_expentries.size()][]; 562 for (int i = 0; i < jj_expentries.size(); i++) { 563 exptokseq[i] = (int[]) jj_expentries.elementAt(i); 564 } 565 return new ParseException(token, exptokseq, tokenImage); 566 } 567 568 final public void enable_tracing() { 569 } 570 571 final public void disable_tracing() { 572 } 573 574 final private void jj_rescan_token() { 575 jj_rescan = true; 576 for (int i = 0; i < 1; i++) { 577 JJCalls p = jj_2_rtns[i]; 578 do { 579 if (p.gen > jj_gen) { 580 jj_la = p.arg; 581 jj_lastpos = jj_scanpos = p.first; 582 switch (i) { 583 case 0 : 584 jj_3_1(); 585 break; 586 } 587 } 588 p = p.next; 589 } while (p != null); 590 } 591 jj_rescan = false; 592 } 593 594 final private void jj_save(int index, int xla) { 595 JJCalls p = jj_2_rtns[index]; 596 while (p.gen > jj_gen) { 597 if (p.next == null) { 598 p = p.next = new JJCalls(); 599 break; 600 } 601 p = p.next; 602 } 603 p.gen = jj_gen + xla - jj_la; 604 p.first = token; 605 p.arg = xla; 606 } 607 608 static final class JJCalls { 609 int gen; 610 Token first; 611 int arg; 612 JJCalls next; 613 } 614 615 } 616 | Popular Tags |