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