1 57 58 package com.sun.org.apache.xerces.internal.impl; 59 60 import java.io.CharConversionException ; 61 import java.io.EOFException ; 62 import java.io.IOException ; 63 64 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDDescription; 65 import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException; 66 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 67 import com.sun.org.apache.xerces.internal.util.NamespaceSupport; 68 import com.sun.org.apache.xerces.internal.util.XMLChar; 69 import com.sun.org.apache.xerces.internal.util.XMLEntityDescriptionImpl; 70 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer; 71 import com.sun.org.apache.xerces.internal.xni.Augmentations; 72 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 73 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 74 import com.sun.org.apache.xerces.internal.xni.XMLString; 75 import com.sun.org.apache.xerces.internal.xni.XNIException; 76 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 77 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 78 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner; 79 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 80 81 107 public class XMLDocumentScannerImpl 108 extends XMLDocumentFragmentScannerImpl { 109 110 114 116 117 protected static final int SCANNER_STATE_XML_DECL = 0; 118 119 120 protected static final int SCANNER_STATE_PROLOG = 5; 121 122 123 protected static final int SCANNER_STATE_TRAILING_MISC = 12; 124 125 126 protected static final int SCANNER_STATE_DTD_INTERNAL_DECLS = 17; 127 128 129 protected static final int SCANNER_STATE_DTD_EXTERNAL = 18; 130 131 132 protected static final int SCANNER_STATE_DTD_EXTERNAL_DECLS = 19; 133 134 136 137 protected static final String LOAD_EXTERNAL_DTD = 138 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; 139 140 141 protected static final String DISALLOW_DOCTYPE_DECL_FEATURE = 142 Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE; 143 144 146 147 protected static final String DTD_SCANNER = 148 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY; 149 150 151 protected static final String VALIDATION_MANAGER = 152 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 153 154 155 protected static final String NAMESPACE_CONTEXT = 156 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY; 157 158 159 160 162 163 private static final String [] RECOGNIZED_FEATURES = { 164 LOAD_EXTERNAL_DTD, 165 DISALLOW_DOCTYPE_DECL_FEATURE, 166 }; 167 168 169 private static final Boolean [] FEATURE_DEFAULTS = { 170 Boolean.TRUE, 171 Boolean.FALSE, 172 }; 173 174 175 private static final String [] RECOGNIZED_PROPERTIES = { 176 DTD_SCANNER, 177 VALIDATION_MANAGER, 178 NAMESPACE_CONTEXT, 179 }; 180 181 182 private static final Object [] PROPERTY_DEFAULTS = { 183 null, 184 null, 185 null, 186 }; 187 188 192 194 195 protected XMLDTDScanner fDTDScanner; 196 197 protected ValidationManager fValidationManager; 198 199 201 202 protected boolean fScanningDTD; 203 204 206 207 protected String fDoctypeName; 208 209 210 protected String fDoctypePublicId; 211 212 213 protected String fDoctypeSystemId; 214 215 216 protected NamespaceContext fNamespaceContext = new NamespaceSupport(); 217 218 220 221 protected boolean fLoadExternalDTD = true; 222 223 224 protected boolean fDisallowDoctype = false; 225 226 228 229 protected boolean fSeenDoctypeDecl; 230 231 233 234 protected Dispatcher fXMLDeclDispatcher = new XMLDeclDispatcher(); 235 236 237 protected Dispatcher fPrologDispatcher = new PrologDispatcher(); 238 239 240 protected Dispatcher fDTDDispatcher = new DTDDispatcher(); 241 242 243 protected Dispatcher fTrailingMiscDispatcher = new TrailingMiscDispatcher(); 244 245 247 248 private String [] fStrings = new String [3]; 249 250 251 private XMLString fString = new XMLString(); 252 253 254 private XMLStringBuffer fStringBuffer = new XMLStringBuffer(); 255 256 257 private XMLInputSource fExternalSubsetSource = null; 258 259 263 264 public XMLDocumentScannerImpl() {} 266 270 277 public void setInputSource(XMLInputSource inputSource) throws IOException { 278 fEntityManager.setEntityHandler(this); 279 fEntityManager.startDocumentEntity(inputSource); 280 } 283 287 301 public void reset(XMLComponentManager componentManager) 302 throws XMLConfigurationException { 303 304 super.reset(componentManager); 305 306 fDoctypeName = null; 308 fDoctypePublicId = null; 309 fDoctypeSystemId = null; 310 fSeenDoctypeDecl = false; 311 fScanningDTD = false; 312 fExternalSubsetSource = null; 313 314 if (!fParserSettings) { 315 fNamespaceContext.reset(); 317 setScannerState(SCANNER_STATE_XML_DECL); 319 setDispatcher(fXMLDeclDispatcher); 320 return; 321 } 322 323 try { 325 fLoadExternalDTD = componentManager.getFeature(LOAD_EXTERNAL_DTD); 326 } 327 catch (XMLConfigurationException e) { 328 fLoadExternalDTD = true; 329 } 330 try { 331 fDisallowDoctype = componentManager.getFeature(DISALLOW_DOCTYPE_DECL_FEATURE); 332 } 333 catch (XMLConfigurationException e) { 334 fDisallowDoctype = false; 335 } 336 337 fDTDScanner = (XMLDTDScanner)componentManager.getProperty(DTD_SCANNER); 339 try { 340 fValidationManager = (ValidationManager)componentManager.getProperty(VALIDATION_MANAGER); 341 } 342 catch (XMLConfigurationException e) { 343 fValidationManager = null; 344 } 345 346 try { 347 fNamespaceContext = (NamespaceContext)componentManager.getProperty(NAMESPACE_CONTEXT); 348 } 349 catch (XMLConfigurationException e) { } 350 if (fNamespaceContext == null) { 351 fNamespaceContext = new NamespaceSupport(); 352 } 353 fNamespaceContext.reset(); 354 355 setScannerState(SCANNER_STATE_XML_DECL); 357 setDispatcher(fXMLDeclDispatcher); 358 359 } 361 366 public String [] getRecognizedFeatures() { 367 String [] featureIds = super.getRecognizedFeatures(); 368 int length = featureIds != null ? featureIds.length : 0; 369 String [] combinedFeatureIds = new String [length + RECOGNIZED_FEATURES.length]; 370 if (featureIds != null) { 371 System.arraycopy(featureIds, 0, combinedFeatureIds, 0, featureIds.length); 372 } 373 System.arraycopy(RECOGNIZED_FEATURES, 0, combinedFeatureIds, length, RECOGNIZED_FEATURES.length); 374 return combinedFeatureIds; 375 } 377 392 public void setFeature(String featureId, boolean state) 393 throws XMLConfigurationException { 394 395 super.setFeature(featureId, state); 396 397 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 399 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 400 401 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 402 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { 403 fLoadExternalDTD = state; 404 return; 405 } 406 else if (suffixLength == Constants.DISALLOW_DOCTYPE_DECL_FEATURE.length() && 407 featureId.endsWith(Constants.DISALLOW_DOCTYPE_DECL_FEATURE)) { 408 fDisallowDoctype = state; 409 return; 410 } 411 } 412 413 } 415 420 public String [] getRecognizedProperties() { 421 String [] propertyIds = super.getRecognizedProperties(); 422 int length = propertyIds != null ? propertyIds.length : 0; 423 String [] combinedPropertyIds = new String [length + RECOGNIZED_PROPERTIES.length]; 424 if (propertyIds != null) { 425 System.arraycopy(propertyIds, 0, combinedPropertyIds, 0, propertyIds.length); 426 } 427 System.arraycopy(RECOGNIZED_PROPERTIES, 0, combinedPropertyIds, length, RECOGNIZED_PROPERTIES.length); 428 return combinedPropertyIds; 429 } 431 446 public void setProperty(String propertyId, Object value) 447 throws XMLConfigurationException { 448 449 super.setProperty(propertyId, value); 450 451 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 453 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 454 455 if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 456 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) { 457 fDTDScanner = (XMLDTDScanner)value; 458 } 459 if (suffixLength == Constants.NAMESPACE_CONTEXT_PROPERTY.length() && 460 propertyId.endsWith(Constants.NAMESPACE_CONTEXT_PROPERTY)) { 461 if (value != null) { 462 fNamespaceContext = (NamespaceContext)value; 463 } 464 } 465 466 return; 467 } 468 469 } 471 480 public Boolean getFeatureDefault(String featureId) { 481 482 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 483 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 484 return FEATURE_DEFAULTS[i]; 485 } 486 } 487 return super.getFeatureDefault(featureId); 488 } 490 499 public Object getPropertyDefault(String propertyId) { 500 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 501 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 502 return PROPERTY_DEFAULTS[i]; 503 } 504 } 505 return super.getPropertyDefault(propertyId); 506 } 508 512 527 public void startEntity(String name, 528 XMLResourceIdentifier identifier, 529 String encoding, Augmentations augs) throws XNIException { 530 531 super.startEntity(name, identifier, encoding, augs); 532 533 if (!name.equals("[xml]") && fEntityScanner.isExternal()) { 535 setScannerState(SCANNER_STATE_TEXT_DECL); 536 } 537 538 if (fDocumentHandler != null && name.equals("[xml]")) { 540 fDocumentHandler.startDocument(fEntityScanner, encoding, fNamespaceContext, null); 541 } 542 543 } 545 554 public void endEntity(String name, Augmentations augs) throws XNIException { 555 556 super.endEntity(name, augs); 557 558 if (fDocumentHandler != null && name.equals("[xml]")) { 560 fDocumentHandler.endDocument(null); 561 } 562 563 } 565 569 571 572 protected Dispatcher createContentDispatcher() { 573 return new ContentDispatcher(); 574 } 576 578 579 protected boolean scanDoctypeDecl() throws IOException , XNIException { 580 581 if (!fEntityScanner.skipSpaces()) { 583 reportFatalError("MSG_SPACE_REQUIRED_BEFORE_ROOT_ELEMENT_TYPE_IN_DOCTYPEDECL", 584 null); 585 } 586 587 fDoctypeName = fEntityScanner.scanName(); 589 if (fDoctypeName == null) { 590 reportFatalError("MSG_ROOT_ELEMENT_TYPE_REQUIRED", null); 591 } 592 593 if (fEntityScanner.skipSpaces()) { 595 scanExternalID(fStrings, false); 596 fDoctypeSystemId = fStrings[0]; 597 fDoctypePublicId = fStrings[1]; 598 fEntityScanner.skipSpaces(); 599 } 600 601 fHasExternalDTD = fDoctypeSystemId != null; 602 603 if (!fHasExternalDTD && fExternalSubsetResolver != null) { 605 XMLDTDDescription desc = new XMLDTDDescription(null, 606 null, fEntityManager.getCurrentResourceIdentifier().getExpandedSystemId(), null, fDoctypeName); 607 fExternalSubsetSource = fExternalSubsetResolver.getExternalSubset(desc); 608 fHasExternalDTD = fExternalSubsetSource != null; 609 } 610 611 if (fDocumentHandler != null) { 613 if (fExternalSubsetSource == null) { 619 fDocumentHandler.doctypeDecl(fDoctypeName, fDoctypePublicId, fDoctypeSystemId, null); 620 } 621 else { 622 fDocumentHandler.doctypeDecl(fDoctypeName, fExternalSubsetSource.getPublicId(), fExternalSubsetSource.getSystemId(), null); 623 } 624 } 625 626 boolean internalSubset = true; 628 if (!fEntityScanner.skipChar('[')) { 629 internalSubset = false; 630 fEntityScanner.skipSpaces(); 631 if (!fEntityScanner.skipChar('>')) { 632 reportFatalError("DoctypedeclUnterminated", new Object []{fDoctypeName}); 633 } 634 fMarkupDepth--; 635 } 636 637 return internalSubset; 638 639 } 641 645 646 protected String getScannerStateName(int state) { 647 648 switch (state) { 649 case SCANNER_STATE_XML_DECL: return "SCANNER_STATE_XML_DECL"; 650 case SCANNER_STATE_PROLOG: return "SCANNER_STATE_PROLOG"; 651 case SCANNER_STATE_TRAILING_MISC: return "SCANNER_STATE_TRAILING_MISC"; 652 case SCANNER_STATE_DTD_INTERNAL_DECLS: return "SCANNER_STATE_DTD_INTERNAL_DECLS"; 653 case SCANNER_STATE_DTD_EXTERNAL: return "SCANNER_STATE_DTD_EXTERNAL"; 654 case SCANNER_STATE_DTD_EXTERNAL_DECLS: return "SCANNER_STATE_DTD_EXTERNAL_DECLS"; 655 } 656 return super.getScannerStateName(state); 657 658 } 660 664 669 protected final class XMLDeclDispatcher 670 implements Dispatcher { 671 672 676 688 public boolean dispatch(boolean complete) 689 throws IOException , XNIException { 690 691 setScannerState(SCANNER_STATE_PROLOG); 694 setDispatcher(fPrologDispatcher); 695 696 try { 698 if (fEntityScanner.skipString("<?xml")) { 699 fMarkupDepth++; 700 if (XMLChar.isName(fEntityScanner.peekChar())) { 703 fStringBuffer.clear(); 704 fStringBuffer.append("xml"); 705 if (fNamespaces) { 706 while (XMLChar.isNCName(fEntityScanner.peekChar())) { 707 fStringBuffer.append((char)fEntityScanner.scanChar()); 708 } 709 } 710 else { 711 while (XMLChar.isName(fEntityScanner.peekChar())) { 712 fStringBuffer.append((char)fEntityScanner.scanChar()); 713 } 714 } 715 String target = fSymbolTable.addSymbol(fStringBuffer.ch, fStringBuffer.offset, fStringBuffer.length); 716 scanPIData(target, fString); 717 } 718 719 else { 721 scanXMLDeclOrTextDecl(false); 722 } 723 } 724 fEntityManager.fCurrentEntity.mayReadChunks = true; 725 726 return true; 728 } 729 catch (MalformedByteSequenceException e) { 731 fErrorReporter.reportError(e.getDomain(), e.getKey(), 732 e.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); 733 return false; 734 } 735 catch (CharConversionException e) { 736 reportFatalError("CharConversionFailure", null); 737 return false; 738 } 739 catch (EOFException e) { 741 reportFatalError("PrematureEOF", null); 742 return false; 743 } 745 746 747 } 749 } 751 756 protected final class PrologDispatcher 757 implements Dispatcher { 758 759 763 775 public boolean dispatch(boolean complete) 776 throws IOException , XNIException { 777 778 try { 779 boolean again; 780 do { 781 again = false; 782 switch (fScannerState) { 783 case SCANNER_STATE_PROLOG: { 784 fEntityScanner.skipSpaces(); 785 if (fEntityScanner.skipChar('<')) { 786 setScannerState(SCANNER_STATE_START_OF_MARKUP); 787 again = true; 788 } 789 else if (fEntityScanner.skipChar('&')) { 790 setScannerState(SCANNER_STATE_REFERENCE); 791 again = true; 792 } 793 else { 794 setScannerState(SCANNER_STATE_CONTENT); 795 again = true; 796 } 797 break; 798 } 799 case SCANNER_STATE_START_OF_MARKUP: { 800 fMarkupDepth++; 801 if (fEntityScanner.skipChar('!')) { 802 if (fEntityScanner.skipChar('-')) { 803 if (!fEntityScanner.skipChar('-')) { 804 reportFatalError("InvalidCommentStart", 805 null); 806 } 807 setScannerState(SCANNER_STATE_COMMENT); 808 again = true; 809 } 810 else if (fEntityScanner.skipString("DOCTYPE")) { 811 setScannerState(SCANNER_STATE_DOCTYPE); 812 again = true; 813 } 814 else { 815 reportFatalError("MarkupNotRecognizedInProlog", 816 null); 817 } 818 } 819 else if (isValidNameStartChar(fEntityScanner.peekChar())) { 820 setScannerState(SCANNER_STATE_ROOT_ELEMENT); 821 setDispatcher(fContentDispatcher); 822 return true; 823 } 824 else if (fEntityScanner.skipChar('?')) { 825 setScannerState(SCANNER_STATE_PI); 826 again = true; 827 } 828 else if (isValidNameStartHighSurrogate(fEntityScanner.peekChar())) { 829 setScannerState(SCANNER_STATE_ROOT_ELEMENT); 830 setDispatcher(fContentDispatcher); 831 return true; 832 } 833 else { 834 reportFatalError("MarkupNotRecognizedInProlog", 835 null); 836 } 837 break; 838 } 839 case SCANNER_STATE_COMMENT: { 840 scanComment(); 841 setScannerState(SCANNER_STATE_PROLOG); 842 break; 843 } 844 case SCANNER_STATE_PI: { 845 scanPI(); 846 setScannerState(SCANNER_STATE_PROLOG); 847 break; 848 } 849 case SCANNER_STATE_DOCTYPE: { 850 if (fDisallowDoctype) { 851 reportFatalError("DoctypeNotAllowed", null); 852 } 853 if (fSeenDoctypeDecl) { 854 reportFatalError("AlreadySeenDoctype", null); 855 } 856 fSeenDoctypeDecl = true; 857 858 if (scanDoctypeDecl()) { 861 setScannerState(SCANNER_STATE_DTD_INTERNAL_DECLS); 862 setDispatcher(fDTDDispatcher); 863 return true; 864 } 865 866 if (fDoctypeSystemId != null) { 868 if (((fValidation || fLoadExternalDTD) 869 && (fValidationManager == null || !fValidationManager.isCachedDTD()))) { 870 setScannerState(SCANNER_STATE_DTD_EXTERNAL); 871 setDispatcher(fDTDDispatcher); 872 return true; 873 } 874 } 875 else if (fExternalSubsetSource != null) { 876 if (((fValidation || fLoadExternalDTD) 877 && (fValidationManager == null || !fValidationManager.isCachedDTD()))) { 878 fDTDScanner.setInputSource(fExternalSubsetSource); 880 fExternalSubsetSource = null; 881 setScannerState(SCANNER_STATE_DTD_EXTERNAL_DECLS); 882 setDispatcher(fDTDDispatcher); 883 return true; 884 } 885 } 886 887 892 fDTDScanner.setInputSource(null); 895 setScannerState(SCANNER_STATE_PROLOG); 896 break; 897 } 898 case SCANNER_STATE_CONTENT: { 899 reportFatalError("ContentIllegalInProlog", null); 900 fEntityScanner.scanChar(); 901 } 902 case SCANNER_STATE_REFERENCE: { 903 reportFatalError("ReferenceIllegalInProlog", null); 904 } 905 } 906 } while (complete || again); 907 908 if (complete) { 909 if (fEntityScanner.scanChar() != '<') { 910 reportFatalError("RootElementRequired", null); 911 } 912 setScannerState(SCANNER_STATE_ROOT_ELEMENT); 913 setDispatcher(fContentDispatcher); 914 } 915 } 916 catch (MalformedByteSequenceException e) { 918 fErrorReporter.reportError(e.getDomain(), e.getKey(), 919 e.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); 920 return false; 921 } 922 catch (CharConversionException e) { 923 reportFatalError("CharConversionFailure", null); 924 return false; 925 } 926 catch (EOFException e) { 928 reportFatalError("PrematureEOF", null); 929 return false; 930 } 932 933 return true; 934 935 } 937 } 939 944 protected final class DTDDispatcher 945 implements Dispatcher { 946 947 951 963 public boolean dispatch(boolean complete) 964 throws IOException , XNIException { 965 fEntityManager.setEntityHandler(null); 966 try { 967 boolean again; 968 XMLEntityDescriptionImpl entityDescription = new XMLEntityDescriptionImpl(); 969 do { 970 again = false; 971 switch (fScannerState) { 972 case SCANNER_STATE_DTD_INTERNAL_DECLS: { 973 boolean completeDTD = true; 976 977 boolean moreToScan = fDTDScanner.scanDTDInternalSubset(completeDTD, fStandalone, fHasExternalDTD && fLoadExternalDTD); 978 if (!moreToScan) { 979 if (!fEntityScanner.skipChar(']')) { 981 reportFatalError("EXPECTED_SQUARE_BRACKET_TO_CLOSE_INTERNAL_SUBSET", 982 null); 983 } 984 fEntityScanner.skipSpaces(); 985 if (!fEntityScanner.skipChar('>')) { 986 reportFatalError("DoctypedeclUnterminated", new Object []{fDoctypeName}); 987 } 988 fMarkupDepth--; 989 990 if (fDoctypeSystemId != null) { 992 if ((fValidation || fLoadExternalDTD) 993 && (fValidationManager == null || !fValidationManager.isCachedDTD())) { 994 setScannerState(SCANNER_STATE_DTD_EXTERNAL); 995 break; 996 } 997 } 998 else if (fExternalSubsetSource != null) { 999 if ((fValidation || fLoadExternalDTD) 1000 && (fValidationManager == null || !fValidationManager.isCachedDTD())) { 1001 fDTDScanner.setInputSource(fExternalSubsetSource); 1003 fExternalSubsetSource = null; 1004 setScannerState(SCANNER_STATE_DTD_EXTERNAL_DECLS); 1005 break; 1006 } 1007 } 1008 1009 setScannerState(SCANNER_STATE_PROLOG); 1011 setDispatcher(fPrologDispatcher); 1012 fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this); 1013 return true; 1014 } 1015 break; 1016 } 1017 case SCANNER_STATE_DTD_EXTERNAL: { 1018 entityDescription.setDescription("[dtd]", fDoctypePublicId, fDoctypeSystemId, null, null); 1019 XMLInputSource xmlInputSource = 1020 fEntityManager.resolveEntity(entityDescription); 1021 fDTDScanner.setInputSource(xmlInputSource); 1022 setScannerState(SCANNER_STATE_DTD_EXTERNAL_DECLS); 1023 again = true; 1024 break; 1025 } 1026 case SCANNER_STATE_DTD_EXTERNAL_DECLS: { 1027 boolean completeDTD = true; 1030 boolean moreToScan = fDTDScanner.scanDTDExternalSubset(completeDTD); 1031 if (!moreToScan) { 1032 setScannerState(SCANNER_STATE_PROLOG); 1033 setDispatcher(fPrologDispatcher); 1034 fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this); 1035 return true; 1036 } 1037 break; 1038 } 1039 default: { 1040 throw new XNIException("DTDDispatcher#dispatch: scanner state="+fScannerState+" ("+getScannerStateName(fScannerState)+')'); 1041 } 1042 } 1043 } while (complete || again); 1044 } 1045 catch (MalformedByteSequenceException e) { 1047 fErrorReporter.reportError(e.getDomain(), e.getKey(), 1048 e.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); 1049 return false; 1050 } 1051 catch (CharConversionException e) { 1052 reportFatalError("CharConversionFailure", null); 1053 return false; 1054 } 1055 catch (EOFException e) { 1057 reportFatalError("PrematureEOF", null); 1058 return false; 1059 } 1061 1062 finally { 1064 fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this); 1065 } 1066 1067 return true; 1068 1069 } 1071 } 1073 1079 protected class ContentDispatcher 1080 extends FragmentContentDispatcher { 1081 1082 1086 1088 1091 1099 protected boolean scanForDoctypeHook() 1100 throws IOException , XNIException { 1101 1102 if (fEntityScanner.skipString("DOCTYPE")) { 1103 setScannerState(SCANNER_STATE_DOCTYPE); 1104 return true; 1105 } 1106 return false; 1107 1108 } 1110 1123 protected boolean elementDepthIsZeroHook() 1124 throws IOException , XNIException { 1125 1126 setScannerState(SCANNER_STATE_TRAILING_MISC); 1127 setDispatcher(fTrailingMiscDispatcher); 1128 return true; 1129 1130 } 1132 1144 protected boolean scanRootElementHook() 1145 throws IOException , XNIException { 1146 1147 if (fExternalSubsetResolver != null && !fSeenDoctypeDecl 1148 && !fDisallowDoctype && (fValidation || fLoadExternalDTD)) { 1149 scanStartElementName(); 1150 resolveExternalSubsetAndRead(); 1151 if (scanStartElementAfterName()) { 1152 setScannerState(SCANNER_STATE_TRAILING_MISC); 1153 setDispatcher(fTrailingMiscDispatcher); 1154 return true; 1155 } 1156 } 1157 else if (scanStartElement()) { 1158 setScannerState(SCANNER_STATE_TRAILING_MISC); 1159 setDispatcher(fTrailingMiscDispatcher); 1160 return true; 1161 } 1162 return false; 1163 1164 } 1166 1173 protected void endOfFileHook(EOFException e) 1174 throws IOException , XNIException { 1175 1176 reportFatalError("PrematureEOF", null); 1177 1180 } 1182 1186 protected void resolveExternalSubsetAndRead() 1187 throws IOException , XNIException { 1188 XMLDTDDescription desc = new XMLDTDDescription(null, 1189 null, fEntityManager.getCurrentResourceIdentifier().getExpandedSystemId(), null, fElementQName.rawname); 1190 XMLInputSource src = fExternalSubsetResolver.getExternalSubset(desc); 1191 if (src != null) { 1192 fDoctypeName = fElementQName.rawname; 1193 fDoctypePublicId = src.getPublicId(); 1194 fDoctypeSystemId = src.getSystemId(); 1195 if (fDocumentHandler != null) { 1197 fDocumentHandler.doctypeDecl(fDoctypeName, fDoctypePublicId, fDoctypeSystemId, null); 1200 } 1201 try { 1202 fDTDScanner.setInputSource(src); 1203 while (fDTDScanner.scanDTDExternalSubset(true)); 1204 } 1205 finally { 1206 fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this); 1207 } 1208 } 1209 } 1211 } 1213 1219 protected final class TrailingMiscDispatcher 1220 implements Dispatcher { 1221 1222 1226 1238 public boolean dispatch(boolean complete) 1239 throws IOException , XNIException { 1240 1241 try { 1242 boolean again; 1243 do { 1244 again = false; 1245 switch (fScannerState) { 1246 case SCANNER_STATE_TRAILING_MISC: { 1247 fEntityScanner.skipSpaces(); 1248 if (fEntityScanner.skipChar('<')) { 1249 setScannerState(SCANNER_STATE_START_OF_MARKUP); 1250 again = true; 1251 } 1252 else { 1253 setScannerState(SCANNER_STATE_CONTENT); 1254 again = true; 1255 } 1256 break; 1257 } 1258 case SCANNER_STATE_START_OF_MARKUP: { 1259 fMarkupDepth++; 1260 if (fEntityScanner.skipChar('?')) { 1261 setScannerState(SCANNER_STATE_PI); 1262 again = true; 1263 } 1264 else if (fEntityScanner.skipChar('!')) { 1265 setScannerState(SCANNER_STATE_COMMENT); 1266 again = true; 1267 } 1268 else if (fEntityScanner.skipChar('/')) { 1269 reportFatalError("MarkupNotRecognizedInMisc", 1270 null); 1271 again = true; 1272 } 1273 else if (isValidNameStartChar(fEntityScanner.peekChar())) { 1274 reportFatalError("MarkupNotRecognizedInMisc", 1275 null); 1276 scanStartElement(); 1277 setScannerState(SCANNER_STATE_CONTENT); 1278 } 1279 else if (isValidNameStartHighSurrogate(fEntityScanner.peekChar())) { 1280 reportFatalError("MarkupNotRecognizedInMisc", 1281 null); 1282 scanStartElement(); 1283 setScannerState(SCANNER_STATE_CONTENT); 1284 } 1285 else { 1286 reportFatalError("MarkupNotRecognizedInMisc", 1287 null); 1288 } 1289 break; 1290 } 1291 case SCANNER_STATE_PI: { 1292 scanPI(); 1293 setScannerState(SCANNER_STATE_TRAILING_MISC); 1294 break; 1295 } 1296 case SCANNER_STATE_COMMENT: { 1297 if (!fEntityScanner.skipString("--")) { 1298 reportFatalError("InvalidCommentStart", null); 1299 } 1300 scanComment(); 1301 setScannerState(SCANNER_STATE_TRAILING_MISC); 1302 break; 1303 } 1304 case SCANNER_STATE_CONTENT: { 1305 int ch = fEntityScanner.peekChar(); 1306 if (ch == -1) { 1307 setScannerState(SCANNER_STATE_TERMINATED); 1308 return false; 1309 } 1310 reportFatalError("ContentIllegalInTrailingMisc", 1311 null); 1312 fEntityScanner.scanChar(); 1313 setScannerState(SCANNER_STATE_TRAILING_MISC); 1314 break; 1315 } 1316 case SCANNER_STATE_REFERENCE: { 1317 reportFatalError("ReferenceIllegalInTrailingMisc", 1318 null); 1319 setScannerState(SCANNER_STATE_TRAILING_MISC); 1320 break; 1321 } 1322 case SCANNER_STATE_TERMINATED: { 1323 return false; 1324 } 1325 } 1326 } while (complete || again); 1327 } 1328 catch (MalformedByteSequenceException e) { 1330 fErrorReporter.reportError(e.getDomain(), e.getKey(), 1331 e.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); 1332 return false; 1333 } 1334 catch (CharConversionException e) { 1335 reportFatalError("CharConversionFailure", null); 1336 return false; 1337 } 1338 catch (EOFException e) { 1339 if (fMarkupDepth != 0) { 1343 reportFatalError("PrematureEOF", null); 1344 return false; 1345 } 1347 1348 setScannerState(SCANNER_STATE_TERMINATED); 1349 return false; 1350 } 1351 1352 return true; 1353 1354 } 1356 } 1358} | Popular Tags |