src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/util/NamespaceContextImplementation.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  *
    25  * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    26  */
    28 package com.sun.xml.internal.fastinfoset.util;
    30 import java.util.ArrayList;
    31 import java.util.Iterator;
    32 import java.util.List;
    33 import javax.xml.namespace.NamespaceContext;
    35 /**
    36  *
    37  * @author Paul.Sandoz@Sun.Com
    38  */
    39 final public class NamespaceContextImplementation implements NamespaceContext {
    40     private static int DEFAULT_SIZE = 8;
    42     private String[] prefixes = new String[DEFAULT_SIZE];
    43     private String[] namespaceURIs = new String[DEFAULT_SIZE];
    44     private int namespacePosition;
    46     private int[] contexts = new int[DEFAULT_SIZE];
    47     private int contextPosition;
    49     private int currentContext;
    51     public NamespaceContextImplementation() {
    52         prefixes[0] = "xml";
    53         namespaceURIs[0] = "http://www.w3.org/XML/1998/namespace";
    54         prefixes[1] = "xmlns";
    55         namespaceURIs[1] = "http://www.w3.org/2000/xmlns/";
    57         currentContext = namespacePosition = 2;
    58     }
    61     public String getNamespaceURI(String prefix) {
    62         if (prefix == null) throw new IllegalArgumentException();
    64 //        prefix = prefix.intern();
    66         for (int i = namespacePosition - 1; i >= 0; i--) {
    67             final String declaredPrefix = prefixes[i];
    68             if (declaredPrefix.equals(prefix)) {
    69                 return namespaceURIs[i];
    70             }
    71         }
    73         return "";
    74     }
    76     public String getPrefix(String namespaceURI) {
    77         if (namespaceURI == null) throw new IllegalArgumentException();
    79         // namespaceURI = namespaceURI.intern();
    81         for (int i = namespacePosition - 1; i >= 0; i--) {
    82             final String declaredNamespaceURI = namespaceURIs[i];
    83             if (declaredNamespaceURI.equals(namespaceURI)) {
    84                 final String declaredPrefix = prefixes[i];
    86                 // Check if prefix is out of scope
    87                 boolean isOutOfScope = false;
    88                 for (int j = i + 1; j < namespacePosition; j++)
    89                     if (declaredPrefix.equals(prefixes[j])) {
    90                         isOutOfScope = true;
    91                         break;
    92                     }
    94                 if (!isOutOfScope) {
    95                     return declaredPrefix;
    96                 }
    97             }
    98         }
   100         return null;
   101     }
   103     public String getNonDefaultPrefix(String namespaceURI) {
   104         if (namespaceURI == null) throw new IllegalArgumentException();
   106         // namespaceURI = namespaceURI.intern();
   108         for (int i = namespacePosition - 1; i >= 0; i--) {
   109             final String declaredNamespaceURI = namespaceURIs[i];
   110             if (declaredNamespaceURI.equals(namespaceURI) &&
   111                 prefixes[i].length() > 0){
   112                 final String declaredPrefix = prefixes[i];
   114                 // Check if prefix is out of scope
   115                 for (++i; i < namespacePosition; i++)
   116                     if (declaredPrefix.equals(prefixes[i]))
   117                         return null;
   119                 return declaredPrefix;
   120             }
   121         }
   123         return null;
   124     }
   126     public Iterator getPrefixes(String namespaceURI) {
   127         if (namespaceURI == null) throw new IllegalArgumentException();
   129         // namespaceURI = namespaceURI.intern();
   131         List l = new ArrayList();
   133         NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 0; i--) {
   134             final String declaredNamespaceURI = namespaceURIs[i];
   135             if (declaredNamespaceURI.equals(namespaceURI)) {
   136                 final String declaredPrefix = prefixes[i];
   138                 // Check if prefix is out of scope
   139                 for (int j = i + 1; j < namespacePosition; j++)
   140                     if (declaredPrefix.equals(prefixes[j]))
   141                         continue NAMESPACE_LOOP;
   143                 l.add(declaredPrefix);
   144             }
   145         }
   147         return l.iterator();
   148     }
   151     public String getPrefix(int index) {
   152         return prefixes[index];
   153     }
   155     public String getNamespaceURI(int index) {
   156         return namespaceURIs[index];
   157     }
   159     public int getCurrentContextStartIndex() {
   160         return currentContext;
   161     }
   163     public int getCurrentContextEndIndex() {
   164         return namespacePosition;
   165     }
   167     public boolean isCurrentContextEmpty() {
   168         return currentContext == namespacePosition;
   169     }
   171     public void declarePrefix(String prefix, String namespaceURI) {
   172         prefix = prefix.intern();
   173         namespaceURI = namespaceURI.intern();
   175         // Ignore the "xml" or "xmlns" declarations
   176         if (prefix == "xml" || prefix == "xmlns")
   177             return;
   179         // Replace any previous declaration
   180         for (int i = currentContext; i < namespacePosition; i++) {
   181             final String declaredPrefix = prefixes[i];
   182             if (declaredPrefix == prefix) {
   183                 prefixes[i] = prefix;
   184                 namespaceURIs[i] = namespaceURI;
   185                 return;
   186             }
   187         }
   189         if (namespacePosition == namespaceURIs.length)
   190             resizeNamespaces();
   192         // Add new declaration
   193         prefixes[namespacePosition] = prefix;
   194         namespaceURIs[namespacePosition++] = namespaceURI;
   195     }
   197     private void resizeNamespaces() {
   198         final int newLength = namespaceURIs.length * 3 / 2 + 1;
   200         String[] newPrefixes = new String[newLength];
   201         System.arraycopy(prefixes, 0, newPrefixes, 0, prefixes.length);
   202         prefixes = newPrefixes;
   204         String[] newNamespaceURIs = new String[newLength];
   205         System.arraycopy(namespaceURIs, 0, newNamespaceURIs, 0, namespaceURIs.length);
   206         namespaceURIs = newNamespaceURIs;
   207     }
   209     public void pushContext() {
   210         if (contextPosition == contexts.length)
   211             resizeContexts();
   213         contexts[contextPosition++] = currentContext = namespacePosition;
   214     }
   216     private void resizeContexts() {
   217         int[] newContexts = new int[contexts.length * 3 / 2 + 1];
   218         System.arraycopy(contexts, 0, newContexts, 0, contexts.length);
   219         contexts = newContexts;
   220     }
   222     public void popContext() {
   223         if (contextPosition > 0) {
   224             namespacePosition = currentContext = contexts[--contextPosition];
   225         }
   226     }
   228     public void reset() {
   229         currentContext = namespacePosition = 2;
   230     }
   231 }

mercurial