1 57 58 package com.sun.org.apache.xerces.internal.impl; 59 60 import java.io.IOException ; 61 62 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 63 import com.sun.org.apache.xerces.internal.util.SymbolTable; 64 import com.sun.org.apache.xerces.internal.util.XMLChar; 65 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl; 66 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer; 67 import com.sun.org.apache.xerces.internal.xni.Augmentations; 68 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 69 import com.sun.org.apache.xerces.internal.xni.XMLString; 70 import com.sun.org.apache.xerces.internal.xni.XNIException; 71 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 72 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 73 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 74 75 99 public abstract class XMLScanner 100 implements XMLComponent { 101 102 106 108 109 protected static final String VALIDATION = 110 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 111 112 113 protected static final String NAMESPACES = 114 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 115 116 117 protected static final String NOTIFY_CHAR_REFS = 118 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE; 119 120 protected static final String PARSER_SETTINGS = 121 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 122 123 125 126 protected static final String SYMBOL_TABLE = 127 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 128 129 130 protected static final String ERROR_REPORTER = 131 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 132 133 134 protected static final String ENTITY_MANAGER = 135 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 136 137 139 140 protected static final boolean DEBUG_ATTR_NORMALIZATION = false; 141 142 146 147 149 153 protected boolean fValidation = false; 154 155 156 protected boolean fNamespaces; 157 158 159 protected boolean fNotifyCharRefs = false; 160 161 162 protected boolean fParserSettings = true; 163 164 166 167 protected SymbolTable fSymbolTable; 168 169 170 protected XMLErrorReporter fErrorReporter; 171 172 173 protected XMLEntityManager fEntityManager; 174 175 177 178 protected XMLEntityScanner fEntityScanner; 179 180 181 protected int fEntityDepth; 182 183 184 protected String fCharRefLiteral = null; 185 186 187 protected boolean fScanningAttribute; 188 189 190 protected boolean fReportEntity; 191 192 194 195 protected final static String fVersionSymbol = "version".intern(); 196 197 198 protected final static String fEncodingSymbol = "encoding".intern(); 199 200 201 protected final static String fStandaloneSymbol = "standalone".intern(); 202 203 204 protected final static String fAmpSymbol = "amp".intern(); 205 206 207 protected final static String fLtSymbol = "lt".intern(); 208 209 210 protected final static String fGtSymbol = "gt".intern(); 211 212 213 protected final static String fQuotSymbol = "quot".intern(); 214 215 216 protected final static String fAposSymbol = "apos".intern(); 217 218 220 226 227 private XMLString fString = new XMLString(); 228 229 230 private XMLStringBuffer fStringBuffer = new XMLStringBuffer(); 231 232 233 private XMLStringBuffer fStringBuffer2 = new XMLStringBuffer(); 234 235 236 private XMLStringBuffer fStringBuffer3 = new XMLStringBuffer(); 237 238 protected XMLResourceIdentifierImpl fResourceIdentifier = new XMLResourceIdentifierImpl(); 240 241 245 253 public void reset(XMLComponentManager componentManager) 254 throws XMLConfigurationException { 255 256 try { 257 fParserSettings = componentManager.getFeature(PARSER_SETTINGS); 258 } catch (XMLConfigurationException e) { 259 fParserSettings = true; 260 } 261 262 if (!fParserSettings) { 263 init(); 265 return; 266 } 267 268 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); 270 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); 271 fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER); 272 273 try { 275 fValidation = componentManager.getFeature(VALIDATION); 276 } 277 catch (XMLConfigurationException e) { 278 fValidation = false; 279 } 280 try { 281 fNamespaces = componentManager.getFeature(NAMESPACES); 282 } 283 catch (XMLConfigurationException e) { 284 fNamespaces = true; 285 } 286 try { 287 fNotifyCharRefs = componentManager.getFeature(NOTIFY_CHAR_REFS); 288 } 289 catch (XMLConfigurationException e) { 290 fNotifyCharRefs = false; 291 } 292 293 init(); 294 295 } 297 303 public void setProperty(String propertyId, Object value) 304 throws XMLConfigurationException { 305 306 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 308 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 309 310 if (suffixLength == Constants.SYMBOL_TABLE_PROPERTY.length() && 311 propertyId.endsWith(Constants.SYMBOL_TABLE_PROPERTY)) { 312 fSymbolTable = (SymbolTable)value; 313 } 314 else if (suffixLength == Constants.ERROR_REPORTER_PROPERTY.length() && 315 propertyId.endsWith(Constants.ERROR_REPORTER_PROPERTY)) { 316 fErrorReporter = (XMLErrorReporter)value; 317 } 318 else if (suffixLength == Constants.ENTITY_MANAGER_PROPERTY.length() && 319 propertyId.endsWith(Constants.ENTITY_MANAGER_PROPERTY)) { 320 fEntityManager = (XMLEntityManager)value; 321 } 322 } 323 324 } 326 329 public void setFeature(String featureId, boolean value) 330 throws XMLConfigurationException { 331 332 if (VALIDATION.equals(featureId)) { 333 fValidation = value; 334 } else if (NOTIFY_CHAR_REFS.equals(featureId)) { 335 fNotifyCharRefs = value; 336 } 337 } 338 339 342 public boolean getFeature(String featureId) 343 throws XMLConfigurationException { 344 345 if (VALIDATION.equals(featureId)) { 346 return fValidation; 347 } else if (NOTIFY_CHAR_REFS.equals(featureId)) { 348 return fNotifyCharRefs; 349 } 350 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, featureId); 351 } 352 353 357 protected void reset() { 359 init(); 360 361 fValidation = true; 363 fNotifyCharRefs = false; 364 365 } 366 367 369 393 protected void scanXMLDeclOrTextDecl(boolean scanningTextDecl, 394 String [] pseudoAttributeValues) 395 throws IOException , XNIException { 396 397 String version = null; 399 String encoding = null; 400 String standalone = null; 401 402 final int STATE_VERSION = 0; 404 final int STATE_ENCODING = 1; 405 final int STATE_STANDALONE = 2; 406 final int STATE_DONE = 3; 407 int state = STATE_VERSION; 408 409 boolean dataFoundForTarget = false; 410 boolean sawSpace = fEntityScanner.skipDeclSpaces(); 411 XMLEntityManager.ScannedEntity currEnt = fEntityManager.getCurrentEntity(); 419 boolean currLiteral = currEnt.literal; 420 currEnt.literal = false; 421 while (fEntityScanner.peekChar() != '?') { 422 dataFoundForTarget = true; 423 String name = scanPseudoAttribute(scanningTextDecl, fString); 424 switch (state) { 425 case STATE_VERSION: { 426 if (name == fVersionSymbol) { 427 if (!sawSpace) { 428 reportFatalError(scanningTextDecl 429 ? "SpaceRequiredBeforeVersionInTextDecl" 430 : "SpaceRequiredBeforeVersionInXMLDecl", 431 null); 432 } 433 version = fString.toString(); 434 state = STATE_ENCODING; 435 if (!versionSupported(version)) { 436 reportFatalError(getVersionNotSupportedKey(), 437 new Object []{version}); 438 } 439 } 440 else if (name == fEncodingSymbol) { 441 if (!scanningTextDecl) { 442 reportFatalError("VersionInfoRequired", null); 443 } 444 if (!sawSpace) { 445 reportFatalError(scanningTextDecl 446 ? "SpaceRequiredBeforeEncodingInTextDecl" 447 : "SpaceRequiredBeforeEncodingInXMLDecl", 448 null); 449 } 450 encoding = fString.toString(); 451 state = scanningTextDecl ? STATE_DONE : STATE_STANDALONE; 452 } 453 else { 454 if (scanningTextDecl) { 455 reportFatalError("EncodingDeclRequired", null); 456 } 457 else { 458 reportFatalError("VersionInfoRequired", null); 459 } 460 } 461 break; 462 } 463 case STATE_ENCODING: { 464 if (name == fEncodingSymbol) { 465 if (!sawSpace) { 466 reportFatalError(scanningTextDecl 467 ? "SpaceRequiredBeforeEncodingInTextDecl" 468 : "SpaceRequiredBeforeEncodingInXMLDecl", 469 null); 470 } 471 encoding = fString.toString(); 472 state = scanningTextDecl ? STATE_DONE : STATE_STANDALONE; 473 } 476 else if (!scanningTextDecl && name == fStandaloneSymbol) { 477 if (!sawSpace) { 478 reportFatalError("SpaceRequiredBeforeStandalone", 479 null); 480 } 481 standalone = fString.toString(); 482 state = STATE_DONE; 483 if (!standalone.equals("yes") && !standalone.equals("no")) { 484 reportFatalError("SDDeclInvalid", new Object [] {standalone}); 485 } 486 } 487 else { 488 reportFatalError("EncodingDeclRequired", null); 489 } 490 break; 491 } 492 case STATE_STANDALONE: { 493 if (name == fStandaloneSymbol) { 494 if (!sawSpace) { 495 reportFatalError("SpaceRequiredBeforeStandalone", 496 null); 497 } 498 standalone = fString.toString(); 499 state = STATE_DONE; 500 if (!standalone.equals("yes") && !standalone.equals("no")) { 501 reportFatalError("SDDeclInvalid", new Object [] {standalone}); 502 } 503 } 504 else { 505 reportFatalError("EncodingDeclRequired", null); 506 } 507 break; 508 } 509 default: { 510 reportFatalError("NoMorePseudoAttributes", null); 511 } 512 } 513 sawSpace = fEntityScanner.skipDeclSpaces(); 514 } 515 if(currLiteral) 517 currEnt.literal = true; 518 if (scanningTextDecl && state != STATE_DONE) { 520 reportFatalError("MorePseudoAttributes", null); 521 } 522 523 if (scanningTextDecl) { 526 if (!dataFoundForTarget && encoding == null) { 527 reportFatalError("EncodingDeclRequired", null); 528 } 529 } 530 else { 531 if (!dataFoundForTarget && version == null) { 532 reportFatalError("VersionInfoRequired", null); 533 } 534 } 535 536 if (!fEntityScanner.skipChar('?')) { 538 reportFatalError("XMLDeclUnterminated", null); 539 } 540 if (!fEntityScanner.skipChar('>')) { 541 reportFatalError("XMLDeclUnterminated", null); 542 543 } 544 545 pseudoAttributeValues[0] = version; 547 pseudoAttributeValues[1] = encoding; 548 pseudoAttributeValues[2] = standalone; 549 550 } 552 567 public String scanPseudoAttribute(boolean scanningTextDecl, 568 XMLString value) 569 throws IOException , XNIException { 570 571 String name = fEntityScanner.scanName(); 584 XMLEntityManager.print(fEntityManager.getCurrentEntity()); 585 if (name == null) { 586 reportFatalError("PseudoAttrNameExpected", null); 587 } 588 fEntityScanner.skipDeclSpaces(); 589 if (!fEntityScanner.skipChar('=')) { 590 reportFatalError(scanningTextDecl ? "EqRequiredInTextDecl" 591 : "EqRequiredInXMLDecl", new Object []{name}); 592 } 593 fEntityScanner.skipDeclSpaces(); 594 int quote = fEntityScanner.peekChar(); 595 if (quote != '\'' && quote != '"') { 596 reportFatalError(scanningTextDecl ? "QuoteRequiredInTextDecl" 597 : "QuoteRequiredInXMLDecl" , new Object []{name}); 598 } 599 fEntityScanner.scanChar(); 600 int c = fEntityScanner.scanLiteral(quote, value); 601 if (c != quote) { 602 fStringBuffer2.clear(); 603 do { 604 fStringBuffer2.append(value); 605 if (c != -1) { 606 if (c == '&' || c == '%' || c == '<' || c == ']') { 607 fStringBuffer2.append((char)fEntityScanner.scanChar()); 608 } 609 else if (XMLChar.isHighSurrogate(c)) { 613 scanSurrogates(fStringBuffer2); 614 } 615 else if (isInvalidLiteral(c)) { 616 String key = scanningTextDecl 617 ? "InvalidCharInTextDecl" : "InvalidCharInXMLDecl"; 618 reportFatalError(key, 619 new Object [] {Integer.toString(c, 16)}); 620 fEntityScanner.scanChar(); 621 } 622 } 623 c = fEntityScanner.scanLiteral(quote, value); 624 } while (c != quote); 625 fStringBuffer2.append(value); 626 value.setValues(fStringBuffer2); 627 } 628 if (!fEntityScanner.skipChar(quote)) { 629 reportFatalError(scanningTextDecl ? "CloseQuoteMissingInTextDecl" 630 : "CloseQuoteMissingInXMLDecl", 631 new Object []{name}); 632 } 633 634 return name; 636 637 } 639 649 protected void scanPI() throws IOException , XNIException { 650 651 fReportEntity = false; 653 String target = null; 654 if(fNamespaces) { 655 target = fEntityScanner.scanNCName(); 656 } else { 657 target = fEntityScanner.scanName(); 658 } 659 if (target == null) { 660 reportFatalError("PITargetRequired", null); 661 } 662 663 scanPIData(target, fString); 665 fReportEntity = true; 666 667 } 669 680 protected void scanPIData(String target, XMLString data) 681 throws IOException , XNIException { 682 683 if (target.length() == 3) { 685 char c0 = Character.toLowerCase(target.charAt(0)); 686 char c1 = Character.toLowerCase(target.charAt(1)); 687 char c2 = Character.toLowerCase(target.charAt(2)); 688 if (c0 == 'x' && c1 == 'm' && c2 == 'l') { 689 reportFatalError("ReservedPITarget", null); 690 } 691 } 692 693 if (!fEntityScanner.skipSpaces()) { 695 if (fEntityScanner.skipString("?>")) { 696 data.clear(); 698 return; 699 } 700 else { 701 if(fNamespaces && fEntityScanner.peekChar() == ':') { 702 fEntityScanner.scanChar(); 703 XMLStringBuffer colonName = new XMLStringBuffer(target); 704 colonName.append(":"); 705 String str = fEntityScanner.scanName(); 706 if (str != null) 707 colonName.append(str); 708 reportFatalError("ColonNotLegalWithNS", new Object [] {colonName.toString()}); 709 fEntityScanner.skipSpaces(); 710 } else { 711 reportFatalError("SpaceRequiredInPI", null); 713 } 714 } 715 } 716 717 fStringBuffer.clear(); 718 if (fEntityScanner.scanData("?>", fStringBuffer)) { 720 do { 721 int c = fEntityScanner.peekChar(); 722 if (c != -1) { 723 if (XMLChar.isHighSurrogate(c)) { 724 scanSurrogates(fStringBuffer); 725 } 726 else if (isInvalidLiteral(c)) { 727 reportFatalError("InvalidCharInPI", 728 new Object []{Integer.toHexString(c)}); 729 fEntityScanner.scanChar(); 730 } 731 } 732 } while (fEntityScanner.scanData("?>", fStringBuffer)); 733 } 734 data.setValues(fStringBuffer); 735 736 } 738 751 protected void scanComment(XMLStringBuffer text) 752 throws IOException , XNIException { 753 754 text.clear(); 757 while (fEntityScanner.scanData("--", text)) { 758 int c = fEntityScanner.peekChar(); 759 if (c != -1) { 760 if (XMLChar.isHighSurrogate(c)) { 761 scanSurrogates(text); 762 } 763 else if (isInvalidLiteral(c)) { 764 reportFatalError("InvalidCharInComment", 765 new Object [] { Integer.toHexString(c) }); 766 fEntityScanner.scanChar(); 767 } 768 } 769 } 770 if (!fEntityScanner.skipChar('>')) { 771 reportFatalError("DashDashInComment", null); 772 } 773 774 } 776 793 protected void scanAttributeValue(XMLString value, 794 XMLString nonNormalizedValue, 795 String atName, 796 boolean checkEntities,String eleName) 797 throws IOException , XNIException 798 { 799 int quote = fEntityScanner.peekChar(); 801 if (quote != '\'' && quote != '"') { 802 reportFatalError("OpenQuoteExpected", new Object []{eleName,atName}); 803 } 804 805 fEntityScanner.scanChar(); 806 int entityDepth = fEntityDepth; 807 808 int c = fEntityScanner.scanLiteral(quote, value); 809 if (DEBUG_ATTR_NORMALIZATION) { 810 System.out.println("** scanLiteral -> \"" 811 + value.toString() + "\""); 812 } 813 fStringBuffer2.clear(); 814 fStringBuffer2.append(value); 815 normalizeWhitespace(value); 816 if (DEBUG_ATTR_NORMALIZATION) { 817 System.out.println("** normalizeWhitespace -> \"" 818 + value.toString() + "\""); 819 } 820 if (c != quote) { 821 fScanningAttribute = true; 822 fStringBuffer.clear(); 823 do { 824 fStringBuffer.append(value); 825 if (DEBUG_ATTR_NORMALIZATION) { 826 System.out.println("** value2: \"" 827 + fStringBuffer.toString() + "\""); 828 } 829 if (c == '&') { 830 fEntityScanner.skipChar('&'); 831 if (entityDepth == fEntityDepth) { 832 fStringBuffer2.append('&'); 833 } 834 if (fEntityScanner.skipChar('#')) { 835 if (entityDepth == fEntityDepth) { 836 fStringBuffer2.append('#'); 837 } 838 int ch = scanCharReferenceValue(fStringBuffer, fStringBuffer2); 839 if (ch != -1) { 840 if (DEBUG_ATTR_NORMALIZATION) { 841 System.out.println("** value3: \"" 842 + fStringBuffer.toString() 843 + "\""); 844 } 845 } 846 } 847 else { 848 String entityName = fEntityScanner.scanName(); 849 if (entityName == null) { 850 reportFatalError("NameRequiredInReference", null); 851 } 852 else if (entityDepth == fEntityDepth) { 853 fStringBuffer2.append(entityName); 854 } 855 if (!fEntityScanner.skipChar(';')) { 856 reportFatalError("SemicolonRequiredInReference", 857 new Object []{entityName}); 858 } 859 else if (entityDepth == fEntityDepth) { 860 fStringBuffer2.append(';'); 861 } 862 if (entityName == fAmpSymbol) { 863 fStringBuffer.append('&'); 864 if (DEBUG_ATTR_NORMALIZATION) { 865 System.out.println("** value5: \"" 866 + fStringBuffer.toString() 867 + "\""); 868 } 869 } 870 else if (entityName == fAposSymbol) { 871 fStringBuffer.append('\''); 872 if (DEBUG_ATTR_NORMALIZATION) { 873 System.out.println("** value7: \"" 874 + fStringBuffer.toString() 875 + "\""); 876 } 877 } 878 else if (entityName == fLtSymbol) { 879 fStringBuffer.append('<'); 880 if (DEBUG_ATTR_NORMALIZATION) { 881 System.out.println("** value9: \"" 882 + fStringBuffer.toString() 883 + "\""); 884 } 885 } 886 else if (entityName == fGtSymbol) { 887 fStringBuffer.append('>'); 888 if (DEBUG_ATTR_NORMALIZATION) { 889 System.out.println("** valueB: \"" 890 + fStringBuffer.toString() 891 + "\""); 892 } 893 } 894 else if (entityName == fQuotSymbol) { 895 fStringBuffer.append('"'); 896 if (DEBUG_ATTR_NORMALIZATION) { 897 System.out.println("** valueD: \"" 898 + fStringBuffer.toString() 899 + "\""); 900 } 901 } 902 else { 903 if (fEntityManager.isExternalEntity(entityName)) { 904 reportFatalError("ReferenceToExternalEntity", 905 new Object [] { entityName }); 906 } 907 else { 908 if (!fEntityManager.isDeclaredEntity(entityName)) { 909 if (checkEntities) { 911 if (fValidation) { 912 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 913 "EntityNotDeclared", 914 new Object []{entityName}, 915 XMLErrorReporter.SEVERITY_ERROR); 916 } 917 } 918 else { 919 reportFatalError("EntityNotDeclared", 920 new Object []{entityName}); 921 } 922 } 923 fEntityManager.startEntity(entityName, true); 924 } 925 } 926 } 927 } 928 else if (c == '<') { 929 reportFatalError("LessthanInAttValue", 930 new Object [] { eleName, atName }); 931 fEntityScanner.scanChar(); 932 if (entityDepth == fEntityDepth) { 933 fStringBuffer2.append((char)c); 934 } 935 } 936 else if (c == '%' || c == ']') { 937 fEntityScanner.scanChar(); 938 fStringBuffer.append((char)c); 939 if (entityDepth == fEntityDepth) { 940 fStringBuffer2.append((char)c); 941 } 942 if (DEBUG_ATTR_NORMALIZATION) { 943 System.out.println("** valueF: \"" 944 + fStringBuffer.toString() + "\""); 945 } 946 } 947 else if (c == '\n' || c == '\r') { 948 fEntityScanner.scanChar(); 949 fStringBuffer.append(' '); 950 if (entityDepth == fEntityDepth) { 951 fStringBuffer2.append('\n'); 952 } 953 } 954 else if (c != -1 && XMLChar.isHighSurrogate(c)) { 955 fStringBuffer3.clear(); 956 if (scanSurrogates(fStringBuffer3)) { 957 fStringBuffer.append(fStringBuffer3); 958 if (entityDepth == fEntityDepth) { 959 fStringBuffer2.append(fStringBuffer3); 960 } 961 if (DEBUG_ATTR_NORMALIZATION) { 962 System.out.println("** valueI: \"" 963 + fStringBuffer.toString() 964 + "\""); 965 } 966 } 967 } 968 else if (c != -1 && isInvalidLiteral(c)) { 969 reportFatalError("InvalidCharInAttValue", 970 new Object [] {eleName, atName, Integer.toString(c, 16)}); 971 fEntityScanner.scanChar(); 972 if (entityDepth == fEntityDepth) { 973 fStringBuffer2.append((char)c); 974 } 975 } 976 c = fEntityScanner.scanLiteral(quote, value); 977 if (entityDepth == fEntityDepth) { 978 fStringBuffer2.append(value); 979 } 980 normalizeWhitespace(value); 981 } while (c != quote || entityDepth != fEntityDepth); 982 fStringBuffer.append(value); 983 if (DEBUG_ATTR_NORMALIZATION) { 984 System.out.println("** valueN: \"" 985 + fStringBuffer.toString() + "\""); 986 } 987 value.setValues(fStringBuffer); 988 fScanningAttribute = false; 989 } 990 nonNormalizedValue.setValues(fStringBuffer2); 991 992 int cquote = fEntityScanner.scanChar(); 994 if (cquote != quote) { 995 reportFatalError("CloseQuoteExpected", new Object []{eleName,atName}); 996 } 997 } 999 1000 1010 protected void scanExternalID(String [] identifiers, 1011 boolean optionalSystemId) 1012 throws IOException , XNIException { 1013 1014 String systemId = null; 1015 String publicId = null; 1016 if (fEntityScanner.skipString("PUBLIC")) { 1017 if (!fEntityScanner.skipSpaces()) { 1018 reportFatalError("SpaceRequiredAfterPUBLIC", null); 1019 } 1020 scanPubidLiteral(fString); 1021 publicId = fString.toString(); 1022 1023 if (!fEntityScanner.skipSpaces() && !optionalSystemId) { 1024 reportFatalError("SpaceRequiredBetweenPublicAndSystem", null); 1025 } 1026 } 1027 1028 if (publicId != null || fEntityScanner.skipString("SYSTEM")) { 1029 if (publicId == null && !fEntityScanner.skipSpaces()) { 1030 reportFatalError("SpaceRequiredAfterSYSTEM", null); 1031 } 1032 int quote = fEntityScanner.peekChar(); 1033 if (quote != '\'' && quote != '"') { 1034 if (publicId != null && optionalSystemId) { 1035 identifiers[0] = null; 1038 identifiers[1] = publicId; 1039 return; 1040 } 1041 reportFatalError("QuoteRequiredInSystemID", null); 1042 } 1043 fEntityScanner.scanChar(); 1044 XMLString ident = fString; 1045 if (fEntityScanner.scanLiteral(quote, ident) != quote) { 1046 fStringBuffer.clear(); 1047 do { 1048 fStringBuffer.append(ident); 1049 int c = fEntityScanner.peekChar(); 1050 if (XMLChar.isMarkup(c) || c == ']') { 1051 fStringBuffer.append((char)fEntityScanner.scanChar()); 1052 } 1053 } while (fEntityScanner.scanLiteral(quote, ident) != quote); 1054 fStringBuffer.append(ident); 1055 ident = fStringBuffer; 1056 } 1057 systemId = ident.toString(); 1058 if (!fEntityScanner.skipChar(quote)) { 1059 reportFatalError("SystemIDUnterminated", null); 1060 } 1061 } 1062 1063 identifiers[0] = systemId; 1065 identifiers[1] = publicId; 1066 } 1067 1068 1069 1088 protected boolean scanPubidLiteral(XMLString literal) 1089 throws IOException , XNIException 1090 { 1091 int quote = fEntityScanner.scanChar(); 1092 if (quote != '\'' && quote != '"') { 1093 reportFatalError("QuoteRequiredInPublicID", null); 1094 return false; 1095 } 1096 1097 fStringBuffer.clear(); 1098 boolean skipSpace = true; 1100 boolean dataok = true; 1101 while (true) { 1102 int c = fEntityScanner.scanChar(); 1103 if (c == ' ' || c == '\n' || c == '\r') { 1104 if (!skipSpace) { 1105 fStringBuffer.append(' '); 1107 skipSpace = true; 1108 } 1109 } 1110 else if (c == quote) { 1111 if (skipSpace) { 1112 fStringBuffer.length--; 1114 } 1115 literal.setValues(fStringBuffer); 1116 break; 1117 } 1118 else if (XMLChar.isPubid(c)) { 1119 fStringBuffer.append((char)c); 1120 skipSpace = false; 1121 } 1122 else if (c == -1) { 1123 reportFatalError("PublicIDUnterminated", null); 1124 return false; 1125 } 1126 else { 1127 dataok = false; 1128 reportFatalError("InvalidCharInPublicID", 1129 new Object []{Integer.toHexString(c)}); 1130 } 1131 } 1132 return dataok; 1133 } 1134 1135 1136 1140 protected void normalizeWhitespace(XMLString value) { 1141 int end = value.offset + value.length; 1142 for (int i = value.offset; i < end; i++) { 1143 int c = value.ch[i]; 1144 if (c < 0x20) { 1151 value.ch[i] = ' '; 1152 } 1153 } 1154 } 1155 1156 1160 1177 public void startEntity(String name, 1178 XMLResourceIdentifier identifier, 1179 String encoding, Augmentations augs) throws XNIException { 1180 1181 fEntityDepth++; 1183 fEntityScanner = fEntityManager.getEntityScanner(); 1185 1186 } 1188 1199 public void endEntity(String name, Augmentations augs) throws XNIException { 1200 1201 fEntityDepth--; 1203 1204 } 1206 1223 protected int scanCharReferenceValue(XMLStringBuffer buf, XMLStringBuffer buf2) 1224 throws IOException , XNIException { 1225 1226 boolean hex = false; 1228 if (fEntityScanner.skipChar('x')) { 1229 if (buf2 != null) { buf2.append('x'); } 1230 hex = true; 1231 fStringBuffer3.clear(); 1232 boolean digit = true; 1233 1234 int c = fEntityScanner.peekChar(); 1235 digit = (c >= '0' && c <= '9') || 1236 (c >= 'a' && c <= 'f') || 1237 (c >= 'A' && c <= 'F'); 1238 if (digit) { 1239 if (buf2 != null) { buf2.append((char)c); } 1240 fEntityScanner.scanChar(); 1241 fStringBuffer3.append((char)c); 1242 1243 do { 1244 c = fEntityScanner.peekChar(); 1245 digit = (c >= '0' && c <= '9') || 1246 (c >= 'a' && c <= 'f') || 1247 (c >= 'A' && c <= 'F'); 1248 if (digit) { 1249 if (buf2 != null) { buf2.append((char)c); } 1250 fEntityScanner.scanChar(); 1251 fStringBuffer3.append((char)c); 1252 } 1253 } while (digit); 1254 } 1255 else { 1256 reportFatalError("HexdigitRequiredInCharRef", null); 1257 } 1258 } 1259 1260 else { 1262 fStringBuffer3.clear(); 1263 boolean digit = true; 1264 1265 int c = fEntityScanner.peekChar(); 1266 digit = c >= '0' && c <= '9'; 1267 if (digit) { 1268 if (buf2 != null) { buf2.append((char)c); } 1269 fEntityScanner.scanChar(); 1270 fStringBuffer3.append((char)c); 1271 1272 do { 1273 c = fEntityScanner.peekChar(); 1274 digit = c >= '0' && c <= '9'; 1275 if (digit) { 1276 if (buf2 != null) { buf2.append((char)c); } 1277 fEntityScanner.scanChar(); 1278 fStringBuffer3.append((char)c); 1279 } 1280 } while (digit); 1281 } 1282 else { 1283 reportFatalError("DigitRequiredInCharRef", null); 1284 } 1285 } 1286 1287 if (!fEntityScanner.skipChar(';')) { 1289 reportFatalError("SemicolonRequiredInCharRef", null); 1290 } 1291 if (buf2 != null) { buf2.append(';'); } 1292 1293 int value = -1; 1295 try { 1296 value = Integer.parseInt(fStringBuffer3.toString(), 1297 hex ? 16 : 10); 1298 1299 if (isInvalid(value)) { 1301 StringBuffer errorBuf = new StringBuffer (fStringBuffer3.length + 1); 1302 if (hex) errorBuf.append('x'); 1303 errorBuf.append(fStringBuffer3.ch, fStringBuffer3.offset, fStringBuffer3.length); 1304 reportFatalError("InvalidCharRef", 1305 new Object []{errorBuf.toString()}); 1306 } 1307 } 1308 catch (NumberFormatException e) { 1309 StringBuffer errorBuf = new StringBuffer (fStringBuffer3.length + 1); 1312 if (hex) errorBuf.append('x'); 1313 errorBuf.append(fStringBuffer3.ch, fStringBuffer3.offset, fStringBuffer3.length); 1314 reportFatalError("InvalidCharRef", 1315 new Object []{errorBuf.toString()}); 1316 } 1317 1318 if (!XMLChar.isSupplemental(value)) { 1320 buf.append((char) value); 1321 } 1322 else { 1323 buf.append(XMLChar.highSurrogate(value)); 1325 buf.append(XMLChar.lowSurrogate(value)); 1326 } 1327 1328 if (fNotifyCharRefs && value != -1) { 1330 String literal = "#" + (hex ? "x" : "") + fStringBuffer3.toString(); 1331 if (!fScanningAttribute) { 1332 fCharRefLiteral = literal; 1333 } 1334 } 1335 1336 return value; 1337 } 1338 1339 protected boolean isInvalid(int value) { 1343 return (XMLChar.isInvalid(value)); 1344 } 1346 protected boolean isInvalidLiteral(int value) { 1350 return (XMLChar.isInvalid(value)); 1351 } 1353 protected boolean isValidNameChar(int value) { 1357 return (XMLChar.isName(value)); 1358 } 1360 protected boolean isValidNameStartChar(int value) { 1364 return (XMLChar.isNameStart(value)); 1365 } 1367 protected boolean isValidNCName(int value) { 1371 return (XMLChar.isNCName(value)); 1372 } 1374 protected boolean isValidNameStartHighSurrogate(int value) { 1379 return false; 1380 } 1382 protected boolean versionSupported(String version ) { 1383 return version.equals("1.0"); 1384 } 1386 protected String getVersionNotSupportedKey () { 1390 return "VersionNotSupported"; 1391 } 1393 1402 protected boolean scanSurrogates(XMLStringBuffer buf) 1403 throws IOException , XNIException { 1404 1405 int high = fEntityScanner.scanChar(); 1406 int low = fEntityScanner.peekChar(); 1407 if (!XMLChar.isLowSurrogate(low)) { 1408 reportFatalError("InvalidCharInContent", 1409 new Object [] {Integer.toString(high, 16)}); 1410 return false; 1411 } 1412 fEntityScanner.scanChar(); 1413 1414 int c = XMLChar.supplemental((char)high, (char)low); 1416 1417 if (isInvalid(c)) { 1419 reportFatalError("InvalidCharInContent", 1420 new Object []{Integer.toString(c, 16)}); 1421 return false; 1422 } 1423 1424 buf.append((char)high); 1426 buf.append((char)low); 1427 1428 return true; 1429 1430 } 1432 1433 1436 protected void reportFatalError(String msgId, Object [] args) 1437 throws XNIException { 1438 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 1439 msgId, args, 1440 XMLErrorReporter.SEVERITY_FATAL_ERROR); 1441 } 1442 1443 private void init() { 1445 fEntityScanner = null; 1446 fEntityDepth = 0; 1448 fReportEntity = true; 1449 fResourceIdentifier.clear(); 1450 } 1451 1452} | Popular Tags |