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