jaxp/src/com/sun/org/apache/xml/internal/serialize/XML11Serializer.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/xml/internal/serialize/XML11Serializer.java@7f561c08de6b
child 23777 ce87cedb71cf
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
// Sep 14, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
//  Fixed problem with namespace handling. Contributed by
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
//  David Blondeau <blondeau@intalio.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
// Sep 14, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
//  Fixed serializer to report IO exception directly, instead at
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
//  the end of document processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
//  Reported by Patrick Higgins <phiggins@transzap.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
// Aug 21, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
//  Fixed bug in startDocument not calling prepare.
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
//  Reported by Mikael Staldal <d96-mst-ingen-reklam@d.kth.se>
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
// Aug 21, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
//  Added ability to omit DOCTYPE declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
package com.sun.org.apache.xml.internal.serialize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import java.io.OutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import java.io.Writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import com.sun.org.apache.xerces.internal.impl.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xerces.internal.util.SymbolTable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
import com.sun.org.apache.xerces.internal.util.XML11Char;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import com.sun.org.apache.xerces.internal.util.XMLChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
import org.w3c.dom.DOMError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * Implements an XML serializer supporting both DOM and SAX pretty
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * serializing. For usage instructions see {@link Serializer}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * If an output stream is used, the encoding is taken from the
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * output format (defaults to <tt>UTF-8</tt>). If a writer is
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 * used, make sure the writer uses the same encoding (if applies)
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * as specified in the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * The serializer supports both DOM and SAX. SAX serializing is done by firing
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 * SAX events and using the serializer as a document handler. DOM serializing is done
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
 * by calling {@link #serialize(Document)} or by using DOM Level 3
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
 * {@link org.w3c.dom.ls.DOMSerializer} and
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 * serializing with {@link org.w3c.dom.ls.DOMSerializer#write},
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * {@link org.w3c.dom.ls.DOMSerializer#writeToString}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * If an I/O exception occurs while serializing, the serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 * will not throw an exception directly, but only throw it
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * at the end of serializing (either DOM or SAX's {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 * org.xml.sax.DocumentHandler#endDocument}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
 * For elements that are not specified as whitespace preserving,
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
 * the serializer will potentially break long text lines at space
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
 * boundaries, indent lines, and serialize elements on separate
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
 * lines. Line terminators will be regarded as spaces, and
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
 * spaces at beginning of line will be stripped.
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
 * @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
 * @author Elena Litani IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
 * @see Serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
public class XML11Serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
extends XMLSerializer {
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    // constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    protected static final boolean DEBUG = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    // data
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    // DOM Level 3 implementation: variables intialized in DOMSerializerImpl
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    /** stores namespaces in scope */
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    protected NamespaceSupport fNSBinder;
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    /** stores all namespace bindings on the current element */
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    protected NamespaceSupport fLocalNSBinder;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    /** symbol table for serialization */
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    protected SymbolTable fSymbolTable;
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    // is node dom level 1 node?
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    protected boolean fDOML1 = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    // counter for new prefix names
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    protected int fNamespaceCounter = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    protected final static String PREFIX = "NS";
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
     * Controls whether namespace fixup should be performed during
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
     * the serialization.
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
     * NOTE: if this field is set to true the following
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * fields need to be initialized: fNSBinder, fLocalNSBinder, fSymbolTable,
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * XMLSymbols.EMPTY_STRING, fXmlSymbol, fXmlnsSymbol, fNamespaceCounter.
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    protected boolean fNamespaces = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    private boolean fPreserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * Constructs a new serializer. The serializer cannot be used without
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     * first.
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    public XML11Serializer() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        super( );
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        _format.setVersion("1.1");
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     * Constructs a new serializer. The serializer cannot be used without
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
     * first.
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    public XML11Serializer( OutputFormat format ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        super( format );
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
        _format.setVersion("1.1");
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     * Constructs a new serializer that writes to the specified writer
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     * using the specified output format. If <tt>format</tt> is null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
     * will use a default output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
     * @param writer The writer to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     * @param format The output format to use, null for the default
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    public XML11Serializer( Writer writer, OutputFormat format ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        super( writer, format );
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
        _format.setVersion("1.1");
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     * Constructs a new serializer that writes to the specified output
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     * stream using the specified output format. If <tt>format</tt>
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     * is null, will use a default output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
     * @param output The output stream to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
     * @param format The output format to use, null for the default
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    public XML11Serializer( OutputStream output, OutputFormat format ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
        super( output, format != null ? format : new OutputFormat( Method.XML, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        _format.setVersion("1.1");
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    // SAX content handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    public void characters( char[] chars, int start, int length )
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
            state = content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
            // Check if text should be print as CDATA section or unescaped
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
            // based on elements listed in the output format (the element
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
            // state) or whether we are inside a CDATA section or entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
            if ( state.inCData || state.doCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
                int          saveIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
                // Print a CDATA section. The text is not escaped, but ']]>'
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
                // appearing in the code must be identified and dealt with.
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
                // The contents of a text node is considered space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                if ( ! state.inCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                    _printer.printText( "<![CDATA[" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                    state.inCData = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
                saveIndent = _printer.getNextIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
                _printer.setNextIndent( 0 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
                char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
                final int end = start + length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
                for ( int index = start; index < end; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
                    ch = chars[index];
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
                    if ( ch == ']' && index + 2 < end &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
                        chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
                        _printer.printText("]]]]><![CDATA[>");
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                        index +=2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
                        continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
                    if (!XML11Char.isXML11Valid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
                        // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
                        if (++index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
                            surrogates(ch, chars[index]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
                        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
                            fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
                        continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
                        if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
                            _printer.printText((char)ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
                        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
                            // The character is not printable -- split CDATA section
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
                            _printer.printText("]]>&#x");
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
                            _printer.printText(Integer.toHexString(ch));
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
                            _printer.printText(";<![CDATA[");
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
                _printer.setNextIndent( saveIndent );
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
                int saveIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
                if ( state.preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
                    // If preserving space then hold of indentation so no
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
                    // excessive spaces are printed at line breaks, escape
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
                    // the text content without replacing spaces and print
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
                    // the text breaking only at line breaks.
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
                    saveIndent = _printer.getNextIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
                    _printer.setNextIndent( 0 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
                    printText( chars, start, length, true, state.unescaped );
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                    _printer.setNextIndent( saveIndent );
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
                    printText( chars, start, length, false, state.unescaped );
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
    // overwrite printing functions to make sure serializer prints out valid XML
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    protected void printEscaped( String source ) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
        int length = source.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
        for ( int i = 0 ; i < length ; ++i ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            int ch = source.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
            if (!XML11Char.isXML11Valid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
                if (++i <length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
                    surrogates(ch, source.charAt(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
                    fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
                continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
            if (ch == '\n' || ch == '\r' || ch == '\t' || ch == 0x0085 || ch == 0x2028){
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
                                printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
                        } else if (ch == '<') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
                                _printer.printText("&lt;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
                        } else if (ch == '&') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
                                _printer.printText("&amp;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
                        } else if (ch == '"') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
                                _printer.printText("&quot;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
                        } else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
                                _printer.printText((char) ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
                        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
                                printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    protected final void printCDATAText(String text) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
        int length = text.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
        char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
        for (int index = 0; index < length; ++index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
            ch = text.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
            if (ch == ']'
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
                && index + 2 < length
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
                && text.charAt(index + 1) == ']'
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                && text.charAt(index + 2) == '>') { // check for ']]>'
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
                if (fDOMErrorHandler != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
                    // REVISIT: this means that if DOM Error handler is not registered we don't report any
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                    // fatal errors and might serialize not wellformed document
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
                if ((features & DOMSerializerImpl.SPLITCDATA) == 0
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
                    && (features & DOMSerializerImpl.WELLFORMED) == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
                    // issue fatal error
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
                    String msg =
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
                        DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
                            DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
                            "EndingCDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
                            null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
                    modifyDOMError(
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
                        msg,
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
                        DOMError.SEVERITY_FATAL_ERROR,
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
                        null, fCurrentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
                    boolean continueProcess =
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
                        fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
                    if (!continueProcess) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
                        throw new IOException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
                    // issue warning
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
                    String msg =
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
                        DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
                            DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
                            "SplittingCDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
                            null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
                    modifyDOMError(
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
                        msg,
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
                        DOMError.SEVERITY_WARNING,
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
                        null, fCurrentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
                    fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
                // split CDATA section
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
                _printer.printText("]]]]><![CDATA[>");
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
                index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
                continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
            if (!XML11Char.isXML11Valid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
                // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
                if (++index < length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
                    surrogates(ch, text.charAt(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
                    fatalError(
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
                        "The character '"
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
                            + (char) ch
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
                            + "' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
                continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
                if (_encodingInfo.isPrintable((char) ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
                    && XML11Char.isXML11ValidLiteral(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
                    _printer.printText((char) ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
                    // The character is not printable -- split CDATA section
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
                    _printer.printText("]]>&#x");
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
                    _printer.printText(Integer.toHexString(ch));
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
                    _printer.printText(";<![CDATA[");
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
    // note that this "int" should, in all cases, be a char.
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
    // REVISIT:  make it a char...
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
    protected final void printXMLChar( int ch ) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
        if (ch == '\r' || ch == 0x0085 || ch == 0x2028) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
                        printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
        } else if ( ch == '<') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
            _printer.printText("&lt;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
        } else if (ch == '&') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
            _printer.printText("&amp;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
                } else if (ch == '>'){
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
                        // character sequence "]]>" can't appear in content, therefore
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
                        // we should escape '>'
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
                        _printer.printText("&gt;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        } else if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
            _printer.printText((char)ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
             printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
    protected final void surrogates(int high, int low) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
        if (XMLChar.isHighSurrogate(high)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
            if (!XMLChar.isLowSurrogate(low)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
                //Invalid XML
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
                fatalError("The character '"+(char)low+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
                int supplemental = XMLChar.supplemental((char)high, (char)low);
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
                if (!XML11Char.isXML11Valid(supplemental)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
                    //Invalid XML
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
                    fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
                    if (content().inCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
                        _printer.printText("]]>&#x");
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
                        _printer.printText(Integer.toHexString(supplemental));
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
                        _printer.printText(";<![CDATA[");
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
                    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
                                                printHex(supplemental);
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
            fatalError("The character '"+(char)high+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
    protected void printText( String text, boolean preserveSpace, boolean unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
        int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
        char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
        int length = text.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
        if ( preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
            // Preserving spaces: the text must print exactly as it is,
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
            // without breaking when spaces appear in the text and without
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
            // consolidating spaces. If a line terminator is used, a line
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
            // break will occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
            for ( index = 0 ; index < length ; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
                ch = text.charAt( index );
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
                if (!XML11Char.isXML11Valid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
                    if (++index <length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
                        surrogates(ch, text.charAt(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
                if ( unescaped  && XML11Char.isXML11ValidLiteral(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
                } else
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
                    printXMLChar( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
            // Not preserving spaces: print one part at a time, and
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
            // use spaces between parts to break them into different
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
            // lines. Spaces at beginning of line will be stripped
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
            // by printing mechanism. Line terminator is treated
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
            // no different than other text part.
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
            for ( index = 0 ; index < length ; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
                ch = text.charAt( index );
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
                if (!XML11Char.isXML11Valid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
                    if (++index <length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
                        surrogates(ch, text.charAt(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
                if ( unescaped && XML11Char.isXML11ValidLiteral(ch) )
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
                    printXMLChar( ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
    protected void printText( char[] chars, int start, int length,
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
                              boolean preserveSpace, boolean unescaped ) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
        int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
        char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
        if ( preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
            // Preserving spaces: the text must print exactly as it is,
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
            // without breaking when spaces appear in the text and without
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
            // consolidating spaces. If a line terminator is used, a line
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
            // break will occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
            while ( length-- > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
                ch = chars[start++];
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
                if (!XML11Char.isXML11Valid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
                    if ( length-- > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
                        surrogates(ch, chars[start++]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
                if ( unescaped && XML11Char.isXML11ValidLiteral(ch))
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
                    printXMLChar( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
            // Not preserving spaces: print one part at a time, and
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
            // use spaces between parts to break them into different
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
            // lines. Spaces at beginning of line will be stripped
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
            // by printing mechanism. Line terminator is treated
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
            // no different than other text part.
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
            while ( length-- > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
                ch = chars[start++];
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
                if (!XML11Char.isXML11Valid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
                    if ( length-- > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
                        surrogates(ch, chars[start++]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
                if ( unescaped && XML11Char.isXML11ValidLiteral(ch))
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
                    printXMLChar( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
    public boolean reset() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
        super.reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
}