1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/stax/NamespaceContexHelper.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,289 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.stream.buffer.stax; 1.30 + 1.31 +import java.util.ArrayList; 1.32 +import java.util.Collections; 1.33 +import java.util.Iterator; 1.34 +import java.util.List; 1.35 +import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx; 1.36 + 1.37 +/** 1.38 + * A helper class for managing the declaration of namespaces. 1.39 + * <p> 1.40 + * A namespace is declared on a namespace context. 1.41 + * Namespace contexts are pushed on and popped off the namespace context stack. 1.42 + * <p> 1.43 + * A declared namespace will be in scope iff the context that it was declared on 1.44 + * has not been popped off the stack. 1.45 + * <p> 1.46 + * When instantiated the namespace stack consists of the root namespace context, 1.47 + * which contains, by default, the "xml" and "xmlns" declarations. 1.48 + * Namespaces may be declarations may be declared on the root context. 1.49 + * The root context cannot be popped but can be reset to contain just the 1.50 + * "xml" and "xmlns" declarations. 1.51 + * <p> 1.52 + * Implementation note: determining the prefix from a namespace URI 1.53 + * (or vice versa) is efficient when there are few namespace 1.54 + * declarations i.e. what is considered to be the case for namespace 1.55 + * declarations in 'average' XML documents. The look up of a namespace URI 1.56 + * given a prefix is performed in O(n) time. The look up of a prefix given 1.57 + * a namespace URI is performed in O(2n) time. 1.58 + * <p> 1.59 + * The implementation does not scale when there are many namespace 1.60 + * declarations. TODO: Use a hash map when there are many namespace 1.61 + * declarations. 1.62 + * 1.63 + * @author Paul.Sandoz@Sun.Com 1.64 + */ 1.65 +final public class NamespaceContexHelper implements NamespaceContextEx { 1.66 + private static int DEFAULT_SIZE = 8; 1.67 + 1.68 + // The prefixes of the namespace declarations 1.69 + private String[] prefixes = new String[DEFAULT_SIZE]; 1.70 + // The URIs of the namespace declarations 1.71 + private String[] namespaceURIs = new String[DEFAULT_SIZE]; 1.72 + // Current position to store the next namespace declaration 1.73 + private int namespacePosition; 1.74 + 1.75 + // The namespace contexts 1.76 + private int[] contexts = new int[DEFAULT_SIZE]; 1.77 + // Current position to store the next namespace context 1.78 + private int contextPosition; 1.79 + 1.80 + // The current namespace context 1.81 + private int currentContext; 1.82 + 1.83 + /** 1.84 + * Create a new NamespaceContexHelper. 1.85 + * 1.86 + */ 1.87 + public NamespaceContexHelper() { 1.88 + // The default namespace declarations that are always in scope 1.89 + prefixes[0] = "xml"; 1.90 + namespaceURIs[0] = "http://www.w3.org/XML/1998/namespace"; 1.91 + prefixes[1] = "xmlns"; 1.92 + namespaceURIs[1] = "http://www.w3.org/2000/xmlns/"; 1.93 + 1.94 + currentContext = namespacePosition = 2; 1.95 + } 1.96 + 1.97 + 1.98 + // NamespaceContext interface 1.99 + 1.100 + public String getNamespaceURI(String prefix) { 1.101 + if (prefix == null) throw new IllegalArgumentException(); 1.102 + 1.103 + prefix = prefix.intern(); 1.104 + 1.105 + for (int i = namespacePosition - 1; i >= 0; i--) { 1.106 + final String declaredPrefix = prefixes[i]; 1.107 + if (declaredPrefix == prefix) { 1.108 + return namespaceURIs[i]; 1.109 + } 1.110 + } 1.111 + 1.112 + return ""; 1.113 + } 1.114 + 1.115 + public String getPrefix(String namespaceURI) { 1.116 + if (namespaceURI == null) throw new IllegalArgumentException(); 1.117 + 1.118 + for (int i = namespacePosition - 1; i >= 0; i--) { 1.119 + final String declaredNamespaceURI = namespaceURIs[i]; 1.120 + if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) { 1.121 + final String declaredPrefix = prefixes[i]; 1.122 + 1.123 + // Check if prefix is out of scope 1.124 + for (++i; i < namespacePosition; i++) 1.125 + if (declaredPrefix == prefixes[i]) 1.126 + return null; 1.127 + 1.128 + return declaredPrefix; 1.129 + } 1.130 + } 1.131 + 1.132 + return null; 1.133 + } 1.134 + 1.135 + public Iterator getPrefixes(String namespaceURI) { 1.136 + if (namespaceURI == null) throw new IllegalArgumentException(); 1.137 + 1.138 + List<String> l = new ArrayList<String>(); 1.139 + 1.140 + NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 0; i--) { 1.141 + final String declaredNamespaceURI = namespaceURIs[i]; 1.142 + if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) { 1.143 + final String declaredPrefix = prefixes[i]; 1.144 + 1.145 + // Check if prefix is out of scope 1.146 + for (int j = i + 1; j < namespacePosition; j++) 1.147 + if (declaredPrefix == prefixes[j]) 1.148 + continue NAMESPACE_LOOP; 1.149 + 1.150 + l.add(declaredPrefix); 1.151 + } 1.152 + } 1.153 + 1.154 + return l.iterator(); 1.155 + } 1.156 + 1.157 + // NamespaceContextEx interface 1.158 + 1.159 + public Iterator<NamespaceContextEx.Binding> iterator() { 1.160 + if (namespacePosition == 2) 1.161 + return Collections.EMPTY_LIST.iterator(); 1.162 + 1.163 + final List<NamespaceContextEx.Binding> namespaces = 1.164 + new ArrayList<NamespaceContextEx.Binding>(namespacePosition); 1.165 + 1.166 + NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 2; i--) { 1.167 + final String declaredPrefix = prefixes[i]; 1.168 + 1.169 + // Check if prefix is out of scope 1.170 + for (int j = i + 1; j < namespacePosition; j++) { 1.171 + if (declaredPrefix == prefixes[j]) 1.172 + continue NAMESPACE_LOOP; 1.173 + 1.174 + namespaces.add(new NamespaceBindingImpl(i)); 1.175 + } 1.176 + } 1.177 + 1.178 + return namespaces.iterator(); 1.179 + } 1.180 + 1.181 + final private class NamespaceBindingImpl implements NamespaceContextEx.Binding { 1.182 + int index; 1.183 + 1.184 + NamespaceBindingImpl(int index) { 1.185 + this.index = index; 1.186 + } 1.187 + 1.188 + public String getPrefix() { 1.189 + return prefixes[index]; 1.190 + } 1.191 + 1.192 + public String getNamespaceURI() { 1.193 + return namespaceURIs[index]; 1.194 + } 1.195 + } 1.196 + 1.197 + /** 1.198 + * Declare a default namespace. 1.199 + * <p> 1.200 + * @param namespaceURI the namespace URI to declare, may be null. 1.201 + */ 1.202 + public void declareDefaultNamespace(String namespaceURI) { 1.203 + declareNamespace("", namespaceURI); 1.204 + } 1.205 + 1.206 + /** 1.207 + * Declare a namespace. 1.208 + * <p> 1.209 + * The namespace will be declared on the current namespace context. 1.210 + * <p> 1.211 + * The namespace can be removed by popping the current namespace 1.212 + * context, or, if the declaration occured in the root context, by 1.213 + * reseting the namespace context. 1.214 + * <p> 1.215 + * A default namespace can be declared by passing <code>""</code> as 1.216 + * the value of the prefix parameter. 1.217 + * A namespace may be undeclared by passing <code>null</code> as the 1.218 + * value of the namespaceURI parameter. 1.219 + * <p> 1.220 + * @param prefix the namespace prefix to declare, may not be null. 1.221 + * @param namespaceURI the namespace URI to declare, may be null. 1.222 + * @throws IllegalArgumentException, if the prefix is null. 1.223 + */ 1.224 + public void declareNamespace(String prefix, String namespaceURI) { 1.225 + if (prefix == null) throw new IllegalArgumentException(); 1.226 + 1.227 + prefix = prefix.intern(); 1.228 + // Ignore the "xml" or "xmlns" declarations 1.229 + if (prefix == "xml" || prefix == "xmlns") 1.230 + return; 1.231 + 1.232 + // Check for undeclaration 1.233 + if (namespaceURI != null) 1.234 + namespaceURI = namespaceURI.intern(); 1.235 + 1.236 + if (namespacePosition == namespaceURIs.length) 1.237 + resizeNamespaces(); 1.238 + 1.239 + // Add new declaration 1.240 + prefixes[namespacePosition] = prefix; 1.241 + namespaceURIs[namespacePosition++] = namespaceURI; 1.242 + } 1.243 + 1.244 + private void resizeNamespaces() { 1.245 + final int newLength = namespaceURIs.length * 3 / 2 + 1; 1.246 + 1.247 + String[] newPrefixes = new String[newLength]; 1.248 + System.arraycopy(prefixes, 0, newPrefixes, 0, prefixes.length); 1.249 + prefixes = newPrefixes; 1.250 + 1.251 + String[] newNamespaceURIs = new String[newLength]; 1.252 + System.arraycopy(namespaceURIs, 0, newNamespaceURIs, 0, namespaceURIs.length); 1.253 + namespaceURIs = newNamespaceURIs; 1.254 + } 1.255 + 1.256 + /** 1.257 + * Push a namespace context on the stack. 1.258 + */ 1.259 + public void pushContext() { 1.260 + if (contextPosition == contexts.length) 1.261 + resizeContexts(); 1.262 + 1.263 + contexts[contextPosition++] = currentContext = namespacePosition; 1.264 + } 1.265 + 1.266 + private void resizeContexts() { 1.267 + int[] newContexts = new int[contexts.length * 3 / 2 + 1]; 1.268 + System.arraycopy(contexts, 0, newContexts, 0, contexts.length); 1.269 + contexts = newContexts; 1.270 + } 1.271 + 1.272 + /** 1.273 + * Pop the namespace context off the stack. 1.274 + * <p> 1.275 + * Namespaces declared within the context (to be popped) 1.276 + * will be removed and no longer be in scope. 1.277 + */ 1.278 + public void popContext() { 1.279 + if (contextPosition > 0) { 1.280 + namespacePosition = currentContext = contexts[--contextPosition]; 1.281 + } 1.282 + } 1.283 + 1.284 + /** 1.285 + * Reset namespace contexts. 1.286 + * <p> 1.287 + * Pop all namespace contexts and reset the root context. 1.288 + */ 1.289 + public void resetContexts() { 1.290 + currentContext = namespacePosition = 2; 1.291 + } 1.292 +}