jaxp/src/com/sun/org/apache/xml/internal/serialize/BaseMarkupSerializer.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/BaseMarkupSerializer.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
// Sep 14, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
//  Fixed comments to preserve whitespaces and add a line break
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
//  when indenting. Reported by Gervase Markham <gerv@gerv.net>
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
// Sep 14, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
//  Fixed serializer to report IO exception directly, instead at
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
//  the end of document processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
//  Reported by Patrick Higgins <phiggins@transzap.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
// Sep 13, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
//   CR in character data will print as &#0D;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
// Aug 25, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
//   Fixed processing instruction printing inside element content
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
//   to not escape content. Reported by Mikael Staldal
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
//   <d96-mst@d.kth.se>
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
// Aug 25, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
//   Added ability to omit comments.
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
//   Contributed by Anupam Bagchi <abagchi@jtcsv.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
// Aug 26, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
//   Fixed bug in newline handling when preserving spaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
//   Contributed by Mike Dusseault <mdusseault@home.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
// Aug 29, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
//   Fixed state.unescaped not being set to false when
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
//   entering element state.
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
//   Reported by Lowell Vaughn <lvaughn@agillion.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
package com.sun.org.apache.xml.internal.serialize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
import java.io.OutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
import java.io.Writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
import java.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
import com.sun.org.apache.xerces.internal.util.XMLChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
import org.w3c.dom.DOMImplementation;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
import org.w3c.dom.Document;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
import org.w3c.dom.DocumentFragment;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
import org.w3c.dom.DocumentType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
import org.w3c.dom.DOMError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
import org.w3c.dom.DOMErrorHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
import org.w3c.dom.Element;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
import org.w3c.dom.Entity;
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
import org.w3c.dom.NamedNodeMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
import org.w3c.dom.Notation;
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
import org.w3c.dom.ls.LSException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
import org.w3c.dom.ls.LSSerializerFilter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
import org.w3c.dom.traversal.NodeFilter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
import org.xml.sax.ContentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
import org.xml.sax.DTDHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
import org.xml.sax.DocumentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
import org.xml.sax.Locator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
import org.xml.sax.ext.DeclHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
import org.xml.sax.ext.LexicalHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
 * Base class for a serializer supporting both DOM and SAX pretty
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
 * serializing of XML/HTML/XHTML documents. Derives classes perform
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
 * the method-specific serializing, this class provides the common
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
 * serializing mechanisms.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
 * The serializer must be initialized with the proper writer and
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
 * output format before it can be used by calling {@link #setOutputCharStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
 * or {@link #setOutputByteStream} for the writer and {@link #setOutputFormat}
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
 * for the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
 * The serializer can be reused any number of times, but cannot
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
 * be used concurrently by two threads.
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
 * If an output stream is used, the encoding is taken from the
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
 * output format (defaults to <tt>UTF-8</tt>). If a writer is
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
 * used, make sure the writer uses the same encoding (if applies)
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
 * as specified in the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
 * The serializer supports both DOM and SAX. DOM serializing is done
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
 * by calling {@link #serialize(Document)} and SAX serializing is done by firing
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
 * SAX events and using the serializer as a document handler.
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
 * This also applies to derived class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
 * If an I/O exception occurs while serializing, the serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
 * will not throw an exception directly, but only throw it
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
 * at the end of serializing (either DOM or SAX's {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
 * org.xml.sax.DocumentHandler#endDocument}.
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
 * For elements that are not specified as whitespace preserving,
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
 * the serializer will potentially break long text lines at space
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
 * boundaries, indent lines, and serialize elements on separate
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
 * lines. Line terminators will be regarded as spaces, and
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
 * spaces at beginning of line will be stripped.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
 * When indenting, the serializer is capable of detecting seemingly
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
 * element content, and serializing these elements indented on separate
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
 * lines. An element is serialized indented when it is the first or
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
 * last child of an element, or immediate following or preceding
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
 * another element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
 * @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
 * @author Elena Litani, IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
 * @author Sunitha Reddy, Sun Microsystems
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
 * @see Serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
 * @see LSSerializer
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
public abstract class BaseMarkupSerializer
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    implements ContentHandler, DocumentHandler, LexicalHandler,
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
               DTDHandler, DeclHandler, DOMSerializer, Serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
{
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
    // DOM L3 implementation
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    protected short features = 0xFFFFFFFF;
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    protected DOMErrorHandler fDOMErrorHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    protected final DOMErrorImpl fDOMError = new DOMErrorImpl();
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    protected LSSerializerFilter fDOMFilter;
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    protected EncodingInfo _encodingInfo;
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
     * Holds array of all element states that have been entered.
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
     * The array is automatically resized. When leaving an element,
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
     * it's state is not removed but reused when later returning
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     * to the same nesting level.
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    private ElementState[]  _elementStates;
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
     * The index of the next state to place in the array,
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
     * or one plus the index of the current state. When zero,
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     * we are in no state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    private int             _elementStateCount;
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
     * Vector holding comments and PIs that come before the root
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
     * element (even after it), see {@link #serializePreRoot}.
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    private Vector          _preRoot;
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
     * If the document has been started (header serialized), this
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
     * flag is set to true so it's not started twice.
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    protected boolean       _started;
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     * True if the serializer has been prepared. This flag is set
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
     * to false when the serializer is reset prior to using it,
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
     * and to true after it has been prepared for usage.
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    private boolean         _prepared;
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
     * Association between namespace URIs (keys) and prefixes (values).
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
     * Accumulated here prior to starting an element and placing this
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
     * list in the element state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    protected Hashtable     _prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
     * The system identifier of the document type, if known.
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    protected String        _docTypePublicId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     * The system identifier of the document type, if known.
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    protected String        _docTypeSystemId;
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
     * The output format associated with this serializer. This will never
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
     * be a null reference. If no format was passed to the constructor,
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
     * the default one for this document type will be used. The format
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
     * object is never changed by the serializer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    protected OutputFormat   _format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
     * The printer used for printing text parts.
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    protected Printer       _printer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
     * True if indenting printer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
    protected boolean       _indenting;
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    /** Temporary buffer to store character data */
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    protected final StringBuffer fStrBuffer = new StringBuffer(40);
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
     * The underlying writer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    private Writer          _writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
     * The output stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    private OutputStream    _output;
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
    /** Current node that is being processed  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    protected Node fCurrentNode = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
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
    // Constructor and initialization //
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    //--------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
     * Protected constructor can only be used by derived class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
     * Must initialize the serializer before serializing any document,
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
     * by calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                 * first
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
    protected BaseMarkupSerializer( OutputFormat format )
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
        int i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
        _elementStates = new ElementState[ 10 ];
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        for ( i = 0 ; i < _elementStates.length ; ++i )
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
            _elementStates[ i ] = new ElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
        _format = format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    public DocumentHandler asDocumentHandler()
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
        prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
        return this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    public ContentHandler asContentHandler()
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
        prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
        return this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
    public DOMSerializer asDOMSerializer()
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
        return this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
    public void setOutputByteStream( OutputStream output )
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        if ( output == null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
                                                           "ArgumentIsNull", new Object[]{"output"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            throw new NullPointerException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        _output = output;
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
        _writer = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
    public void setOutputCharStream( Writer writer )
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
        if ( writer == null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                                                           "ArgumentIsNull", new Object[]{"writer"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
            throw new NullPointerException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
        _writer = writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
        _output = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
    public void setOutputFormat( OutputFormat format )
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
        if ( format == null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
                                                           "ArgumentIsNull", new Object[]{"format"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
            throw new NullPointerException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
        _format = format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
    public boolean reset()
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
        if ( _elementStateCount > 1 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
                                                           "ResetInMiddle", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
            throw new IllegalStateException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
        _prepared = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
        fCurrentNode = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
        fStrBuffer.setLength(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
    protected void prepare()
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
        if ( _prepared )
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
        if ( _writer == null && _output == null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
                                                           "NoWriterSupplied", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
            throw new IOException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
        // If the output stream has been set, use it to construct
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
        // the writer. It is possible that the serializer has been
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
        // reused with the same output stream and different encoding.
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
        _encodingInfo = _format.getEncodingInfo();
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
        if ( _output != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
            _writer = _encodingInfo.getWriter(_output);
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
        if ( _format.getIndenting() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
            _indenting = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
            _printer = new IndentPrinter( _writer, _format );
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
            _indenting = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
            _printer = new Printer( _writer, _format );
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
        _elementStateCount = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
        state = _elementStates[ 0 ];
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
        state.namespaceURI = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
        state.localName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
        state.rawName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
        state.preserveSpace = _format.getPreserveSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
        state.empty = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
        state.afterElement = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
        state.afterComment = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
        state.doCData = state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
        state.prefixes = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
        _docTypePublicId = _format.getDoctypePublic();
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        _docTypeSystemId = _format.getDoctypeSystem();
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
        _started = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
        _prepared = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
    }
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
    // DOM document serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
    //----------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     * Serializes the DOM element using the previously specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
     * writer and output format. Throws an exception only if
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
     * an I/O exception occured while serializing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
     * @param elem The element to serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
    public void serialize( Element elem )
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
        serializeNode( elem );
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
        _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
        if ( _printer.getException() != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
            throw _printer.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
     * Serializes a node using the previously specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
     * writer and output format. Throws an exception only if
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
     * an I/O exception occured while serializing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
     * @param node Node to serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
     * @throws IOException An I/O exception occured while serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    public void serialize( Node node ) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
        prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
        serializeNode( node );
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
        //Print any PIs and Comments which appeared in 'node'
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
        serializePreRoot();
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
        if ( _printer.getException() != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
            throw _printer.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
     * Serializes the DOM document fragmnt using the previously specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
     * writer and output format. Throws an exception only if
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
     * an I/O exception occured while serializing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
     * @param elem The element to serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
    public void serialize( DocumentFragment frag )
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
        prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
        serializeNode( frag );
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
        _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
        if ( _printer.getException() != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
            throw _printer.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
     * Serializes the DOM document using the previously specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
     * writer and output format. Throws an exception only if
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
     * an I/O exception occured while serializing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
     * @param doc The document to serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
    public void serialize( Document doc )
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
        prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
        serializeNode( doc );
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
        serializePreRoot();
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
        _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
        if ( _printer.getException() != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
            throw _printer.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
    // SAX document handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
    public void startDocument()
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
            prepare();
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
            throw new SAXException( except.toString() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
        // Nothing to do here. All the magic happens in startDocument(String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
    public void characters( char[] chars, int start, int length )
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
        state = content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
        // Check if text should be print as CDATA section or unescaped
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
        // based on elements listed in the output format (the element
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        // state) or whether we are inside a CDATA section or entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
        if ( state.inCData || state.doCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
            int          saveIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
            // Print a CDATA section. The text is not escaped, but ']]>'
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
            // appearing in the code must be identified and dealt with.
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
            // The contents of a text node is considered space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
            if ( ! state.inCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
                _printer.printText( "<![CDATA[" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
                state.inCData = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
            saveIndent = _printer.getNextIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
            _printer.setNextIndent( 0 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
            char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
            final int end = start + length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
            for ( int index = start ; index < end; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
                ch = chars[index];
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
                if ( ch == ']' && index + 2 < end &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
                     chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
                    _printer.printText("]]]]><![CDATA[>");
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
                    index +=2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
                if (!XMLChar.isValid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
                    if (++index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
                        surrogates(ch, chars[index]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
                    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
                    if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
                        ch == '\n' || ch == '\r' || ch == '\t' ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
                        _printer.printText((char)ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
                        // The character is not printable -- split CDATA section
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
                        _printer.printText("]]>&#x");
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
                        _printer.printText(Integer.toHexString(ch));
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
                        _printer.printText(";<![CDATA[");
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
            _printer.setNextIndent( saveIndent );
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
            int saveIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
            if ( state.preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
                // If preserving space then hold of indentation so no
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
                // excessive spaces are printed at line breaks, escape
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
                // the text content without replacing spaces and print
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
                // the text breaking only at line breaks.
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
                saveIndent = _printer.getNextIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
                _printer.setNextIndent( 0 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
                printText( chars, start, length, true, state.unescaped );
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
                _printer.setNextIndent( saveIndent );
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
                printText( chars, start, length, false, state.unescaped );
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
    public void ignorableWhitespace( char[] chars, int start, int length )
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
        int i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
        content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
        // Print ignorable whitespaces only when indenting, after
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
        // all they are indentation. Cancel the indentation to
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
        // not indent twice.
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
        if ( _indenting ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
            _printer.setThisIndent( 0 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
            for ( i = start ; length-- > 0 ; ++i )
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
                _printer.printText( chars[ i ] );
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
    public final void processingInstruction( String target, String code )
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
            processingInstructionIO( target, code );
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
        throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
    public void processingInstructionIO( String target, String code )
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
        int          index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
        state = content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
        // Create the processing instruction textual representation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
        // Make sure we don't have '?>' inside either target or code.
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
        index = target.indexOf( "?>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
        if ( index >= 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
            fStrBuffer.append( "<?" ).append( target.substring( 0, index ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
            fStrBuffer.append( "<?" ).append( target );
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
        if ( code != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
            fStrBuffer.append( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
            index = code.indexOf( "?>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
            if ( index >= 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
                fStrBuffer.append( code.substring( 0, index ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
                fStrBuffer.append( code );
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
        fStrBuffer.append( "?>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
        // If before the root element (or after it), do not print
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
        // the PI directly but place it in the pre-root vector.
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
        if ( isDocumentState() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
            if ( _preRoot == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
                _preRoot = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
            _preRoot.addElement( fStrBuffer.toString() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
            _printer.indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
            printText( fStrBuffer.toString(), true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
            _printer.unindent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
            if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
            state.afterElement = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
        fStrBuffer.setLength(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
    public void comment( char[] chars, int start, int length )
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
        comment( new String( chars, start, length ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
    public void comment( String text )
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
        int          index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
        if ( _format.getOmitComments() )
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
        state  = content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
        // Create the processing comment textual representation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
        // Make sure we don't have '-->' inside the comment.
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
        index = text.indexOf( "-->" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
        if ( index >= 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
            fStrBuffer.append( "<!--" ).append( text.substring( 0, index ) ).append( "-->" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
            fStrBuffer.append( "<!--" ).append( text ).append( "-->" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
        // If before the root element (or after it), do not print
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
        // the comment directly but place it in the pre-root vector.
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
        if ( isDocumentState() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
            if ( _preRoot == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
                _preRoot = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
            _preRoot.addElement( fStrBuffer.toString() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
            // Indent this element on a new line if the first
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
            // content of the parent element or immediately
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
            // following an element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
            if ( _indenting && ! state.preserveSpace)
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
                                                _printer.indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
            printText( fStrBuffer.toString(), true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
                                                _printer.unindent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
            if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
                state.afterElement = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
        fStrBuffer.setLength(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
        state.afterComment = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
        state.afterElement = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
    public void startCDATA()
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
        state.doCData = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
    public void endCDATA()
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
        state.doCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
    public void startNonEscaping()
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
        state.unescaped = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
    public void endNonEscaping()
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
        state.unescaped = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
    public void startPreserving()
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
        state.preserveSpace = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
    public void endPreserving()
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
        state.preserveSpace = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
     * Called at the end of the document to wrap it up.
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
     * Will flush the output stream and throw an exception
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
     * if any I/O error occured while serializing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
     * @throws SAXException An I/O exception occured during
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
     *  serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
    public void endDocument()
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
        // Print all the elements accumulated outside of
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
        // the root element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
        serializePreRoot();
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
        // Flush the output, this is necessary for fStrBuffered output.
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
        _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
    public void startEntity( String name )
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
        // ???
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
    public void endEntity( String name )
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
        // ???
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
    public void setDocumentLocator( Locator locator )
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
        // Nothing to do
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
    // SAX content handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
    public void skippedEntity ( String name )
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
        endCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
        content();
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
        _printer.printText( '&' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
        _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
        _printer.printText( ';' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
    public void startPrefixMapping( String prefix, String uri )
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
        if ( _prefixes == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
            _prefixes = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
        _prefixes.put( uri, prefix == null ? "" : prefix );
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
    public void endPrefixMapping( String prefix )
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
    // SAX DTD/Decl handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
    public final void startDTD( String name, String publicId, String systemId )
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
        _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
        _docTypePublicId = publicId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
        _docTypeSystemId = systemId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
    public void endDTD()
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
        // Nothing to do here, all the magic occurs in startDocument(String).
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
    public void elementDecl( String name, String model )
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
        _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
        _printer.printText( "<!ELEMENT " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
        _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
        _printer.printText( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
        _printer.printText( model );
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
        _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
        if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
            _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
    public void attributeDecl( String eName, String aName, String type,
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
                               String valueDefault, String value )
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
        _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
        _printer.printText( "<!ATTLIST " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
        _printer.printText( eName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
        _printer.printText( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
        _printer.printText( aName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
        _printer.printText( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
        _printer.printText( type );
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
        if ( valueDefault != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
            _printer.printText( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
            _printer.printText( valueDefault );
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
        if ( value != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
            _printer.printText( " \"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
            printEscaped( value );
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
            _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
        _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
        if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
            _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
    public void internalEntityDecl( String name, String value )
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
        _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
        _printer.printText( "<!ENTITY " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
        _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
        _printer.printText( " \"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
        printEscaped( value );
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
        _printer.printText( "\">" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
        if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
            _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
    public void externalEntityDecl( String name, String publicId, String systemId )
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
        _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
        unparsedEntityDecl( name, publicId, systemId, null );
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
    public void unparsedEntityDecl( String name, String publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
                                    String systemId, String notationName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
        _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
        if ( publicId == null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
            _printer.printText( "<!ENTITY " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
            _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
            _printer.printText( " SYSTEM " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
            printDoctypeURL( systemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
            _printer.printText( "<!ENTITY " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
            _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
            _printer.printText( " PUBLIC " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
            printDoctypeURL( publicId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
            _printer.printText( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
            printDoctypeURL( systemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
        if ( notationName != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
            _printer.printText( " NDATA " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
            _printer.printText( notationName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
        _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
        if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
            _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
    public void notationDecl( String name, String publicId, String systemId )
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
        _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
        if ( publicId != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
            _printer.printText( "<!NOTATION " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
            _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
            _printer.printText( " PUBLIC " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
            printDoctypeURL( publicId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
            if ( systemId != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
                _printer.printText( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
                printDoctypeURL( systemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
            _printer.printText( "<!NOTATION " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
            _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
            _printer.printText( " SYSTEM " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
            printDoctypeURL( systemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
        _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
        if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
            _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
    // Generic node serializing methods methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
     * Serialize the DOM node. This method is shared across XML, HTML and XHTML
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
     * serializers and the differences are masked out in a separate {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
     * #serializeElement}.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
     * @param node The node to serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
     * @see #serializeElement
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
    protected void serializeNode( Node node )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
        fCurrentNode = node;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
        // Based on the node type call the suitable SAX handler.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
        // Only comments entities and documents which are not
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
        // handled by SAX are serialized directly.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
        switch ( node.getNodeType() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
        case Node.TEXT_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
            String text;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
            text = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
            if ( text != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
                if (fDOMFilter !=null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
                    (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_TEXT)!= 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
                    short code = fDOMFilter.acceptNode(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
                    switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
                        case NodeFilter.FILTER_REJECT:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
                        case NodeFilter.FILTER_SKIP: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
                            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
                        default: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
                            characters(text);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
                else if ( !_indenting || getElementState().preserveSpace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
                     || (text.replace('\n',' ').trim().length() != 0))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
                    characters( text );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
        case Node.CDATA_SECTION_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
            String text = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
            if ((features & DOMSerializerImpl.CDATA) != 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
                if (text != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
                    if (fDOMFilter != null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
                        && (fDOMFilter.getWhatToShow()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
                            & NodeFilter.SHOW_CDATA_SECTION)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
                            != 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
                        short code = fDOMFilter.acceptNode(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
                        switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
                            case NodeFilter.FILTER_REJECT :
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
                            case NodeFilter.FILTER_SKIP :
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
                                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
                                    // skip the CDATA node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
                                    return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
                                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
                            default :
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
                                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
                                    //fall through..
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
                                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
                    startCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
                    characters(text);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
                    endCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
                // transform into a text node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
                characters(text);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
        case Node.COMMENT_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
            String text;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
            if ( ! _format.getOmitComments() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
                text = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
                if ( text != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
                    if (fDOMFilter !=null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
                          (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_COMMENT)!= 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
                          short code = fDOMFilter.acceptNode(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
                          switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
                              case NodeFilter.FILTER_REJECT:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
                              case NodeFilter.FILTER_SKIP: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
                                  // skip the comment node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
                                  return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
                              }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
                              default: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
                                   // fall through
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
                              }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
                          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
                    comment( text );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
        case Node.ENTITY_REFERENCE_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
            Node         child;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
            endCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
            content();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
            if (((features & DOMSerializerImpl.ENTITIES) != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
                || (node.getFirstChild() == null)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
                if (fDOMFilter !=null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
                      (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ENTITY_REFERENCE)!= 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
                      short code = fDOMFilter.acceptNode(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
                      switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
                        case NodeFilter.FILTER_REJECT:{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
                            return; // remove the node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
                          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
                          case NodeFilter.FILTER_SKIP: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
                              child = node.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
                              while ( child != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
                                  serializeNode( child );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
                                  child = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
                              }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
                              return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
                          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
                          default: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
                               // fall through
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
                          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
                      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
                  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
                checkUnboundNamespacePrefixedNode(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
7f561c08de6b Initial load
duke
parents:
diff changeset
  1139
                _printer.printText("&");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
                _printer.printText(node.getNodeName());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
                _printer.printText(";");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
                child = node.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
                while ( child != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
                    serializeNode( child );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
                    child = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
        case Node.PROCESSING_INSTRUCTION_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
            if (fDOMFilter !=null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
                  (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_PROCESSING_INSTRUCTION)!= 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
                  short code = fDOMFilter.acceptNode(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
                  switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
                    case NodeFilter.FILTER_REJECT:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
                    case NodeFilter.FILTER_SKIP: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
                          return;  // skip this node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
                    default: { // fall through
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
                  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
            processingInstructionIO( node.getNodeName(), node.getNodeValue() );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
        case Node.ELEMENT_NODE :  {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
            if (fDOMFilter !=null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
                  (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ELEMENT)!= 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
                  short code = fDOMFilter.acceptNode(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
                  switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
                    case NodeFilter.FILTER_REJECT: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
                        return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
                    case NodeFilter.FILTER_SKIP: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
                        Node child = node.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
                        while ( child != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
                            serializeNode( child );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
                            child = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
                        return;  // skip this node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
                    default: { // fall through
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
                  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
            serializeElement( (Element) node );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
        case Node.DOCUMENT_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
            DocumentType      docType;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
            DOMImplementation domImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1199
            NamedNodeMap      map;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
            Entity            entity;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
            Notation          notation;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1202
            int               i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1203
7f561c08de6b Initial load
duke
parents:
diff changeset
  1204
            serializeDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1205
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
            // If there is a document type, use the SAX events to
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
            // serialize it.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
            docType = ( (Document) node ).getDoctype();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
            if (docType != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
                // DOM Level 2 (or higher)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
                domImpl = ( (Document) node ).getImplementation();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1212
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
                    String internal;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
                    _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1216
                    _docTypePublicId = docType.getPublicId();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
                    _docTypeSystemId = docType.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
                    internal = docType.getInternalSubset();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
                    if ( internal != null && internal.length() > 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
                        _printer.printText( internal );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
                    endDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
                // DOM Level 1 -- does implementation have methods?
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
                catch (NoSuchMethodError nsme) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1225
                    Class docTypeClass = docType.getClass();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
                    String docTypePublicId = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
                    String docTypeSystemId = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
                    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1230
                        java.lang.reflect.Method getPublicId = docTypeClass.getMethod("getPublicId", (Class[]) null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1231
                        if (getPublicId.getReturnType().equals(String.class)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
                            docTypePublicId = (String)getPublicId.invoke(docType, (Object[]) null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1235
                    catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
                        // ignore
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
                    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
                        java.lang.reflect.Method getSystemId = docTypeClass.getMethod("getSystemId", (Class[]) null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1240
                        if (getSystemId.getReturnType().equals(String.class)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
                            docTypeSystemId = (String)getSystemId.invoke(docType, (Object[]) null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
                    catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1245
                        // ignore
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
                    _printer.enterDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
                    _docTypePublicId = docTypePublicId;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
                    _docTypeSystemId = docTypeSystemId;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1250
                    endDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
                serializeDTD(docType.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1254
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
            _started = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
            // !! Fall through
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1260
        case Node.DOCUMENT_FRAGMENT_NODE : {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1261
            Node         child;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
            // By definition this will happen if the node is a document,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
            // document fragment, etc. Just serialize its contents. It will
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
            // work well for other nodes that we do not know how to serialize.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
            child = node.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
            while ( child != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
                serializeNode( child );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
                child = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1270
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
        default:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
    /* Serializes XML Declaration, according to 'xml-declaration' property.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
    protected void serializeDocument()throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
        int    i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
7f561c08de6b Initial load
duke
parents:
diff changeset
  1285
        String dtd = _printer.leaveDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
        if (! _started) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
            if (! _format.getOmitXMLDeclaration()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1289
                StringBuffer    buffer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1290
7f561c08de6b Initial load
duke
parents:
diff changeset
  1291
                // Serialize the document declaration appreaing at the head
7f561c08de6b Initial load
duke
parents:
diff changeset
  1292
                // of very XML document (unless asked not to).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1293
                buffer = new StringBuffer( "<?xml version=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1294
                if (_format.getVersion() != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1295
                    buffer.append( _format.getVersion() );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1296
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1297
                    buffer.append( "1.0" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1298
                buffer.append( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1299
                String format_encoding =  _format.getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1300
                if (format_encoding != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1301
                    buffer.append( " encoding=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1302
                    buffer.append( format_encoding );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1303
                    buffer.append( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1304
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1305
                if (_format.getStandalone() && _docTypeSystemId == null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1306
                    _docTypePublicId == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1307
                    buffer.append( " standalone=\"yes\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1308
                buffer.append( "?>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1309
                _printer.printText( buffer );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1310
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1311
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1312
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1313
7f561c08de6b Initial load
duke
parents:
diff changeset
  1314
        // Always serialize these, even if not te first root element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1315
        serializePreRoot();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1316
7f561c08de6b Initial load
duke
parents:
diff changeset
  1317
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1318
7f561c08de6b Initial load
duke
parents:
diff changeset
  1319
    /* Serializes DTD, if present.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1320
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1321
    protected void serializeDTD(String name) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1322
7f561c08de6b Initial load
duke
parents:
diff changeset
  1323
        String dtd = _printer.leaveDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1324
        if (! _format.getOmitDocumentType()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1325
            if (_docTypeSystemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1326
                // System identifier must be specified to print DOCTYPE.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1327
                // If public identifier is specified print 'PUBLIC
7f561c08de6b Initial load
duke
parents:
diff changeset
  1328
                // <public> <system>', if not, print 'SYSTEM <system>'.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1329
                _printer.printText( "<!DOCTYPE " );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1330
                _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1331
                if (_docTypePublicId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1332
                    _printer.printText( " PUBLIC " );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1333
                    printDoctypeURL( _docTypePublicId );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1334
                    if (_indenting) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1335
                        _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1336
                        for (int i = 0 ; i < 18 + name.length() ; ++i)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1337
                            _printer.printText( " " );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1338
                    } else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1339
                        _printer.printText( " " );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1340
                    printDoctypeURL( _docTypeSystemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1341
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1342
                    _printer.printText( " SYSTEM " );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1343
                    printDoctypeURL( _docTypeSystemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1344
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1345
7f561c08de6b Initial load
duke
parents:
diff changeset
  1346
                // If we accumulated any DTD contents while printing.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1347
                // this would be the place to print it.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1348
                if (dtd != null && dtd.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1349
                    _printer.printText( " [" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1350
                    printText( dtd, true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1351
                    _printer.printText( ']' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1352
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1353
7f561c08de6b Initial load
duke
parents:
diff changeset
  1354
                _printer.printText( ">" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1355
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1356
            } else if (dtd != null && dtd.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1357
                _printer.printText( "<!DOCTYPE " );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1358
                _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1359
                _printer.printText( " [" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1360
                printText( dtd, true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1361
                _printer.printText( "]>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1362
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1363
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1364
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1365
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1366
7f561c08de6b Initial load
duke
parents:
diff changeset
  1367
7f561c08de6b Initial load
duke
parents:
diff changeset
  1368
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1369
     * Must be called by a method about to print any type of content.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1370
     * If the element was just opened, the opening tag is closed and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1371
     * will be matched to a closing tag. Returns the current element
7f561c08de6b Initial load
duke
parents:
diff changeset
  1372
     * state with <tt>empty</tt> and <tt>afterElement</tt> set to false.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1373
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1374
     * @return The current element state
7f561c08de6b Initial load
duke
parents:
diff changeset
  1375
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
  1376
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1377
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1378
    protected ElementState content()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1379
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1380
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1381
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1382
7f561c08de6b Initial load
duke
parents:
diff changeset
  1383
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1384
        if ( ! isDocumentState() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1385
            // Need to close CData section first
7f561c08de6b Initial load
duke
parents:
diff changeset
  1386
            if ( state.inCData && ! state.doCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1387
                _printer.printText( "]]>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1388
                state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1389
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1390
            // If this is the first content in the element,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1391
            // change the state to not-empty and close the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1392
            // opening element tag.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1393
            if ( state.empty ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1394
                _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1395
                state.empty = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1396
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1397
            // Except for one content type, all of them
7f561c08de6b Initial load
duke
parents:
diff changeset
  1398
            // are not last element. That one content
7f561c08de6b Initial load
duke
parents:
diff changeset
  1399
            // type will take care of itself.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1400
            state.afterElement = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1401
            // Except for one content type, all of them
7f561c08de6b Initial load
duke
parents:
diff changeset
  1402
            // are not last comment. That one content
7f561c08de6b Initial load
duke
parents:
diff changeset
  1403
            // type will take care of itself.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1404
            state.afterComment = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1405
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1406
        return state;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1407
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1408
7f561c08de6b Initial load
duke
parents:
diff changeset
  1409
7f561c08de6b Initial load
duke
parents:
diff changeset
  1410
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1411
     * Called to print the text contents in the prevailing element format.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1412
     * Since this method is capable of printing text as CDATA, it is used
7f561c08de6b Initial load
duke
parents:
diff changeset
  1413
     * for that purpose as well. White space handling is determined by the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1414
     * current element state. In addition, the output format can dictate
7f561c08de6b Initial load
duke
parents:
diff changeset
  1415
     * whether the text is printed as CDATA or unescaped.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1416
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1417
     * @param text The text to print
7f561c08de6b Initial load
duke
parents:
diff changeset
  1418
     * @param unescaped True is should print unescaped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1419
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
  1420
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1421
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1422
    protected void characters( String text )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1423
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1424
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1425
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1426
7f561c08de6b Initial load
duke
parents:
diff changeset
  1427
        state = content();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1428
        // Check if text should be print as CDATA section or unescaped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1429
        // based on elements listed in the output format (the element
7f561c08de6b Initial load
duke
parents:
diff changeset
  1430
        // state) or whether we are inside a CDATA section or entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1431
7f561c08de6b Initial load
duke
parents:
diff changeset
  1432
        if ( state.inCData || state.doCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1433
            int          index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1434
            int          saveIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1435
7f561c08de6b Initial load
duke
parents:
diff changeset
  1436
            // Print a CDATA section. The text is not escaped, but ']]>'
7f561c08de6b Initial load
duke
parents:
diff changeset
  1437
            // appearing in the code must be identified and dealt with.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1438
            // The contents of a text node is considered space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1439
            if ( ! state.inCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1440
                _printer.printText("<![CDATA[");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1441
                state.inCData = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1442
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1443
            saveIndent = _printer.getNextIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1444
            _printer.setNextIndent( 0 );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1445
            printCDATAText( text);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1446
            _printer.setNextIndent( saveIndent );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1447
7f561c08de6b Initial load
duke
parents:
diff changeset
  1448
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1449
7f561c08de6b Initial load
duke
parents:
diff changeset
  1450
            int saveIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1451
7f561c08de6b Initial load
duke
parents:
diff changeset
  1452
            if ( state.preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1453
                // If preserving space then hold of indentation so no
7f561c08de6b Initial load
duke
parents:
diff changeset
  1454
                // excessive spaces are printed at line breaks, escape
7f561c08de6b Initial load
duke
parents:
diff changeset
  1455
                // the text content without replacing spaces and print
7f561c08de6b Initial load
duke
parents:
diff changeset
  1456
                // the text breaking only at line breaks.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1457
                saveIndent = _printer.getNextIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1458
                _printer.setNextIndent( 0 );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1459
                printText( text, true, state.unescaped );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1460
                _printer.setNextIndent( saveIndent );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1461
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1462
                printText( text, false, state.unescaped );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1463
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1464
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1465
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1466
7f561c08de6b Initial load
duke
parents:
diff changeset
  1467
7f561c08de6b Initial load
duke
parents:
diff changeset
  1468
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1469
     * Returns the suitable entity reference for this character value,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1470
     * or null if no such entity exists. Calling this method with <tt>'&amp;'</tt>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1471
     * will return <tt>"&amp;amp;"</tt>.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1472
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1473
     * @param ch Character value
7f561c08de6b Initial load
duke
parents:
diff changeset
  1474
     * @return Character entity name, or null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1475
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1476
    protected abstract String getEntityRef( int ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1477
7f561c08de6b Initial load
duke
parents:
diff changeset
  1478
7f561c08de6b Initial load
duke
parents:
diff changeset
  1479
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1480
     * Called to serializee the DOM element. The element is serialized based on
7f561c08de6b Initial load
duke
parents:
diff changeset
  1481
     * the serializer's method (XML, HTML, XHTML).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1482
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1483
     * @param elem The element to serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
  1484
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
  1485
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1486
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1487
    protected abstract void serializeElement( Element elem )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1488
        throws IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1489
7f561c08de6b Initial load
duke
parents:
diff changeset
  1490
7f561c08de6b Initial load
duke
parents:
diff changeset
  1491
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1492
     * Comments and PIs cannot be serialized before the root element,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1493
     * because the root element serializes the document type, which
7f561c08de6b Initial load
duke
parents:
diff changeset
  1494
     * generally comes first. Instead such PIs and comments are
7f561c08de6b Initial load
duke
parents:
diff changeset
  1495
     * accumulated inside a vector and serialized by calling this
7f561c08de6b Initial load
duke
parents:
diff changeset
  1496
     * method. Will be called when the root element is serialized
7f561c08de6b Initial load
duke
parents:
diff changeset
  1497
     * and when the document finished serializing.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1498
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1499
     * @throws IOException An I/O exception occured while
7f561c08de6b Initial load
duke
parents:
diff changeset
  1500
     *   serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1501
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1502
    protected void serializePreRoot()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1503
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1504
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1505
        int i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1506
7f561c08de6b Initial load
duke
parents:
diff changeset
  1507
        if ( _preRoot != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1508
            for ( i = 0 ; i < _preRoot.size() ; ++i ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1509
                printText( (String) _preRoot.elementAt( i ), true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1510
                if ( _indenting )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1511
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1512
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1513
            _preRoot.removeAllElements();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1514
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1515
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1516
7f561c08de6b Initial load
duke
parents:
diff changeset
  1517
7f561c08de6b Initial load
duke
parents:
diff changeset
  1518
    //---------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
  1519
    // Text pretty printing and formatting methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1520
    //---------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
  1521
7f561c08de6b Initial load
duke
parents:
diff changeset
  1522
    protected void printCDATAText( String text ) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1523
        int length = text.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1524
        char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1525
7f561c08de6b Initial load
duke
parents:
diff changeset
  1526
        for ( int index = 0 ; index <  length; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1527
            ch = text.charAt( index );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1528
            if (ch == ']'
7f561c08de6b Initial load
duke
parents:
diff changeset
  1529
                && index + 2 < length
7f561c08de6b Initial load
duke
parents:
diff changeset
  1530
                && text.charAt(index + 1) == ']'
7f561c08de6b Initial load
duke
parents:
diff changeset
  1531
                && text.charAt(index + 2) == '>') { // check for ']]>'
7f561c08de6b Initial load
duke
parents:
diff changeset
  1532
                if (fDOMErrorHandler != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1533
                    // REVISIT: this means that if DOM Error handler is not registered we don't report any
7f561c08de6b Initial load
duke
parents:
diff changeset
  1534
                    // fatal errors and might serialize not wellformed document
7f561c08de6b Initial load
duke
parents:
diff changeset
  1535
                    if ((features & DOMSerializerImpl.SPLITCDATA) == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1536
                        String msg = DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1537
                            DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1538
                            "EndingCDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
  1539
                            null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1540
                        if ((features & DOMSerializerImpl.WELLFORMED) != 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1541
                            // issue fatal error
7f561c08de6b Initial load
duke
parents:
diff changeset
  1542
                            modifyDOMError(msg, DOMError.SEVERITY_FATAL_ERROR, "wf-invalid-character", fCurrentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1543
                            fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1544
                            throw new LSException(LSException.SERIALIZE_ERR, msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1545
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1546
                        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1547
                            // issue error
7f561c08de6b Initial load
duke
parents:
diff changeset
  1548
                            modifyDOMError(msg, DOMError.SEVERITY_ERROR, "cdata-section-not-splitted", fCurrentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1549
                            if (!fDOMErrorHandler.handleError(fDOMError)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1550
                                throw new LSException(LSException.SERIALIZE_ERR, msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1551
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1552
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1553
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1554
                        // issue warning
7f561c08de6b Initial load
duke
parents:
diff changeset
  1555
                        String msg =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1556
                            DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1557
                                DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1558
                                "SplittingCDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
  1559
                                null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1560
                        modifyDOMError(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1561
                            msg,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1562
                            DOMError.SEVERITY_WARNING,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1563
                            null, fCurrentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1564
                        fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1565
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1566
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1567
                // split CDATA section
7f561c08de6b Initial load
duke
parents:
diff changeset
  1568
                _printer.printText("]]]]><![CDATA[>");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1569
                index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1570
                continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1571
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1572
7f561c08de6b Initial load
duke
parents:
diff changeset
  1573
            if (!XMLChar.isValid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1574
                // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
  1575
                if (++index <length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1576
                    surrogates(ch, text.charAt(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1577
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1578
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1579
                    fatalError("The character '"+(char)ch+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1580
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1581
                continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1582
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1583
                if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1584
                     ch == '\n' || ch == '\r' || ch == '\t' ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1585
                    _printer.printText((char)ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1586
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1587
7f561c08de6b Initial load
duke
parents:
diff changeset
  1588
                    // The character is not printable -- split CDATA section
7f561c08de6b Initial load
duke
parents:
diff changeset
  1589
                    _printer.printText("]]>&#x");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1590
                    _printer.printText(Integer.toHexString(ch));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1591
                    _printer.printText(";<![CDATA[");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1592
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1593
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1594
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1595
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1596
7f561c08de6b Initial load
duke
parents:
diff changeset
  1597
7f561c08de6b Initial load
duke
parents:
diff changeset
  1598
    protected void surrogates(int high, int low) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1599
        if (XMLChar.isHighSurrogate(high)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1600
            if (!XMLChar.isLowSurrogate(low)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1601
                //Invalid XML
7f561c08de6b Initial load
duke
parents:
diff changeset
  1602
                fatalError("The character '"+(char)low+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1603
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1604
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1605
                int supplemental = XMLChar.supplemental((char)high, (char)low);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1606
                if (!XMLChar.isValid(supplemental)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1607
                    //Invalid XML
7f561c08de6b Initial load
duke
parents:
diff changeset
  1608
                    fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1609
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1610
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1611
                    if (content().inCData ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1612
                        _printer.printText("]]>&#x");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1613
                        _printer.printText(Integer.toHexString(supplemental));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1614
                        _printer.printText(";<![CDATA[");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1615
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1616
                    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1617
                        printHex(supplemental);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1618
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1619
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1620
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1621
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1622
            fatalError("The character '"+(char)high+"' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1623
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1624
7f561c08de6b Initial load
duke
parents:
diff changeset
  1625
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1626
7f561c08de6b Initial load
duke
parents:
diff changeset
  1627
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1628
     * Called to print additional text with whitespace handling.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1629
     * If spaces are preserved, the text is printed as if by calling
7f561c08de6b Initial load
duke
parents:
diff changeset
  1630
     * {@link #printText(String,boolean,boolean)} with a call to {@link Printer#breakLine}
7f561c08de6b Initial load
duke
parents:
diff changeset
  1631
     * for each new line. If spaces are not preserved, the text is
7f561c08de6b Initial load
duke
parents:
diff changeset
  1632
     * broken at space boundaries if longer than the line width;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1633
     * Multiple spaces are printed as such, but spaces at beginning
7f561c08de6b Initial load
duke
parents:
diff changeset
  1634
     * of line are removed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1635
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1636
     * @param text The text to print
7f561c08de6b Initial load
duke
parents:
diff changeset
  1637
     * @param preserveSpace Space preserving flag
7f561c08de6b Initial load
duke
parents:
diff changeset
  1638
     * @param unescaped Print unescaped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1639
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1640
    protected void printText( char[] chars, int start, int length,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1641
                                    boolean preserveSpace, boolean unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1642
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1643
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1644
        int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1645
        char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1646
7f561c08de6b Initial load
duke
parents:
diff changeset
  1647
        if ( preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1648
            // Preserving spaces: the text must print exactly as it is,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1649
            // without breaking when spaces appear in the text and without
7f561c08de6b Initial load
duke
parents:
diff changeset
  1650
            // consolidating spaces. If a line terminator is used, a line
7f561c08de6b Initial load
duke
parents:
diff changeset
  1651
            // break will occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1652
            while ( length-- > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1653
                ch = chars[ start ];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1654
                ++start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1655
                if ( ch == '\n' || ch == '\r' || unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1656
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1657
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1658
                    printEscaped( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1659
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1660
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1661
            // Not preserving spaces: print one part at a time, and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1662
            // use spaces between parts to break them into different
7f561c08de6b Initial load
duke
parents:
diff changeset
  1663
            // lines. Spaces at beginning of line will be stripped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1664
            // by printing mechanism. Line terminator is treated
7f561c08de6b Initial load
duke
parents:
diff changeset
  1665
            // no different than other text part.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1666
            while ( length-- > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1667
                ch = chars[ start ];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1668
                ++start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1669
                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1670
                    _printer.printSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1671
                else if ( unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1672
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1673
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1674
                    printEscaped( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1675
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1676
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1677
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1678
7f561c08de6b Initial load
duke
parents:
diff changeset
  1679
7f561c08de6b Initial load
duke
parents:
diff changeset
  1680
    protected void printText( String text, boolean preserveSpace, boolean unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1681
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1682
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1683
        int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1684
        char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1685
7f561c08de6b Initial load
duke
parents:
diff changeset
  1686
        if ( preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1687
            // Preserving spaces: the text must print exactly as it is,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1688
            // without breaking when spaces appear in the text and without
7f561c08de6b Initial load
duke
parents:
diff changeset
  1689
            // consolidating spaces. If a line terminator is used, a line
7f561c08de6b Initial load
duke
parents:
diff changeset
  1690
            // break will occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1691
            for ( index = 0 ; index < text.length() ; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1692
                ch = text.charAt( index );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1693
                if ( ch == '\n' || ch == '\r' || unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1694
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1695
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1696
                    printEscaped( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1697
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1698
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1699
            // Not preserving spaces: print one part at a time, and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1700
            // use spaces between parts to break them into different
7f561c08de6b Initial load
duke
parents:
diff changeset
  1701
            // lines. Spaces at beginning of line will be stripped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1702
            // by printing mechanism. Line terminator is treated
7f561c08de6b Initial load
duke
parents:
diff changeset
  1703
            // no different than other text part.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1704
            for ( index = 0 ; index < text.length() ; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1705
                ch = text.charAt( index );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1706
                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1707
                    _printer.printSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1708
                else if ( unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1709
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1710
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1711
                    printEscaped( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1712
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1713
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1714
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1715
7f561c08de6b Initial load
duke
parents:
diff changeset
  1716
7f561c08de6b Initial load
duke
parents:
diff changeset
  1717
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1718
     * Print a document type public or system identifier URL.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1719
     * Encapsulates the URL in double quotes, escapes non-printing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1720
     * characters and print it equivalent to {@link #printText}.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1721
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1722
     * @param url The document type url to print
7f561c08de6b Initial load
duke
parents:
diff changeset
  1723
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1724
    protected void printDoctypeURL( String url )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1725
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1726
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1727
        int                i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1728
7f561c08de6b Initial load
duke
parents:
diff changeset
  1729
        _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1730
        for( i = 0 ; i < url.length() ; ++i ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1731
            if ( url.charAt( i ) == '"' ||  url.charAt( i ) < 0x20 || url.charAt( i ) > 0x7F ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1732
                _printer.printText( '%' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1733
                _printer.printText( Integer.toHexString( url.charAt( i ) ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1734
            } else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1735
                _printer.printText( url.charAt( i ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1736
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1737
        _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1738
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1739
7f561c08de6b Initial load
duke
parents:
diff changeset
  1740
7f561c08de6b Initial load
duke
parents:
diff changeset
  1741
    protected void printEscaped( int ch )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1742
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1743
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1744
        String charRef;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1745
        // If there is a suitable entity reference for this
7f561c08de6b Initial load
duke
parents:
diff changeset
  1746
        // character, print it. The list of available entity
7f561c08de6b Initial load
duke
parents:
diff changeset
  1747
        // references is almost but not identical between
7f561c08de6b Initial load
duke
parents:
diff changeset
  1748
        // XML and HTML.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1749
        charRef = getEntityRef( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1750
        if ( charRef != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1751
            _printer.printText( '&' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1752
            _printer.printText( charRef );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1753
            _printer.printText( ';' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1754
        } else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1755
                    ch == '\n' || ch == '\r' || ch == '\t' ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1756
            // Non printables are below ASCII space but not tab or line
7f561c08de6b Initial load
duke
parents:
diff changeset
  1757
            // terminator, ASCII delete, or above a certain Unicode threshold.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1758
            if (ch < 0x10000) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1759
                _printer.printText((char)ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1760
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1761
                _printer.printText((char)(((ch-0x10000)>>10)+0xd800));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1762
                _printer.printText((char)(((ch-0x10000)&0x3ff)+0xdc00));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1763
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1764
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1765
                        printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1766
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1767
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1768
7f561c08de6b Initial load
duke
parents:
diff changeset
  1769
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1770
         * Escapes chars
7f561c08de6b Initial load
duke
parents:
diff changeset
  1771
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1772
         final void printHex( int ch) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1773
                 _printer.printText( "&#x" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1774
                 _printer.printText(Integer.toHexString(ch));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1775
                 _printer.printText( ';' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1776
7f561c08de6b Initial load
duke
parents:
diff changeset
  1777
         }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1778
7f561c08de6b Initial load
duke
parents:
diff changeset
  1779
7f561c08de6b Initial load
duke
parents:
diff changeset
  1780
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1781
     * Escapes a string so it may be printed as text content or attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
  1782
     * value. Non printable characters are escaped using character references.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1783
     * Where the format specifies a deault entity reference, that reference
7f561c08de6b Initial load
duke
parents:
diff changeset
  1784
     * is used (e.g. <tt>&amp;lt;</tt>).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1785
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1786
     * @param source The string to escape
7f561c08de6b Initial load
duke
parents:
diff changeset
  1787
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1788
    protected void printEscaped( String source )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1789
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1790
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1791
        for ( int i = 0 ; i < source.length() ; ++i ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1792
            int ch = source.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1793
            if ((ch & 0xfc00) == 0xd800 && i+1 < source.length()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1794
                int lowch = source.charAt(i+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1795
                if ((lowch & 0xfc00) == 0xdc00) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1796
                    ch = 0x10000 + ((ch-0xd800)<<10) + lowch-0xdc00;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1797
                    i++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1798
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1799
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1800
            printEscaped(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1801
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1802
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1803
7f561c08de6b Initial load
duke
parents:
diff changeset
  1804
7f561c08de6b Initial load
duke
parents:
diff changeset
  1805
    //--------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
  1806
    // Element state handling methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1807
    //--------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
  1808
7f561c08de6b Initial load
duke
parents:
diff changeset
  1809
7f561c08de6b Initial load
duke
parents:
diff changeset
  1810
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1811
     * Return the state of the current element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1812
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1813
     * @return Current element state
7f561c08de6b Initial load
duke
parents:
diff changeset
  1814
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1815
    protected ElementState getElementState()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1816
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1817
        return _elementStates[ _elementStateCount ];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1818
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1819
7f561c08de6b Initial load
duke
parents:
diff changeset
  1820
7f561c08de6b Initial load
duke
parents:
diff changeset
  1821
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1822
     * Enter a new element state for the specified element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1823
     * Tag name and space preserving is specified, element
7f561c08de6b Initial load
duke
parents:
diff changeset
  1824
     * state is initially empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1825
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1826
     * @return Current element state, or null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1827
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1828
    protected ElementState enterElementState( String namespaceURI, String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1829
                                              String rawName, boolean preserveSpace )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1830
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1831
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1832
7f561c08de6b Initial load
duke
parents:
diff changeset
  1833
        if ( _elementStateCount + 1 == _elementStates.length ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1834
            ElementState[] newStates;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1835
7f561c08de6b Initial load
duke
parents:
diff changeset
  1836
            // Need to create a larger array of states. This does not happen
7f561c08de6b Initial load
duke
parents:
diff changeset
  1837
            // often, unless the document is really deep.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1838
            newStates = new ElementState[ _elementStates.length + 10 ];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1839
            for ( int i = 0 ; i < _elementStates.length ; ++i )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1840
                newStates[ i ] = _elementStates[ i ];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1841
            for ( int i = _elementStates.length ; i < newStates.length ; ++i )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1842
                newStates[ i ] = new ElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1843
            _elementStates = newStates;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1844
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1845
7f561c08de6b Initial load
duke
parents:
diff changeset
  1846
        ++_elementStateCount;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1847
        state = _elementStates[ _elementStateCount ];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1848
        state.namespaceURI = namespaceURI;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1849
        state.localName = localName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1850
        state.rawName = rawName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1851
        state.preserveSpace = preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1852
        state.empty = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1853
        state.afterElement = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1854
        state.afterComment = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1855
        state.doCData = state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1856
        state.unescaped = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1857
        state.prefixes = _prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1858
7f561c08de6b Initial load
duke
parents:
diff changeset
  1859
        _prefixes = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1860
        return state;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1861
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1862
7f561c08de6b Initial load
duke
parents:
diff changeset
  1863
7f561c08de6b Initial load
duke
parents:
diff changeset
  1864
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1865
     * Leave the current element state and return to the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1866
     * state of the parent element. If this was the root
7f561c08de6b Initial load
duke
parents:
diff changeset
  1867
     * element, return to the state of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1868
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1869
     * @return Previous element state
7f561c08de6b Initial load
duke
parents:
diff changeset
  1870
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1871
    protected ElementState leaveElementState()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1872
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1873
        if ( _elementStateCount > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1874
            /*Corrected by David Blondeau (blondeau@intalio.com)*/
7f561c08de6b Initial load
duke
parents:
diff changeset
  1875
                _prefixes = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1876
                //_prefixes = _elementStates[ _elementStateCount ].prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1877
            -- _elementStateCount;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1878
            return _elementStates[ _elementStateCount ];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1879
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1880
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "Internal", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1881
            throw new IllegalStateException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1882
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1883
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1884
7f561c08de6b Initial load
duke
parents:
diff changeset
  1885
7f561c08de6b Initial load
duke
parents:
diff changeset
  1886
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1887
     * Returns true if in the state of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1888
     * Returns true before entering any element and after
7f561c08de6b Initial load
duke
parents:
diff changeset
  1889
     * leaving the root element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1890
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1891
     * @return True if in the state of the document
7f561c08de6b Initial load
duke
parents:
diff changeset
  1892
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1893
    protected boolean isDocumentState()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1894
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1895
        return _elementStateCount == 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1896
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1897
7f561c08de6b Initial load
duke
parents:
diff changeset
  1898
7f561c08de6b Initial load
duke
parents:
diff changeset
  1899
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1900
     * Returns the namespace prefix for the specified URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1901
     * If the URI has been mapped to a prefix, returns the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1902
     * prefix, otherwise returns null.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1903
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1904
     * @param namespaceURI The namespace URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1905
     * @return The namespace prefix if known, or null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1906
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1907
    protected String getPrefix( String namespaceURI )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1908
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1909
        String    prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1910
7f561c08de6b Initial load
duke
parents:
diff changeset
  1911
        if ( _prefixes != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1912
            prefix = (String) _prefixes.get( namespaceURI );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1913
            if ( prefix != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1914
                return prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1915
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1916
        if ( _elementStateCount == 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1917
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1918
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1919
            for ( int i = _elementStateCount ; i > 0 ; --i ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1920
                if ( _elementStates[ i ].prefixes != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1921
                    prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1922
                    if ( prefix != null )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1923
                        return prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1924
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1925
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1926
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1927
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1928
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1929
7f561c08de6b Initial load
duke
parents:
diff changeset
  1930
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1931
     * The method modifies global DOM error object
7f561c08de6b Initial load
duke
parents:
diff changeset
  1932
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1933
     * @param message
7f561c08de6b Initial load
duke
parents:
diff changeset
  1934
     * @param severity
7f561c08de6b Initial load
duke
parents:
diff changeset
  1935
     * @param type
7f561c08de6b Initial load
duke
parents:
diff changeset
  1936
     * @return a DOMError
7f561c08de6b Initial load
duke
parents:
diff changeset
  1937
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1938
    protected DOMError modifyDOMError(String message, short severity, String type, Node node){
7f561c08de6b Initial load
duke
parents:
diff changeset
  1939
            fDOMError.reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1940
            fDOMError.fMessage = message;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1941
            fDOMError.fType = type;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1942
            fDOMError.fSeverity = severity;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1943
            fDOMError.fLocator = new DOMLocatorImpl(-1, -1, -1, node, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1944
            return fDOMError;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1945
7f561c08de6b Initial load
duke
parents:
diff changeset
  1946
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1947
7f561c08de6b Initial load
duke
parents:
diff changeset
  1948
7f561c08de6b Initial load
duke
parents:
diff changeset
  1949
    protected void fatalError(String message) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1950
        if (fDOMErrorHandler != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1951
            modifyDOMError(message, DOMError.SEVERITY_FATAL_ERROR, null, fCurrentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1952
            fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1953
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1954
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1955
            throw new IOException(message);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1956
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1957
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1958
7f561c08de6b Initial load
duke
parents:
diff changeset
  1959
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1960
         * DOM level 3:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1961
         * Check a node to determine if it contains unbound namespace prefixes.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1962
         *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1963
         * @param node The node to check for unbound namespace prefices
7f561c08de6b Initial load
duke
parents:
diff changeset
  1964
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1965
         protected void checkUnboundNamespacePrefixedNode (Node node) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1966
7f561c08de6b Initial load
duke
parents:
diff changeset
  1967
         }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1968
}