basemarkupserializer.java

来自「JAVA 所有包」· Java 代码 · 共 1,946 行 · 第 1/5 页

JAVA
1,946
字号
                        fDOMErrorHandler.handleError(fDOMError);                    }                }                // split CDATA section                _printer.printText("]]]]><![CDATA[>");                index += 2;                continue;            }                        if (!XMLChar.isValid(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 ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||                     ch == '\n' || ch == '\r' || ch == '\t' ) {                    _printer.printText((char)ch);                } else {                    // The character is not printable -- split CDATA section                    _printer.printText("]]>&#x");                                            _printer.printText(Integer.toHexString(ch));                                            _printer.printText(";<![CDATA[");                }            }        }    }    protected 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 (!XMLChar.isValid(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");         }    }    /**     * Called to print additional text with whitespace handling.     * If spaces are preserved, the text is printed as if by calling     * {@link #printText(String,boolean,boolean)} with a call to {@link Printer#breakLine}     * for each new line. If spaces are not preserved, the text is     * broken at space boundaries if longer than the line width;     * Multiple spaces are printed as such, but spaces at beginning     * of line are removed.     *     * @param text The text to print     * @param preserveSpace Space preserving flag     * @param unescaped Print unescaped     */    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 ];                ++start;                if ( ch == '\n' || ch == '\r' || unescaped )                    _printer.printText( ch );                else                    printEscaped( 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 ];                ++start;                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )                    _printer.printSpace();                else if ( unescaped )                    _printer.printText( ch );                else                    printEscaped( ch );            }        }    }    protected void printText( String text, 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.            for ( index = 0 ; index < text.length() ; ++index ) {                ch = text.charAt( index );                if ( ch == '\n' || ch == '\r' || unescaped )                    _printer.printText( ch );                else                    printEscaped( 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 < text.length() ; ++index ) {                ch = text.charAt( index );                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )                    _printer.printSpace();                else if ( unescaped )                    _printer.printText( ch );                else                    printEscaped( ch );            }        }    }    /**     * Print a document type public or system identifier URL.     * Encapsulates the URL in double quotes, escapes non-printing     * characters and print it equivalent to {@link #printText}.     *     * @param url The document type url to print     */    protected void printDoctypeURL( String url )        throws IOException    {        int                i;        _printer.printText( '"' );        for( i = 0 ; i < url.length() ; ++i ) {            if ( url.charAt( i ) == '"' ||  url.charAt( i ) < 0x20 || url.charAt( i ) > 0x7F ) {                _printer.printText( '%' );                _printer.printText( Integer.toHexString( url.charAt( i ) ) );            } else                _printer.printText( url.charAt( i ) );        }        _printer.printText( '"' );    }    protected void printEscaped( int ch )        throws IOException    {        String charRef;        // If there is a suitable entity reference for this        // character, print it. The list of available entity        // references is almost but not identical between        // XML and HTML.        charRef = getEntityRef( ch );        if ( charRef != null ) {            _printer.printText( '&' );            _printer.printText( charRef );            _printer.printText( ';' );        } else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||                    ch == '\n' || ch == '\r' || ch == '\t' ) {            // Non printables are below ASCII space but not tab or line            // terminator, ASCII delete, or above a certain Unicode threshold.            if (ch < 0x10000) {                _printer.printText((char)ch );            } else {                _printer.printText((char)(((ch-0x10000)>>10)+0xd800));                _printer.printText((char)(((ch-0x10000)&0x3ff)+0xdc00));            }        } else {			printHex(ch);        }    }    	/**	 * Escapes chars	 */ 	 final void printHex( int ch) throws IOException {                 _printer.printText( "&#x" );		 _printer.printText(Integer.toHexString(ch));		 _printer.printText( ';' );                 	 }    /**     * Escapes a string so it may be printed as text content or attribute     * value. Non printable characters are escaped using character references.     * Where the format specifies a deault entity reference, that reference     * is used (e.g. <tt>&amp;lt;</tt>).     *     * @param source The string to escape     */    protected void printEscaped( String source )        throws IOException    {        for ( int i = 0 ; i < source.length() ; ++i ) {            int ch = source.charAt(i);            if ((ch & 0xfc00) == 0xd800 && i+1 < source.length()) {                int lowch = source.charAt(i+1);                if ((lowch & 0xfc00) == 0xdc00) {                    ch = 0x10000 + ((ch-0xd800)<<10) + lowch-0xdc00;                    i++;                }            }            printEscaped(ch);        }    }    //--------------------------------//    // Element state handling methods //    //--------------------------------//    /**     * Return the state of the current element.     *     * @return Current element state     */    protected ElementState getElementState()    {        return _elementStates[ _elementStateCount ];    }    /**     * Enter a new element state for the specified element.     * Tag name and space preserving is specified, element     * state is initially empty.     *     * @return Current element state, or null     */    protected ElementState enterElementState( String namespaceURI, String localName,                                              String rawName, boolean preserveSpace )    {        ElementState state;        if ( _elementStateCount + 1 == _elementStates.length ) {            ElementState[] newStates;            // Need to create a larger array of states. This does not happen            // often, unless the document is really deep.            newStates = new ElementState[ _elementStates.length + 10 ];            for ( int i = 0 ; i < _elementStates.length ; ++i )                newStates[ i ] = _elementStates[ i ];            for ( int i = _elementStates.length ; i < newStates.length ; ++i )                newStates[ i ] = new ElementState();            _elementStates = newStates;        }        ++_elementStateCount;        state = _elementStates[ _elementStateCount ];        state.namespaceURI = namespaceURI;        state.localName = localName;        state.rawName = rawName;        state.preserveSpace = preserveSpace;        state.empty = true;        state.afterElement = false;        state.afterComment = false;        state.doCData = state.inCData = false;        state.unescaped = false;        state.prefixes = _prefixes;        _prefixes = null;        return state;    }    /**     * Leave the current element state and return to the     * state of the parent element. If this was the root     * element, return to the state of the document.     *                                                                                  * @return Previous element state     */    protected ElementState leaveElementState()    {        if ( _elementStateCount > 0 ) {            /*Corrected by David Blondeau (blondeau@intalio.com)*/		_prefixes = null;		//_prefixes = _elementStates[ _elementStateCount ].prefixes;            -- _elementStateCount;            return _elementStates[ _elementStateCount ];        } else {            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "Internal", null);            throw new IllegalStateException(msg);        }    }    /**     * Returns true if in the state of the document.     * Returns true before entering any element and after     * leaving the root element.     *     * @return True if in the state of the document     */    protected boolean isDocumentState()    {        return _elementStateCount == 0;    }    /**     * Returns the namespace prefix for the specified URI.     * If the URI has been mapped to a prefix, returns the     * prefix, otherwise returns null.     *     * @param namespaceURI The namespace URI     * @return The namespace prefix if known, or null     */    protected String getPrefix( String namespaceURI )    {        String    prefix;        if ( _prefixes != null ) {            prefix = (String) _prefixes.get( namespaceURI );            if ( prefix != null )                return prefix;        }        if ( _elementStateCount == 0 )            return null;        else {            for ( int i = _elementStateCount ; i > 0 ; --i ) {                if ( _elementStates[ i ].prefixes != null ) {                    prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );                    if ( prefix != null )                        return prefix;                }            }        }        return null;    }    /**     * The method modifies global DOM error object     *      * @param message     * @param severity     * @param type     * @return a DOMError     */    protected DOMError modifyDOMError(String message, short severity, String type, Node node){            fDOMError.reset();            fDOMError.fMessage = message;            fDOMError.fType = type;            fDOMError.fSeverity = severity;            fDOMError.fLocator = new DOMLocatorImpl(-1, -1, -1, node, null);            return fDOMError;            }    prote

⌨️ 快捷键说明

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