1 57 58 package com.sun.org.apache.xerces.internal.impl; 59 60 import java.io.EOFException ; 61 import java.io.IOException ; 62 import java.util.Locale ; 63 64 import com.sun.org.apache.xerces.internal.impl.io.UCSReader; 65 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 66 import com.sun.org.apache.xerces.internal.util.SymbolTable; 67 import com.sun.org.apache.xerces.internal.util.XMLChar; 68 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer; 69 import com.sun.org.apache.xerces.internal.xni.QName; 70 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 71 import com.sun.org.apache.xerces.internal.xni.XMLString; 72 73 80 81 public class XMLEntityScanner implements XMLLocator { 82 83 private static final boolean DEBUG_ENCODINGS = false; 85 private static final boolean DEBUG_BUFFER = false; 86 87 91 private XMLEntityManager fEntityManager = null; 92 protected XMLEntityManager.ScannedEntity fCurrentEntity = null; 93 94 protected SymbolTable fSymbolTable = null; 95 96 protected int fBufferSize = XMLEntityManager.DEFAULT_BUFFER_SIZE; 97 98 102 protected XMLErrorReporter fErrorReporter; 103 107 108 public XMLEntityScanner() { 109 } 111 115 119 public String getBaseSystemId() { 120 return (fCurrentEntity != null && fCurrentEntity.entityLocation != null) ? fCurrentEntity.entityLocation.getExpandedSystemId() : null; 121 } 123 141 public void setEncoding(String encoding) throws IOException { 142 143 if (DEBUG_ENCODINGS) { 144 System.out.println("$$$ setEncoding: "+encoding); 145 } 146 147 if (fCurrentEntity.stream != null) { 148 if (fCurrentEntity.encoding == null || 156 !fCurrentEntity.encoding.equals(encoding)) { 157 if(fCurrentEntity.encoding != null && fCurrentEntity.encoding.startsWith("UTF-16")) { 162 String ENCODING = encoding.toUpperCase(Locale.ENGLISH); 163 if(ENCODING.equals("UTF-16")) return; 164 if(ENCODING.equals("ISO-10646-UCS-4")) { 165 if(fCurrentEntity.encoding.equals("UTF-16BE")) { 166 fCurrentEntity.reader = new UCSReader(fCurrentEntity.stream, UCSReader.UCS4BE); 167 } else { 168 fCurrentEntity.reader = new UCSReader(fCurrentEntity.stream, UCSReader.UCS4LE); 169 } 170 return; 171 } 172 if(ENCODING.equals("ISO-10646-UCS-2")) { 173 if(fCurrentEntity.encoding.equals("UTF-16BE")) { 174 fCurrentEntity.reader = new UCSReader(fCurrentEntity.stream, UCSReader.UCS2BE); 175 } else { 176 fCurrentEntity.reader = new UCSReader(fCurrentEntity.stream, UCSReader.UCS2LE); 177 } 178 return; 179 } 180 } 181 if (DEBUG_ENCODINGS) { 184 System.out.println("$$$ creating new reader from stream: "+ 185 fCurrentEntity.stream); 186 } 187 fCurrentEntity.setReader(fCurrentEntity.stream, encoding, null); 189 fCurrentEntity.encoding = encoding; 190 } else { 191 if (DEBUG_ENCODINGS) 192 System.out.println("$$$ reusing old reader on stream"); 193 } 194 } 195 196 } 198 199 public boolean isExternal() { 200 return fCurrentEntity.isExternal(); 201 } 203 211 public int peekChar() throws IOException { 212 if (DEBUG_BUFFER) { 213 System.out.print("(peekChar: "); 214 XMLEntityManager.print(fCurrentEntity); 215 System.out.println(); 216 } 217 218 if (fCurrentEntity.position == fCurrentEntity.count) { 220 load(0, true); 221 } 222 223 int c = fCurrentEntity.ch[fCurrentEntity.position]; 225 226 if (DEBUG_BUFFER) { 228 System.out.print(")peekChar: "); 229 XMLEntityManager.print(fCurrentEntity); 230 if (fCurrentEntity.isExternal()) { 231 System.out.println(" -> '"+(c!='\r'?(char)c:'\n')+"'"); 232 } 233 else { 234 System.out.println(" -> '"+(char)c+"'"); 235 } 236 } 237 if (fCurrentEntity.isExternal()) { 238 return c != '\r' ? c : '\n'; 239 } 240 else { 241 return c; 242 } 243 244 } 246 254 public int scanChar() throws IOException { 255 if (DEBUG_BUFFER) { 256 System.out.print("(scanChar: "); 257 XMLEntityManager.print(fCurrentEntity); 258 System.out.println(); 259 } 260 261 if (fCurrentEntity.position == fCurrentEntity.count) { 263 load(0, true); 264 } 265 266 int c = fCurrentEntity.ch[fCurrentEntity.position++]; 268 boolean external = false; 269 if (c == '\n' || 270 (c == '\r' && (external = fCurrentEntity.isExternal()))) { 271 fCurrentEntity.lineNumber++; 272 fCurrentEntity.columnNumber = 1; 273 if (fCurrentEntity.position == fCurrentEntity.count) { 274 fCurrentEntity.ch[0] = (char)c; 275 load(1, false); 276 } 277 if (c == '\r' && external) { 278 if (fCurrentEntity.ch[fCurrentEntity.position++] != '\n') { 279 fCurrentEntity.position--; 280 } 281 c = '\n'; 282 } 283 } 284 285 if (DEBUG_BUFFER) { 287 System.out.print(")scanChar: "); 288 XMLEntityManager.print(fCurrentEntity); 289 System.out.println(" -> '"+(char)c+"'"); 290 } 291 fCurrentEntity.columnNumber++; 292 return c; 293 294 } 296 311 public String scanNmtoken() throws IOException { 312 if (DEBUG_BUFFER) { 313 System.out.print("(scanNmtoken: "); 314 XMLEntityManager.print(fCurrentEntity); 315 System.out.println(); 316 } 317 318 if (fCurrentEntity.position == fCurrentEntity.count) { 320 load(0, true); 321 } 322 323 int offset = fCurrentEntity.position; 325 while (XMLChar.isName(fCurrentEntity.ch[fCurrentEntity.position])) { 326 if (++fCurrentEntity.position == fCurrentEntity.count) { 327 int length = fCurrentEntity.position - offset; 328 if (length == fCurrentEntity.ch.length) { 329 char[] tmp = new char[fCurrentEntity.ch.length << 1]; 331 System.arraycopy(fCurrentEntity.ch, offset, 332 tmp, 0, length); 333 fCurrentEntity.ch = tmp; 334 } 335 else { 336 System.arraycopy(fCurrentEntity.ch, offset, 337 fCurrentEntity.ch, 0, length); 338 } 339 offset = 0; 340 if (load(length, false)) { 341 break; 342 } 343 } 344 } 345 int length = fCurrentEntity.position - offset; 346 fCurrentEntity.columnNumber += length; 347 348 String symbol = null; 350 if (length > 0) { 351 symbol = fSymbolTable.addSymbol(fCurrentEntity.ch, offset, length); 352 } 353 if (DEBUG_BUFFER) { 354 System.out.print(")scanNmtoken: "); 355 XMLEntityManager.print(fCurrentEntity); 356 System.out.println(" -> "+String.valueOf(symbol)); 357 } 358 return symbol; 359 360 } 362 378 public String scanName() throws IOException { 379 if (DEBUG_BUFFER) { 380 System.out.print("(scanName: "); 381 XMLEntityManager.print(fCurrentEntity); 382 System.out.println(); 383 } 384 385 if (fCurrentEntity.position == fCurrentEntity.count) { 387 load(0, true); 388 } 389 390 int offset = fCurrentEntity.position; 392 if (XMLChar.isNameStart(fCurrentEntity.ch[offset])) { 393 if (++fCurrentEntity.position == fCurrentEntity.count) { 394 fCurrentEntity.ch[0] = fCurrentEntity.ch[offset]; 395 offset = 0; 396 if (load(1, false)) { 397 fCurrentEntity.columnNumber++; 398 String symbol = fSymbolTable.addSymbol(fCurrentEntity.ch, 0, 1); 399 if (DEBUG_BUFFER) { 400 System.out.print(")scanName: "); 401 XMLEntityManager.print(fCurrentEntity); 402 System.out.println(" -> "+String.valueOf(symbol)); 403 } 404 return symbol; 405 } 406 } 407 while (XMLChar.isName(fCurrentEntity.ch[fCurrentEntity.position])) { 408 if (++fCurrentEntity.position == fCurrentEntity.count) { 409 int length = fCurrentEntity.position - offset; 410 if (length == fCurrentEntity.ch.length) { 411 char[] tmp = new char[fCurrentEntity.ch.length << 1]; 413 System.arraycopy(fCurrentEntity.ch, offset, 414 tmp, 0, length); 415 fCurrentEntity.ch = tmp; 416 } 417 else { 418 System.arraycopy(fCurrentEntity.ch, offset, 419 fCurrentEntity.ch, 0, length); 420 } 421 offset = 0; 422 if (load(length, false)) { 423 break; 424 } 425 } 426 } 427 } 428 int length = fCurrentEntity.position - offset; 429 fCurrentEntity.columnNumber += length; 430 431 String symbol = null; 433 if (length > 0) { 434 symbol = fSymbolTable.addSymbol(fCurrentEntity.ch, offset, length); 435 } 436 if (DEBUG_BUFFER) { 437 System.out.print(")scanName: "); 438 XMLEntityManager.print(fCurrentEntity); 439 System.out.println(" -> "+String.valueOf(symbol)); 440 } 441 return symbol; 442 443 } 445 461 public String scanNCName() throws IOException { 462 if (DEBUG_BUFFER) { 463 System.out.print("(scanNCName: "); 464 XMLEntityManager.print(fCurrentEntity); 465 System.out.println(); 466 } 467 468 if (fCurrentEntity.position == fCurrentEntity.count) { 470 load(0, true); 471 } 472 473 int offset = fCurrentEntity.position; 475 if (XMLChar.isNCNameStart(fCurrentEntity.ch[offset])) { 476 if (++fCurrentEntity.position == fCurrentEntity.count) { 477 fCurrentEntity.ch[0] = fCurrentEntity.ch[offset]; 478 offset = 0; 479 if (load(1, false)) { 480 fCurrentEntity.columnNumber++; 481 String symbol = fSymbolTable.addSymbol(fCurrentEntity.ch, 0, 1); 482 if (DEBUG_BUFFER) { 483 System.out.print(")scanNCName: "); 484 XMLEntityManager.print(fCurrentEntity); 485 System.out.println(" -> "+String.valueOf(symbol)); 486 } 487 return symbol; 488 } 489 } 490 while (XMLChar.isNCName(fCurrentEntity.ch[fCurrentEntity.position])) { 491 if (++fCurrentEntity.position == fCurrentEntity.count) { 492 int length = fCurrentEntity.position - offset; 493 if (length == fCurrentEntity.ch.length) { 494 char[] tmp = new char[fCurrentEntity.ch.length << 1]; 496 System.arraycopy(fCurrentEntity.ch, offset, 497 tmp, 0, length); 498 fCurrentEntity.ch = tmp; 499 } 500 else { 501 System.arraycopy(fCurrentEntity.ch, offset, 502 fCurrentEntity.ch, 0, length); 503 } 504 offset = 0; 505 if (load(length, false)) { 506 break; 507 } 508 } 509 } 510 } 511 int length = fCurrentEntity.position - offset; 512 fCurrentEntity.columnNumber += length; 513 514 String symbol = null; 516 if (length > 0) { 517 symbol = fSymbolTable.addSymbol(fCurrentEntity.ch, offset, length); 518 } 519 if (DEBUG_BUFFER) { 520 System.out.print(")scanNCName: "); 521 XMLEntityManager.print(fCurrentEntity); 522 System.out.println(" -> "+String.valueOf(symbol)); 523 } 524 return symbol; 525 526 } 528 550 public boolean scanQName(QName qname) throws IOException { 551 if (DEBUG_BUFFER) { 552 System.out.print("(scanQName, "+qname+": "); 553 XMLEntityManager.print(fCurrentEntity); 554 System.out.println(); 555 } 556 557 if (fCurrentEntity.position == fCurrentEntity.count) { 559 load(0, true); 560 } 561 562 int offset = fCurrentEntity.position; 564 if (XMLChar.isNCNameStart(fCurrentEntity.ch[offset])) { 565 if (++fCurrentEntity.position == fCurrentEntity.count) { 566 fCurrentEntity.ch[0] = fCurrentEntity.ch[offset]; 567 offset = 0; 568 if (load(1, false)) { 569 fCurrentEntity.columnNumber++; 570 String name = 571 fSymbolTable.addSymbol(fCurrentEntity.ch, 0, 1); 572 qname.setValues(null, name, name, null); 573 if (DEBUG_BUFFER) { 574 System.out.print(")scanQName, "+qname+": "); 575 XMLEntityManager.print(fCurrentEntity); 576 System.out.println(" -> true"); 577 } 578 return true; 579 } 580 } 581 int index = -1; 582 while (XMLChar.isName(fCurrentEntity.ch[fCurrentEntity.position])) { 583 char c = fCurrentEntity.ch[fCurrentEntity.position]; 584 585 if (c == ':') { 586 if (index != -1) { 587 break; 588 } 589 index = fCurrentEntity.position; 590 } 591 if (++fCurrentEntity.position == fCurrentEntity.count) { 592 int length = fCurrentEntity.position - offset; 593 if (length == fCurrentEntity.ch.length) { 594 char[] tmp = new char[fCurrentEntity.ch.length << 1]; 596 System.arraycopy(fCurrentEntity.ch, offset, 597 tmp, 0, length); 598 fCurrentEntity.ch = tmp; 599 } 600 else { 601 System.arraycopy(fCurrentEntity.ch, offset, 602 fCurrentEntity.ch, 0, length); 603 } 604 if (index != -1) { 605 index = index - offset; 606 } 607 offset = 0; 608 if (load(length, false)) { 609 break; 610 } 611 } 612 } 613 int length = fCurrentEntity.position - offset; 614 fCurrentEntity.columnNumber += length; 615 if (length > 0) { 616 String prefix = null; 617 String localpart = null; 618 String rawname = fSymbolTable.addSymbol(fCurrentEntity.ch, 619 offset, length); 620 if (index != -1) { 621 int prefixLength = index - offset; 622 prefix = fSymbolTable.addSymbol(fCurrentEntity.ch, 623 offset, prefixLength); 624 int len = length - prefixLength - 1; 625 int startLocal = index +1; 626 if (!XMLChar.isNCNameStart(fCurrentEntity.ch[startLocal])){ 627 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 628 "IllegalQName", 629 null, 630 XMLErrorReporter.SEVERITY_FATAL_ERROR); 631 } 632 localpart = fSymbolTable.addSymbol(fCurrentEntity.ch, 633 startLocal, len); 634 635 } 636 else { 637 localpart = rawname; 638 } 639 qname.setValues(prefix, localpart, rawname, null); 640 if (DEBUG_BUFFER) { 641 System.out.print(")scanQName, "+qname+": "); 642 XMLEntityManager.print(fCurrentEntity); 643 System.out.println(" -> true"); 644 } 645 return true; 646 } 647 } 648 649 if (DEBUG_BUFFER) { 651 System.out.print(")scanQName, "+qname+": "); 652 XMLEntityManager.print(fCurrentEntity); 653 System.out.println(" -> false"); 654 } 655 return false; 656 657 } 659 685 public int scanContent(XMLString content) throws IOException { 686 if (DEBUG_BUFFER) { 687 System.out.print("(scanContent: "); 688 XMLEntityManager.print(fCurrentEntity); 689 System.out.println(); 690 } 691 692 if (fCurrentEntity.position == fCurrentEntity.count) { 694 load(0, true); 695 } 696 else if (fCurrentEntity.position == fCurrentEntity.count - 1) { 697 fCurrentEntity.ch[0] = fCurrentEntity.ch[fCurrentEntity.count - 1]; 698 load(1, false); 699 fCurrentEntity.position = 0; 700 } 701 702 int offset = fCurrentEntity.position; 704 int c = fCurrentEntity.ch[offset]; 705 int newlines = 0; 706 boolean external = fCurrentEntity.isExternal(); 707 if (c == '\n' || (c == '\r' && external)) { 708 if (DEBUG_BUFFER) { 709 System.out.print("[newline, "+offset+", "+fCurrentEntity.position+": "); 710 XMLEntityManager.print(fCurrentEntity); 711 System.out.println(); 712 } 713 do { 714 c = fCurrentEntity.ch[fCurrentEntity.position++]; 715 if (c == '\r' && external) { 716 newlines++; 717 fCurrentEntity.lineNumber++; 718 fCurrentEntity.columnNumber = 1; 719 if (fCurrentEntity.position == fCurrentEntity.count) { 720 offset = 0; 721 fCurrentEntity.position = newlines; 722 if (load(newlines, false)) { 723 break; 724 } 725 } 726 if (fCurrentEntity.ch[fCurrentEntity.position] == '\n') { 727 fCurrentEntity.position++; 728 offset++; 729 } 730 731 else { 732 newlines++; 733 } 734 } 735 else if (c == '\n') { 736 newlines++; 737 fCurrentEntity.lineNumber++; 738 fCurrentEntity.columnNumber = 1; 739 if (fCurrentEntity.position == fCurrentEntity.count) { 740 offset = 0; 741 fCurrentEntity.position = newlines; 742 if (load(newlines, false)) { 743 break; 744 } 745 } 746 } 747 else { 748 fCurrentEntity.position--; 749 break; 750 } 751 } while (fCurrentEntity.position < fCurrentEntity.count - 1); 752 for (int i = offset; i < fCurrentEntity.position; i++) { 753 fCurrentEntity.ch[i] = '\n'; 754 } 755 int length = fCurrentEntity.position - offset; 756 if (fCurrentEntity.position == fCurrentEntity.count - 1) { 757 content.setValues(fCurrentEntity.ch, offset, length); 758 if (DEBUG_BUFFER) { 759 System.out.print("]newline, "+offset+", "+fCurrentEntity.position+": "); 760 XMLEntityManager.print(fCurrentEntity); 761 System.out.println(); 762 } 763 return -1; 764 } 765 if (DEBUG_BUFFER) { 766 System.out.print("]newline, "+offset+", "+fCurrentEntity.position+": "); 767 XMLEntityManager.print(fCurrentEntity); 768 System.out.println(); 769 } 770 } 771 772 while (fCurrentEntity.position < fCurrentEntity.count) { 774 c = fCurrentEntity.ch[fCurrentEntity.position++]; 775 if (!XMLChar.isContent(c)) { 776 fCurrentEntity.position--; 777 break; 778 } 779 } 780 int length = fCurrentEntity.position - offset; 781 fCurrentEntity.columnNumber += length - newlines; 782 content.setValues(fCurrentEntity.ch, offset, length); 783 784 if (fCurrentEntity.position != fCurrentEntity.count) { 786 c = fCurrentEntity.ch[fCurrentEntity.position]; 787 if (c == '\r' && external) { 790 c = '\n'; 791 } 792 } 793 else { 794 c = -1; 795 } 796 if (DEBUG_BUFFER) { 797 System.out.print(")scanContent: "); 798 XMLEntityManager.print(fCurrentEntity); 799 System.out.println(" -> '"+(char)c+"'"); 800 } 801 return c; 802 803 } 805 833 public int scanLiteral(int quote, XMLString content) 834 throws IOException { 835 if (DEBUG_BUFFER) { 836 System.out.print("(scanLiteral, '"+(char)quote+"': "); 837 XMLEntityManager.print(fCurrentEntity); 838 System.out.println(); 839 } 840 841 if (fCurrentEntity.position == fCurrentEntity.count) { 843 load(0, true); 844 } 845 else if (fCurrentEntity.position == fCurrentEntity.count - 1) { 846 fCurrentEntity.ch[0] = fCurrentEntity.ch[fCurrentEntity.count - 1]; 847 load(1, false); 848 fCurrentEntity.position = 0; 849 } 850 851 int offset = fCurrentEntity.position; 853 int c = fCurrentEntity.ch[offset]; 854 int newlines = 0; 855 boolean external = fCurrentEntity.isExternal(); 856 if (c == '\n' || (c == '\r' && external)) { 857 if (DEBUG_BUFFER) { 858 System.out.print("[newline, "+offset+", "+fCurrentEntity.position+": "); 859 XMLEntityManager.print(fCurrentEntity); 860 System.out.println(); 861 } 862 do { 863 c = fCurrentEntity.ch[fCurrentEntity.position++]; 864 if (c == '\r' && external) { 865 newlines++; 866 fCurrentEntity.lineNumber++; 867 fCurrentEntity.columnNumber = 1; 868 if (fCurrentEntity.position == fCurrentEntity.count) { 869 offset = 0; 870 fCurrentEntity.position = newlines; 871 if (load(newlines, false)) { 872 break; 873 } 874 } 875 if (fCurrentEntity.ch[fCurrentEntity.position] == '\n') { 876 fCurrentEntity.position++; 877 offset++; 878 } 879 880 else { 881 newlines++; 882 } 883 884 } 885 else if (c == '\n') { 886 newlines++; 887 fCurrentEntity.lineNumber++; 888 fCurrentEntity.columnNumber = 1; 889 if (fCurrentEntity.position == fCurrentEntity.count) { 890 offset = 0; 891 fCurrentEntity.position = newlines; 892 if (load(newlines, false)) { 893 break; 894 } 895 } 896 } 897 else { 898 fCurrentEntity.position--; 899 break; 900 } 901 } while (fCurrentEntity.position < fCurrentEntity.count - 1); 902 for (int i = offset; i < fCurrentEntity.position; i++) { 903 fCurrentEntity.ch[i] = '\n'; 904 } 905 int length = fCurrentEntity.position - offset; 906 if (fCurrentEntity.position == fCurrentEntity.count - 1) { 907 content.setValues(fCurrentEntity.ch, offset, length); 908 if (DEBUG_BUFFER) { 909 System.out.print("]newline, "+offset+", "+fCurrentEntity.position+": "); 910 XMLEntityManager.print(fCurrentEntity); 911 System.out.println(); 912 } 913 return -1; 914 } 915 if (DEBUG_BUFFER) { 916 System.out.print("]newline, "+offset+", "+fCurrentEntity.position+": "); 917 XMLEntityManager.print(fCurrentEntity); 918 System.out.println(); 919 } 920 } 921 922 while (fCurrentEntity.position < fCurrentEntity.count) { 924 c = fCurrentEntity.ch[fCurrentEntity.position++]; 925 if ((c == quote && 926 (!fCurrentEntity.literal || external)) 927 || c == '%' || !XMLChar.isContent(c)) { 928 fCurrentEntity.position--; 929 break; 930 } 931 } 932 int length = fCurrentEntity.position - offset; 933 fCurrentEntity.columnNumber += length - newlines; 934 content.setValues(fCurrentEntity.ch, offset, length); 935 936 if (fCurrentEntity.position != fCurrentEntity.count) { 938 c = fCurrentEntity.ch[fCurrentEntity.position]; 939 if (c == quote && fCurrentEntity.literal) { 943 c = -1; 944 } 945 } 946 else { 947 c = -1; 948 } 949 if (DEBUG_BUFFER) { 950 System.out.print(")scanLiteral, '"+(char)quote+"': "); 951 XMLEntityManager.print(fCurrentEntity); 952 System.out.println(" -> '"+(char)c+"'"); 953 } 954 return c; 955 956 } 958 988 public boolean scanData(String delimiter, XMLStringBuffer buffer) 989 throws IOException { 990 991 1002 boolean found = false; 1003 int delimLen = delimiter.length(); 1004 char charAt0 = delimiter.charAt(0); 1005 boolean external = fCurrentEntity.isExternal(); 1006 if (DEBUG_BUFFER) { 1007 System.out.print("(scanData: "); 1008 XMLEntityManager.print(fCurrentEntity); 1009 System.out.println(); 1010 } 1011 1012 1014 if (fCurrentEntity.position == fCurrentEntity.count) { 1015 load(0, true); 1016 } 1017 1018 boolean bNextEntity = false; 1019 1020 while ((fCurrentEntity.position >= fCurrentEntity.count - delimLen) 1021 && (!bNextEntity)) 1022 { 1023 System.arraycopy(fCurrentEntity.ch, 1024 fCurrentEntity.position, 1025 fCurrentEntity.ch, 1026 0, 1027 fCurrentEntity.count - fCurrentEntity.position); 1028 1029 bNextEntity = load(fCurrentEntity.count - fCurrentEntity.position, false); 1030 fCurrentEntity.position = 0; 1031 } 1032 1033 if (fCurrentEntity.position >= fCurrentEntity.count - delimLen) { 1034 int length = fCurrentEntity.count - fCurrentEntity.position; 1036 buffer.append (fCurrentEntity.ch, fCurrentEntity.position, length); 1037 fCurrentEntity.columnNumber += fCurrentEntity.count; 1038 fCurrentEntity.position = fCurrentEntity.count; 1039 load(0,true); 1040 return false; 1041 } 1042 1043 int offset = fCurrentEntity.position; 1045 int c = fCurrentEntity.ch[offset]; 1046 int newlines = 0; 1047 if (c == '\n' || (c == '\r' && external)) { 1048 if (DEBUG_BUFFER) { 1049 System.out.print("[newline, "+offset+", "+fCurrentEntity.position+": "); 1050 XMLEntityManager.print(fCurrentEntity); 1051 System.out.println(); 1052 } 1053 do { 1054 c = fCurrentEntity.ch[fCurrentEntity.position++]; 1055 if (c == '\r' && external) { 1056 newlines++; 1057 fCurrentEntity.lineNumber++; 1058 fCurrentEntity.columnNumber = 1; 1059 if (fCurrentEntity.position == fCurrentEntity.count) { 1060 offset = 0; 1061 fCurrentEntity.position = newlines; 1062 if (load(newlines, false)) { 1063 break; 1064 } 1065 } 1066 if (fCurrentEntity.ch[fCurrentEntity.position] == '\n') { 1067 fCurrentEntity.position++; 1068 offset++; 1069 } 1070 1071 else { 1072 newlines++; 1073 } 1074 } 1075 else if (c == '\n') { 1076 newlines++; 1077 fCurrentEntity.lineNumber++; 1078 fCurrentEntity.columnNumber = 1; 1079 if (fCurrentEntity.position == fCurrentEntity.count) { 1080 offset = 0; 1081 fCurrentEntity.position = newlines; 1082 fCurrentEntity.count = newlines; 1083 if (load(newlines, false)) { 1084 break; 1085 } 1086 } 1087 } 1088 else { 1089 fCurrentEntity.position--; 1090 break; 1091 } 1092 } while (fCurrentEntity.position < fCurrentEntity.count - 1); 1093 for (int i = offset; i < fCurrentEntity.position; i++) { 1094 fCurrentEntity.ch[i] = '\n'; 1095 } 1096 int length = fCurrentEntity.position - offset; 1097 if (fCurrentEntity.position == fCurrentEntity.count - 1) { 1098 buffer.append(fCurrentEntity.ch, offset, length); 1099 if (DEBUG_BUFFER) { 1100 System.out.print("]newline, "+offset+", "+fCurrentEntity.position+": "); 1101 XMLEntityManager.print(fCurrentEntity); 1102 System.out.println(); 1103 } 1104 return true; 1105 } 1106 if (DEBUG_BUFFER) { 1107 System.out.print("]newline, "+offset+", "+fCurrentEntity.position+": "); 1108 XMLEntityManager.print(fCurrentEntity); 1109 System.out.println(); 1110 } 1111 } 1112 1113 OUTER: while (fCurrentEntity.position < fCurrentEntity.count) { 1115 c = fCurrentEntity.ch[fCurrentEntity.position++]; 1116 if (c == charAt0) { 1117 int delimOffset = fCurrentEntity.position - 1; 1119 for (int i = 1; i < delimLen; i++) { 1120 if (fCurrentEntity.position == fCurrentEntity.count) { 1121 fCurrentEntity.position -= i; 1122 break OUTER; 1123 } 1124 c = fCurrentEntity.ch[fCurrentEntity.position++]; 1125 if (delimiter.charAt(i) != c) { 1126 fCurrentEntity.position--; 1127 break; 1128 } 1129 } 1130 if (fCurrentEntity.position == delimOffset + delimLen) { 1131 found = true; 1132 break; 1133 } 1134 } 1135 else if (c == '\n' || (external && c == '\r')) { 1136 fCurrentEntity.position--; 1137 break; 1138 } 1139 else if (XMLChar.isInvalid(c)) { 1140 fCurrentEntity.position--; 1141 int length = fCurrentEntity.position - offset; 1142 fCurrentEntity.columnNumber += length - newlines; 1143 buffer.append(fCurrentEntity.ch, offset, length); 1144 return true; 1145 } 1146 } 1147 int length = fCurrentEntity.position - offset; 1148 fCurrentEntity.columnNumber += length - newlines; 1149 if (found) { 1150 length -= delimLen; 1151 } 1152 buffer.append (fCurrentEntity.ch, offset, length); 1153 1154 if (DEBUG_BUFFER) { 1156 System.out.print(")scanData: "); 1157 XMLEntityManager.print(fCurrentEntity); 1158 System.out.println(" -> " + !found); 1159 } 1160 return !found; 1161 1162 } 1164 1177 public boolean skipChar(int c) throws IOException { 1178 if (DEBUG_BUFFER) { 1179 System.out.print("(skipChar, '"+(char)c+"': "); 1180 XMLEntityManager.print(fCurrentEntity); 1181 System.out.println(); 1182 } 1183 1184 if (fCurrentEntity.position == fCurrentEntity.count) { 1186 load(0, true); 1187 } 1188 1189 int cc = fCurrentEntity.ch[fCurrentEntity.position]; 1191 if (cc == c) { 1192 fCurrentEntity.position++; 1193 if (c == '\n') { 1194 fCurrentEntity.lineNumber++; 1195 fCurrentEntity.columnNumber = 1; 1196 } 1197 else { 1198 fCurrentEntity.columnNumber++; 1199 } 1200 if (DEBUG_BUFFER) { 1201 System.out.print(")skipChar, '"+(char)c+"': "); 1202 XMLEntityManager.print(fCurrentEntity); 1203 System.out.println(" -> true"); 1204 } 1205 return true; 1206 } 1207 else if (c == '\n' && cc == '\r' && fCurrentEntity.isExternal()) { 1208 if (fCurrentEntity.position == fCurrentEntity.count) { 1210 fCurrentEntity.ch[0] = (char)cc; 1211 load(1, false); 1212 } 1213 fCurrentEntity.position++; 1214 if (fCurrentEntity.ch[fCurrentEntity.position] == '\n') { 1215 fCurrentEntity.position++; 1216 } 1217 fCurrentEntity.lineNumber++; 1218 fCurrentEntity.columnNumber = 1; 1219 if (DEBUG_BUFFER) { 1220 System.out.print(")skipChar, '"+(char)c+"': "); 1221 XMLEntityManager.print(fCurrentEntity); 1222 System.out.println(" -> true"); 1223 } 1224 return true; 1225 } 1226 1227 if (DEBUG_BUFFER) { 1229 System.out.print(")skipChar, '"+(char)c+"': "); 1230 XMLEntityManager.print(fCurrentEntity); 1231 System.out.println(" -> false"); 1232 } 1233 return false; 1234 1235 } 1237 1250 public boolean skipSpaces() throws IOException { 1251 if (DEBUG_BUFFER) { 1252 System.out.print("(skipSpaces: "); 1253 XMLEntityManager.print(fCurrentEntity); 1254 System.out.println(); 1255 } 1256 1257 if (fCurrentEntity.position == fCurrentEntity.count) { 1259 load(0, true); 1260 } 1261 1262 int c = fCurrentEntity.ch[fCurrentEntity.position]; 1264 if (XMLChar.isSpace(c)) { 1265 boolean external = fCurrentEntity.isExternal(); 1266 do { 1267 boolean entityChanged = false; 1268 if (c == '\n' || (external && c == '\r')) { 1270 fCurrentEntity.lineNumber++; 1271 fCurrentEntity.columnNumber = 1; 1272 if (fCurrentEntity.position == fCurrentEntity.count - 1) { 1273 fCurrentEntity.ch[0] = (char)c; 1274 entityChanged = load(1, true); 1275 if (!entityChanged) 1276 fCurrentEntity.position = 0; 1279 } 1280 if (c == '\r' && external) { 1281 if (fCurrentEntity.ch[++fCurrentEntity.position] != '\n') { 1284 fCurrentEntity.position--; 1285 } 1286 } 1287 1295 } 1296 else { 1297 fCurrentEntity.columnNumber++; 1298 } 1299 if (!entityChanged) 1301 fCurrentEntity.position++; 1302 if (fCurrentEntity.position == fCurrentEntity.count) { 1303 load(0, true); 1304 } 1305 } while (XMLChar.isSpace(c = fCurrentEntity.ch[fCurrentEntity.position])); 1306 if (DEBUG_BUFFER) { 1307 System.out.print(")skipSpaces: "); 1308 XMLEntityManager.print(fCurrentEntity); 1309 System.out.println(" -> true"); 1310 } 1311 return true; 1312 } 1313 1314 if (DEBUG_BUFFER) { 1316 System.out.print(")skipSpaces: "); 1317 XMLEntityManager.print(fCurrentEntity); 1318 System.out.println(" -> false"); 1319 } 1320 return false; 1321 1322 } 1324 1341 public boolean skipDeclSpaces() throws IOException { 1342 if (DEBUG_BUFFER) { 1343 System.out.print("(skipDeclSpaces: "); 1344 XMLEntityManager.print(fCurrentEntity); 1345 System.out.println(); 1346 } 1347 1348 if (fCurrentEntity.position == fCurrentEntity.count) { 1350 load(0, true); 1351 } 1352 1353 int c = fCurrentEntity.ch[fCurrentEntity.position]; 1355 if (XMLChar.isSpace(c)) { 1356 boolean external = fCurrentEntity.isExternal(); 1357 do { 1358 boolean entityChanged = false; 1359 if (c == '\n' || (external && c == '\r')) { 1361 fCurrentEntity.lineNumber++; 1362 fCurrentEntity.columnNumber = 1; 1363 if (fCurrentEntity.position == fCurrentEntity.count - 1) { 1364 fCurrentEntity.ch[0] = (char)c; 1365 entityChanged = load(1, true); 1366 if (!entityChanged) 1367 fCurrentEntity.position = 0; 1370 } 1371 if (c == '\r' && external) { 1372 if (fCurrentEntity.ch[++fCurrentEntity.position] != '\n') { 1375 fCurrentEntity.position--; 1376 } 1377 } 1378 1386 } 1387 else { 1388 fCurrentEntity.columnNumber++; 1389 } 1390 if (!entityChanged) 1392 fCurrentEntity.position++; 1393 if (fCurrentEntity.position == fCurrentEntity.count) { 1394 load(0, true); 1395 } 1396 } while (XMLChar.isSpace(c = fCurrentEntity.ch[fCurrentEntity.position])); 1397 if (DEBUG_BUFFER) { 1398 System.out.print(")skipDeclSpaces: "); 1399 XMLEntityManager.print(fCurrentEntity); 1400 System.out.println(" -> true"); 1401 } 1402 return true; 1403 } 1404 1405 if (DEBUG_BUFFER) { 1407 System.out.print(")skipDeclSpaces: "); 1408 XMLEntityManager.print(fCurrentEntity); 1409 System.out.println(" -> false"); 1410 } 1411 return false; 1412 1413 } 1415 1428 public boolean skipString(String s) throws IOException { 1429 if (DEBUG_BUFFER) { 1430 System.out.print("(skipString, \""+s+"\": "); 1431 XMLEntityManager.print(fCurrentEntity); 1432 System.out.println(); 1433 } 1434 1435 if (fCurrentEntity.position == fCurrentEntity.count) { 1437 load(0, true); 1438 } 1439 1440 final int length = s.length(); 1442 for (int i = 0; i < length; i++) { 1443 char c = fCurrentEntity.ch[fCurrentEntity.position++]; 1444 if (c != s.charAt(i)) { 1445 fCurrentEntity.position -= i + 1; 1446 if (DEBUG_BUFFER) { 1447 System.out.print(")skipString, \""+s+"\": "); 1448 XMLEntityManager.print(fCurrentEntity); 1449 System.out.println(" -> false"); 1450 } 1451 return false; 1452 } 1453 if (i < length - 1 && fCurrentEntity.position == fCurrentEntity.count) { 1454 System.arraycopy(fCurrentEntity.ch, fCurrentEntity.count - i - 1, fCurrentEntity.ch, 0, i + 1); 1455 if (load(i + 1, false)) { 1458 fCurrentEntity.position -= i + 1; 1459 if (DEBUG_BUFFER) { 1460 System.out.print(")skipString, \""+s+"\": "); 1461 XMLEntityManager.print(fCurrentEntity); 1462 System.out.println(" -> false"); 1463 } 1464 return false; 1465 } 1466 } 1467 } 1468 if (DEBUG_BUFFER) { 1469 System.out.print(")skipString, \""+s+"\": "); 1470 XMLEntityManager.print(fCurrentEntity); 1471 System.out.println(" -> true"); 1472 } 1473 fCurrentEntity.columnNumber += length; 1474 return true; 1475 1476 } 1478 1482 1492 public String getPublicId() { 1493 return (fCurrentEntity != null && fCurrentEntity.entityLocation != null) ? fCurrentEntity.entityLocation.getPublicId() : null; 1494 } 1496 1509 public String getExpandedSystemId() { 1510 if (fCurrentEntity != null) { 1511 if (fCurrentEntity.entityLocation != null && 1512 fCurrentEntity.entityLocation.getExpandedSystemId() != null ) { 1513 return fCurrentEntity.entityLocation.getExpandedSystemId(); 1514 } 1515 else { 1516 return fCurrentEntity.getExpandedSystemId(); 1518 } 1519 } 1520 return null; 1521 } 1523 1533 public String getLiteralSystemId() { 1534 if (fCurrentEntity != null) { 1535 if (fCurrentEntity.entityLocation != null && 1536 fCurrentEntity.entityLocation.getLiteralSystemId() != null ) { 1537 return fCurrentEntity.entityLocation.getLiteralSystemId(); 1538 } 1539 else { 1540 return fCurrentEntity.getLiteralSystemId(); 1542 } 1543 } 1544 return null; 1545 } 1547 1565 public int getLineNumber() { 1566 if (fCurrentEntity != null) { 1567 if (fCurrentEntity.isExternal()) { 1568 return fCurrentEntity.lineNumber; 1569 } 1570 else { 1571 return fCurrentEntity.getLineNumber(); 1573 } 1574 } 1575 1576 return -1; 1577 1578 } 1580 1602 public int getColumnNumber() { 1603 if (fCurrentEntity != null) { 1604 if (fCurrentEntity.isExternal()) { 1605 return fCurrentEntity.columnNumber; 1606 } 1607 else { 1608 return fCurrentEntity.getColumnNumber(); 1610 } 1611 } 1612 1613 return -1; 1614 } 1616 1623 public String getEncoding() { 1624 if (fCurrentEntity != null) { 1625 if (fCurrentEntity.isExternal()) { 1626 return fCurrentEntity.encoding; 1627 } 1628 else { 1629 return fCurrentEntity.getEncoding(); 1631 } 1632 } 1633 1634 return null; 1635 } 1637 1640 public void setColumnNumber(int col) { 1641 } 1643 1644 1647 public void setLineNumber(int line) { 1648 } 1650 1651 1654 public void setBaseSystemId(String systemId) { 1655 } 1657 1658 1659 1662 public void setExpandedSystemId(String systemId) { 1663 } 1665 1666 1669 public void setLiteralSystemId(String systemId) { 1670 } 1672 1673 1676 public void setPublicId(String publicId) { 1677 } 1679 1680 public void setCurrentEntity(XMLEntityManager.ScannedEntity ent) { 1682 fCurrentEntity = ent; 1683 } 1684 1685 public void setBufferSize(int size) { 1687 fBufferSize = size; 1698 } 1699 1700 public void reset(SymbolTable symbolTable, XMLEntityManager entityManager, 1702 XMLErrorReporter reporter) { 1703 fCurrentEntity = null; 1704 fSymbolTable = symbolTable; 1705 fEntityManager = entityManager; 1706 fErrorReporter = reporter; 1707 } 1708 1709 1713 1727 final boolean load(int offset, boolean changeEntity) 1728 throws IOException { 1729 if (DEBUG_BUFFER) { 1730 System.out.print("(load, "+offset+": "); 1731 XMLEntityManager.print(fCurrentEntity); 1732 System.out.println(); 1733 } 1734 1735 int length = fCurrentEntity.mayReadChunks? 1737 (fCurrentEntity.ch.length - offset): 1738 (XMLEntityManager.DEFAULT_XMLDECL_BUFFER_SIZE); 1739 if (DEBUG_BUFFER) System.out.println(" length to try to read: "+length); 1740 int count = fCurrentEntity.reader.read(fCurrentEntity.ch, offset, length); 1741 if (DEBUG_BUFFER) System.out.println(" length actually read: "+count); 1742 1743 boolean entityChanged = false; 1745 if (count != -1) { 1746 if (count != 0) { 1747 fCurrentEntity.count = count + offset; 1748 fCurrentEntity.position = offset; 1749 } 1750 } 1751 1752 else { 1754 fCurrentEntity.count = offset; 1755 fCurrentEntity.position = offset; 1756 entityChanged = true; 1757 if (changeEntity) { 1758 fEntityManager.endEntity(); 1759 if (fCurrentEntity == null) { 1760 throw new EOFException (); 1761 } 1762 if (fCurrentEntity.position == fCurrentEntity.count) { 1764 load(0, true); 1765 } 1766 } 1767 } 1768 if (DEBUG_BUFFER) { 1769 System.out.print(")load, "+offset+": "); 1770 XMLEntityManager.print(fCurrentEntity); 1771 System.out.println(); 1772 } 1773 1774 return entityChanged; 1775 1776 } 1778 1779} 1781 | Popular Tags |