xmlscanner.java

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

JAVA
1,453
字号
            reportFatalError("QuoteRequiredInPublicID", null);            return false;        }        fStringBuffer.clear();        // skip leading whitespace        boolean skipSpace = true;        boolean dataok = true;        while (true) {            int c = fEntityScanner.scanChar();            if (c == ' ' || c == '\n' || c == '\r') {                if (!skipSpace) {                    // take the first whitespace as a space and skip the others                    fStringBuffer.append(' ');                    skipSpace = true;                }            }            else if (c == quote) {                if (skipSpace) {                    // if we finished on a space let's trim it                    fStringBuffer.length--;                }                literal.setValues(fStringBuffer);                break;            }            else if (XMLChar.isPubid(c)) {                fStringBuffer.append((char)c);                skipSpace = false;            }            else if (c == -1) {                reportFatalError("PublicIDUnterminated", null);                return false;            }            else {                dataok = false;                reportFatalError("InvalidCharInPublicID",                                 new Object[]{Integer.toHexString(c)});            }        }        return dataok;   }    /**     * Normalize whitespace in an XMLString converting all whitespace     * characters to space characters.     */    protected void normalizeWhitespace(XMLString value) {        int end = value.offset + value.length;        for (int i = value.offset; i < end; i++) {            int c = value.ch[i];            // Performance: For XML 1.0 documents take advantage of             // the fact that the only legal characters below 0x20             // are 0x09 (TAB), 0x0A (LF) and 0x0D (CR). Since we've             // already determined the well-formedness of these            // characters it is sufficient (and safe) to check            // against 0x20. -- mrglavas            if (c < 0x20) {                value.ch[i] = ' ';            }        }    }    //    // XMLEntityHandler methods    //    /**     * This method notifies of the start of an entity. The document entity     * has the pseudo-name of "[xml]" the DTD has the pseudo-name of "[dtd]"      * parameter entity names start with '%'; and general entities are just     * specified by their name.     *      * @param name     The name of the entity.     * @param identifier The resource identifier.     * @param encoding The auto-detected IANA encoding name of the entity     *                 stream. This value will be null in those situations     *                 where the entity encoding is not auto-detected (e.g.     *                 internal entities or a document entity that is     *                 parsed from a java.io.Reader).     * @param augs     Additional information that may include infoset augmentations     *     * @throws XNIException Thrown by handler to signal an error.     */    public void startEntity(String name,                             XMLResourceIdentifier identifier,                            String encoding, Augmentations augs) throws XNIException {        // keep track of the entity depth        fEntityDepth++;        // must reset entity scanner        fEntityScanner = fEntityManager.getEntityScanner();    } // startEntity(String,XMLResourceIdentifier,String)    /**     * This method notifies the end of an entity. The document entity has     * the pseudo-name of "[xml]" the DTD has the pseudo-name of "[dtd]"      * parameter entity names start with '%'; and general entities are just     * specified by their name.     *      * @param name The name of the entity.     * @param augs Additional information that may include infoset augmentations     *     * @throws XNIException Thrown by handler to signal an error.     */    public void endEntity(String name, Augmentations augs) throws XNIException {        // keep track of the entity depth        fEntityDepth--;    } // endEntity(String)    /**     * Scans a character reference and append the corresponding chars to the     * specified buffer.     *     * <p>     * <pre>     * [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'     * </pre>     *     * <strong>Note:</strong> This method uses fStringBuffer, anything in it     * at the time of calling is lost.     *     * @param buf the character buffer to append chars to     * @param buf2 the character buffer to append non-normalized chars to     *     * @return the character value or (-1) on conversion failure     */    protected int scanCharReferenceValue(XMLStringBuffer buf, XMLStringBuffer buf2)         throws IOException, XNIException {        // scan hexadecimal value        boolean hex = false;        if (fEntityScanner.skipChar('x')) {            if (buf2 != null) { buf2.append('x'); }            hex = true;            fStringBuffer3.clear();            boolean digit = true;                        int c = fEntityScanner.peekChar();            digit = (c >= '0' && c <= '9') ||                    (c >= 'a' && c <= 'f') ||                    (c >= 'A' && c <= 'F');            if (digit) {                if (buf2 != null) { buf2.append((char)c); }                fEntityScanner.scanChar();                fStringBuffer3.append((char)c);                                do {                    c = fEntityScanner.peekChar();                    digit = (c >= '0' && c <= '9') ||                            (c >= 'a' && c <= 'f') ||                            (c >= 'A' && c <= 'F');                    if (digit) {                        if (buf2 != null) { buf2.append((char)c); }                        fEntityScanner.scanChar();                        fStringBuffer3.append((char)c);                    }                } while (digit);            }            else {                reportFatalError("HexdigitRequiredInCharRef", null);            }        }        // scan decimal value        else {            fStringBuffer3.clear();            boolean digit = true;                        int c = fEntityScanner.peekChar();            digit = c >= '0' && c <= '9';            if (digit) {                if (buf2 != null) { buf2.append((char)c); }                fEntityScanner.scanChar();                fStringBuffer3.append((char)c);                                do {                    c = fEntityScanner.peekChar();                    digit = c >= '0' && c <= '9';                    if (digit) {                        if (buf2 != null) { buf2.append((char)c); }                        fEntityScanner.scanChar();                        fStringBuffer3.append((char)c);                    }                } while (digit);            }            else {                reportFatalError("DigitRequiredInCharRef", null);            }        }        // end        if (!fEntityScanner.skipChar(';')) {            reportFatalError("SemicolonRequiredInCharRef", null);        }        if (buf2 != null) { buf2.append(';'); }                // convert string to number        int value = -1;        try {            value = Integer.parseInt(fStringBuffer3.toString(),                                     hex ? 16 : 10);                        // character reference must be a valid XML character            if (isInvalid(value)) {            	StringBuffer errorBuf = new StringBuffer(fStringBuffer3.length + 1);                if (hex) errorBuf.append('x');                errorBuf.append(fStringBuffer3.ch, fStringBuffer3.offset, fStringBuffer3.length);                reportFatalError("InvalidCharRef",                                 new Object[]{errorBuf.toString()});            }        }        catch (NumberFormatException e) {            // Conversion failed, let -1 value drop through.            // If we end up here, the character reference was invalid.            StringBuffer errorBuf = new StringBuffer(fStringBuffer3.length + 1);            if (hex) errorBuf.append('x');            errorBuf.append(fStringBuffer3.ch, fStringBuffer3.offset, fStringBuffer3.length);            reportFatalError("InvalidCharRef",                             new Object[]{errorBuf.toString()});        }        // append corresponding chars to the given buffer        if (!XMLChar.isSupplemental(value)) {            buf.append((char) value);        }        else {            // character is supplemental, split it into surrogate chars            buf.append(XMLChar.highSurrogate(value));            buf.append(XMLChar.lowSurrogate(value));        }        // char refs notification code        if (fNotifyCharRefs && value != -1) {            String literal = "#" + (hex ? "x" : "") + fStringBuffer3.toString();            if (!fScanningAttribute) {                fCharRefLiteral = literal;            }        }                        return value;    }    // returns true if the given character is not    // valid with respect to the version of    // XML understood by this scanner.    protected boolean isInvalid(int value) {        return (XMLChar.isInvalid(value));     } // isInvalid(int):  boolean    // returns true if the given character is not    // valid or may not be used outside a character reference     // with respect to the version of XML understood by this scanner.    protected boolean isInvalidLiteral(int value) {        return (XMLChar.isInvalid(value));     } // isInvalidLiteral(int):  boolean    // returns true if the given character is     // a valid nameChar with respect to the version of    // XML understood by this scanner.    protected boolean isValidNameChar(int value) {        return (XMLChar.isName(value));     } // isValidNameChar(int):  boolean    // returns true if the given character is     // a valid nameStartChar with respect to the version of    // XML understood by this scanner.    protected boolean isValidNameStartChar(int value) {        return (XMLChar.isNameStart(value));     } // isValidNameStartChar(int):  boolean        // returns true if the given character is    // a valid NCName character with respect to the version of    // XML understood by this scanner.    protected boolean isValidNCName(int value) {        return (XMLChar.isNCName(value));    } // isValidNCName(int):  boolean        // returns true if the given character is     // a valid high surrogate for a nameStartChar     // with respect to the version of XML understood     // by this scanner.    protected boolean isValidNameStartHighSurrogate(int value) {        return false;     } // isValidNameStartHighSurrogate(int):  boolean        protected boolean versionSupported(String version ) {        return version.equals("1.0");    } // version Supported        // returns the error message key for unsupported    // versions of XML with respect to the version of    // XML understood by this scanner.    protected String getVersionNotSupportedKey () {        return "VersionNotSupported";    } // getVersionNotSupportedKey: String    /**     * Scans surrogates and append them to the specified buffer.     * <p>     * <strong>Note:</strong> This assumes the current char has already been     * identified as a high surrogate.     *     * @param buf The StringBuffer to append the read surrogates to.     * @return True if it succeeded.     */    protected boolean scanSurrogates(XMLStringBuffer buf)        throws IOException, XNIException {        int high = fEntityScanner.scanChar();        int low = fEntityScanner.peekChar();        if (!XMLChar.isLowSurrogate(low)) {            reportFatalError("InvalidCharInContent",                             new Object[] {Integer.toString(high, 16)});            return false;        }        fEntityScanner.scanChar();        // convert surrogates to supplemental character        int c = XMLChar.supplemental((char)high, (char)low);        // supplemental character must be a valid XML character        if (isInvalid(c)) {            reportFatalError("InvalidCharInContent",                             new Object[]{Integer.toString(c, 16)});             return false;        }        // fill in the buffer        buf.append((char)high);        buf.append((char)low);        return true;    } // scanSurrogates():boolean    /**     * Convenience function used in all XML scanners.     */    protected void reportFatalError(String msgId, Object[] args)        throws XNIException {        fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN,                                   msgId, args,                                   XMLErrorReporter.SEVERITY_FATAL_ERROR);    }    // private methods    private void init() {         fEntityScanner = null;               // initialize vars        fEntityDepth = 0;        fReportEntity = true;        fResourceIdentifier.clear();    } } // class XMLScanner

⌨️ 快捷键说明

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