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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 2005, 2012, 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 /**
ohair@286 78 * Create a new NamespaceContexHelper.
ohair@286 79 *
ohair@286 80 */
ohair@286 81 public NamespaceContexHelper() {
ohair@286 82 // The default namespace declarations that are always in scope
ohair@286 83 prefixes[0] = "xml";
ohair@286 84 namespaceURIs[0] = "http://www.w3.org/XML/1998/namespace";
ohair@286 85 prefixes[1] = "xmlns";
ohair@286 86 namespaceURIs[1] = "http://www.w3.org/2000/xmlns/";
ohair@286 87
alanb@368 88 namespacePosition = 2;
ohair@286 89 }
ohair@286 90
ohair@286 91
ohair@286 92 // NamespaceContext interface
ohair@286 93
ohair@286 94 public String getNamespaceURI(String prefix) {
ohair@286 95 if (prefix == null) throw new IllegalArgumentException();
ohair@286 96
ohair@286 97 prefix = prefix.intern();
ohair@286 98
ohair@286 99 for (int i = namespacePosition - 1; i >= 0; i--) {
ohair@286 100 final String declaredPrefix = prefixes[i];
ohair@286 101 if (declaredPrefix == prefix) {
ohair@286 102 return namespaceURIs[i];
ohair@286 103 }
ohair@286 104 }
ohair@286 105
ohair@286 106 return "";
ohair@286 107 }
ohair@286 108
ohair@286 109 public String getPrefix(String namespaceURI) {
ohair@286 110 if (namespaceURI == null) throw new IllegalArgumentException();
ohair@286 111
ohair@286 112 for (int i = namespacePosition - 1; i >= 0; i--) {
ohair@286 113 final String declaredNamespaceURI = namespaceURIs[i];
ohair@286 114 if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) {
ohair@286 115 final String declaredPrefix = prefixes[i];
ohair@286 116
ohair@286 117 // Check if prefix is out of scope
ohair@286 118 for (++i; i < namespacePosition; i++)
ohair@286 119 if (declaredPrefix == prefixes[i])
ohair@286 120 return null;
ohair@286 121
ohair@286 122 return declaredPrefix;
ohair@286 123 }
ohair@286 124 }
ohair@286 125
ohair@286 126 return null;
ohair@286 127 }
ohair@286 128
ohair@286 129 public Iterator getPrefixes(String namespaceURI) {
ohair@286 130 if (namespaceURI == null) throw new IllegalArgumentException();
ohair@286 131
ohair@286 132 List<String> l = new ArrayList<String>();
ohair@286 133
ohair@286 134 NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 0; i--) {
ohair@286 135 final String declaredNamespaceURI = namespaceURIs[i];
ohair@286 136 if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) {
ohair@286 137 final String declaredPrefix = prefixes[i];
ohair@286 138
ohair@286 139 // Check if prefix is out of scope
ohair@286 140 for (int j = i + 1; j < namespacePosition; j++)
ohair@286 141 if (declaredPrefix == prefixes[j])
ohair@286 142 continue NAMESPACE_LOOP;
ohair@286 143
ohair@286 144 l.add(declaredPrefix);
ohair@286 145 }
ohair@286 146 }
ohair@286 147
ohair@286 148 return l.iterator();
ohair@286 149 }
ohair@286 150
ohair@286 151 // NamespaceContextEx interface
ohair@286 152
ohair@286 153 public Iterator<NamespaceContextEx.Binding> iterator() {
ohair@286 154 if (namespacePosition == 2)
ohair@286 155 return Collections.EMPTY_LIST.iterator();
ohair@286 156
ohair@286 157 final List<NamespaceContextEx.Binding> namespaces =
ohair@286 158 new ArrayList<NamespaceContextEx.Binding>(namespacePosition);
ohair@286 159
ohair@286 160 NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 2; i--) {
ohair@286 161 final String declaredPrefix = prefixes[i];
ohair@286 162
ohair@286 163 // Check if prefix is out of scope
ohair@286 164 for (int j = i + 1; j < namespacePosition; j++) {
ohair@286 165 if (declaredPrefix == prefixes[j])
ohair@286 166 continue NAMESPACE_LOOP;
ohair@286 167
ohair@286 168 namespaces.add(new NamespaceBindingImpl(i));
ohair@286 169 }
ohair@286 170 }
ohair@286 171
ohair@286 172 return namespaces.iterator();
ohair@286 173 }
ohair@286 174
ohair@286 175 final private class NamespaceBindingImpl implements NamespaceContextEx.Binding {
ohair@286 176 int index;
ohair@286 177
ohair@286 178 NamespaceBindingImpl(int index) {
ohair@286 179 this.index = index;
ohair@286 180 }
ohair@286 181
ohair@286 182 public String getPrefix() {
ohair@286 183 return prefixes[index];
ohair@286 184 }
ohair@286 185
ohair@286 186 public String getNamespaceURI() {
ohair@286 187 return namespaceURIs[index];
ohair@286 188 }
ohair@286 189 }
ohair@286 190
ohair@286 191 /**
ohair@286 192 * Declare a default namespace.
ohair@286 193 * <p>
ohair@286 194 * @param namespaceURI the namespace URI to declare, may be null.
ohair@286 195 */
ohair@286 196 public void declareDefaultNamespace(String namespaceURI) {
ohair@286 197 declareNamespace("", namespaceURI);
ohair@286 198 }
ohair@286 199
ohair@286 200 /**
ohair@286 201 * Declare a namespace.
ohair@286 202 * <p>
ohair@286 203 * The namespace will be declared on the current namespace context.
ohair@286 204 * <p>
ohair@286 205 * The namespace can be removed by popping the current namespace
ohair@286 206 * context, or, if the declaration occured in the root context, by
ohair@286 207 * reseting the namespace context.
ohair@286 208 * <p>
ohair@286 209 * A default namespace can be declared by passing <code>""</code> as
ohair@286 210 * the value of the prefix parameter.
ohair@286 211 * A namespace may be undeclared by passing <code>null</code> as the
ohair@286 212 * value of the namespaceURI parameter.
ohair@286 213 * <p>
ohair@286 214 * @param prefix the namespace prefix to declare, may not be null.
ohair@286 215 * @param namespaceURI the namespace URI to declare, may be null.
ohair@286 216 * @throws IllegalArgumentException, if the prefix is null.
ohair@286 217 */
ohair@286 218 public void declareNamespace(String prefix, String namespaceURI) {
ohair@286 219 if (prefix == null) throw new IllegalArgumentException();
ohair@286 220
ohair@286 221 prefix = prefix.intern();
ohair@286 222 // Ignore the "xml" or "xmlns" declarations
ohair@286 223 if (prefix == "xml" || prefix == "xmlns")
ohair@286 224 return;
ohair@286 225
ohair@286 226 // Check for undeclaration
ohair@286 227 if (namespaceURI != null)
ohair@286 228 namespaceURI = namespaceURI.intern();
ohair@286 229
ohair@286 230 if (namespacePosition == namespaceURIs.length)
ohair@286 231 resizeNamespaces();
ohair@286 232
ohair@286 233 // Add new declaration
ohair@286 234 prefixes[namespacePosition] = prefix;
ohair@286 235 namespaceURIs[namespacePosition++] = namespaceURI;
ohair@286 236 }
ohair@286 237
ohair@286 238 private void resizeNamespaces() {
ohair@286 239 final int newLength = namespaceURIs.length * 3 / 2 + 1;
ohair@286 240
ohair@286 241 String[] newPrefixes = new String[newLength];
ohair@286 242 System.arraycopy(prefixes, 0, newPrefixes, 0, prefixes.length);
ohair@286 243 prefixes = newPrefixes;
ohair@286 244
ohair@286 245 String[] newNamespaceURIs = new String[newLength];
ohair@286 246 System.arraycopy(namespaceURIs, 0, newNamespaceURIs, 0, namespaceURIs.length);
ohair@286 247 namespaceURIs = newNamespaceURIs;
ohair@286 248 }
ohair@286 249
ohair@286 250 /**
ohair@286 251 * Push a namespace context on the stack.
ohair@286 252 */
ohair@286 253 public void pushContext() {
ohair@286 254 if (contextPosition == contexts.length)
ohair@286 255 resizeContexts();
ohair@286 256
alanb@368 257 contexts[contextPosition++] = namespacePosition;
ohair@286 258 }
ohair@286 259
ohair@286 260 private void resizeContexts() {
ohair@286 261 int[] newContexts = new int[contexts.length * 3 / 2 + 1];
ohair@286 262 System.arraycopy(contexts, 0, newContexts, 0, contexts.length);
ohair@286 263 contexts = newContexts;
ohair@286 264 }
ohair@286 265
ohair@286 266 /**
ohair@286 267 * Pop the namespace context off the stack.
ohair@286 268 * <p>
ohair@286 269 * Namespaces declared within the context (to be popped)
ohair@286 270 * will be removed and no longer be in scope.
ohair@286 271 */
ohair@286 272 public void popContext() {
ohair@286 273 if (contextPosition > 0) {
alanb@368 274 namespacePosition = contexts[--contextPosition];
ohair@286 275 }
ohair@286 276 }
ohair@286 277
ohair@286 278 /**
ohair@286 279 * Reset namespace contexts.
ohair@286 280 * <p>
ohair@286 281 * Pop all namespace contexts and reset the root context.
ohair@286 282 */
ohair@286 283 public void resetContexts() {
alanb@368 284 namespacePosition = 2;
ohair@286 285 }
ohair@286 286 }

mercurial