xml11serializer.java

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

JAVA
539
字号
/* * Copyright 1999-2002,2004,2005 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */// Sep 14, 2000://  Fixed problem with namespace handling. Contributed by//  David Blondeau <blondeau@intalio.com>// Sep 14, 2000://  Fixed serializer to report IO exception directly, instead at//  the end of document processing.//  Reported by Patrick Higgins <phiggins@transzap.com>// Aug 21, 2000://  Fixed bug in startDocument not calling prepare.//  Reported by Mikael Staldal <d96-mst-ingen-reklam@d.kth.se>// Aug 21, 2000://  Added ability to omit DOCTYPE declaration.package com.sun.org.apache.xml.internal.serialize;import java.io.IOException;import java.io.OutputStream;import java.io.Writer;import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.util.NamespaceSupport;import com.sun.org.apache.xerces.internal.util.SymbolTable;import com.sun.org.apache.xerces.internal.util.XML11Char;import com.sun.org.apache.xerces.internal.util.XMLChar;import org.xml.sax.SAXException;import org.w3c.dom.DOMError;/** * Implements an XML serializer supporting both DOM and SAX pretty * serializing. For usage instructions see {@link Serializer}. * <p> * If an output stream is used, the encoding is taken from the * output format (defaults to <tt>UTF-8</tt>). If a writer is * used, make sure the writer uses the same encoding (if applies) * as specified in the output format. * <p> * The serializer supports both DOM and SAX. SAX serializing is done by firing * SAX events and using the serializer as a document handler. DOM serializing is done * by calling {@link #serialize(Document)} or by using DOM Level 3   * {@link org.w3c.dom.ls.DOMSerializer} and * serializing with {@link org.w3c.dom.ls.DOMSerializer#write}, * {@link org.w3c.dom.ls.DOMSerializer#writeToString}. * <p> * If an I/O exception occurs while serializing, the serializer * will not throw an exception directly, but only throw it * at the end of serializing (either DOM or SAX's {@link * org.xml.sax.DocumentHandler#endDocument}. * <p> * For elements that are not specified as whitespace preserving, * the serializer will potentially break long text lines at space * boundaries, indent lines, and serialize elements on separate * lines. Line terminators will be regarded as spaces, and * spaces at beginning of line will be stripped. * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a> * @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a> * @author Elena Litani IBM * @version $Revision: 1.2.6.1 $ $Date: 2005/09/09 07:26:18 $ * @see Serializer */public class XML11Serializerextends XMLSerializer {    //    // constants    //    protected static final boolean DEBUG = false;    //     // data    //    //     // DOM Level 3 implementation: variables intialized in DOMSerializerImpl    //     /** stores namespaces in scope */    protected NamespaceSupport fNSBinder;    /** stores all namespace bindings on the current element */    protected NamespaceSupport fLocalNSBinder;    /** symbol table for serialization */    protected SymbolTable fSymbolTable;        // is node dom level 1 node?    protected boolean fDOML1 = false;    // counter for new prefix names    protected int fNamespaceCounter = 1;    protected final static String PREFIX = "NS";    /**     * Controls whether namespace fixup should be performed during     * the serialization.      * NOTE: if this field is set to true the following      * fields need to be initialized: fNSBinder, fLocalNSBinder, fSymbolTable,      * XMLSymbols.EMPTY_STRING, fXmlSymbol, fXmlnsSymbol, fNamespaceCounter.     */    protected boolean fNamespaces = false;    private boolean fPreserveSpace;    /**     * Constructs a new serializer. The serializer cannot be used without     * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}     * first.     */    public XML11Serializer() {        super( );        _format.setVersion("1.1");    }    /**     * Constructs a new serializer. The serializer cannot be used without     * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}     * first.     */    public XML11Serializer( OutputFormat format ) {        super( format );        _format.setVersion("1.1");    }    /**     * Constructs a new serializer that writes to the specified writer     * using the specified output format. If <tt>format</tt> is null,     * will use a default output format.     *     * @param writer The writer to use     * @param format The output format to use, null for the default     */    public XML11Serializer( Writer writer, OutputFormat format ) {        super( writer, format );        _format.setVersion("1.1");    }    /**     * Constructs a new serializer that writes to the specified output     * stream using the specified output format. If <tt>format</tt>     * is null, will use a default output format.     *     * @param output The output stream to use     * @param format The output format to use, null for the default     */    public XML11Serializer( OutputStream output, OutputFormat format ) {        super( output, format != null ? format : new OutputFormat( Method.XML, null, false ) );        _format.setVersion("1.1");    }    //-----------------------------------------//    // SAX content handler serializing methods //    //-----------------------------------------//    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 (!XML11Char.isXML11Valid(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 ( _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[");                        }                    }                }                _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 );        }    }    //    // overwrite printing functions to make sure serializer prints out valid XML    //    protected void printEscaped( String source ) throws IOException {        int length = source.length();        for ( int i = 0 ; i < length ; ++i ) {            int ch = source.charAt(i);            if (!XML11Char.isXML11Valid(ch)) {                if (++i <length) {                    surrogates(ch, source.charAt(i));                } else {                    fatalError("The character '"+(char)ch+"' is an invalid XML character"); 

⌨️ 快捷键说明

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