xmldocumentfragmentscannerimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,788 行 · 第 1/5 页
JAVA
1,788 行
* Sets the dispatcher. * * @param dispatcher The new dispatcher. */ protected final void setDispatcher(Dispatcher dispatcher) { fDispatcher = dispatcher; if (DEBUG_DISPATCHER) { System.out.print("%%% setDispatcher: "); System.out.print(getDispatcherName(dispatcher)); System.out.println(); } } // // Private methods // /** Returns the scanner state name. */ protected String getScannerStateName(int state) { switch (state) { case SCANNER_STATE_DOCTYPE: return "SCANNER_STATE_DOCTYPE"; case SCANNER_STATE_ROOT_ELEMENT: return "SCANNER_STATE_ROOT_ELEMENT"; case SCANNER_STATE_START_OF_MARKUP: return "SCANNER_STATE_START_OF_MARKUP"; case SCANNER_STATE_COMMENT: return "SCANNER_STATE_COMMENT"; case SCANNER_STATE_PI: return "SCANNER_STATE_PI"; case SCANNER_STATE_CONTENT: return "SCANNER_STATE_CONTENT"; case SCANNER_STATE_REFERENCE: return "SCANNER_STATE_REFERENCE"; case SCANNER_STATE_END_OF_INPUT: return "SCANNER_STATE_END_OF_INPUT"; case SCANNER_STATE_TERMINATED: return "SCANNER_STATE_TERMINATED"; case SCANNER_STATE_CDATA: return "SCANNER_STATE_CDATA"; case SCANNER_STATE_TEXT_DECL: return "SCANNER_STATE_TEXT_DECL"; } return "??? ("+state+')'; } // getScannerStateName(int):String /** Returns the dispatcher name. */ public String getDispatcherName(Dispatcher dispatcher) { if (DEBUG_DISPATCHER) { if (dispatcher != null) { String name = dispatcher.getClass().getName(); int index = name.lastIndexOf('.'); if (index != -1) { name = name.substring(index + 1); index = name.lastIndexOf('$'); if (index != -1) { name = name.substring(index + 1); } } return name; } } return "null"; } // getDispatcherName():String // // Classes // /** * Element stack. This stack operates without synchronization, error * checking, and it re-uses objects instead of throwing popped items * away. * * @author Andy Clark, IBM */ protected static class ElementStack { // // Data // /** The stack data. */ protected QName[] fElements; /** The size of the stack. */ protected int fSize; // // Constructors // /** Default constructor. */ public ElementStack() { fElements = new QName[10]; for (int i = 0; i < fElements.length; i++) { fElements[i] = new QName(); } } // <init>() // // Public methods // /** * Pushes an element on the stack. * <p> * <strong>Note:</strong> The QName values are copied into the * stack. In other words, the caller does <em>not</em> orphan * the element to the stack. Also, the QName object returned * is <em>not</em> orphaned to the caller. It should be * considered read-only. * * @param element The element to push onto the stack. * * @return Returns the actual QName object that stores the */ public QName pushElement(QName element) { if (fSize == fElements.length) { QName[] array = new QName[fElements.length * 2]; System.arraycopy(fElements, 0, array, 0, fSize); fElements = array; for (int i = fSize; i < fElements.length; i++) { fElements[i] = new QName(); } } fElements[fSize].setValues(element); return fElements[fSize++]; } // pushElement(QName):QName /** * Pops an element off of the stack by setting the values of * the specified QName. * <p> * <strong>Note:</strong> The object returned is <em>not</em> * orphaned to the caller. Therefore, the caller should consider * the object to be read-only. */ public void popElement(QName element) { element.setValues(fElements[--fSize]); } // popElement(QName) /** Clears the stack without throwing away existing QName objects. */ public void clear() { fSize = 0; } // clear() } // class ElementStack /** * This interface defines an XML "event" dispatching model. Classes * that implement this interface are responsible for scanning parts * of the XML document and dispatching callbacks. * * @author Glenn Marcy, IBM */ protected interface 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; } // interface Dispatcher /** * Dispatcher to handle content scanning. * * @author Andy Clark, IBM * @author Eric Ye, IBM */ protected class FragmentContentDispatcher 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_CONTENT: { if (fEntityScanner.skipChar('<')) { setScannerState(SCANNER_STATE_START_OF_MARKUP); again = true; } else if (fEntityScanner.skipChar('&')) { setScannerState(SCANNER_STATE_REFERENCE); again = true; } else { do { int c = scanContent(); if (c == '<') { fEntityScanner.scanChar(); setScannerState(SCANNER_STATE_START_OF_MARKUP); break; } else if (c == '&') { fEntityScanner.scanChar(); setScannerState(SCANNER_STATE_REFERENCE); break; } else if (c != -1 && isInvalidLiteral(c)) { if (XMLChar.isHighSurrogate(c)) { // special case: surrogates fStringBuffer.clear(); if (scanSurrogates(fStringBuffer)) { // call handler if (fDocumentHandler != null) { fDocumentHandler.characters(fStringBuffer, null); } } } else { reportFatalError("InvalidCharInContent", new Object[] { Integer.toString(c, 16)}); fEntityScanner.scanChar(); } } } while (complete); } break; } case SCANNER_STATE_START_OF_MARKUP: { fMarkupDepth++; if (fEntityScanner.skipChar('/')) { if (scanEndElement() == 0) { if (elementDepthIsZeroHook()) { return true; } } setScannerState(SCANNER_STATE_CONTENT); } else if (isValidNameStartChar(fEntityScanner.peekChar())) { scanStartElement(); setScannerState(SCANNER_STATE_CONTENT); } else if (fEntityScanner.skipChar('!')) { if (fEntityScanner.skipChar('-')) { if (!fEntityScanner.skipChar('-')) { reportFatalError("InvalidCommentStart", null); } setScannerState(SCANNER_STATE_COMMENT); again = true; } else if (fEntityScanner.skipString("[CDATA[")) { setScannerState(SCANNER_STATE_CDATA); again = true; } else if (!scanForDoctypeHook()) { reportFatalError("MarkupNotRecognizedInContent", null); } } else if (fEntityScanner.skipChar('?')) { setScannerState(SCANNER_STATE_PI); again = true; } else if (isValidNameStartHighSurrogate(fEntityScanner.peekChar())) { scanStartElement(); setScannerState(SCANNER_STATE_CONTENT); } else { reportFatalError("MarkupNotRecognizedInContent", null); setScannerState(SCANNER_STATE_CONTENT); } break; } case SCANNER_STATE_COMMENT: { scanComment(); setScannerState(SCANNER_STATE_CONTENT); break; } case SCANNER_STATE_PI: { scanPI(); setScannerState(SCANNER_STATE_CONTENT); break; } case SCANNER_STATE_CDATA: { scanCDATASection(complete); setScannerState(SCANNER_STATE_CONTENT); break; } case SCANNER_STATE_REFERENCE: { fMarkupDepth++; // NOTE: We need to set the state beforehand // because the XMLEntityHandler#startEntity // callback could set the state to // SCANNER_STATE_TEXT_DECL and we don't want // to override that scanner state. setScannerState(SCANNER_STATE_CONTENT); if (fEntityScanner.skipChar('#')) { scanCharReference(); } else { scanEntityReference(); } break; } case SCANNER_STATE_TEXT_DECL: { // scan text decl if (fEntityScanner.skipString("<?xml")) { fMarkupDepth++; // NOTE: special case where entity starts with a PI // whose name starts with "xml" (e.g. "xmlfoo") if (isValidNameChar(fEntityScanner.peekChar())) { fStringBuffer.clear(); fStringBuffer.append("xml"); if (fNamespaces) { while (isValidNCName(fEntityScanner.peekChar())) { fStringBuffer.append((char)fEntityScanner.scanChar()); } } else { while (isValidNameChar(fEntityScanner.peekChar())) { fStringBuffer.append((char)fEntityScanner.scanChar()); } } String target = fSymbolTable.addSymbol(fStringBuffer.ch, fStringBuffer.offset, fStringBuffer.length); scanPIData(target, fTempString); } // standard text declaration else { scanXMLDeclOrTextDecl(true); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?