xmldocumentfragmentscannerimpl.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,788 行 · 第 1/5 页

JAVA
1,788
字号
        // set standalone        fStandalone = standalone != null && standalone.equals("yes");        fEntityManager.setStandalone(fStandalone);        // call handler        if (fDocumentHandler != null) {            if (scanningTextDecl) {                fDocumentHandler.textDecl(version, encoding, null);            }            else {                fDocumentHandler.xmlDecl(version, encoding, standalone, null);            }        }        // set encoding on reader        if (encoding != null && !fEntityScanner.fCurrentEntity.isDeclaredEncoding()) {            fEntityScanner.setEncoding(encoding);        }    } // scanXMLDeclOrTextDecl(boolean)    /**     * Scans a processing data. This is needed to handle the situation     * where a document starts with a processing instruction whose      * target name <em>starts with</em> "xml". (e.g. xmlfoo)     *     * @param target The PI target     * @param data The string to fill in with the data     */    protected void scanPIData(String target, XMLString data)         throws IOException, XNIException {        super.scanPIData(target, data);        fMarkupDepth--;        // call handler        if (fDocumentHandler != null) {            fDocumentHandler.processingInstruction(target, data, null);        }    } // scanPIData(String)    /**     * Scans a comment.     * <p>     * <pre>     * [15] Comment ::= '&lt!--' ((Char - '-') | ('-' (Char - '-')))* '-->'     * </pre>     * <p>     * <strong>Note:</strong> Called after scanning past '&lt;!--'     */    protected void scanComment() throws IOException, XNIException {        scanComment(fStringBuffer);        fMarkupDepth--;        // call handler        if (fDocumentHandler != null) {            fDocumentHandler.comment(fStringBuffer, null);        }    } // scanComment()        /**      * Scans a start element. This method will handle the binding of     * namespace information and notifying the handler of the start     * of the element.     * <p>     * <pre>     * [44] EmptyElemTag ::= '&lt;' Name (S Attribute)* S? '/>'     * [40] STag ::= '&lt;' Name (S Attribute)* S? '>'     * </pre>      * <p>     * <strong>Note:</strong> This method assumes that the leading     * '&lt;' character has been consumed.     * <p>     * <strong>Note:</strong> This method uses the fElementQName and     * fAttributes variables. The contents of these variables will be     * destroyed. The caller should copy important information out of     * these variables before calling this method.     *     * @return True if element is empty. (i.e. It matches     *          production [44].     */    protected boolean scanStartElement()         throws IOException, XNIException {        if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanStartElement()");        // name        if (fNamespaces) {            fEntityScanner.scanQName(fElementQName);        }        else {            String name = fEntityScanner.scanName();            fElementQName.setValues(null, name, name, null);        }        String rawname = fElementQName.rawname;        // push element stack        fCurrentElement = fElementStack.pushElement(fElementQName);        // attributes        boolean empty = false;        fAttributes.removeAllAttributes();        do {            // spaces            boolean sawSpace = fEntityScanner.skipSpaces();            // end tag?            int c = fEntityScanner.peekChar();            if (c == '>') {                fEntityScanner.scanChar();                break;            }            else if (c == '/') {                fEntityScanner.scanChar();                if (!fEntityScanner.skipChar('>')) {                    reportFatalError("ElementUnterminated",                                     new Object[]{rawname});                }                empty = true;                break;            }            else if (!isValidNameStartChar(c) || !sawSpace) {                // Second chance. Check if this character is a high                // surrogate of a valid name start character.                if (!isValidNameStartHighSurrogate(c) || !sawSpace) {                    reportFatalError("ElementUnterminated",                                     new Object[] { rawname });                }            }            // attributes            scanAttribute(fAttributes);            if (fSecurityManager != null && fAttributes.getLength() > fElementAttributeLimit){                                fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN,                                             "ElementAttributeLimit",                                             new Object[]{rawname, new Integer(fAttributes.getLength()) },                                             XMLErrorReporter.SEVERITY_FATAL_ERROR );            }                   } while (true);        // call handler        if (fDocumentHandler != null) {            if (empty) {                //decrease the markup depth..                fMarkupDepth--;                // check that this element was opened in the same entity                if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) {                    reportFatalError("ElementEntityMismatch",                                     new Object[]{fCurrentElement.rawname});                }                fDocumentHandler.emptyElement(fElementQName, fAttributes, null);                //pop the element off the stack..                fElementStack.popElement(fElementQName);            }            else {                fDocumentHandler.startElement(fElementQName, fAttributes, null);            }        }        if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanStartElement(): "+empty);        return empty;    } // scanStartElement():boolean        /**     * Scans the name of an element in a start or empty tag.      *      * @see #scanStartElement()     */    protected void scanStartElementName ()        throws IOException, XNIException {        // name        if (fNamespaces) {            fEntityScanner.scanQName(fElementQName);        }        else {            String name = fEntityScanner.scanName();            fElementQName.setValues(null, name, name, null);        }        // Must skip spaces here because the DTD scanner        // would consume them at the end of the external subset.        fSawSpace = fEntityScanner.skipSpaces();    } // scanStartElementName()    /**     * Scans the remainder of a start or empty tag after the element name.     *      * @see #scanStartElement     * @return True if element is empty.     */    protected boolean scanStartElementAfterName()        throws IOException, XNIException {        String rawname = fElementQName.rawname;        // push element stack        fCurrentElement = fElementStack.pushElement(fElementQName);        // attributes        boolean empty = false;        fAttributes.removeAllAttributes();        do {        	            // end tag?            int c = fEntityScanner.peekChar();            if (c == '>') {                fEntityScanner.scanChar();                break;            }            else if (c == '/') {                fEntityScanner.scanChar();                if (!fEntityScanner.skipChar('>')) {                    reportFatalError("ElementUnterminated",                                     new Object[]{rawname});                }                empty = true;                break;            }            else if (!isValidNameStartChar(c) || !fSawSpace) {                // Second chance. Check if this character is a high                // surrogate of a valid name start character.                if (!isValidNameStartHighSurrogate(c) || !fSawSpace) {                    reportFatalError("ElementUnterminated",                                     new Object[] { rawname });                }            }            // attributes            scanAttribute(fAttributes);                        // spaces            fSawSpace = fEntityScanner.skipSpaces();        } while (true);        // call handler        if (fDocumentHandler != null) {            if (empty) {                //decrease the markup depth..                fMarkupDepth--;                // check that this element was opened in the same entity                if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) {                    reportFatalError("ElementEntityMismatch",                                     new Object[]{fCurrentElement.rawname});                }                fDocumentHandler.emptyElement(fElementQName, fAttributes, null);                //pop the element off the stack..                fElementStack.popElement(fElementQName);            }            else {                fDocumentHandler.startElement(fElementQName, fAttributes, null);            }        }        if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanStartElementAfterName(): "+empty);        return empty;    } // scanStartElementAfterName()    /**      * Scans an attribute.     * <p>     * <pre>     * [41] Attribute ::= Name Eq AttValue     * </pre>      * <p>     * <strong>Note:</strong> This method assumes that the next      * character on the stream is the first character of the attribute     * name.     * <p>     * <strong>Note:</strong> This method uses the fAttributeQName and     * fQName variables. The contents of these variables will be     * destroyed.     *     * @param attributes The attributes list for the scanned attribute.     */    protected void scanAttribute(XMLAttributes attributes)         throws IOException, XNIException {        if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanAttribute()");        // name        if (fNamespaces) {            fEntityScanner.scanQName(fAttributeQName);        }        else {            String name = fEntityScanner.scanName();            fAttributeQName.setValues(null, name, name, null);        }        // equals        fEntityScanner.skipSpaces();        if (!fEntityScanner.skipChar('=')) {            reportFatalError("EqRequiredInAttribute",                             new Object[]{fCurrentElement.rawname,fAttributeQName.rawname});        }        fEntityScanner.skipSpaces();        // content        int oldLen = attributes.getLength();        int attrIndex = attributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, null);        // WFC: Unique Att Spec        if (oldLen == attributes.getLength()) {            reportFatalError("AttributeNotUnique",                             new Object[]{fCurrentElement.rawname,                                          fAttributeQName.rawname});        }        //REVISIT: one more case needs to be included: external PE and standalone is no        boolean isVC =  fHasExternalDTD && !fStandalone;                scanAttributeValue(fTempString, fTempString2,                           fAttributeQName.rawname, isVC,                            fCurrentElement.rawname);        attributes.setValue(attrIndex, fTempString.toString());        attributes.setNonNormalizedValue(attrIndex, fTempString2.toString());        attributes.setSpecified(attrIndex, true);        if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanAttribute()");    } // scanAttribute(XMLAttributes)    /**     * Scans element content.     *     * @return Returns the next character on the stream.     */    protected int scanContent() throws IOException, XNIException {        XMLString content = fTempString;        int c = fEntityScanner.scanContent(content);        if (c == '\r') {            // happens when there is the character reference &#13;            fEntityScanner.scanChar();            fStringBuffer.clear();            fStringBuffer.append(fTempString);            fStringBuffer.append((char)c);            content = fStringBuffer;            c = -1;        }        if (fDocumentHandler != null && content.length > 0) {            fDocumentHandler.characters(content, null);        }        if (c == ']' && fTempString.length == 0) {            fStringBuffer.clear();            fStringBuffer.append((char)fEntityScanner.scanChar());            // remember where we are in case we get an endEntity before we            // could flush the buffer out - this happens when we're parsing an            // entity which ends with a ]            fInScanContent = true;            //            // We work on a single character basis to handle cases such as:            // ']]]>' which we might otherwise miss.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?