xml11serializer.java

来自「JAVA 所有包」· Java 代码 · 共 539 行 · 第 1/2 页

JAVA
539
字号
                }                continue;            }            if (ch == '\n' || ch == '\r' || ch == '\t' || ch == 0x0085 || ch == 0x2028){				printHex(ch);			} else if (ch == '<') {				_printer.printText("&lt;");			} else if (ch == '&') {				_printer.printText("&amp;");			} else if (ch == '"') {				_printer.printText("&quot;");			} else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) {				_printer.printText((char) ch);			} else {				printHex(ch);			}        }    }    protected final void printCDATAText(String text) throws IOException {        int length = text.length();        char ch;        for (int index = 0; index < length; ++index) {            ch = text.charAt(index);            if (ch == ']'                && index + 2 < length                && text.charAt(index + 1) == ']'                && text.charAt(index + 2) == '>') { // check for ']]>'                if (fDOMErrorHandler != null){                    // REVISIT: this means that if DOM Error handler is not registered we don't report any                    // fatal errors and might serialize not wellformed document                if ((features & DOMSerializerImpl.SPLITCDATA) == 0                    && (features & DOMSerializerImpl.WELLFORMED) == 0) {                    // issue fatal error                    String msg =                        DOMMessageFormatter.formatMessage(                            DOMMessageFormatter.SERIALIZER_DOMAIN,                            "EndingCDATA",                            null);                    modifyDOMError(                        msg,                        DOMError.SEVERITY_FATAL_ERROR,                        null, fCurrentNode);                    boolean continueProcess =                        fDOMErrorHandler.handleError(fDOMError);                    if (!continueProcess) {                        throw new IOException();                    }                } else {                    // issue warning                    String msg =                        DOMMessageFormatter.formatMessage(                            DOMMessageFormatter.SERIALIZER_DOMAIN,                            "SplittingCDATA",                            null);                    modifyDOMError(                        msg,                        DOMError.SEVERITY_WARNING,                        null, fCurrentNode);                    fDOMErrorHandler.handleError(fDOMError);                }                }                // split CDATA section                _printer.printText("]]]]><![CDATA[>");                index += 2;                continue;            }            if (!XML11Char.isXML11Valid(ch)) {                // check if it is surrogate                if (++index < length) {                    surrogates(ch, text.charAt(index));                } else {                    fatalError(                        "The character '"                            + (char) ch                            + "' is an invalid XML character");                }                continue;            } else {                if (_encodingInfo.isPrintable((char) ch)                    && XML11Char.isXML11ValidLiteral(ch)) {                    _printer.printText((char) ch);                } else {                    // The character is not printable -- split CDATA section                    _printer.printText("]]>&#x");                    _printer.printText(Integer.toHexString(ch));                    _printer.printText(";<![CDATA[");                }            }        }    }    // note that this "int" should, in all cases, be a char.    // REVISIT:  make it a char...    protected final void printXMLChar( int ch ) throws IOException {    	    	if (ch == '\r' || ch == 0x0085 || ch == 0x2028) {			printHex(ch);    	} else if ( ch == '<') {            _printer.printText("&lt;");        } else if (ch == '&') {            _printer.printText("&amp;");		} else if (ch == '>'){			// character sequence "]]>" can't appear in content, therefore			// we should escape '>' 			_printer.printText("&gt;");        } else if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) {             _printer.printText((char)ch);        } else {             printHex(ch);        }    }    protected final void surrogates(int high, int low) throws IOException{        if (XMLChar.isHighSurrogate(high)) {            if (!XMLChar.isLowSurrogate(low)) {                //Invalid XML                fatalError("The character '"+(char)low+"' is an invalid XML character");             }            else {                int supplemental = XMLChar.supplemental((char)high, (char)low);                if (!XML11Char.isXML11Valid(supplemental)) {                    //Invalid XML                    fatalError("The character '"+(char)supplemental+"' is an invalid XML character");                 }                else {                    if (content().inCData ) {                        _printer.printText("]]>&#x");                                                _printer.printText(Integer.toHexString(supplemental));                                                _printer.printText(";<![CDATA[");                    }                      else {						printHex(supplemental);                    }                }            }        } else {            fatalError("The character '"+(char)high+"' is an invalid XML character");         }    }    protected void printText( String text, boolean preserveSpace, boolean unescaped )    throws IOException {        int index;        char ch;        int length = text.length();        if ( preserveSpace ) {            // Preserving spaces: the text must print exactly as it is,            // without breaking when spaces appear in the text and without            // consolidating spaces. If a line terminator is used, a line            // break will occur.            for ( index = 0 ; index < length ; ++index ) {                ch = text.charAt( index );                if (!XML11Char.isXML11Valid(ch)) {                    // check if it is surrogate                    if (++index <length) {                        surrogates(ch, text.charAt(index));                    } else {                        fatalError("The character '"+(char)ch+"' is an invalid XML character");                     }                    continue;                }                if ( unescaped  && XML11Char.isXML11ValidLiteral(ch)) {                    _printer.printText( ch );                } else                    printXMLChar( ch );            }        } else {            // Not preserving spaces: print one part at a time, and            // use spaces between parts to break them into different            // lines. Spaces at beginning of line will be stripped            // by printing mechanism. Line terminator is treated            // no different than other text part.            for ( index = 0 ; index < length ; ++index ) {                ch = text.charAt( index );                if (!XML11Char.isXML11Valid(ch)) {                    // check if it is surrogate                    if (++index <length) {                        surrogates(ch, text.charAt(index));                    } else {                        fatalError("The character '"+(char)ch+"' is an invalid XML character");                     }                    continue;                }                if ( unescaped && XML11Char.isXML11ValidLiteral(ch) )                    _printer.printText( ch );                else                    printXMLChar( ch);            }        }    }    protected void printText( char[] chars, int start, int length,                              boolean preserveSpace, boolean unescaped ) throws IOException {        int index;        char ch;        if ( preserveSpace ) {            // Preserving spaces: the text must print exactly as it is,            // without breaking when spaces appear in the text and without            // consolidating spaces. If a line terminator is used, a line            // break will occur.            while ( length-- > 0 ) {                ch = chars[start++];                if (!XML11Char.isXML11Valid(ch)) {                    // check if it is surrogate                    if ( length-- > 0) {                        surrogates(ch, chars[start++]);                    } else {                        fatalError("The character '"+(char)ch+"' is an invalid XML character");                     }                    continue;                }                if ( unescaped && XML11Char.isXML11ValidLiteral(ch))                    _printer.printText( ch );                else                    printXMLChar( ch );            }        } else {            // Not preserving spaces: print one part at a time, and            // use spaces between parts to break them into different            // lines. Spaces at beginning of line will be stripped            // by printing mechanism. Line terminator is treated            // no different than other text part.            while ( length-- > 0 ) {                ch = chars[start++];                if (!XML11Char.isXML11Valid(ch)) {                    // check if it is surrogate                    if ( length-- > 0) {                        surrogates(ch, chars[start++]);                    } else {                        fatalError("The character '"+(char)ch+"' is an invalid XML character");                     }                    continue;                }                              if ( unescaped && XML11Char.isXML11ValidLiteral(ch))                    _printer.printText( ch );                else                    printXMLChar( ch );            }        }    }    public boolean reset() {        super.reset();        return true;    }}

⌨️ 快捷键说明

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