basemarkupserializer.java

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

JAVA
1,946
字号
    //----------------------------------//    /**     * Serializes the DOM element using the previously specified     * writer and output format. Throws an exception only if     * an I/O exception occured while serializing.     *     * @param elem The element to serialize     * @throws IOException An I/O exception occured while     *   serializing     */    public void serialize( Element elem )        throws IOException    {        reset();        prepare();        serializeNode( elem );        _printer.flush();        if ( _printer.getException() != null )            throw _printer.getException();    }        /**     * Serializes a node using the previously specified     * writer and output format. Throws an exception only if     * an I/O exception occured while serializing.     *     * @param node Node to serialize     * @throws IOException An I/O exception occured while serializing     */    public void serialize( Node node ) throws IOException {        reset();        prepare();        serializeNode( node );        //Print any PIs and Comments which appeared in 'node'        serializePreRoot();         _printer.flush();        if ( _printer.getException() != null )            throw _printer.getException();    }        /**     * Serializes the DOM document fragmnt using the previously specified     * writer and output format. Throws an exception only if     * an I/O exception occured while serializing.     *     * @param elem The element to serialize     * @throws IOException An I/O exception occured while     *   serializing     */    public void serialize( DocumentFragment frag )        throws IOException    {        reset();        prepare();        serializeNode( frag );        _printer.flush();        if ( _printer.getException() != null )            throw _printer.getException();    }    /**     * Serializes the DOM document using the previously specified     * writer and output format. Throws an exception only if     * an I/O exception occured while serializing.     *     * @param doc The document to serialize     * @throws IOException An I/O exception occured while     *   serializing     */    public void serialize( Document doc )        throws IOException    {        reset();        prepare();        serializeNode( doc );        serializePreRoot();         _printer.flush();        if ( _printer.getException() != null )            throw _printer.getException();    }    //------------------------------------------//    // SAX document handler serializing methods //    //------------------------------------------//    public void startDocument()        throws SAXException    {        try {            prepare();        } catch ( IOException except ) {            throw new SAXException( except.toString() );        }        // Nothing to do here. All the magic happens in startDocument(String)    }            public void characters( char[] chars, int start, int length )        throws SAXException    {        ElementState state;        try {        state = content();        // Check if text should be print as CDATA section or unescaped        // based on elements listed in the output format (the element        // state) or whether we are inside a CDATA section or entity.        if ( state.inCData || state.doCData ) {            int          saveIndent;            // Print a CDATA section. The text is not escaped, but ']]>'            // appearing in the code must be identified and dealt with.            // The contents of a text node is considered space preserving.            if ( ! state.inCData ) {                _printer.printText( "<![CDATA[" );                state.inCData = true;            }            saveIndent = _printer.getNextIndent();            _printer.setNextIndent( 0 );            char ch;            final int end = start + length;            for ( int index = start ; index < end; ++index ) {                ch = chars[index];                if ( ch == ']' && index + 2 < end &&                     chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) {                    _printer.printText("]]]]><![CDATA[>");                    index +=2;                     continue;                }                if (!XMLChar.isValid(ch)) {                    // check if it is surrogate                    if (++index < end) {                        surrogates(ch, chars[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[");                    }                }            }            _printer.setNextIndent( saveIndent );        } else {            int saveIndent;            if ( state.preserveSpace ) {                // If preserving space then hold of indentation so no                // excessive spaces are printed at line breaks, escape                // the text content without replacing spaces and print                // the text breaking only at line breaks.                saveIndent = _printer.getNextIndent();                _printer.setNextIndent( 0 );                printText( chars, start, length, true, state.unescaped );                _printer.setNextIndent( saveIndent );            } else {                printText( chars, start, length, false, state.unescaped );            }        }        } catch ( IOException except ) {            throw new SAXException( except );        }    }    public void ignorableWhitespace( char[] chars, int start, int length )        throws SAXException    {        int i;        try {        content();        // Print ignorable whitespaces only when indenting, after        // all they are indentation. Cancel the indentation to        // not indent twice.        if ( _indenting ) {            _printer.setThisIndent( 0 );            for ( i = start ; length-- > 0 ; ++i )                _printer.printText( chars[ i ] );        }        } catch ( IOException except ) {            throw new SAXException( except );        }    }    public final void processingInstruction( String target, String code )        throws SAXException    {        try {            processingInstructionIO( target, code );        } catch ( IOException except ) {        throw new SAXException( except );        }    }    public void processingInstructionIO( String target, String code )        throws IOException    {        int          index;        ElementState state;        state = content();        // Create the processing instruction textual representation.        // Make sure we don't have '?>' inside either target or code.        index = target.indexOf( "?>" );        if ( index >= 0 )            fStrBuffer.append( "<?" ).append( target.substring( 0, index ) );        else            fStrBuffer.append( "<?" ).append( target );        if ( code != null ) {            fStrBuffer.append( ' ' );            index = code.indexOf( "?>" );            if ( index >= 0 )                fStrBuffer.append( code.substring( 0, index ) );            else                fStrBuffer.append( code );        }        fStrBuffer.append( "?>" );        // If before the root element (or after it), do not print        // the PI directly but place it in the pre-root vector.        if ( isDocumentState() ) {            if ( _preRoot == null )                _preRoot = new Vector();            _preRoot.addElement( fStrBuffer.toString() );        } else {            _printer.indent();            printText( fStrBuffer.toString(), true, true );            _printer.unindent();            if ( _indenting )            state.afterElement = true;        }        fStrBuffer.setLength(0);    }    public void comment( char[] chars, int start, int length )        throws SAXException    {        try {        comment( new String( chars, start, length ) );        } catch ( IOException except ) {            throw new SAXException( except );    }    }    public void comment( String text )        throws IOException    {        int          index;        ElementState state;                if ( _format.getOmitComments() )            return;                state  = content();        // Create the processing comment textual representation.        // Make sure we don't have '-->' inside the comment.        index = text.indexOf( "-->" );        if ( index >= 0 )            fStrBuffer.append( "<!--" ).append( text.substring( 0, index ) ).append( "-->" );        else            fStrBuffer.append( "<!--" ).append( text ).append( "-->" );        // If before the root element (or after it), do not print        // the comment directly but place it in the pre-root vector.        if ( isDocumentState() ) {            if ( _preRoot == null )                _preRoot = new Vector();            _preRoot.addElement( fStrBuffer.toString() );        } else {            // Indent this element on a new line if the first            // content of the parent element or immediately            // following an element.            if ( _indenting && ! state.preserveSpace)                _printer.breakLine();						_printer.indent();            printText( fStrBuffer.toString(), true, true );						_printer.unindent();            if ( _indenting )                state.afterElement = true;        }        fStrBuffer.setLength(0);	state.afterComment = true;	state.afterElement = false;    }    public void startCDATA()    {        ElementState state;        state = getElementState();        state.doCData = true;    }    public void endCDATA()    {        ElementState state;        state = getElementState();        state.doCData = false;    }    public void startNonEscaping()    {        ElementState state;        state = getElementState();        state.unescaped = true;    }    public void endNonEscaping()    {        ElementState state;        state = getElementState();        state.unescaped = false;    }    public void startPreserving()    {        ElementState state;        state = getElementState();        state.preserveSpace = true;    }    public void endPreserving()    {        ElementState state;        state = getElementState();        state.preserveSpace = false;    }    /**     * Called at the end of the document to wrap it up.     * Will flush the output stream and throw an exception     * if any I/O error occured while serializing.     *     * @throws SAXException An I/O exception occured during     *  serializing     */    public void endDocument()        throws SAXException    {        try {        // Print all the elements accumulated outside of        // the root element.        serializePreRoot();        // Flush the output, this is necessary for fStrBuffered output.        _printer.flush();        } catch ( IOException except ) {            throw new SAXException( except );    }    }    public void startEntity( String name )    {

⌨️ 快捷键说明

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