src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/stax/NamespaceContexHelper.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.stream.buffer.stax;
ohair@286 27
ohair@286 28 import java.util.ArrayList;
ohair@286 29 import java.util.Collections;
ohair@286 30 import java.util.Iterator;
ohair@286 31 import java.util.List;
ohair@286 32 import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx;
ohair@286 33
ohair@286 34 /**
ohair@286 35 * A helper class for managing the declaration of namespaces.
ohair@286 36 * <p>
ohair@286 37 * A namespace is declared on a namespace context.
ohair@286 38 * Namespace contexts are pushed on and popped off the namespace context stack.
ohair@286 39 * <p>
ohair@286 40 * A declared namespace will be in scope iff the context that it was declared on
ohair@286 41 * has not been popped off the stack.
ohair@286 42 * <p>
ohair@286 43 * When instantiated the namespace stack consists of the root namespace context,
ohair@286 44 * which contains, by default, the "xml" and "xmlns" declarations.
ohair@286 45 * Namespaces may be declarations may be declared on the root context.
ohair@286 46 * The root context cannot be popped but can be reset to contain just the
ohair@286 47 * "xml" and "xmlns" declarations.
ohair@286 48 * <p>
ohair@286 49 * Implementation note: determining the prefix from a namespace URI
ohair@286 50 * (or vice versa) is efficient when there are few namespace
ohair@286 51 * declarations i.e. what is considered to be the case for namespace
ohair@286 52 * declarations in 'average' XML documents. The look up of a namespace URI
ohair@286 53 * given a prefix is performed in O(n) time. The look up of a prefix given
ohair@286 54 * a namespace URI is performed in O(2n) time.
ohair@286 55 * <p>
ohair@286 56 * The implementation does not scale when there are many namespace
ohair@286 57 * declarations. TODO: Use a hash map when there are many namespace
ohair@286 58 * declarations.
ohair@286 59 *
ohair@286 60 * @author Paul.Sandoz@Sun.Com
ohair@286 61 */
ohair@286 62 final public class NamespaceContexHelper implements NamespaceContextEx {
ohair@286 63 private static int DEFAULT_SIZE = 8;
ohair@286 64
ohair@286 65 // The prefixes of the namespace declarations
ohair@286 66 private String[] prefixes = new String[DEFAULT_SIZE];
ohair@286 67 // The URIs of the namespace declarations
ohair@286 68 private String[] namespaceURIs = new String[DEFAULT_SIZE];
ohair@286 69 // Current position to store the next namespace declaration
ohair@286 70 private int namespacePosition;
ohair@286 71
ohair@286 72 // The namespace contexts
ohair@286 73 private int[] contexts = new int[DEFAULT_SIZE];
ohair@286 74 // Current position to store the next namespace context
ohair@286 75 private int contextPosition;
ohair@286 76
ohair@286 77 // The current namespace context
ohair@286 78 private int currentContext;
ohair@286 79
ohair@286 80 /**
ohair@286 81 * Create a new NamespaceContexHelper.
ohair@286 82 *
ohair@286 83 */
ohair@286 84 public NamespaceContexHelper() {
ohair@286 85 // The default namespace declarations that are always in scope
ohair@286 86 prefixes[0] = "xml";
ohair@286 87 namespaceURIs[0] = "http://www.w3.org/XML/1998/namespace";
ohair@286 88 prefixes[1] = "xmlns";
ohair@286 89 namespaceURIs[1] = "http://www.w3.org/2000/xmlns/";
ohair@286 90
ohair@286 91 currentContext = namespacePosition = 2;
ohair@286 92 }
ohair@286 93
ohair@286 94
ohair@286 95 // NamespaceContext interface
ohair@286 96
ohair@286 97 public String getNamespaceURI(String prefix) {
ohair@286 98 if (prefix == null) throw new IllegalArgumentException();
ohair@286 99
ohair@286 100 prefix = prefix.intern();
ohair@286 101
ohair@286 102 for (int i = namespacePosition - 1; i >= 0; i--) {
ohair@286 103 final String declaredPrefix = prefixes[i];
ohair@286 104 if (declaredPrefix == prefix) {
ohair@286 105 return namespaceURIs[i];
ohair@286 106 }
ohair@286 107 }
ohair@286 108
ohair@286 109 return "";
ohair@286 110 }
ohair@286 111
ohair@286 112 public String getPrefix(String namespaceURI) {
ohair@286 113 if (namespaceURI == null) throw new IllegalArgumentException();
ohair@286 114
ohair@286 115 for (int i = namespacePosition - 1; i >= 0; i--) {
ohair@286 116 final String declaredNamespaceURI = namespaceURIs[i];
ohair@286 117 if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) {
ohair@286 118 final String declaredPrefix = prefixes[i];
ohair@286 119
ohair@286 120 // Check if prefix is out of scope
ohair@286 121 for (++i; i < namespacePosition; i++)
ohair@286 122 if (declaredPrefix == prefixes[i])
ohair@286 123 return null;
ohair@286 124
ohair@286 125 return declaredPrefix;
ohair@286 126 }
ohair@286 127 }
ohair@286 128
ohair@286 129 return null;
ohair@286 130 }
ohair@286 131
ohair@286 132 public Iterator getPrefixes(String namespaceURI) {
ohair@286 133 if (namespaceURI == null) throw new IllegalArgumentException();
ohair@286 134
ohair@286 135 List<String> l = new ArrayList<String>();
ohair@286 136
ohair@286 137 NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 0; i--) {
ohair@286 138 final String declaredNamespaceURI = namespaceURIs[i];
ohair@286 139 if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) {
ohair@286 140 final String declaredPrefix = prefixes[i];
ohair@286 141
ohair@286 142 // Check if prefix is out of scope
ohair@286 143 for (int j = i + 1; j < namespacePosition; j++)
ohair@286 144 if (declaredPrefix == prefixes[j])
ohair@286 145 continue NAMESPACE_LOOP;
ohair@286 146
ohair@286 147 l.add(declaredPrefix);
ohair@286 148 }
ohair@286 149 }
ohair@286 150
ohair@286 151 return l.iterator();
ohair@286 152 }
ohair@286 153
ohair@286 154 // NamespaceContextEx interface
ohair@286 155
ohair@286 156 public Iterator<NamespaceContextEx.Binding> iterator() {
ohair@286 157 if (namespacePosition == 2)
ohair@286 158 return Collections.EMPTY_LIST.iterator();
ohair@286 159
ohair@286 160 final List<NamespaceContextEx.Binding> namespaces =
ohair@286 161 new ArrayList<NamespaceContextEx.Binding>(namespacePosition);
ohair@286 162
ohair@286 163 NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 2; i--) {
ohair@286 164 final String declaredPrefix = prefixes[i];
ohair@286 165
ohair@286 166 // Check if prefix is out of scope
ohair@286 167 for (int j = i + 1; j < namespacePosition; j++) {
ohair@286 168 if (declaredPrefix == prefixes[j])
ohair@286 169 continue NAMESPACE_LOOP;
ohair@286 170
ohair@286 171 namespaces.add(new NamespaceBindingImpl(i));
ohair@286 172 }
ohair@286 173 }
ohair@286 174
ohair@286 175 return namespaces.iterator();
ohair@286 176 }
ohair@286 177
ohair@286 178 final private class NamespaceBindingImpl implements NamespaceContextEx.Binding {
ohair@286 179 int index;
ohair@286 180
ohair@286 181 NamespaceBindingImpl(int index) {
ohair@286 182 this.index = index;
ohair@286 183 }
ohair@286 184
ohair@286 185 public String getPrefix() {
ohair@286 186 return prefixes[index];
ohair@286 187 }
ohair@286 188
ohair@286 189 public String getNamespaceURI() {
ohair@286 190 return namespaceURIs[index];
ohair@286 191 }
ohair@286 192 }
ohair@286 193
ohair@286 194 /**
ohair@286 195 * Declare a default namespace.
ohair@286 196 * <p>
ohair@286 197 * @param namespaceURI the namespace URI to declare, may be null.
ohair@286 198 */
ohair@286 199 public void declareDefaultNamespace(String namespaceURI) {
ohair@286 200 declareNamespace("", namespaceURI);
ohair@286 201 }
ohair@286 202
ohair@286 203 /**
ohair@286 204 * Declare a namespace.
ohair@286 205 * <p>
ohair@286 206 * The namespace will be declared on the current namespace context.
ohair@286 207 * <p>
ohair@286 208 * The namespace can be removed by popping the current namespace
ohair@286 209 * context, or, if the declaration occured in the root context, by
ohair@286 210 * reseting the namespace context.
ohair@286 211 * <p>
ohair@286 212 * A default namespace can be declared by passing <code>""</code> as
ohair@286 213 * the value of the prefix parameter.
ohair@286 214 * A namespace may be undeclared by passing <code>null</code> as the
ohair@286 215 * value of the namespaceURI parameter.
ohair@286 216 * <p>
ohair@286 217 * @param prefix the namespace prefix to declare, may not be null.
ohair@286 218 * @param namespaceURI the namespace URI to declare, may be null.
ohair@286 219 * @throws IllegalArgumentException, if the prefix is null.
ohair@286 220 */
ohair@286 221 public void declareNamespace(String prefix, String namespaceURI) {
ohair@286 222 if (prefix == null) throw new IllegalArgumentException();
ohair@286 223
ohair@286 224 prefix = prefix.intern();
ohair@286 225 // Ignore the "xml" or "xmlns" declarations
ohair@286 226 if (prefix == "xml" || prefix == "xmlns")
ohair@286 227 return;
ohair@286 228
ohair@286 229 // Check for undeclaration
ohair@286 230 if (namespaceURI != null)
ohair@286 231 namespaceURI = namespaceURI.intern();
ohair@286 232
ohair@286 233 if (namespacePosition == namespaceURIs.length)
ohair@286 234 resizeNamespaces();
ohair@286 235
ohair@286 236 // Add new declaration
ohair@286 237 prefixes[namespacePosition] = prefix;
ohair@286 238 namespaceURIs[namespacePosition++] = namespaceURI;
ohair@286 239 }
ohair@286 240
ohair@286 241 private void resizeNamespaces() {
ohair@286 242 final int newLength = namespaceURIs.length * 3 / 2 + 1;
ohair@286 243
ohair@286 244 String[] newPrefixes = new String[newLength];
ohair@286 245 System.arraycopy(prefixes, 0, newPrefixes, 0, prefixes.length);
ohair@286 246 prefixes = newPrefixes;
ohair@286 247
ohair@286 248 String[] newNamespaceURIs = new String[newLength];
ohair@286 249 System.arraycopy(namespaceURIs, 0, newNamespaceURIs, 0, namespaceURIs.length);
ohair@286 250 namespaceURIs = newNamespaceURIs;
ohair@286 251 }
ohair@286 252
ohair@286 253 /**
ohair@286 254 * Push a namespace context on the stack.
ohair@286 255 */
ohair@286 256 public void pushContext() {
ohair@286 257 if (contextPosition == contexts.length)
ohair@286 258 resizeContexts();
ohair@286 259
ohair@286 260 contexts[contextPosition++] = currentContext = namespacePosition;
ohair@286 261 }
ohair@286 262
ohair@286 263 private void resizeContexts() {
ohair@286 264 int[] newContexts = new int[contexts.length * 3 / 2 + 1];
ohair@286 265 System.arraycopy(contexts, 0, newContexts, 0, contexts.length);
ohair@286 266 contexts = newContexts;
ohair@286 267 }
ohair@286 268
ohair@286 269 /**
ohair@286 270 * Pop the namespace context off the stack.
ohair@286 271 * <p>
ohair@286 272 * Namespaces declared within the context (to be popped)
ohair@286 273 * will be removed and no longer be in scope.
ohair@286 274 */
ohair@286 275 public void popContext() {
ohair@286 276 if (contextPosition > 0) {
ohair@286 277 namespacePosition = currentContext = contexts[--contextPosition];
ohair@286 278 }
ohair@286 279 }
ohair@286 280
ohair@286 281 /**
ohair@286 282 * Reset namespace contexts.
ohair@286 283 * <p>
ohair@286 284 * Pop all namespace contexts and reset the root context.
ohair@286 285 */
ohair@286 286 public void resetContexts() {
ohair@286 287 currentContext = namespacePosition = 2;
ohair@286 288 }
ohair@286 289 }

mercurial