basemarkupserializer.java

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

JAVA
1,887
字号
    {        if ( _prepared )            return;        if ( _writer == null && _output == null ) {            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN,                                                           "NoWriterSupplied", null);            throw new IOException(msg);        }        // If the output stream has been set, use it to construct        // the writer. It is possible that the serializer has been        // reused with the same output stream and different encoding.        _encodingInfo = _format.getEncodingInfo();        if ( _output != null ) {            _writer = _encodingInfo.getWriter(_output);        }        if ( _format.getIndenting() ) {            _indenting = true;            _printer = new IndentPrinter( _writer, _format );        } else {            _indenting = false;            _printer = new Printer( _writer, _format );        }        ElementState state;        _elementStateCount = 0;        state = _elementStates[ 0 ];        state.namespaceURI = null;        state.localName = null;        state.rawName = null;        state.preserveSpace = _format.getPreserveSpace();        state.empty = true;        state.afterElement = false;        state.afterComment = false;        state.doCData = state.inCData = false;        state.prefixes = null;        _docTypePublicId = _format.getDoctypePublic();        _docTypeSystemId = _format.getDoctypeSystem();        _started = false;        _prepared = true;    }    //----------------------------------//    // DOM document serializing methods //    //----------------------------------//    /**     * 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 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;            for ( int index = start ; index < length ; ++index ) {                ch = chars[index];                if ( ch == ']' && index + 2 < length &&                     chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) {                    _printer.printText("]]]]><![CDATA[>");                    index +=2;                     continue;                }                if (!XMLChar.isValid(ch)) {                    // check if it is surrogate                    if (++index <length) {                        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;    }

⌨️ 快捷键说明

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