jaxp/src/com/sun/org/apache/xml/internal/serialize/XMLSerializer.java
author aefimov
Mon, 31 Mar 2014 19:03:41 +0400
changeset 23777 ce87cedb71cf
parent 12457 c348e06f0e82
child 25834 aba3efbf4ec5
permissions -rw-r--r--
8035437: Xerces Update: xml/serialize/DOMSerializerImpl Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
     8
 * this work for additional information regarding copyright ownership.
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
// Sep 14, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
//  Fixed problem with namespace handling. Contributed by
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
//  David Blondeau <blondeau@intalio.com>
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
// Aug 21, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
//  Fixed bug in startDocument not calling prepare.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
//  Reported by Mikael Staldal <d96-mst-ingen-reklam@d.kth.se>
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
// Aug 21, 2000:
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
//  Added ability to omit DOCTYPE declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
package com.sun.org.apache.xml.internal.serialize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import java.io.OutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import java.io.Writer;
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    40
import java.util.Iterator;
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    41
import java.util.Map;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import com.sun.org.apache.xerces.internal.util.SymbolTable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import com.sun.org.apache.xerces.internal.util.XMLChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xerces.internal.util.XMLSymbols;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import org.w3c.dom.Attr;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import org.w3c.dom.DOMError;
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    51
import org.w3c.dom.Document;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
import org.w3c.dom.Element;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
import org.w3c.dom.NamedNodeMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
import org.w3c.dom.traversal.NodeFilter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
import org.xml.sax.AttributeList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
import org.xml.sax.Attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
import org.xml.sax.helpers.AttributesImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * Implements an XML serializer supporting both DOM and SAX pretty
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 * serializing. For usage instructions see {@link Serializer}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
 * If an output stream is used, the encoding is taken from the
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 * output format (defaults to <tt>UTF-8</tt>). If a writer is
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * used, make sure the writer uses the same encoding (if applies)
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * as specified in the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 * The serializer supports both DOM and SAX. SAX serializing is done by firing
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * SAX events and using the serializer as a document handler. DOM serializing is done
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 * by calling {@link #serialize(Document)} or by using DOM Level 3
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    73
 * {@link org.w3c.dom.ls.LSSerializer} and
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    74
 * serializing with {@link org.w3c.dom.ls.LSSerializer#write},
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
    75
 * {@link org.w3c.dom.ls.LSSerializer#writeToString}.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
 * If an I/O exception occurs while serializing, the serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
 * will not throw an exception directly, but only throw it
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
 * at the end of serializing (either DOM or SAX's {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
 * org.xml.sax.DocumentHandler#endDocument}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
 * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
 * For elements that are not specified as whitespace preserving,
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
 * the serializer will potentially break long text lines at space
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
 * boundaries, indent lines, and serialize elements on separate
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
 * lines. Line terminators will be regarded as spaces, and
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
 * spaces at beginning of line will be stripped.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
 * @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
 * @author Elena Litani IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
 * @see Serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
public class XMLSerializer
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
extends BaseMarkupSerializer {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    // constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    protected static final boolean DEBUG = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    // data
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    // DOM Level 3 implementation: variables intialized in DOMSerializerImpl
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    /** stores namespaces in scope */
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    protected NamespaceSupport fNSBinder;
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    /** stores all namespace bindings on the current element */
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    protected NamespaceSupport fLocalNSBinder;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    /** symbol table for serialization */
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    protected SymbolTable fSymbolTable;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    protected final static String PREFIX = "NS";
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * Controls whether namespace fixup should be performed during
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     * the serialization.
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     * NOTE: if this field is set to true the following
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     * fields need to be initialized: fNSBinder, fLocalNSBinder, fSymbolTable,
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * XMLSymbols.EMPTY_STRING, fXmlSymbol, fXmlnsSymbol
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    protected boolean fNamespaces = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * Controls whether namespace prefixes will be printed out during serialization
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    protected boolean fNamespacePrefixes = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    private boolean fPreserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     * Constructs a new serializer. The serializer cannot be used without
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
     * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     * first.
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    public XMLSerializer() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        super( new OutputFormat( Method.XML, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     * Constructs a new serializer. The serializer cannot be used without
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     * first.
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    public XMLSerializer( OutputFormat format ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        super( format != null ? format : new OutputFormat( Method.XML, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        _format.setMethod( Method.XML );
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
     * Constructs a new serializer that writes to the specified writer
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
     * using the specified output format. If <tt>format</tt> is null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
     * will use a default output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
     * @param writer The writer to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     * @param format The output format to use, null for the default
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    public XMLSerializer( Writer writer, OutputFormat format ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
        super( format != null ? format : new OutputFormat( Method.XML, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
        _format.setMethod( Method.XML );
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        setOutputCharStream( writer );
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * Constructs a new serializer that writes to the specified output
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     * stream using the specified output format. If <tt>format</tt>
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     * is null, will use a default output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
     * @param output The output stream to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     * @param format The output format to use, null for the default
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    public XMLSerializer( OutputStream output, OutputFormat format ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        super( format != null ? format : new OutputFormat( Method.XML, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        _format.setMethod( Method.XML );
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        setOutputByteStream( output );
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    public void setOutputFormat( OutputFormat format ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        super.setOutputFormat( format != null ? format : new OutputFormat( Method.XML, null, false ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     * This methods turns on namespace fixup algorithm during
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
     * DOM serialization.
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   197
     * @see org.w3c.dom.ls.LSSerializer
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     * @param namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    public void setNamespaces (boolean namespaces){
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
        fNamespaces = namespaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
        if (fNSBinder == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
            fNSBinder = new NamespaceSupport();
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
            fLocalNSBinder = new NamespaceSupport();
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
            fSymbolTable = new SymbolTable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    // SAX content handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
    //-----------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
    public void startElement( String namespaceURI, String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
                              String rawName, Attributes attrs )
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
    throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
        int          i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        boolean      preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        String       name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
        String       value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
            System.out.println("==>startElement("+namespaceURI+","+localName+
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
                               ","+rawName+")");
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
            if (_printer == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "NoWriterSupplied", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
                throw new IllegalStateException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
            state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
            if (isDocumentState()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
                // If this is the root element handle it differently.
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
                // If the first root element in the document, serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
                // the document's DOCTYPE. Space preserving defaults
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
                // to that of the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
                if (! _started)
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
                    startDocument( ( localName == null || localName.length() == 0 ) ? rawName : localName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
                // For any other element, if first in parent, then
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
                // close parent's opening tag and use the parnet's
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
                // space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
                if (state.empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
                    _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
                // Must leave CData section first
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                if (state.inCData) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                    _printer.printText( "]]>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
                    state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
                // Indent this element on a new line if the first
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
                // content of the parent element or immediately
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
                // following an element or a comment
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
                if (_indenting && ! state.preserveSpace &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
                    ( state.empty || state.afterElement || state.afterComment))
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
                    _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
            preserveSpace = state.preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
            //We remove the namespaces from the attributes list so that they will
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
            //be in _prefixes
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
            attrs = extractNamespaces(attrs);
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            // Do not change the current element state yet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
            // This only happens in endElement().
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
            if (rawName == null || rawName.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
                if (localName == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
                    String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "NoName", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
                    throw new SAXException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
                if (namespaceURI != null && ! namespaceURI.equals( "" )) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
                    String prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
                    prefix = getPrefix( namespaceURI );
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   278
                    if (prefix != null && prefix.length() > 0) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
                        rawName = prefix + ":" + localName;
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   280
                    }
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   281
                    else {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
                        rawName = localName;
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   283
                    }
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   284
                }
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   285
                else {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
                    rawName = localName;
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   287
                }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
            _printer.printText( '<' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
            _printer.printText( rawName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
            _printer.indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            // For each attribute print it's name and value as one part,
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
            // separated with a space so the element can be broken on
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
            // multiple lines.
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            if (attrs != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                for (i = 0 ; i < attrs.getLength() ; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
                    _printer.printSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
                    name = attrs.getQName( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
                    if (name != null && name.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                        String prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
                        String attrURI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                        name = attrs.getLocalName( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
                        attrURI = attrs.getURI( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
                        if (( attrURI != null && attrURI.length() != 0 ) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
                            ( namespaceURI == null || namespaceURI.length() == 0 ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
                              ! attrURI.equals( namespaceURI ) )) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
                            prefix = getPrefix( attrURI );
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
                            if (prefix != null && prefix.length() > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
                                name = prefix + ":" + name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
                    value = attrs.getValue( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
                    if (value == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
                        value = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
                    _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
                    _printer.printText( "=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
                    printEscaped( value );
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
                    _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
                    // If the attribute xml:space exists, determine whether
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
                    // to preserve spaces in this and child nodes based on
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
                    // its value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
                    if (name.equals( "xml:space" )) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
                        if (value.equals( "preserve" ))
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
                            preserveSpace = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
                        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
                            preserveSpace = _format.getPreserveSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
            if (_prefixes != null) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   338
                Iterator entries = _prefixes.entrySet().iterator();
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   339
                while (entries.hasNext()) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
                    _printer.printSpace();
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   341
                    Map.Entry entry = (Map.Entry) entries.next();
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   342
                    value = (String) entry.getKey();
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   343
                    name = (String) entry.getValue();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
                    if (name.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
                        _printer.printText( "xmlns=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
                        printEscaped( value );
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
                        _printer.printText( '"' );
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   348
                    }
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   349
                    else {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
                        _printer.printText( "xmlns:" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
                        _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
                        _printer.printText( "=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
                        printEscaped( value );
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
                        _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
            // Now it's time to enter a new element state
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
            // with the tag name and space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
            // We still do not change the curent element state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
            state = enterElementState( namespaceURI, localName, rawName, preserveSpace );
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
            name = ( localName == null || localName.length() == 0 ) ? rawName : namespaceURI + "^" + localName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
            state.doCData = _format.isCDataElement( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
            state.unescaped = _format.isNonEscapingElement( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
        } catch (IOException except) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
    public void endElement( String namespaceURI, String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
                            String rawName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
    throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
            endElementIO( namespaceURI, localName, rawName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
        } catch (IOException except) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
    public void endElementIO( String namespaceURI, String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
                              String rawName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
    throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
        if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
            System.out.println("==>endElement: " +rawName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
        // Works much like content() with additions for closing
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
        // an element. Note the different checks for the closed
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
        // element's state and the parent element's state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
        _printer.unindent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
        if (state.empty) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
            _printer.printText( "/>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
            // Must leave CData section first
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
            if (state.inCData)
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
                _printer.printText( "]]>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
            // This element is not empty and that last content was
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
            // another element, so print a line break before that
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
            // last element and this element's closing tag.
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
            if (_indenting && ! state.preserveSpace && (state.afterElement || state.afterComment))
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
            _printer.printText( "</" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
            _printer.printText( state.rawName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
            _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
        // Leave the element state and update that of the parent
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
        // (if we're not root) to not empty and after element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
        state = leaveElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
        state.afterElement = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
        state.afterComment = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        state.empty = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
        if (isDocumentState())
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
            _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
    // SAX document handler serializing methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
    public void startElement( String tagName, AttributeList attrs )
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
    throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        int          i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
        boolean      preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
        String       name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
        String       value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
        if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
            System.out.println("==>startElement("+tagName+")");
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
            if (_printer == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "NoWriterSupplied", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
                throw new IllegalStateException(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
            state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
            if (isDocumentState()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
                // If this is the root element handle it differently.
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
                // If the first root element in the document, serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
                // the document's DOCTYPE. Space preserving defaults
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
                // to that of the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
                if (! _started)
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
                    startDocument( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
                // For any other element, if first in parent, then
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
                // close parent's opening tag and use the parnet's
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
                // space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
                if (state.empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
                    _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
                // Must leave CData section first
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
                if (state.inCData) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
                    _printer.printText( "]]>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
                    state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
                // Indent this element on a new line if the first
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
                // content of the parent element or immediately
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
                // following an element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
                if (_indenting && ! state.preserveSpace &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
                    ( state.empty || state.afterElement || state.afterComment))
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
                    _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
            preserveSpace = state.preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
            // Do not change the current element state yet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
            // This only happens in endElement().
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
            _printer.printText( '<' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
            _printer.printText( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
            _printer.indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
            // For each attribute print it's name and value as one part,
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
            // separated with a space so the element can be broken on
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
            // multiple lines.
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
            if (attrs != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
                for (i = 0 ; i < attrs.getLength() ; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
                    _printer.printSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
                    name = attrs.getName( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
                    value = attrs.getValue( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
                    if (value != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
                        _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
                        _printer.printText( "=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
                        printEscaped( value );
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
                        _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
                    // If the attribute xml:space exists, determine whether
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
                    // to preserve spaces in this and child nodes based on
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
                    // its value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
                    if (name.equals( "xml:space" )) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
                        if (value.equals( "preserve" ))
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
                            preserveSpace = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
                        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
                            preserveSpace = _format.getPreserveSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
            // Now it's time to enter a new element state
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
            // with the tag name and space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
            // We still do not change the curent element state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
            state = enterElementState( null, null, tagName, preserveSpace );
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
            state.doCData = _format.isCDataElement( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
            state.unescaped = _format.isNonEscapingElement( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
        } catch (IOException except) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
            throw new SAXException( except );
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
    public void endElement( String tagName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
    throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
        endElement( null, null, tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
    // Generic node serializing methods methods //
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
    //------------------------------------------//
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
     * Called to serialize the document's DOCTYPE by the root element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
     * The document type declaration must name the root element,
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
     * but the root element is only known when that element is serialized,
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
     * and not at the start of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
     * This method will check if it has not been called before ({@link #_started}),
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
     * will serialize the document type declaration, and will serialize all
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
     * pre-root comments and PIs that were accumulated in the document
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
     * (see {@link #serializePreRoot}). Pre-root will be serialized even if
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
     * this is not the first root element of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
    protected void startDocument( String rootTagName )
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
    throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
        int    i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
        String dtd;
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
        dtd = _printer.leaveDTD();
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
        if (! _started) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
            if (! _format.getOmitXMLDeclaration()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
                StringBuffer    buffer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
                // Serialize the document declaration appreaing at the head
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
                // of very XML document (unless asked not to).
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
                buffer = new StringBuffer( "<?xml version=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
                if (_format.getVersion() != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
                    buffer.append( _format.getVersion() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
                    buffer.append( "1.0" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
                buffer.append( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
                String format_encoding =  _format.getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
                if (format_encoding != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
                    buffer.append( " encoding=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
                    buffer.append( format_encoding );
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
                    buffer.append( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
                if (_format.getStandalone() && _docTypeSystemId == null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
                    _docTypePublicId == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
                    buffer.append( " standalone=\"yes\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
                buffer.append( "?>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
                _printer.printText( buffer );
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
            if (! _format.getOmitDocumentType()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
                if (_docTypeSystemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
                    // System identifier must be specified to print DOCTYPE.
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
                    // If public identifier is specified print 'PUBLIC
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
                    // <public> <system>', if not, print 'SYSTEM <system>'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
                    _printer.printText( "<!DOCTYPE " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
                    _printer.printText( rootTagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
                    if (_docTypePublicId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
                        _printer.printText( " PUBLIC " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
                        printDoctypeURL( _docTypePublicId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
                        if (_indenting) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
                            _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
                            for (i = 0 ; i < 18 + rootTagName.length() ; ++i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
                                _printer.printText( " " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
                        } else
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
                            _printer.printText( " " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
                        printDoctypeURL( _docTypeSystemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
                        _printer.printText( " SYSTEM " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
                        printDoctypeURL( _docTypeSystemId );
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
                    // If we accumulated any DTD contents while printing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
                    // this would be the place to print it.
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
                    if (dtd != null && dtd.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
                        _printer.printText( " [" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
                        printText( dtd, true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
                        _printer.printText( ']' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
                    _printer.printText( ">" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
                    _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
                } else if (dtd != null && dtd.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
                    _printer.printText( "<!DOCTYPE " );
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
                    _printer.printText( rootTagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
                    _printer.printText( " [" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
                    printText( dtd, true, true );
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
                    _printer.printText( "]>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
                    _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
        _started = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
        // Always serialize these, even if not te first root element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
        serializePreRoot();
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
     * Called to serialize a DOM element. Equivalent to calling {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
     * #startElement}, {@link #endElement} and serializing everything
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
     * inbetween, but better optimized.
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
    protected void serializeElement( Element elem )
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
    throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
        Attr         attr;
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
        NamedNodeMap attrMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
        int          i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
        Node         child;
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
        ElementState state;
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
        String       name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
        String       value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
        String       tagName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
        String prefix, localUri;
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
        String uri;
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
        if (fNamespaces) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
            // local binder stores namespace declaration
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
            // that has been printed out during namespace fixup of
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
            // the current element
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
            fLocalNSBinder.reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
            // add new namespace context
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
            fNSBinder.pushContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
        if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
            System.out.println("==>startElement: " +elem.getNodeName() +" ns="+elem.getNamespaceURI());
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
        tagName = elem.getTagName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
        state = getElementState();
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
        if (isDocumentState()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
            // If this is the root element handle it differently.
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
            // If the first root element in the document, serialize
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
            // the document's DOCTYPE. Space preserving defaults
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
            // to that of the output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
            if (! _started) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
                startDocument( tagName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
            // For any other element, if first in parent, then
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
            // close parent's opening tag and use the parent's
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
            // space preserving.
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
            if (state.empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
                _printer.printText( '>' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
            // Must leave CData section first
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
            if (state.inCData) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
                _printer.printText( "]]>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
                state.inCData = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
            // Indent this element on a new line if the first
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
            // content of the parent element or immediately
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
            // following an element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
            if (_indenting && ! state.preserveSpace &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
                ( state.empty || state.afterElement || state.afterComment))
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
                _printer.breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
        // Do not change the current element state yet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
        // This only happens in endElement().
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
        fPreserveSpace = state.preserveSpace;
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
        int length = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
        attrMap = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
        // retrieve attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
        if (elem.hasAttributes()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
            attrMap = elem.getAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
            length = attrMap.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
        if (!fNamespaces) { // no namespace fixup should be performed
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
            // serialize element name
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
            _printer.printText( '<' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
            _printer.printText( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
            _printer.indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
            // For each attribute print it's name and value as one part,
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
            // separated with a space so the element can be broken on
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
            // multiple lines.
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
            for ( i = 0 ; i < length ; ++i ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
                attr = (Attr) attrMap.item( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
                name = attr.getName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
                value = attr.getValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
                if ( value == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
                    value = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
                printAttribute (name, value, attr.getSpecified(), attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
        } else { // do namespace fixup
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
            // REVISIT: some optimization could probably be done to avoid traversing
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
            //          attributes twice.
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
            //
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
            // ---------------------------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
            // record all valid namespace declarations
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
            // before attempting to fix element's namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
            // ---------------------------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
            for (i = 0;i < length;i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
                attr = (Attr) attrMap.item( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
                uri = attr.getNamespaceURI();
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
                // check if attribute is a namespace decl
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
                if (uri != null && uri.equals(NamespaceContext.XMLNS_URI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
                    value = attr.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
                    if (value == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
                        value=XMLSymbols.EMPTY_STRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
                    if (value.equals(NamespaceContext.XMLNS_URI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
                        if (fDOMErrorHandler != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
                            String msg = DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
                                DOMMessageFormatter.XML_DOMAIN,"CantBindXMLNS",null );
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
                            modifyDOMError(msg,  DOMError.SEVERITY_ERROR, null, attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
                            boolean continueProcess = fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
                            if (!continueProcess) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
                                // stop the namespace fixup and validation
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
                                throw new RuntimeException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
                                    DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
                                    DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
                                    "SerializationStopped", null));
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
                        prefix = attr.getPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
                        prefix = (prefix == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
                                  prefix.length() == 0) ? XMLSymbols.EMPTY_STRING :fSymbolTable.addSymbol(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
                        String localpart = fSymbolTable.addSymbol( attr.getLocalName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
                        if (prefix == XMLSymbols.PREFIX_XMLNS) { //xmlns:prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
                            value = fSymbolTable.addSymbol(value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
                            // record valid decl
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
                            if (value.length() != 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
                                fNSBinder.declarePrefix(localpart, value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
                            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
                                // REVISIT: issue error on invalid declarations
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
                                //          xmlns:foo = ""
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
                            continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
                        }
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   775
                        // xmlns --- empty prefix is always bound ("" or some string)
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   776
                        value = fSymbolTable.addSymbol(value);
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   777
                        fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, value);
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   778
                        continue;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
                    }  // end-else: valid declaration
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
                } // end-if: namespace declaration
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
            }  // end-for
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
            //-----------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
            // get element uri/prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
            //-----------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
            uri = elem.getNamespaceURI();
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
            prefix = elem.getPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
            //----------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
            // output element name
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
            //----------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
            // REVISIT: this could be removed if we always convert empty string to null
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
            //          for the namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
            if ((uri !=null && prefix !=null ) && uri.length() == 0 && prefix.length()!=0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
                // uri is an empty string and element has some prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
                // the namespace alg later will fix up the namespace attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
                // remove element prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
                prefix = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
                _printer.printText( '<' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
                _printer.printText( elem.getLocalName() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
                _printer.indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
                _printer.printText( '<' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
                _printer.printText( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
                _printer.indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
            // ---------------------------------------------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
            // Fix up namespaces for element: per DOM L3
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
            // Need to consider the following cases:
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
            //
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
            // case 1: <foo:elem xmlns:ns1="myURI" xmlns="default"/>
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
            // Assume "foo", "ns1" are declared on the parent. We should not miss
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
            // redeclaration for both "ns1" and default namespace. To solve this
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
            // we add a local binder that stores declaration only for current element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
            // This way we avoid outputing duplicate declarations for the same element
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
            // as well as we are not omitting redeclarations.
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
            //
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
            // case 2: <elem xmlns="" xmlns="default"/>
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
            // We need to bind default namespace to empty string, to be able to
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
            // omit duplicate declarations for the same element
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
            //
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
            // case 3: <xsl:stylesheet xmlns:xsl="http://xsl">
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
            // We create another element body bound to the "http://xsl" namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
            // as well as namespace attribute rebounding xsl to another namespace.
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
            // <xsl:body xmlns:xsl="http://another">
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
            // Need to make sure that the new namespace decl value is changed to
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
            // "http://xsl"
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
            //
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
            // ---------------------------------------------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
            // check if prefix/namespace is correct for current element
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
            if (uri != null) {  // Element has a namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
                uri = fSymbolTable.addSymbol(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
                prefix = (prefix == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
                          prefix.length() == 0) ? XMLSymbols.EMPTY_STRING :fSymbolTable.addSymbol(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
                if (fNSBinder.getURI(prefix) == uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
                    // The xmlns:prefix=namespace or xmlns="default" was declared at parent.
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
                    // The binder always stores mapping of empty prefix to "".
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
                    // (NOTE: local binder does not store this kind of binding!)
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
                    // Thus the case where element was declared with uri="" (with or without a prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
                    // will be covered here.
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
                    // the prefix is either undeclared
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
                    // or
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
                    // conflict: the prefix is bound to another URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
                    if (fNamespacePrefixes) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
                        printNamespaceAttr(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
                    fLocalNSBinder.declarePrefix(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
                    fNSBinder.declarePrefix(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
            } else { // Element has no namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
                if (elem.getLocalName() == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
                    //  DOM Level 1 node!
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
                    if (fDOMErrorHandler != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
                        String msg = DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
                            DOMMessageFormatter.DOM_DOMAIN, "NullLocalElementName",
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
                            new Object[]{elem.getNodeName()});
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
                        modifyDOMError(msg,DOMError.SEVERITY_ERROR, null, elem);
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
                        boolean continueProcess = fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
                        // REVISIT: should we terminate upon request?
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
                        if (!continueProcess) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
                           throw new RuntimeException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
                               DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
                               DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
                               "SerializationStopped", null));
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
                } else { // uri=null and no colon (DOM L2 node)
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
                    uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
                    if (uri !=null && uri.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
                        // there is a default namespace decl that is bound to
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
                        // non-zero length uri, output xmlns=""
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
                        if (fNamespacePrefixes) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
                            printNamespaceAttr(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
                        fLocalNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
                        fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
            // -----------------------------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
            // Fix up namespaces for attributes: per DOM L3
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
            // check if prefix/namespace is correct the attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
            // -----------------------------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
            for (i = 0; i < length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
                attr = (Attr) attrMap.item( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
                value = attr.getValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
                name = attr.getNodeName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
                uri = attr.getNamespaceURI();
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
                // Fix attribute that was declared with a prefix and namespace=""
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
                if (uri !=null && uri.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
                    uri=null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
                    // we must remove prefix for this attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
                    name=attr.getLocalName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
                if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
                    System.out.println("==>process attribute: "+attr.getNodeName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
                // make sure that value is never null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
                if (value == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
                    value=XMLSymbols.EMPTY_STRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
                if (uri != null) {  // attribute has namespace !=null
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
                    prefix = attr.getPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
                    prefix = prefix == null ? XMLSymbols.EMPTY_STRING :fSymbolTable.addSymbol(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
                    String localpart = fSymbolTable.addSymbol( attr.getLocalName());
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
                    // print namespace declarations namespace declarations
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
                    // ---------------------------------------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
                    if (uri != null && uri.equals(NamespaceContext.XMLNS_URI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
                        // check if we need to output this declaration
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
                        prefix = attr.getPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
                        prefix = (prefix == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
                                  prefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
                        localpart = fSymbolTable.addSymbol( attr.getLocalName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
                        if (prefix == XMLSymbols.PREFIX_XMLNS) { //xmlns:prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
                            localUri = fLocalNSBinder.getURI(localpart);  // local prefix mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
                            value = fSymbolTable.addSymbol(value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
                            if (value.length() != 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
                                if (localUri == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
                                    // declaration was not printed while fixing element namespace binding
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
                                    // If the DOM Level 3 namespace-prefixes feature is set to false
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
                                    // do not print xmlns attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
                                    if (fNamespacePrefixes) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
                                        printNamespaceAttr(localpart, value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
                                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
                                    // case 4: <elem xmlns:xx="foo" xx:attr=""/>
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
                                    // where attribute is bound to "bar".
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
                                    // If the xmlns:xx is output here first, later we should not
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
                                    // redeclare "xx" prefix. Instead we would pick up different prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
                                    // for the attribute.
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
                                    // final: <elem xmlns:xx="foo" NS1:attr="" xmlns:NS1="bar"/>
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
                                    fLocalNSBinder.declarePrefix(localpart, value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
                                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
                            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
                                // REVISIT: issue error on invalid declarations
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
                                //          xmlns:foo = ""
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
                            continue;
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   960
                        }
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   961
                        // xmlns --- empty prefix is always bound ("" or some string)
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   962
                        uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING);
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   963
                        localUri= fLocalNSBinder.getURI(XMLSymbols.EMPTY_STRING);
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   964
                        value = fSymbolTable.addSymbol(value);
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   965
                        if (localUri == null ) {
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   966
                            // declaration was not printed while fixing element namespace binding
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   967
                            if (fNamespacePrefixes) {
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   968
                                printNamespaceAttr(XMLSymbols.EMPTY_STRING, value);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
                            }
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   970
                            // case 4 does not apply here since attributes can't use
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   971
                            // default namespace
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
                        }
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
   973
                        continue;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
                    uri = fSymbolTable.addSymbol(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
                    // find if for this prefix a URI was already declared
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
                    String declaredURI =  fNSBinder.getURI(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
                    if (prefix == XMLSymbols.EMPTY_STRING || declaredURI != uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
                        // attribute has no prefix (default namespace decl does not apply to attributes)
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
                        // OR
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
                        // attribute prefix is not declared
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
                        // OR
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
                        // conflict: attr URI does not match the prefix in scope
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
                        name  = attr.getNodeName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
                        // Find if any prefix for attributes namespace URI is available
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
                        // in the scope
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
                        String declaredPrefix = fNSBinder.getPrefix(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
                        if (declaredPrefix !=null && declaredPrefix !=XMLSymbols.EMPTY_STRING) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
                            // use the prefix that was found
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
                            prefix = declaredPrefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
                            name=prefix+":"+localpart;
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
                        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
                            if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
                                System.out.println("==> cound not find prefix for the attribute: " +prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
                            if (prefix != XMLSymbols.EMPTY_STRING && fLocalNSBinder.getURI(prefix) == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
                                // the current prefix is not null and it has no in scope declaration
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
                                // use this prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
                            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
                                // find a prefix following the pattern "NS" +index (starting at 1)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
                                // make sure this prefix is not declared in the current scope.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
                                int counter = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
                                prefix = fSymbolTable.addSymbol(PREFIX + counter++);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
                                while (fLocalNSBinder.getURI(prefix)!=null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
                                    prefix = fSymbolTable.addSymbol(PREFIX +counter++);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
                                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
                                name=prefix+":"+localpart;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
                            // add declaration for the new prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
                            if (fNamespacePrefixes) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
                                printNamespaceAttr(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
                            value = fSymbolTable.addSymbol(value);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
                            fLocalNSBinder.declarePrefix(prefix, value);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
                            fNSBinder.declarePrefix(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
                        // change prefix for this attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
                    printAttribute (name, (value==null)?XMLSymbols.EMPTY_STRING:value, attr.getSpecified(), attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
                } else { // attribute uri == null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
                    if (attr.getLocalName() == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
                        if (fDOMErrorHandler != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
                            String msg = DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
                                DOMMessageFormatter.DOM_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
                                "NullLocalAttrName", new Object[]{attr.getNodeName()});
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
                            modifyDOMError(msg, DOMError.SEVERITY_ERROR, null, attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
                            boolean continueProcess = fDOMErrorHandler.handleError(fDOMError);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
                            if (!continueProcess) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
                                // stop the namespace fixup and validation
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
                                throw new RuntimeException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
                                   DOMMessageFormatter.formatMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
                                   DOMMessageFormatter.SERIALIZER_DOMAIN,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
                                   "SerializationStopped", null));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
                        printAttribute (name, value, attr.getSpecified(), attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
                    } else { // uri=null and no colon
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
                        // no fix up is needed: default namespace decl does not
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
                        // apply to attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
                        printAttribute (name, value, attr.getSpecified(), attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
            } // end loop for attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
        }// end namespace fixup algorithm
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
        // If element has children, then serialize them, otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
        // serialize en empty tag.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
        if (elem.hasChildNodes()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
            // Enter an element state, and serialize the children
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
            // one by one. Finally, end the element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
            state = enterElementState( null, null, tagName, fPreserveSpace );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
            state.doCData = _format.isCDataElement( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
            state.unescaped = _format.isNonEscapingElement( tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
            child = elem.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
            while (child != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
                serializeNode( child );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
                child = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
            if (fNamespaces) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
                fNSBinder.popContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
            endElementIO( null, null, tagName );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
            if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
                System.out.println("==>endElement: " +elem.getNodeName());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
            if (fNamespaces) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
                fNSBinder.popContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
            _printer.unindent();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
            _printer.printText( "/>" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
            // After element but parent element is no longer empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
            state.afterElement = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
            state.afterComment = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
            state.empty = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
            if (isDocumentState())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
                _printer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
     * Serializes a namespace attribute with the given prefix and value for URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
     * In case prefix is empty will serialize default namespace declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
     * @param prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
     * @param uri
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
     * @exception IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
    private void printNamespaceAttr(String prefix, String uri) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
        _printer.printSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
        if (prefix == XMLSymbols.EMPTY_STRING) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
            if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
                System.out.println("=>add xmlns=\""+uri+"\" declaration");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
            _printer.printText( XMLSymbols.PREFIX_XMLNS );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
            if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
                System.out.println("=>add xmlns:"+prefix+"=\""+uri+"\" declaration");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
            _printer.printText( "xmlns:"+prefix );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
        _printer.printText( "=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
        printEscaped( uri );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
        _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
     * Prints attribute.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
     * NOTE: xml:space attribute modifies output format
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
     * @param name
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
     * @param value
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
     * @param isSpecified
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
     * @exception IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
    private void printAttribute (String name, String value, boolean isSpecified, Attr attr) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
        if (isSpecified || (features & DOMSerializerImpl.DISCARDDEFAULT) == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
            if (fDOMFilter !=null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
                (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ATTRIBUTE)!= 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
                short code = fDOMFilter.acceptNode(attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1139
                switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
                    case NodeFilter.FILTER_REJECT:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
                    case NodeFilter.FILTER_SKIP: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
                        return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
                    default: {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
                        // fall through
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
            _printer.printSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
            _printer.printText( name );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
            _printer.printText( "=\"" );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
            printEscaped( value );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
            _printer.printText( '"' );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
        // If the attribute xml:space exists, determine whether
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
        // to preserve spaces in this and child nodes based on
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
        // its value.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
        if (name.equals( "xml:space" )) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
            if (value.equals( "preserve" ))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
                fPreserveSpace = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
                fPreserveSpace = _format.getPreserveSpace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
    protected String getEntityRef( int ch ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
        // Encode special XML characters into the equivalent character references.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
        // These five are defined by default for all XML documents.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
        switch (ch) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
        case '<':
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
            return "lt";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
        case '>':
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
            return "gt";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
        case '"':
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
            return "quot";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
        case '\'':
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
            return "apos";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
        case '&':
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
            return "amp";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
    /** Retrieve and remove the namespaces declarations from the list of attributes.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
    private Attributes extractNamespaces( Attributes attrs )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
    throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
        AttributesImpl attrsOnly;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
        String         rawName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
        int            i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
        int            length;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
        if (attrs == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1199
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
        length = attrs.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
        attrsOnly = new AttributesImpl( attrs );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1202
7f561c08de6b Initial load
duke
parents:
diff changeset
  1203
        for (i = length - 1 ; i >= 0 ; --i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1204
            rawName = attrsOnly.getQName( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1205
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
            //We have to exclude the namespaces declarations from the attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
            //Append only when the feature http://xml.org/sax/features/namespace-prefixes"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
            //is TRUE
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
            if (rawName.startsWith( "xmlns" )) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
                if (rawName.length() == 5) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
                    startPrefixMapping( "", attrs.getValue( i ) );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1212
                    attrsOnly.removeAttribute( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
                } else if (rawName.charAt(5) == ':') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
                    startPrefixMapping(rawName.substring(6), attrs.getValue(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
                    attrsOnly.removeAttribute( i );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1216
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
        return attrsOnly;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
    // Printing attribute value
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1225
    protected void printEscaped(String source) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
        int length = source.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
        for (int i = 0; i < length; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
            int ch = source.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
            if (!XMLChar.isValid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1230
                if (++i < length) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1231
                    surrogates(ch, source.charAt(i), false);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
                } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
                    fatalError("The character '" + (char) ch + "' is an invalid XML character");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1235
                continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
            // escape NL, CR, TAB
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
            if (ch == '\n' || ch == '\r' || ch == '\t') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
                printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1240
            } else if (ch == '<') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
                _printer.printText("&lt;");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
            } else if (ch == '&') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
                _printer.printText("&amp;");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
            } else if (ch == '"') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1245
                _printer.printText("&quot;");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
            } else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
                _printer.printText((char) ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
                printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1250
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
7f561c08de6b Initial load
duke
parents:
diff changeset
  1254
    /** print text data */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
    protected void printXMLChar( int ch) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
        if (ch == '\r') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
                        printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
        } else if ( ch == '<') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
            _printer.printText("&lt;");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1260
        } else if (ch == '&') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1261
            _printer.printText("&amp;");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
        } else if (ch == '>'){
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
                // character sequence "]]>" can't appear in content, therefore
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
                // we should escape '>'
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
                        _printer.printText("&gt;");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
        } else if ( ch == '\n' ||  ch == '\t' ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
                    ( ch >= ' ' && _encodingInfo.isPrintable((char)ch))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
            _printer.printText((char)ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1270
                        printHex(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
    protected void printText( String text, boolean preserveSpace, boolean unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
    throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
        int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
        char ch;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
        int length = text.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
        if ( preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
            // Preserving spaces: the text must print exactly as it is,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
            // without breaking when spaces appear in the text and without
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
            // consolidating spaces. If a line terminator is used, a line
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
            // break will occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
            for ( index = 0 ; index < length ; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1285
                ch = text.charAt( index );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
                if (!XMLChar.isValid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
                    if (++index <length) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1289
                        surrogates(ch, text.charAt(index), true);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1290
                    } else {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1291
                        fatalError("The character '"+ch+"' is an invalid XML character");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1292
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1293
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1294
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1295
                if ( unescaped ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1296
                    _printer.printText( ch );
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1297
                } else {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1298
                    printXMLChar( ch );
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1299
                }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1300
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1301
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1302
            // Not preserving spaces: print one part at a time, and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1303
            // use spaces between parts to break them into different
7f561c08de6b Initial load
duke
parents:
diff changeset
  1304
            // lines. Spaces at beginning of line will be stripped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1305
            // by printing mechanism. Line terminator is treated
7f561c08de6b Initial load
duke
parents:
diff changeset
  1306
            // no different than other text part.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1307
            for ( index = 0 ; index < length ; ++index ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1308
                ch = text.charAt( index );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1309
                if (!XMLChar.isValid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1310
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
  1311
                    if (++index <length) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1312
                        surrogates(ch, text.charAt(index), true);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1313
                    } else {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1314
                        fatalError("The character '"+ch+"' is an invalid XML character");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1315
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1316
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1317
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1318
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1319
                if ( unescaped ) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1320
                    _printer.printText( ch );
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1321
                } else {
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1322
                    printXMLChar( ch );
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1323
                }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1324
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1325
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1326
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1327
7f561c08de6b Initial load
duke
parents:
diff changeset
  1328
7f561c08de6b Initial load
duke
parents:
diff changeset
  1329
7f561c08de6b Initial load
duke
parents:
diff changeset
  1330
    protected void printText( char[] chars, int start, int length,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1331
                              boolean preserveSpace, boolean unescaped ) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1332
7f561c08de6b Initial load
duke
parents:
diff changeset
  1333
        if ( preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1334
            // Preserving spaces: the text must print exactly as it is,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1335
            // without breaking when spaces appear in the text and without
7f561c08de6b Initial load
duke
parents:
diff changeset
  1336
            // consolidating spaces. If a line terminator is used, a line
7f561c08de6b Initial load
duke
parents:
diff changeset
  1337
            // break will occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1338
            while ( length-- > 0 ) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1339
                char ch = chars[start++];
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1340
                if (!XMLChar.isValid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1341
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
  1342
                    if ( length-- > 0 ) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1343
                        surrogates(ch, chars[start++], true);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1344
                    } else {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1345
                        fatalError("The character '"+ch+"' is an invalid XML character");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1346
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1347
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1348
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1349
                if ( unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1350
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1351
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1352
                    printXMLChar( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1353
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1354
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1355
            // Not preserving spaces: print one part at a time, and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1356
            // use spaces between parts to break them into different
7f561c08de6b Initial load
duke
parents:
diff changeset
  1357
            // lines. Spaces at beginning of line will be stripped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1358
            // by printing mechanism. Line terminator is treated
7f561c08de6b Initial load
duke
parents:
diff changeset
  1359
            // no different than other text part.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1360
            while ( length-- > 0 ) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1361
                char ch = chars[start++];
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1362
                if (!XMLChar.isValid(ch)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1363
                    // check if it is surrogate
7f561c08de6b Initial load
duke
parents:
diff changeset
  1364
                    if ( length-- > 0 ) {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1365
                        surrogates(ch, chars[start++], true);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1366
                    } else {
23777
ce87cedb71cf 8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents: 12457
diff changeset
  1367
                        fatalError("The character '"+ch+"' is an invalid XML character");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1368
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1369
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1370
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1371
                if ( unescaped )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1372
                    _printer.printText( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1373
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1374
                    printXMLChar( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
  1375
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1376
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1377
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1378
7f561c08de6b Initial load
duke
parents:
diff changeset
  1379
7f561c08de6b Initial load
duke
parents:
diff changeset
  1380
   /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1381
    * DOM Level 3:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1382
    * Check a node to determine if it contains unbound namespace prefixes.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1383
    *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1384
    * @param node The node to check for unbound namespace prefices
7f561c08de6b Initial load
duke
parents:
diff changeset
  1385
    */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1386
        protected void checkUnboundNamespacePrefixedNode (Node node) throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
  1387
7f561c08de6b Initial load
duke
parents:
diff changeset
  1388
                if (fNamespaces) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1389
7f561c08de6b Initial load
duke
parents:
diff changeset
  1390
                        if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1391
                            System.out.println("==>serializeNode("+node.getNodeName()+") [Entity Reference - Namespaces on]");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1392
                                System.out.println("==>Declared Prefix Count: " + fNSBinder.getDeclaredPrefixCount());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1393
                                System.out.println("==>Node Name: " + node.getNodeName());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1394
                                System.out.println("==>First Child Node Name: " + node.getFirstChild().getNodeName());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1395
                                System.out.println("==>First Child Node Prefix: " + node.getFirstChild().getPrefix());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1396
                                System.out.println("==>First Child Node NamespaceURI: " + node.getFirstChild().getNamespaceURI());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1397
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1398
7f561c08de6b Initial load
duke
parents:
diff changeset
  1399
7f561c08de6b Initial load
duke
parents:
diff changeset
  1400
                        Node child, next;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1401
                for (child = node.getFirstChild(); child != null; child = next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1402
                    next = child.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1403
                            if (DEBUG) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1404
                                System.out.println("==>serializeNode("+child.getNodeName()+") [Child Node]");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1405
                                System.out.println("==>serializeNode("+child.getPrefix()+") [Child Node Prefix]");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1406
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1407
7f561c08de6b Initial load
duke
parents:
diff changeset
  1408
                            //If a NamespaceURI is not declared for the current
7f561c08de6b Initial load
duke
parents:
diff changeset
  1409
                            //node's prefix, raise a fatal error.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1410
                            String prefix = child.getPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1411
                prefix = (prefix == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1412
                        prefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1413
                            if (fNSBinder.getURI(prefix) == null && prefix != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1414
                                        fatalError("The replacement text of the entity node '"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1415
                                                                + node.getNodeName()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1416
                                                                + "' contains an element node '"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1417
                                                                + child.getNodeName()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1418
                                                                + "' with an undeclared prefix '"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1419
                                                                + prefix + "'.");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1420
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1421
7f561c08de6b Initial load
duke
parents:
diff changeset
  1422
                                if (child.getNodeType() == Node.ELEMENT_NODE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1423
7f561c08de6b Initial load
duke
parents:
diff changeset
  1424
                                        NamedNodeMap attrs = child.getAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1425
7f561c08de6b Initial load
duke
parents:
diff changeset
  1426
                                        for (int i = 0; i< attrs.getLength(); i++ ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1427
7f561c08de6b Initial load
duke
parents:
diff changeset
  1428
                                            String attrPrefix = attrs.item(i).getPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1429
                        attrPrefix = (attrPrefix == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1430
                                attrPrefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(attrPrefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1431
                                            if (fNSBinder.getURI(attrPrefix) == null && attrPrefix != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1432
                                                        fatalError("The replacement text of the entity node '"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1433
                                                                                + node.getNodeName()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1434
                                                                                + "' contains an element node '"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1435
                                                                                + child.getNodeName()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1436
                                                                                + "' with an attribute '"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1437
                                                                                + attrs.item(i).getNodeName()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1438
                                                                                + "' an undeclared prefix '"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1439
                                                                                + attrPrefix + "'.");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1440
                                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1441
7f561c08de6b Initial load
duke
parents:
diff changeset
  1442
                                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1443
7f561c08de6b Initial load
duke
parents:
diff changeset
  1444
                                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1445
7f561c08de6b Initial load
duke
parents:
diff changeset
  1446
                                if (child.hasChildNodes()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1447
                                        checkUnboundNamespacePrefixedNode(child);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1448
                                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1449
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1450
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1451
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1452
7f561c08de6b Initial load
duke
parents:
diff changeset
  1453
    public boolean reset() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1454
        super.reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1455
        if (fNSBinder != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
  1456
            fNSBinder.reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1457
            // during serialization always have a mapping to empty string
7f561c08de6b Initial load
duke
parents:
diff changeset
  1458
            // so we assume there is a declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1459
            fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1460
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1461
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1462
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1463
7f561c08de6b Initial load
duke
parents:
diff changeset
  1464
}