src/java.xml/share/classes/com/sun/org/apache/xerces/internal/util/NamespaceSupport.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    18
 * See the License for the specific language governing permissions and
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    19
 * limitations under the License.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xerces.internal.util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    24
import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    25
import java.util.ArrayList;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.util.Enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.util.Iterator;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    28
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.util.NoSuchElementException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * Namespace support for XML document handlers. This class doesn't
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * perform any error checking and assumes that all strings passed
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * as arguments to methods are unique symbols. The SymbolTable class
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * can be used for this purpose.
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * @author Andy Clark, IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
public class NamespaceSupport implements NamespaceContext {
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
    // Data
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
     * Namespace binding information. This array is composed of a
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     * series of tuples containing the namespace binding information:
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     * <prefix, uri>. The default size can be set to anything
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     * as long as it is a power of 2 greater than 1.
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
     * @see #fNamespaceSize
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
     * @see #fContext
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    protected String[] fNamespace = new String[16 * 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    /** The top of the namespace information array. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    protected int fNamespaceSize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    // NOTE: The constructor depends on the initial context size
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    //       being at least 1. -Ac
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
     * Context indexes. This array contains indexes into the namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
     * information array. The index at the current context is the start
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     * index of declared namespace bindings and runs to the size of the
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
     * namespace information array.
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
     * @see #fNamespaceSize
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    protected int[] fContext = new int[8];
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    /** The current context. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    protected int fCurrentContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    protected String[] fPrefixes = new String[16];
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    // Constructors
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    /** Default constructor. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    public NamespaceSupport() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    } // <init>()
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     * Constructs a namespace context object and initializes it with
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
     * the prefixes declared in the specified context.
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    public NamespaceSupport(NamespaceContext context) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        pushContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        // copy declaration in the context
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    93
        Enumeration<String> prefixes = context.getAllPrefixes();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        while (prefixes.hasMoreElements()){
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    95
            String prefix = prefixes.nextElement();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
            String uri = context.getURI(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            declarePrefix(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    } // <init>(NamespaceContext)
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    // Public methods
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
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#reset()
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    public void reset() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        // reset namespace and context info
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        fNamespaceSize = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        fCurrentContext = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        // bind "xml" prefix to the XML uri
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        // bind "xmlns" prefix to the XMLNS uri
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        fContext[fCurrentContext] = fNamespaceSize;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        //++fCurrentContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    } // reset(SymbolTable)
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#pushContext()
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    public void pushContext() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        // extend the array, if necessary
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        if (fCurrentContext + 1 == fContext.length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            int[] contextarray = new int[fContext.length * 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
            System.arraycopy(fContext, 0, contextarray, 0, fContext.length);
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
            fContext = contextarray;
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        // push context
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        fContext[++fCurrentContext] = fNamespaceSize;
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        //System.out.println("calling push context, current context = " + fCurrentContext);
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    } // pushContext()
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#popContext()
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    public void popContext() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        fNamespaceSize = fContext[fCurrentContext--];
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        //System.out.println("Calling popContext, fCurrentContext = " + fCurrentContext);
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    } // popContext()
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#declarePrefix(String, String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    public boolean declarePrefix(String prefix, String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        // ignore "xml" and "xmlns" prefixes
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        if (prefix == XMLSymbols.PREFIX_XML || prefix == XMLSymbols.PREFIX_XMLNS) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        // see if prefix already exists in current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
        for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
            if (fNamespace[i - 2] == prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
                // REVISIT: [Q] Should the new binding override the
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
                //          previously declared binding or should it
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
                //          it be ignored? -Ac
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
                // NOTE:    The SAX2 "NamespaceSupport" helper allows
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
                //          re-bindings with the new binding overwriting
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                //          the previous binding. -Ac
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                fNamespace[i - 1] = uri;
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
                return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        // resize array, if needed
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        if (fNamespaceSize == fNamespace.length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
            String[] namespacearray = new String[fNamespaceSize * 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
            System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize);
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
            fNamespace = namespacearray;
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        // bind prefix to uri in current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        fNamespace[fNamespaceSize++] = prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
        fNamespace[fNamespaceSize++] = uri;
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    } // declarePrefix(String,String):boolean
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getURI(String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    public String getURI(String prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        // find prefix in current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
        for (int i = fNamespaceSize; i > 0; i -= 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
            if (fNamespace[i - 2] == prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                return fNamespace[i - 1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
        // prefix not found
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    } // getURI(String):String
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
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getPrefix(String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    public String getPrefix(String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        // find uri in current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        for (int i = fNamespaceSize; i > 0; i -= 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
            if (fNamespace[i - 1] == uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
                if (getURI(fNamespace[i - 2]) == uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
                    return fNamespace[i - 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
        // uri not found
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    } // getPrefix(String):String
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getDeclaredPrefixCount()
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
    public int getDeclaredPrefixCount() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
        return (fNamespaceSize - fContext[fCurrentContext]) / 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
    } // getDeclaredPrefixCount():int
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getDeclaredPrefixAt(int)
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    public String getDeclaredPrefixAt(int index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        return fNamespace[fContext[fCurrentContext] + index * 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    } // getDeclaredPrefixAt(int):String
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   243
    public Iterator<String> getPrefixes(){
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
        int count = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        if (fPrefixes.length < (fNamespace.length/2)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
            // resize prefix array
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
            String[] prefixes = new String[fNamespaceSize];
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
            fPrefixes = prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
        String prefix = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
        boolean unique = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
        for (int i = 2; i < (fNamespaceSize-2); i += 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
            prefix = fNamespace[i + 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
            for (int k=0;k<count;k++){
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
                if (fPrefixes[k]==prefix){
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
                    unique = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
                    break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
            if (unique){
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
                fPrefixes[count++] = prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
            unique = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
        return new IteratorPrefixes(fPrefixes, count);
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
    }//getPrefixes
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getAllPrefixes()
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   270
    public Enumeration<String> getAllPrefixes() {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
        int count = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        if (fPrefixes.length < (fNamespace.length/2)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
            // resize prefix array
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
            String[] prefixes = new String[fNamespaceSize];
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
            fPrefixes = prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
        String prefix = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
        boolean unique = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
        for (int i = 2; i < (fNamespaceSize-2); i += 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
            prefix = fNamespace[i + 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
            for (int k=0;k<count;k++){
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
                if (fPrefixes[k]==prefix){
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
                    unique = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
                    break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
            if (unique){
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
                fPrefixes[count++] = prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
            unique = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
        return new Prefixes(fPrefixes, count);
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   295
    public List<String> getPrefixes(String uri){
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        int count = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
        String prefix = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
        boolean unique = true;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   299
        List<String> prefixList = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
        for (int i = fNamespaceSize; i >0 ; i -= 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
            if(fNamespace[i-1] == uri){
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
                if(!prefixList.contains(fNamespace[i-2]))
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                    prefixList.add(fNamespace[i-2]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
        return prefixList;
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
    /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
     * non-NamespaceContext methods
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
     * Checks whether a binding or unbinding for
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
     * the given prefix exists in the context.
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
     * @param prefix The prefix to look up.
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
     * @return true if the given prefix exists in the context
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
    public boolean containsPrefix(String prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
        // find prefix in context
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
        for (int i = fNamespaceSize; i > 0; i -= 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
            if (fNamespace[i - 2] == prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
                return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
        // prefix not found
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
        return false;
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
     * Checks whether a binding or unbinding for
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
     * the given prefix exists in the current context.
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
     * @param prefix The prefix to look up.
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
     * @return true if the given prefix exists in the current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
    public boolean containsPrefixInCurrentContext(String prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
        // find prefix in current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
        for (int i = fContext[fCurrentContext]; i < fNamespaceSize; i += 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
            if (fNamespace[i] == prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
                return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
        // prefix not found
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   355
    protected final class IteratorPrefixes implements Iterator<String>  {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
        private String[] prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
        private int counter = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
        private int size = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
         * Constructor for Prefixes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
        public IteratorPrefixes(String [] prefixes, int size) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
            this.prefixes = prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
            this.size = size;
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
         * @see java.util.Enumeration#hasMoreElements()
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
        public boolean hasNext() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
            return (counter < size);
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
         * @see java.util.Enumeration#nextElement()
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
         */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   378
        public String next() {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
            if (counter< size){
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
                return fPrefixes[counter++];
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
            throw new NoSuchElementException("Illegal access to Namespace prefixes enumeration.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        public String toString(){
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   386
            StringBuilder buf = new StringBuilder();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
            for (int i=0;i<size;i++){
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
                buf.append(prefixes[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
                buf.append(" ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
            return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
        public void remove(){
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
            throw new UnsupportedOperationException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   401
    protected final class Prefixes implements Enumeration<String> {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
        private String[] prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
        private int counter = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
        private int size = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
         * Constructor for Prefixes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        public Prefixes(String [] prefixes, int size) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
            this.prefixes = prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
            this.size = size;
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
         * @see java.util.Enumeration#hasMoreElements()
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        public boolean hasMoreElements() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
            return (counter< size);
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
         * @see java.util.Enumeration#nextElement()
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
         */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   424
        public String nextElement() {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
            if (counter< size){
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
                return fPrefixes[counter++];
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
            throw new NoSuchElementException("Illegal access to Namespace prefixes enumeration.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        public String toString(){
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   432
            StringBuilder buf = new StringBuilder();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
            for (int i=0;i<size;i++){
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
                buf.append(prefixes[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
                buf.append(" ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
            return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
} // class NamespaceSupport