jaxp/src/com/sun/org/apache/xml/internal/serialize/TextSerializer.java
author joehw
Tue, 29 Jul 2014 20:52:36 -0700
changeset 25834 aba3efbf4ec5
parent 12457 c348e06f0e82
permissions -rw-r--r--
8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer. Reviewed-by: lancea
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
/*
25834
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
     8
 * this work for additional information regarding copyright ownership.
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
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 serializer to report IO exception directly, instead at
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
//  the end of document processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
//  Reported by Patrick Higgins <phiggins@transzap.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
package com.sun.org.apache.xml.internal.serialize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import org.w3c.dom.Element;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import org.xml.sax.AttributeList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import org.xml.sax.Attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * Implements a text serializer supporting both DOM and SAX
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * serializing. For usage instructions see {@link Serializer}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * If an output stream is used, the encoding is taken from the
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * output format (defaults to <tt>UTF-8</tt>). If a writer is
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * used, make sure the writer uses the same encoding (if applies)
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * as specified in the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * The serializer supports both DOM and SAX. DOM serializing is done
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * by calling {@link #serialize} and SAX serializing is done by firing
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * SAX events and using the serializer as a document handler.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * If an I/O exception occurs while serializing, the serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * will not throw an exception directly, but only throw it
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * at the end of serializing (either DOM or SAX's {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * org.xml.sax.DocumentHandler#endDocument}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 * @see Serializer
25834
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
    62
 *
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
    63
 * @deprecated As of JDK 1.9, Xerces 2.9.0, Xerces DOM L3 Serializer implementation
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
    64
 * is replaced by that of Xalan. Main class
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
    65
 * {@link com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl} is replaced
aba3efbf4ec5 8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
joehw
parents: 12457
diff changeset
    66
 * by {@link com.sun.org.apache.xml.internal.serializer.dom3.LSSerializerImpl}.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
public class TextSerializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    extends BaseMarkupSerializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     * Constructs a new serializer. The serializer cannot be used without
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
     * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     * first.
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    public TextSerializer()
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        super( new OutputFormat( Method.TEXT, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    public void setOutputFormat( OutputFormat format )
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        super.setOutputFormat( format != null ? format : new OutputFormat( Method.TEXT, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    // SAX content handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    public void startElement( String namespaceURI, String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
                              String rawName, Attributes attrs )
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        startElement( rawName == null ? localName : rawName, null );
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    public void endElement( String namespaceURI, String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                            String rawName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        endElement( rawName == null ? localName : rawName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    // SAX document handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    //------------------------------000---------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    public void startElement( String tagName, AttributeList attrs )
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        boolean      preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
            state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
            if ( isDocumentState() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
                // If this is the root element handle it differently.
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
                // If the first root element in the document, serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
                // the document's DOCTYPE. Space preserving defaults
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
                // to that of the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
                if ( ! _started )
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
                    startDocument( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
            // For any other element, if first in parent, then
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
            // use the parnet's space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
            preserveSpace = state.preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            // Do not change the current element state yet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
            // This only happens in endElement().
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
            // Ignore all other attributes of the element, only printing
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
            // its contents.
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
            // Now it's time to enter a new element state
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
            // with the tag name and space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            // We still do not change the curent element state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
            state = enterElementState( null, null, tagName, preserveSpace );
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
            throw new SAXException( except );
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
    public void endElement( String tagName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
            endElementIO( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    public void endElementIO( String tagName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
        // Works much like content() with additions for closing
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
        // an element. Note the different checks for the closed
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        // element's state and the parent element's state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
        // Leave the element state and update that of the parent
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
        // (if we're not root) to not empty and after element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
        state = leaveElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        state.afterElement = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
        state.empty = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        if ( isDocumentState() )
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
            _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    public void processingInstructionIO( String target, String code ) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    public void comment( String text )
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    public void comment( char[] chars, int start, int length )
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
    public void characters( char[] chars, int start, int length )
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
            state = content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
            state.doCData = state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
            printText( chars, start, length, true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
    protected void characters( String text, boolean unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        state = content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        state.doCData = state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
        printText( text, true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    // Generic node serializing methods methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
     * Called to serialize the document's DOCTYPE by the root element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
     * This method will check if it has not been called before ({@link #_started}),
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
     * will serialize the document type declaration, and will serialize all
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
     * pre-root comments and PIs that were accumulated in the document
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
     * (see {@link #serializePreRoot}). Pre-root will be serialized even if
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
     * this is not the first root element of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    protected void startDocument( String rootTagName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        // Required to stop processing the DTD, even though the DTD
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
        // is not printed.
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
        _printer.leaveDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
        _started = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        // Always serialize these, even if not te first root element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
        serializePreRoot();
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
     * Called to serialize a DOM element. Equivalent to calling {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
     * #startElement}, {@link #endElement} and serializing everything
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
     * inbetween, but better optimized.
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
    protected void serializeElement( Element elem )
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
        Node         child;
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
        boolean      preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
        String       tagName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        tagName = elem.getTagName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
        if ( isDocumentState() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
            // If this is the root element handle it differently.
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
            // If the first root element in the document, serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            // the document's DOCTYPE. Space preserving defaults
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
            // to that of the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
            if ( ! _started )
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
                startDocument( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
        // For any other element, if first in parent, then
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
        // use the parnet's space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
        preserveSpace = state.preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
        // Do not change the current element state yet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
        // This only happens in endElement().
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
        // Ignore all other attributes of the element, only printing
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
        // its contents.
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
        // If element has children, then serialize them, otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        // serialize en empty tag.
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
        if ( elem.hasChildNodes() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
            // Enter an element state, and serialize the children
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
            // one by one. Finally, end the element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
            state = enterElementState( null, null, tagName, preserveSpace );
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
            child = elem.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
            while ( child != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
                serializeNode( child );
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
                child = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            endElementIO( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
            if ( ! isDocumentState() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
                // After element but parent element is no longer empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                state.afterElement = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
                state.empty = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
     * Serialize the DOM node. This method is unique to the Text serializer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
     * @param node The node to serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
    protected void serializeNode( Node node )
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
        // Based on the node type call the suitable SAX handler.
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
        // Only comments entities and documents which are not
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
        // handled by SAX are serialized directly.
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
        switch ( node.getNodeType() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
        case Node.TEXT_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
            String text;
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
            text = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
            if ( text != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
                characters( node.getNodeValue(), true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
        case Node.CDATA_SECTION_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
            String text;
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
            text = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
            if ( text != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
                characters( node.getNodeValue(), true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
        case Node.COMMENT_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
        case Node.ENTITY_REFERENCE_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
            // Ignore.
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
        case Node.PROCESSING_INSTRUCTION_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
        case Node.ELEMENT_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
            serializeElement( (Element) node );
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
        case Node.DOCUMENT_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
            // !!! Fall through
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
        case Node.DOCUMENT_FRAGMENT_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
            Node         child;
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
            // By definition this will happen if the node is a document,
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
            // document fragment, etc. Just serialize its contents. It will
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
            // work well for other nodes that we do not know how to serialize.
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
            child = node.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
            while ( child != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
                serializeNode( child );
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
                child = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
        default:
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
            break;
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
    protected ElementState content()
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
        if ( ! isDocumentState() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
            // If this is the first content in the element,
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
            // change the state to not-empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
            if ( state.empty )
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
                state.empty = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
            // Except for one content type, all of them
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
            // are not last element. That one content
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
            // type will take care of itself.
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
            state.afterElement = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
        return state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
    protected String getEntityRef( int ch )
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
}