xmldocumentscannerimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,359 行 · 第 1/4 页
JAVA
1,359 行
fDTDScanner.setInputSource(xmlInputSource); setScannerState(SCANNER_STATE_DTD_EXTERNAL_DECLS); again = true; break; } case SCANNER_STATE_DTD_EXTERNAL_DECLS: { // REVISIT: Should there be a feature for // the "complete" parameter? boolean completeDTD = true; boolean moreToScan = fDTDScanner.scanDTDExternalSubset(completeDTD); if (!moreToScan) { setScannerState(SCANNER_STATE_PROLOG); setDispatcher(fPrologDispatcher); fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this); return true; } break; } default: { throw new XNIException("DTDDispatcher#dispatch: scanner state="+fScannerState+" ("+getScannerStateName(fScannerState)+')'); } } } while (complete || again); } // encoding errors catch (MalformedByteSequenceException e) { fErrorReporter.reportError(e.getDomain(), e.getKey(), e.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); return false; } catch (CharConversionException e) { reportFatalError("CharConversionFailure", null); return false; } // premature end of file catch (EOFException e) { reportFatalError("PrematureEOF", null); return false; //throw e; } // cleanup finally { fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this); } return true; } // dispatch(boolean):boolean } // class DTDDispatcher /** * Dispatcher to handle content scanning. * * @author Andy Clark, IBM * @author Eric Ye, IBM */ protected class ContentDispatcher extends FragmentContentDispatcher { // // Protected methods // // hooks // NOTE: These hook methods are added so that the full document // scanner can share the majority of code with this class. /** * Scan for DOCTYPE hook. This method is a hook for subclasses * to add code to handle scanning for a the "DOCTYPE" string * after the string "<!" has been scanned. * * @return True if the "DOCTYPE" was scanned; false if "DOCTYPE" * was not scanned. */ protected boolean scanForDoctypeHook() throws IOException, XNIException { if (fEntityScanner.skipString("DOCTYPE")) { setScannerState(SCANNER_STATE_DOCTYPE); return true; } return false; } // scanForDoctypeHook():boolean /** * Element depth iz zero. This methos is a hook for subclasses * to add code to handle when the element depth hits zero. When * scanning a document fragment, an element depth of zero is * normal. However, when scanning a full XML document, the * scanner must handle the trailing miscellanous section of * the document after the end of the document's root element. * * @return True if the caller should stop and return true which * allows the scanner to switch to a new scanning * dispatcher. A return value of false indicates that * the content dispatcher should continue as normal. */ protected boolean elementDepthIsZeroHook() throws IOException, XNIException { setScannerState(SCANNER_STATE_TRAILING_MISC); setDispatcher(fTrailingMiscDispatcher); return true; } // elementDepthIsZeroHook():boolean /** * Scan for root element hook. This method is a hook for * subclasses to add code that handles scanning for the root * element. When scanning a document fragment, there is no * "root" element. However, when scanning a full XML document, * the scanner must handle the root element specially. * * @return True if the caller should stop and return true which * allows the scanner to switch to a new scanning * dispatcher. A return value of false indicates that * the content dispatcher should continue as normal. */ protected boolean scanRootElementHook() throws IOException, XNIException { if (fExternalSubsetResolver != null && !fSeenDoctypeDecl && !fDisallowDoctype && (fValidation || fLoadExternalDTD)) { scanStartElementName(); resolveExternalSubsetAndRead(); if (scanStartElementAfterName()) { setScannerState(SCANNER_STATE_TRAILING_MISC); setDispatcher(fTrailingMiscDispatcher); return true; } } else if (scanStartElement()) { setScannerState(SCANNER_STATE_TRAILING_MISC); setDispatcher(fTrailingMiscDispatcher); return true; } return false; } // scanRootElementHook():boolean /** * End of file hook. This method is a hook for subclasses to * add code that handles the end of file. The end of file in * a document fragment is OK if the markup depth is zero. * However, when scanning a full XML document, an end of file * is always premature. */ protected void endOfFileHook(EOFException e) throws IOException, XNIException { reportFatalError("PrematureEOF", null); // in case continue-after-fatal-error set, should not do this... //throw e; } // endOfFileHook() /** * <p>Attempt to locate an external subset for a document that does not otherwise * have one. If an external subset is located, then it is scanned.</p> */ protected void resolveExternalSubsetAndRead() throws IOException, XNIException { XMLDTDDescription desc = new XMLDTDDescription(null, null, fEntityManager.getCurrentResourceIdentifier().getExpandedSystemId(), null, fElementQName.rawname); XMLInputSource src = fExternalSubsetResolver.getExternalSubset(desc); if (src != null) { fDoctypeName = fElementQName.rawname; fDoctypePublicId = src.getPublicId(); fDoctypeSystemId = src.getSystemId(); // call document handler if (fDocumentHandler != null) { // This inserts a doctypeDecl event into the stream though no // DOCTYPE existed in the instance document. fDocumentHandler.doctypeDecl(fDoctypeName, fDoctypePublicId, fDoctypeSystemId, null); } try { fDTDScanner.setInputSource(src); while (fDTDScanner.scanDTDExternalSubset(true)); } finally { fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this); } } } // resolveExternalSubsetAndRead() } // class ContentDispatcher /** * Dispatcher to handle trailing miscellaneous section scanning. * * @author Andy Clark, IBM * @author Eric Ye, IBM */ protected final class TrailingMiscDispatcher implements Dispatcher { // // Dispatcher methods // /** * Dispatch an XML "event". * * @param complete True if this dispatcher is intended to scan * and dispatch as much as possible. * * @return True if there is more to dispatch either from this * or a another dispatcher. * * @throws IOException Thrown on i/o error. * @throws XNIException Thrown on parse error. */ public boolean dispatch(boolean complete) throws IOException, XNIException { try { boolean again; do { again = false; switch (fScannerState) { case SCANNER_STATE_TRAILING_MISC: { fEntityScanner.skipSpaces(); if (fEntityScanner.skipChar('<')) { setScannerState(SCANNER_STATE_START_OF_MARKUP); again = true; } else { setScannerState(SCANNER_STATE_CONTENT); again = true; } break; } case SCANNER_STATE_START_OF_MARKUP: { fMarkupDepth++; if (fEntityScanner.skipChar('?')) { setScannerState(SCANNER_STATE_PI); again = true; } else if (fEntityScanner.skipChar('!')) { setScannerState(SCANNER_STATE_COMMENT); again = true; } else if (fEntityScanner.skipChar('/')) { reportFatalError("MarkupNotRecognizedInMisc", null); again = true; } else if (isValidNameStartChar(fEntityScanner.peekChar())) { reportFatalError("MarkupNotRecognizedInMisc", null); scanStartElement(); setScannerState(SCANNER_STATE_CONTENT); } else if (isValidNameStartHighSurrogate(fEntityScanner.peekChar())) { reportFatalError("MarkupNotRecognizedInMisc", null); scanStartElement(); setScannerState(SCANNER_STATE_CONTENT); } else { reportFatalError("MarkupNotRecognizedInMisc", null); } break; } case SCANNER_STATE_PI: { scanPI(); setScannerState(SCANNER_STATE_TRAILING_MISC); break; } case SCANNER_STATE_COMMENT: { if (!fEntityScanner.skipString("--")) { reportFatalError("InvalidCommentStart", null); } scanComment(); setScannerState(SCANNER_STATE_TRAILING_MISC); break; } case SCANNER_STATE_CONTENT: { int ch = fEntityScanner.peekChar(); if (ch == -1) { setScannerState(SCANNER_STATE_TERMINATED); return false; } reportFatalError("ContentIllegalInTrailingMisc", null); fEntityScanner.scanChar(); setScannerState(SCANNER_STATE_TRAILING_MISC); break; } case SCANNER_STATE_REFERENCE: { reportFatalError("ReferenceIllegalInTrailingMisc", null); setScannerState(SCANNER_STATE_TRAILING_MISC); break; } case SCANNER_STATE_TERMINATED: { return false; } } } while (complete || again); } // encoding errors catch (MalformedByteSequenceException e) { fErrorReporter.reportError(e.getDomain(), e.getKey(), e.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); return false; } catch (CharConversionException e) { reportFatalError("CharConversionFailure", null); return false; } catch (EOFException e) { // NOTE: This is the only place we're allowed to reach // the real end of the document stream. Unless the // end of file was reached prematurely. if (fMarkupDepth != 0) { reportFatalError("PrematureEOF", null); return false; //throw e; } setScannerState(SCANNER_STATE_TERMINATED); return false; } return true; } // dispatch(boolean):boolean } // class TrailingMiscDispatcher} // class XMLDocumentScannerImpl
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?