src/java.xml/share/classes/com/sun/org/apache/xerces/internal/xinclude/MultipleScopeNamespaceSupport.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
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xerces.internal.xinclude;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.xerces.internal.util.XMLSymbols;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    27
import java.util.Enumeration;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * This implementation of NamespaceContext has the ability to maintain multiple
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * scopes of namespace/prefix bindings.  This is useful in situations when it is
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * not always appropriate for elements to inherit the namespace bindings of their
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * ancestors (such as included elements in XInclude).
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * When searching for a URI to match a prefix, or a prefix to match a URI, it is
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * searched for in the current context, then the ancestors of the current context,
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * up to the beginning of the current scope.  Other scopes are not searched.
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * @author Peter McCracken, IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
public class MultipleScopeNamespaceSupport extends NamespaceSupport {
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    protected int[] fScope = new int[8];
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    protected int fCurrentScope;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    public MultipleScopeNamespaceSupport() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
        super();
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
        fCurrentScope = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
        fScope[0] = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
     * @param context
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    public MultipleScopeNamespaceSupport(NamespaceContext context) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        super(context);
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        fCurrentScope = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
        fScope[0] = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    /* (non-Javadoc)
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getAllPrefixes()
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    68
    public Enumeration<String> getAllPrefixes() {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
        int count = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        if (fPrefixes.length < (fNamespace.length / 2)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
            // resize prefix array
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
            String[] prefixes = new String[fNamespaceSize];
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
            fPrefixes = prefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        String prefix = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        boolean unique = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        for (int i = fContext[fScope[fCurrentScope]];
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
            i <= (fNamespaceSize - 2);
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
            i += 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
            prefix = fNamespace[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
            for (int k = 0; k < count; k++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
                if (fPrefixes[k] == prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
                    unique = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
                    break;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
            if (unique) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
                fPrefixes[count++] = prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            unique = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        return new Prefixes(fPrefixes, count);
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    public int getScopeForContext(int context) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        int scope = fCurrentScope;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        while (context < fScope[scope]) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
            scope--;
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        return scope;
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    /* (non-Javadoc)
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getPrefix(java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    public String getPrefix(String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        return getPrefix(uri, fNamespaceSize, fContext[fScope[fCurrentScope]]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    /* (non-Javadoc)
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getURI(java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    public String getURI(String prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        return getURI(prefix, fNamespaceSize, fContext[fScope[fCurrentScope]]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    public String getPrefix(String uri, int context) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        return getPrefix(uri, fContext[context+1], fContext[fScope[getScopeForContext(context)]]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    public String getURI(String prefix, int context) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        return getURI(prefix, fContext[context+1], fContext[fScope[getScopeForContext(context)]]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    public String getPrefix(String uri, int start, int end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        // this saves us from having a copy of each of these in fNamespace for each scope
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        if (uri == NamespaceContext.XML_URI) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
            return XMLSymbols.PREFIX_XML;
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        if (uri == NamespaceContext.XMLNS_URI) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            return XMLSymbols.PREFIX_XMLNS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        // find uri in current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        for (int i = start; i > end; i -= 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            if (fNamespace[i - 1] == uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
                if (getURI(fNamespace[i - 2]) == uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
                    return fNamespace[i - 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        // uri not found
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    public String getURI(String prefix, int start, int end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
        // this saves us from having a copy of each of these in fNamespace for each scope
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        if (prefix == XMLSymbols.PREFIX_XML) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
            return NamespaceContext.XML_URI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        if (prefix == XMLSymbols.PREFIX_XMLNS) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
            return NamespaceContext.XMLNS_URI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        // find prefix in current context
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        for (int i = start; i > end; i -= 2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
            if (fNamespace[i - 2] == prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
                return fNamespace[i - 1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        // prefix not found
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     * Only resets the current scope -- all namespaces defined in lower scopes
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     * remain valid after a call to reset.
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    public void reset() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
        fCurrentContext = fScope[fCurrentScope];
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
        fNamespaceSize = fContext[fCurrentContext];
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
     * Begins a new scope.  None of the previous namespace bindings will be used,
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     * until the new scope is popped with popScope()
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    public void pushScope() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        if (fCurrentScope + 1 == fScope.length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
            int[] contextarray = new int[fScope.length * 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
            System.arraycopy(fScope, 0, contextarray, 0, fScope.length);
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
            fScope = contextarray;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        pushContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        fScope[++fCurrentScope] = fCurrentContext;
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
     * Pops the current scope.  The namespace bindings from the new current scope
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
     * are then used for searching for namespaces and prefixes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    public void popScope() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
        fCurrentContext = fScope[fCurrentScope--];
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
        popContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
}