jaxp/src/com/sun/org/apache/xalan/internal/xsltc/trax/DOM2SAX.java
author joehw
Tue, 17 Apr 2012 11:17:59 -0700
changeset 12458 d601e4bba306
parent 12457 c348e06f0e82
permissions -rw-r--r--
7160380: Sync JDK8 with JAXP 1.4.5 Summary: bring JDK8 up to date to what we have in 7u4 Reviewed-by: lancea, mullan
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 2001-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: DOM2SAX.java,v 1.2.4.1 2005/09/06 11:52:46 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
package com.sun.org.apache.xalan.internal.xsltc.trax;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.util.Stack;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import org.w3c.dom.NamedNodeMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import org.xml.sax.ContentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import org.xml.sax.DTDHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import org.xml.sax.EntityResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import org.xml.sax.ErrorHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import org.xml.sax.Locator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import org.xml.sax.SAXNotRecognizedException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import org.xml.sax.SAXNotSupportedException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import org.xml.sax.ext.LexicalHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import org.xml.sax.helpers.AttributesImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * @author G. Todd Miller
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
public class DOM2SAX implements XMLReader, Locator {
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    private final static String EMPTYSTRING = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    private static final String XMLNS_PREFIX = "xmlns";
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    private Node _dom = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    private ContentHandler _sax = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    private LexicalHandler _lex = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    private SAXImpl _saxImpl = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    private Hashtable _nsPrefixes = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    public DOM2SAX(Node root) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
        _dom = root;
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    public ContentHandler getContentHandler() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
        return _sax;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    public void setContentHandler(ContentHandler handler) throws
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
        NullPointerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        _sax = handler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        if (handler instanceof LexicalHandler) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
            _lex = (LexicalHandler) handler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        if (handler instanceof SAXImpl) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
            _saxImpl = (SAXImpl)handler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     * Begin the scope of namespace prefix. Forward the event to the
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     * SAX handler only if the prefix is unknown or it is mapped to a
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
     * different URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    private boolean startPrefixMapping(String prefix, String uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        boolean pushed = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        Stack uriStack = (Stack) _nsPrefixes.get(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        if (uriStack != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            if (uriStack.isEmpty()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
                _sax.startPrefixMapping(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
                uriStack.push(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
                final String lastUri = (String) uriStack.peek();
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
                if (!lastUri.equals(uri)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                    _sax.startPrefixMapping(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                    uriStack.push(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
                    pushed = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            _sax.startPrefixMapping(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
            _nsPrefixes.put(prefix, uriStack = new Stack());
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
            uriStack.push(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        return pushed;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * End the scope of a name prefix by popping it from the stack and
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     * passing the event to the SAX Handler.
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    private void endPrefixMapping(String prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        final Stack uriStack = (Stack) _nsPrefixes.get(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        if (uriStack != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
            _sax.endPrefixMapping(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            uriStack.pop();
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     * If the DOM was created using a DOM 1.0 API, the local name may be
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     * null. If so, get the local name from the qualified name before
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     * generating the SAX event.
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    private static String getLocalName(Node node) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        final String localName = node.getLocalName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        if (localName == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            final String qname = node.getNodeName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
            final int col = qname.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
            return (col > 0) ? qname.substring(col + 1) : qname;
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        return localName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    public void parse(InputSource unused) throws IOException, SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        parse(_dom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    public void parse() throws IOException, SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        if (_dom != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
            boolean isIncomplete =
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
                (_dom.getNodeType() != org.w3c.dom.Node.DOCUMENT_NODE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
            if (isIncomplete) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
                _sax.startDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
                parse(_dom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
                _sax.endDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
                parse(_dom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     * Traverse the DOM and generate SAX events for a handler. A
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     * startElement() event passes all attributes, including namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * declarations.
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    private void parse(Node node) throws IOException, SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        Node first = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        if (node == null) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        switch (node.getNodeType()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        case Node.ATTRIBUTE_NODE:         // handled by ELEMENT_NODE
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        case Node.DOCUMENT_FRAGMENT_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        case Node.DOCUMENT_TYPE_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        case Node.ENTITY_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        case Node.ENTITY_REFERENCE_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        case Node.NOTATION_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
            // These node types are ignored!!!
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
        case Node.CDATA_SECTION_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
            final String cdata = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
            if (_lex != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
                _lex.startCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
                _sax.characters(cdata.toCharArray(), 0, cdata.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
                _lex.endCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
                // in the case where there is no lex handler, we still
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
                // want the text of the cdate to make its way through.
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
                _sax.characters(cdata.toCharArray(), 0, cdata.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
        case Node.COMMENT_NODE:           // should be handled!!!
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
            if (_lex != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
                final String value = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
                _lex.comment(value.toCharArray(), 0, value.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
        case Node.DOCUMENT_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
            _sax.setDocumentLocator(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
            _sax.startDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
            Node next = node.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
            while (next != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
                parse(next);
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
                next = next.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
            _sax.endDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
        case Node.ELEMENT_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
            String prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
            Vector pushedPrefixes = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
            final AttributesImpl attrs = new AttributesImpl();
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
            final NamedNodeMap map = node.getAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
            final int length = map.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
            // Process all namespace declarations
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
            for (int i = 0; i < length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
                final Node attr = map.item(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
                final String qnameAttr = attr.getNodeName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
                // Ignore everything but NS declarations here
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
                if (qnameAttr.startsWith(XMLNS_PREFIX)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
                    final String uriAttr = attr.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
                    final int colon = qnameAttr.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
                    prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
                    if (startPrefixMapping(prefix, uriAttr)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
                        pushedPrefixes.addElement(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
            // Process all other attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
            for (int i = 0; i < length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
                final Node attr = map.item(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
                String qnameAttr = attr.getNodeName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
                // Ignore NS declarations here
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
                if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                    final String uriAttr = attr.getNamespaceURI();
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                    final String localNameAttr = getLocalName(attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
                    // Uri may be implicitly declared
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
                    if (uriAttr != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
                        final int colon = qnameAttr.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
                        if (colon > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
                            prefix = qnameAttr.substring(0, colon);
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
                        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
                            // If no prefix for this attr, we need to create
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
                            // one because we cannot use the default ns
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
                            prefix = BasisLibrary.generatePrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
                            qnameAttr = prefix + ':' + qnameAttr;
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
                        if (startPrefixMapping(prefix, uriAttr)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
                            pushedPrefixes.addElement(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
                    // Add attribute to list
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
                    attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr),
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
                        qnameAttr, "CDATA", attr.getNodeValue());
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
            // Now process the element itself
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
            final String qname = node.getNodeName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
            final String uri = node.getNamespaceURI();
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
            final String localName = getLocalName(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
            // Uri may be implicitly declared
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
            if (uri != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
                final int colon = qname.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
                prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
                if (startPrefixMapping(prefix, uri)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
                    pushedPrefixes.addElement(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
            // Generate SAX event to start element
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
            if (_saxImpl != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
                _saxImpl.startElement(uri, localName, qname, attrs, node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                _sax.startElement(uri, localName, qname, attrs);
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
            // Traverse all child nodes of the element (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
            next = node.getFirstChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
            while (next != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
                parse(next);
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                next = next.getNextSibling();
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
            // Generate SAX event to close element
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
            _sax.endElement(uri, localName, qname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
            // Generate endPrefixMapping() for all pushed prefixes
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
            final int nPushedPrefixes = pushedPrefixes.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
            for (int i = 0; i < nPushedPrefixes; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
                endPrefixMapping((String) pushedPrefixes.elementAt(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
        case Node.PROCESSING_INSTRUCTION_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
            _sax.processingInstruction(node.getNodeName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
                                       node.getNodeValue());
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
        case Node.TEXT_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
            final String data = node.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
            _sax.characters(data.toCharArray(), 0, data.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
    public DTDHandler getDTDHandler() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
    public ErrorHandler getErrorHandler() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
    public boolean getFeature(String name) throws SAXNotRecognizedException,
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
        SAXNotSupportedException
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
    public void setFeature(String name, boolean value) throws
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
        SAXNotRecognizedException, SAXNotSupportedException
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
    public void parse(String sysId) throws IOException, SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
        throw new IOException("This method is not yet implemented.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
    public void setDTDHandler(DTDHandler handler) throws NullPointerException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
    public void setEntityResolver(EntityResolver resolver) throws
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
        NullPointerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
    public EntityResolver getEntityResolver() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
    public void setErrorHandler(ErrorHandler handler) throws
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
        NullPointerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
    public void setProperty(String name, Object value) throws
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        SAXNotRecognizedException, SAXNotSupportedException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
    public Object getProperty(String name) throws SAXNotRecognizedException,
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        SAXNotSupportedException
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
        return null;
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
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
    public int getColumnNumber() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
        return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
    public int getLineNumber() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
        return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
    public String getPublicId() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
     * This class is only used internally so this method should never
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
     * be called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
    public String getSystemId() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
    // Debugging
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
    private String getNodeTypeFromCode(short code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
        String retval = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
        switch (code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
        case Node.ATTRIBUTE_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
            retval = "ATTRIBUTE_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
        case Node.CDATA_SECTION_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
            retval = "CDATA_SECTION_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
        case Node.COMMENT_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
            retval = "COMMENT_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
        case Node.DOCUMENT_FRAGMENT_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
            retval = "DOCUMENT_FRAGMENT_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
        case Node.DOCUMENT_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
            retval = "DOCUMENT_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
        case Node.DOCUMENT_TYPE_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
            retval = "DOCUMENT_TYPE_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
        case Node.ELEMENT_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
            retval = "ELEMENT_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
        case Node.ENTITY_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
            retval = "ENTITY_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
        case Node.ENTITY_REFERENCE_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
            retval = "ENTITY_REFERENCE_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
        case Node.NOTATION_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
            retval = "NOTATION_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
        case Node.PROCESSING_INSTRUCTION_NODE :
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
            retval = "PROCESSING_INSTRUCTION_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
        case Node.TEXT_NODE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
            retval = "TEXT_NODE"; break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
        return retval;
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
}