FileDocCategorySizeDatePackage
XML11Serializer.javaAPI DocApache Xerces 3.0.121228Fri Sep 14 20:33:56 BST 2007org.apache.xml.serialize

XML11Serializer

public class XML11Serializer extends XMLSerializer
Implements an XML serializer supporting both DOM and SAX pretty serializing. For usage instructions see {@link Serializer}.

If an output stream is used, the encoding is taken from the output format (defaults to UTF-8). If a writer is used, make sure the writer uses the same encoding (if applies) as specified in the output format.

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.LSSerializer} and serializing with {@link org.w3c.dom.ls.LSSerializer#write}, {@link org.w3c.dom.ls.LSSerializer#writeToString}.

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}.

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.

deprecated
This class was deprecated in Xerces 2.9.0. It is recommended that new applications use the DOM Level 3 LSSerializer or JAXP's Transformation API for XML (TrAX) for serializing XML. See the Xerces documentation for more information.
author
Assaf Arkin
author
Rahul Srivastava
author
Elena Litani IBM
version
$Revision: 476047 $ $Date: 2006-11-16 23:27:45 -0500 (Thu, 16 Nov 2006) $
see
Serializer

Fields Summary
protected static final boolean
DEBUG
protected org.apache.xerces.util.NamespaceSupport
fNSBinder
stores namespaces in scope
protected org.apache.xerces.util.NamespaceSupport
fLocalNSBinder
stores all namespace bindings on the current element
protected org.apache.xerces.util.SymbolTable
fSymbolTable
symbol table for serialization
protected boolean
fDOML1
protected int
fNamespaceCounter
protected static final String
PREFIX
protected boolean
fNamespaces
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.
Constructors Summary
public XML11Serializer()
Constructs a new serializer. The serializer cannot be used without calling {@link #setOutputCharStream} or {@link #setOutputByteStream} first.


                          
      
        super( );
        _format.setVersion("1.1");
    
public XML11Serializer(OutputFormat format)
Constructs a new serializer. The serializer cannot be used without calling {@link #setOutputCharStream} or {@link #setOutputByteStream} first.

        super( format );
        _format.setVersion("1.1");
    
public XML11Serializer(Writer writer, OutputFormat format)
Constructs a new serializer that writes to the specified writer using the specified output format. If format 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

        super( writer, format );
        _format.setVersion("1.1");
    
public XML11Serializer(OutputStream output, OutputFormat format)
Constructs a new serializer that writes to the specified output stream using the specified output format. If format 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

        super( output, format != null ? format : new OutputFormat( Method.XML, null, false ) );
        _format.setVersion("1.1");
    
Methods Summary
public voidcharacters(char[] chars, int start, int length)

        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], true);
                        } 
                        else {
                            fatalError("The character '"+ch+"' is an invalid XML character"); 
                        }
                        continue;
                    }
                    if ( _encodingInfo.isPrintable(ch) && XML11Char.isXML11ValidLiteral(ch)) {
                        _printer.printText(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 );
        }
    
protected final voidprintCDATAText(java.lang.String text)

        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), true);
                } 
                else {
                    fatalError("The character '" + ch + "' is an invalid XML character");
                }
                continue;
            }
            if (_encodingInfo.isPrintable(ch)
                && XML11Char.isXML11ValidLiteral(ch)) {
                _printer.printText(ch);
            } 
            else {
                // The character is not printable -- split CDATA section
                _printer.printText("]]>&#x");
                _printer.printText(Integer.toHexString(ch));
                _printer.printText(";<![CDATA[");
            }
        }
    
protected voidprintEscaped(java.lang.String source)

        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), false);
                } 
                else {
                    fatalError("The character '"+(char)ch+"' is an invalid XML character"); 
                }
                continue;
            }
            if (ch == '\n" || ch == '\r" || ch == '\t" || ch == 0x0085 || ch == 0x2028) {
                printHex(ch);
            } 
            else if (ch == '<") {
                _printer.printText("<");
            } 
            else if (ch == '&") {
                _printer.printText("&");
            } 
            else if (ch == '"") {
                _printer.printText(""");
            } 
            else if ((ch >= ' " && _encodingInfo.isPrintable((char) ch))) {
                _printer.printText((char) ch);
            } 
            else {
                printHex(ch);
            }
        }
    
protected voidprintText(java.lang.String text, boolean preserveSpace, boolean unescaped)

        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), true);
                    } else {
                        fatalError("The character '"+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), true);
                    } else {
                        fatalError("The character '"+ch+"' is an invalid XML character"); 
                    }
                    continue;
                }
                if ( unescaped && XML11Char.isXML11ValidLiteral(ch) ) {
                    _printer.printText( ch );
                }
                else {
                    printXMLChar( ch );
                }
            }
        }
    
protected voidprintText(char[] chars, int start, int length, boolean preserveSpace, boolean unescaped)


        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 ) {
                char ch = chars[start++];
                if (!XML11Char.isXML11Valid(ch)) {
                    // check if it is surrogate
                    if ( length-- > 0) {
                        surrogates(ch, chars[start++], true);
                    } else {
                        fatalError("The character '"+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 ) {
                char ch = chars[start++];
                if (!XML11Char.isXML11Valid(ch)) {
                    // check if it is surrogate
                    if ( length-- > 0) {
                        surrogates(ch, chars[start++], true);
                    } else {
                        fatalError("The character '"+ch+"' is an invalid XML character"); 
                    }
                    continue;
                }
                if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) {
                    _printer.printText( ch );
                }
                else {
                    printXMLChar( ch );
                }
            }
        }
    
protected final voidprintXMLChar(int ch)

        if (ch == '\r" || ch == 0x0085 || ch == 0x2028) {
            printHex(ch);
        } 
        else if ( ch == '<") {
            _printer.printText("<");
        } 
        else if (ch == '&") {
            _printer.printText("&");
        } 
        else if (ch == '>"){
            // character sequence "]]>" can't appear in content, therefore
            // we should escape '>' 
            _printer.printText(">");
        } 
        else if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) { 
            _printer.printText((char)ch);
        } 
        else {
            printHex(ch);
        }
    
public booleanreset()

        super.reset();
        return true;
    
protected final voidsurrogates(int high, int low, boolean inContent)

        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 (inContent && 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"); 
        }