src/share/jaxws_classes/com/sun/xml/internal/ws/util/NamespaceSupport.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 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.ws.util;
ohair@286 27
ohair@286 28 import java.util.ArrayList;
ohair@286 29 import java.util.EmptyStackException;
ohair@286 30 import java.util.HashMap;
ohair@286 31 import java.util.Iterator;
ohair@286 32 import java.util.List;
ohair@286 33 import java.util.Map;
ohair@286 34
ohair@286 35 import com.sun.xml.internal.ws.encoding.soap.streaming.SOAPNamespaceConstants;
ohair@286 36
ohair@286 37 /**
ohair@286 38 * Encapsulate Namespace logic for use by SAX drivers.
ohair@286 39 *
ohair@286 40 * <blockquote>
ohair@286 41 * <em>This module, both source code and documentation, is in the
ohair@286 42 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
ohair@286 43 * </blockquote>
ohair@286 44 *
ohair@286 45 * <p>This class encapsulates the logic of Namespace processing:
ohair@286 46 * it tracks the declarations currently in force for each context
ohair@286 47 * and automatically processes qualified XML 1.0 names into their
ohair@286 48 * Namespace parts; it can also be used in reverse for generating
ohair@286 49 * XML 1.0 from Namespaces.</p>
ohair@286 50 *
ohair@286 51 * <p>Namespace support objects are reusable, but the reset method
ohair@286 52 * must be invoked between each session.</p>
ohair@286 53 *
ohair@286 54 * <p>Here is a simple session:</p>
ohair@286 55 *
ohair@286 56 * <pre>
ohair@286 57 * String parts[] = new String[3];
ohair@286 58 * NamespaceSupport support = new NamespaceSupport();
ohair@286 59 *
ohair@286 60 * support.pushContext();
ohair@286 61 * support.declarePrefix("", "http://www.w3.org/1999/xhtml");
ohair@286 62 * support.declarePrefix("dc", "http://www.purl.org/dc#");
ohair@286 63 *
ohair@286 64 * String parts[] = support.processName("p", parts, false);
ohair@286 65 * System.out.println("Namespace URI: " + parts[0]);
ohair@286 66 * System.out.println("Local name: " + parts[1]);
ohair@286 67 * System.out.println("Raw name: " + parts[2]);
ohair@286 68
ohair@286 69 * String parts[] = support.processName("dc:title", parts, false);
ohair@286 70 * System.out.println("Namespace URI: " + parts[0]);
ohair@286 71 * System.out.println("Local name: " + parts[1]);
ohair@286 72 * System.out.println("Raw name: " + parts[2]);
ohair@286 73
ohair@286 74 * support.popContext();
ohair@286 75 * </pre>
ohair@286 76 *
ohair@286 77 * <p>Note that this class is optimized for the use case where most
ohair@286 78 * elements do not contain Namespace declarations: if the same
ohair@286 79 * prefix/URI mapping is repeated for each context (for example), this
ohair@286 80 * class will be somewhat less efficient.</p>
ohair@286 81 *
ohair@286 82 * @author David Megginson
ohair@286 83 * @author WS Development Team
ohair@286 84 */
alanb@368 85 public final class NamespaceSupport {
ohair@286 86
ohair@286 87 /* added two new methods, slideContextUp() and slideContextDown()
ohair@286 88 * needed to implement the revised streaming parser class (Parser2)
ohair@286 89 */
ohair@286 90
ohair@286 91 ////////////////////////////////////////////////////////////////////
ohair@286 92 // Constants.
ohair@286 93 ////////////////////////////////////////////////////////////////////
ohair@286 94
ohair@286 95 /**
ohair@286 96 * The XML Namespace as a constant.
ohair@286 97 *
ohair@286 98 * <p>This is the Namespace URI that is automatically mapped
ohair@286 99 * to the "xml" prefix.</p>
ohair@286 100 */
ohair@286 101 public final static String XMLNS = "http://www.w3.org/XML/1998/namespace";
ohair@286 102
ohair@286 103 /**
ohair@286 104 * An empty enumeration.
ohair@286 105 */
ohair@286 106 private final static Iterable<String> EMPTY_ENUMERATION =
ohair@286 107 new ArrayList<String>();
ohair@286 108
ohair@286 109 ////////////////////////////////////////////////////////////////////
ohair@286 110 // Constructor.
ohair@286 111 ////////////////////////////////////////////////////////////////////
ohair@286 112
ohair@286 113 /**
ohair@286 114 * Create a new Namespace support object.
ohair@286 115 */
ohair@286 116 public NamespaceSupport() {
ohair@286 117 reset();
ohair@286 118 }
ohair@286 119
ohair@286 120 // PBG May 6 2002 added a copy constructor to support recording
ohair@286 121 public NamespaceSupport(NamespaceSupport that) {
ohair@286 122 contexts = new Context[that.contexts.length];
ohair@286 123 currentContext = null;
ohair@286 124 contextPos = that.contextPos;
ohair@286 125
ohair@286 126 Context currentParent = null;
ohair@286 127
ohair@286 128 for (int i = 0; i < that.contexts.length; i++) {
ohair@286 129 Context thatContext = that.contexts[i];
ohair@286 130
ohair@286 131 if (thatContext == null) {
ohair@286 132 contexts[i] = null;
ohair@286 133 continue;
ohair@286 134 }
ohair@286 135
ohair@286 136 Context thisContext = new Context(thatContext, currentParent);
ohair@286 137 contexts[i] = thisContext;
ohair@286 138 if (that.currentContext == thatContext) {
ohair@286 139 currentContext = thisContext;
ohair@286 140 }
ohair@286 141
ohair@286 142 currentParent = thisContext;
ohair@286 143 }
ohair@286 144 }
ohair@286 145
ohair@286 146 ////////////////////////////////////////////////////////////////////
ohair@286 147 // Context management.
ohair@286 148 ////////////////////////////////////////////////////////////////////
ohair@286 149
ohair@286 150 /**
ohair@286 151 * Reset this Namespace support object for reuse.
ohair@286 152 *
ohair@286 153 * <p>It is necessary to invoke this method before reusing the
ohair@286 154 * Namespace support object for a new session.</p>
ohair@286 155 */
ohair@286 156 public void reset() {
ohair@286 157 contexts = new Context[32];
ohair@286 158 contextPos = 0;
ohair@286 159 contexts[contextPos] = currentContext = new Context();
ohair@286 160 currentContext.declarePrefix("xml", XMLNS);
ohair@286 161 }
ohair@286 162
ohair@286 163 /**
ohair@286 164 * Start a new Namespace context.
ohair@286 165 *
ohair@286 166 * <p>Normally, you should push a new context at the beginning
ohair@286 167 * of each XML element: the new context will automatically inherit
ohair@286 168 * the declarations of its parent context, but it will also keep
ohair@286 169 * track of which declarations were made within this context.</p>
ohair@286 170 *
ohair@286 171 * <p>The Namespace support object always starts with a base context
ohair@286 172 * already in force: in this context, only the "xml" prefix is
ohair@286 173 * declared.</p>
ohair@286 174 *
ohair@286 175 * @see #popContext
ohair@286 176 */
ohair@286 177 public void pushContext() {
ohair@286 178 int max = contexts.length;
ohair@286 179 contextPos++;
ohair@286 180
ohair@286 181 // Extend the array if necessary
ohair@286 182 if (contextPos >= max) {
ohair@286 183 Context newContexts[] = new Context[max * 2];
ohair@286 184 System.arraycopy(contexts, 0, newContexts, 0, max);
ohair@286 185 contexts = newContexts;
ohair@286 186 }
ohair@286 187
ohair@286 188 // Allocate the context if necessary.
ohair@286 189 currentContext = contexts[contextPos];
ohair@286 190 if (currentContext == null) {
ohair@286 191 contexts[contextPos] = currentContext = new Context();
ohair@286 192 }
ohair@286 193
ohair@286 194 // Set the parent, if any.
ohair@286 195 if (contextPos > 0) {
ohair@286 196 currentContext.setParent(contexts[contextPos - 1]);
ohair@286 197 }
ohair@286 198 }
ohair@286 199
ohair@286 200 /**
ohair@286 201 * Revert to the previous Namespace context.
ohair@286 202 *
ohair@286 203 * <p>Normally, you should pop the context at the end of each
ohair@286 204 * XML element. After popping the context, all Namespace prefix
ohair@286 205 * mappings that were previously in force are restored.</p>
ohair@286 206 *
ohair@286 207 * <p>You must not attempt to declare additional Namespace
ohair@286 208 * prefixes after popping a context, unless you push another
ohair@286 209 * context first.</p>
ohair@286 210 *
ohair@286 211 * @see #pushContext
ohair@286 212 */
ohair@286 213 public void popContext() {
ohair@286 214 contextPos--;
ohair@286 215 if (contextPos < 0) {
ohair@286 216 throw new EmptyStackException();
ohair@286 217 }
ohair@286 218 currentContext = contexts[contextPos];
ohair@286 219 }
ohair@286 220
ohair@286 221 /*
ohair@286 222 * added for the revised streaming parser class (Parser2)
ohair@286 223 * Move the context artificially up one level (i.e. contracting it).
ohair@286 224 */
ohair@286 225 public void slideContextUp() {
ohair@286 226 contextPos--;
ohair@286 227 currentContext = contexts[contextPos];
ohair@286 228 }
ohair@286 229
ohair@286 230 /*
ohair@286 231 * added for the revised streaming parser class (Parser2)
ohair@286 232 * Move the context artificially down one level (i.e. expanding it).
ohair@286 233 */
ohair@286 234 public void slideContextDown() {
ohair@286 235 contextPos++;
ohair@286 236
ohair@286 237 if (contexts[contextPos] == null) {
ohair@286 238 // trying to slide to a context that was never created
ohair@286 239 contexts[contextPos] = contexts[contextPos - 1];
ohair@286 240 }
ohair@286 241
ohair@286 242 currentContext = contexts[contextPos];
ohair@286 243 }
ohair@286 244
ohair@286 245 ////////////////////////////////////////////////////////////////////
ohair@286 246 // Operations within a context.
ohair@286 247 ////////////////////////////////////////////////////////////////////
ohair@286 248
ohair@286 249 /**
ohair@286 250 * Declare a Namespace prefix.
ohair@286 251 *
ohair@286 252 * <p>This method declares a prefix in the current Namespace
ohair@286 253 * context; the prefix will remain in force until this context
ohair@286 254 * is popped, unless it is shadowed in a descendant context.</p>
ohair@286 255 *
ohair@286 256 * <p>To declare a default Namespace, use the empty string. The
ohair@286 257 * prefix must not be "xml" or "xmlns".</p>
ohair@286 258 *
ohair@286 259 * <p>Note that you must <em>not</em> declare a prefix after
ohair@286 260 * you've pushed and popped another Namespace.</p>
ohair@286 261 *
ohair@286 262 * <p>Note that there is an asymmetry in this library: while {@link
ohair@286 263 * #getPrefix getPrefix} will not return the default "" prefix,
ohair@286 264 * even if you have declared one; to check for a default prefix,
ohair@286 265 * you have to look it up explicitly using {@link #getURI getURI}.
ohair@286 266 * This asymmetry exists to make it easier to look up prefixes
ohair@286 267 * for attribute names, where the default prefix is not allowed.</p>
ohair@286 268 *
ohair@286 269 * @param prefix The prefix to declare, or null for the empty
ohair@286 270 * string.
ohair@286 271 * @param uri The Namespace URI to associate with the prefix.
ohair@286 272 * @return true if the prefix was legal, false otherwise
ohair@286 273 * @see #processName
ohair@286 274 * @see #getURI
ohair@286 275 * @see #getPrefix
ohair@286 276 */
ohair@286 277 public boolean declarePrefix(String prefix, String uri) {
ohair@286 278 // bugfix#: 4989753
ohair@286 279 if ((prefix.equals("xml") && !uri.equals(SOAPNamespaceConstants.XMLNS))
ohair@286 280 || prefix.equals("xmlns")) {
ohair@286 281 return false;
ohair@286 282 } else {
ohair@286 283 currentContext.declarePrefix(prefix, uri);
ohair@286 284 return true;
ohair@286 285 }
ohair@286 286 }
ohair@286 287
ohair@286 288 /**
ohair@286 289 * Process a raw XML 1.0 name.
ohair@286 290 *
ohair@286 291 * <p>This method processes a raw XML 1.0 name in the current
ohair@286 292 * context by removing the prefix and looking it up among the
ohair@286 293 * prefixes currently declared. The return value will be the
ohair@286 294 * array supplied by the caller, filled in as follows:</p>
ohair@286 295 *
ohair@286 296 * <dl>
ohair@286 297 * <dt>parts[0]</dt>
ohair@286 298 * <dd>The Namespace URI, or an empty string if none is
ohair@286 299 * in use.</dd>
ohair@286 300 * <dt>parts[1]</dt>
ohair@286 301 * <dd>The local name (without prefix).</dd>
ohair@286 302 * <dt>parts[2]</dt>
ohair@286 303 * <dd>The original raw name.</dd>
ohair@286 304 * </dl>
ohair@286 305 *
ohair@286 306 * <p>All of the strings in the array will be internalized. If
ohair@286 307 * the raw name has a prefix that has not been declared, then
ohair@286 308 * the return value will be null.</p>
ohair@286 309 *
ohair@286 310 * <p>Note that attribute names are processed differently than
ohair@286 311 * element names: an unprefixed element name will received the
ohair@286 312 * default Namespace (if any), while an unprefixed element name
ohair@286 313 * will not.</p>
ohair@286 314 *
ohair@286 315 * @param qName The raw XML 1.0 name to be processed.
ohair@286 316 * @param parts An array supplied by the caller, capable of
ohair@286 317 * holding at least three members.
ohair@286 318 * @param isAttribute A flag indicating whether this is an
ohair@286 319 * attribute name (true) or an element name (false).
ohair@286 320 * @return The supplied array holding three internalized strings
ohair@286 321 * representing the Namespace URI (or empty string), the
ohair@286 322 * local name, and the raw XML 1.0 name; or null if there
ohair@286 323 * is an undeclared prefix.
ohair@286 324 * @see #declarePrefix
ohair@286 325 * @see java.lang.String#intern */
ohair@286 326 public String[] processName(
ohair@286 327 String qName,
ohair@286 328 String parts[],
ohair@286 329 boolean isAttribute) {
ohair@286 330 String myParts[] = currentContext.processName(qName, isAttribute);
ohair@286 331 if (myParts == null) {
ohair@286 332 return null;
ohair@286 333 } else {
ohair@286 334 parts[0] = myParts[0];
ohair@286 335 parts[1] = myParts[1];
ohair@286 336 parts[2] = myParts[2];
ohair@286 337 return parts;
ohair@286 338 }
ohair@286 339 }
ohair@286 340
ohair@286 341 /**
ohair@286 342 * Look up a prefix and get the currently-mapped Namespace URI.
ohair@286 343 *
ohair@286 344 * <p>This method looks up the prefix in the current context.
ohair@286 345 * Use the empty string ("") for the default Namespace.</p>
ohair@286 346 *
ohair@286 347 * @param prefix The prefix to look up.
ohair@286 348 * @return The associated Namespace URI, or null if the prefix
ohair@286 349 * is undeclared in this context.
ohair@286 350 * @see #getPrefix
ohair@286 351 * @see #getPrefixes
ohair@286 352 */
ohair@286 353 public String getURI(String prefix) {
ohair@286 354 return currentContext.getURI(prefix);
ohair@286 355 }
ohair@286 356
ohair@286 357 /**
ohair@286 358 * Return an enumeration of all prefixes currently declared.
ohair@286 359 *
ohair@286 360 * <p><strong>Note:</strong> if there is a default prefix, it will not be
ohair@286 361 * returned in this enumeration; check for the default prefix
ohair@286 362 * using the {@link #getURI getURI} with an argument of "".</p>
ohair@286 363 *
ohair@286 364 * @return An enumeration of all prefixes declared in the
ohair@286 365 * current context except for the empty (default)
ohair@286 366 * prefix.
ohair@286 367 * @see #getDeclaredPrefixes
ohair@286 368 * @see #getURI
ohair@286 369 */
ohair@286 370 public Iterable<String> getPrefixes() {
ohair@286 371 return currentContext.getPrefixes();
ohair@286 372 }
ohair@286 373
ohair@286 374 /**
ohair@286 375 * Return one of the prefixes mapped to a Namespace URI.
ohair@286 376 *
ohair@286 377 * <p>If more than one prefix is currently mapped to the same
ohair@286 378 * URI, this method will make an arbitrary selection; if you
ohair@286 379 * want all of the prefixes, use the {@link #getPrefixes}
ohair@286 380 * method instead.</p>
ohair@286 381 *
ohair@286 382 * <p><strong>Note:</strong> this will never return the empty (default) prefix;
ohair@286 383 * to check for a default prefix, use the {@link #getURI getURI}
ohair@286 384 * method with an argument of "".</p>
ohair@286 385 *
ohair@286 386 * @param uri The Namespace URI.
ohair@286 387 * @return One of the prefixes currently mapped to the URI supplied,
ohair@286 388 * or null if none is mapped or if the URI is assigned to
ohair@286 389 * the default Namespace.
ohair@286 390 * @see #getPrefixes(java.lang.String)
ohair@286 391 * @see #getURI
ohair@286 392 */
ohair@286 393 public String getPrefix(String uri) {
ohair@286 394 return currentContext.getPrefix(uri);
ohair@286 395 }
ohair@286 396
ohair@286 397 /**
ohair@286 398 * Return an enumeration of all prefixes currently declared for a URI.
ohair@286 399 *
ohair@286 400 * <p>This method returns prefixes mapped to a specific Namespace
ohair@286 401 * URI. The xml: prefix will be included. If you want only one
ohair@286 402 * prefix that's mapped to the Namespace URI, and you don't care
ohair@286 403 * which one you get, use the {@link #getPrefix getPrefix}
ohair@286 404 * method instead.</p>
ohair@286 405 *
ohair@286 406 * <p><strong>Note:</strong> the empty (default) prefix is <em>never</em> included
ohair@286 407 * in this enumeration; to check for the presence of a default
ohair@286 408 * Namespace, use the {@link #getURI getURI} method with an
ohair@286 409 * argument of "".</p>
ohair@286 410 *
ohair@286 411 * @param uri The Namespace URI.
ohair@286 412 * @return An enumeration of all prefixes declared in the
ohair@286 413 * current context.
ohair@286 414 * @see #getPrefix
ohair@286 415 * @see #getDeclaredPrefixes
ohair@286 416 * @see #getURI
ohair@286 417 */
ohair@286 418 public Iterator getPrefixes(String uri) {
ohair@286 419 List prefixes = new ArrayList();
ohair@286 420 for (String prefix: getPrefixes()) {
ohair@286 421 if (uri.equals(getURI(prefix))) {
ohair@286 422 prefixes.add(prefix);
ohair@286 423 }
ohair@286 424 }
ohair@286 425 return prefixes.iterator();
ohair@286 426 }
ohair@286 427
ohair@286 428 /**
ohair@286 429 * Return an enumeration of all prefixes declared in this context.
ohair@286 430 *
ohair@286 431 * <p>The empty (default) prefix will be included in this
ohair@286 432 * enumeration; note that this behaviour differs from that of
ohair@286 433 * {@link #getPrefix} and {@link #getPrefixes}.</p>
ohair@286 434 *
ohair@286 435 * @return An enumeration of all prefixes declared in this
ohair@286 436 * context.
ohair@286 437 * @see #getPrefixes
ohair@286 438 * @see #getURI
ohair@286 439 */
ohair@286 440 public Iterable<String> getDeclaredPrefixes() {
ohair@286 441 return currentContext.getDeclaredPrefixes();
ohair@286 442 }
ohair@286 443
ohair@286 444 ////////////////////////////////////////////////////////////////////
ohair@286 445 // Internal state.
ohair@286 446 ////////////////////////////////////////////////////////////////////
ohair@286 447
ohair@286 448 private Context contexts[];
ohair@286 449 private Context currentContext;
ohair@286 450 private int contextPos;
ohair@286 451
ohair@286 452 ////////////////////////////////////////////////////////////////////
ohair@286 453 // Internal classes.
ohair@286 454 ////////////////////////////////////////////////////////////////////
ohair@286 455
ohair@286 456 /**
ohair@286 457 * Internal class for a single Namespace context.
ohair@286 458 *
ohair@286 459 * <p>This module caches and reuses Namespace contexts, so the number
ohair@286 460 * allocated will be equal to the element depth of the document, not to the
ohair@286 461 * total number of elements (i.e. 5-10 rather than tens of thousands).</p>
ohair@286 462 */
ohair@286 463 final static class Context {
ohair@286 464
ohair@286 465 /**
ohair@286 466 * Create the root-level Namespace context.
ohair@286 467 */
ohair@286 468 Context() {
ohair@286 469 copyTables();
ohair@286 470 }
ohair@286 471 // PGB May 6 2002 added copy constructor
ohair@286 472 Context(Context that, Context newParent) {
ohair@286 473 if (that == null) {
ohair@286 474 copyTables();
ohair@286 475 return;
ohair@286 476 }
ohair@286 477
ohair@286 478 if (newParent != null && !that.tablesDirty) {
ohair@286 479 prefixTable =
ohair@286 480 that.prefixTable == that.parent.prefixTable
ohair@286 481 ? newParent.prefixTable
ohair@286 482 : (HashMap) that.prefixTable.clone();
ohair@286 483
ohair@286 484 uriTable =
ohair@286 485 that.uriTable == that.parent.uriTable
ohair@286 486 ? newParent.uriTable
ohair@286 487 : (HashMap) that.uriTable.clone();
ohair@286 488 elementNameTable =
ohair@286 489 that.elementNameTable == that.parent.elementNameTable
ohair@286 490 ? newParent.elementNameTable
ohair@286 491 : (HashMap) that.elementNameTable.clone();
ohair@286 492 attributeNameTable =
ohair@286 493 that.attributeNameTable == that.parent.attributeNameTable
ohair@286 494 ? newParent.attributeNameTable
ohair@286 495 : (HashMap) that.attributeNameTable.clone();
ohair@286 496 defaultNS =
ohair@286 497 that.defaultNS == that.parent.defaultNS
ohair@286 498 ? newParent.defaultNS
ohair@286 499 : that.defaultNS;
ohair@286 500 } else {
ohair@286 501 prefixTable = (HashMap) that.prefixTable.clone();
ohair@286 502 uriTable = (HashMap) that.uriTable.clone();
ohair@286 503 elementNameTable = (HashMap) that.elementNameTable.clone();
ohair@286 504 attributeNameTable = (HashMap) that.attributeNameTable.clone();
ohair@286 505 defaultNS = that.defaultNS;
ohair@286 506 }
ohair@286 507
ohair@286 508 tablesDirty = that.tablesDirty;
ohair@286 509 parent = newParent;
ohair@286 510 declarations =
ohair@286 511 that.declarations == null
ohair@286 512 ? null
ohair@286 513 : (ArrayList) that.declarations.clone();
ohair@286 514 }
ohair@286 515
ohair@286 516 /**
ohair@286 517 * (Re)set the parent of this Namespace context.
ohair@286 518 *
ohair@286 519 * @param parent The parent Namespace context object.
ohair@286 520 */
ohair@286 521 void setParent(Context parent) {
ohair@286 522 this.parent = parent;
ohair@286 523 declarations = null;
ohair@286 524 prefixTable = parent.prefixTable;
ohair@286 525 uriTable = parent.uriTable;
ohair@286 526 elementNameTable = parent.elementNameTable;
ohair@286 527 attributeNameTable = parent.attributeNameTable;
ohair@286 528 defaultNS = parent.defaultNS;
ohair@286 529 tablesDirty = false;
ohair@286 530 }
ohair@286 531
ohair@286 532 /**
ohair@286 533 * Declare a Namespace prefix for this context.
ohair@286 534 *
ohair@286 535 * @param prefix The prefix to declare.
ohair@286 536 * @param uri The associated Namespace URI.
ohair@286 537 * @see org.xml.sax.helpers.NamespaceSupport#declarePrefix
ohair@286 538 */
ohair@286 539 void declarePrefix(String prefix, String uri) {
ohair@286 540 // Lazy processing...
ohair@286 541 if (!tablesDirty) {
ohair@286 542 copyTables();
ohair@286 543 }
ohair@286 544 if (declarations == null) {
ohair@286 545 declarations = new ArrayList();
ohair@286 546 }
ohair@286 547
ohair@286 548 prefix = prefix.intern();
ohair@286 549 uri = uri.intern();
ohair@286 550 if ("".equals(prefix)) {
ohair@286 551 if ("".equals(uri)) {
ohair@286 552 defaultNS = null;
ohair@286 553 } else {
ohair@286 554 defaultNS = uri;
ohair@286 555 }
ohair@286 556 } else {
ohair@286 557 prefixTable.put(prefix, uri);
ohair@286 558 uriTable.put(uri, prefix); // may wipe out another prefix
ohair@286 559 }
ohair@286 560 declarations.add(prefix);
ohair@286 561 }
ohair@286 562
ohair@286 563 /**
ohair@286 564 * Process a raw XML 1.0 name in this context.
ohair@286 565 *
ohair@286 566 * @param qName The raw XML 1.0 name.
ohair@286 567 * @param isAttribute true if this is an attribute name.
ohair@286 568 * @return An array of three strings containing the
ohair@286 569 * URI part (or empty string), the local part,
ohair@286 570 * and the raw name, all internalized, or null
ohair@286 571 * if there is an undeclared prefix.
ohair@286 572 * @see org.xml.sax.helpers.NamespaceSupport#processName
ohair@286 573 */
ohair@286 574 String[] processName(String qName, boolean isAttribute) {
ohair@286 575 String name[];
ohair@286 576 Map table;
ohair@286 577
ohair@286 578 // Select the appropriate table.
ohair@286 579 if (isAttribute) {
ohair@286 580 table = elementNameTable;
ohair@286 581 } else {
ohair@286 582 table = attributeNameTable;
ohair@286 583 }
ohair@286 584
ohair@286 585 // Start by looking in the cache, and
ohair@286 586 // return immediately if the name
ohair@286 587 // is already known in this content
ohair@286 588 name = (String[]) table.get(qName);
ohair@286 589 if (name != null) {
ohair@286 590 return name;
ohair@286 591 }
ohair@286 592
ohair@286 593 // We haven't seen this name in this
ohair@286 594 // context before.
ohair@286 595 name = new String[3];
ohair@286 596 int index = qName.indexOf(':');
ohair@286 597
ohair@286 598 // No prefix.
ohair@286 599 if (index == -1) {
ohair@286 600 if (isAttribute || defaultNS == null) {
ohair@286 601 name[0] = "";
ohair@286 602 } else {
ohair@286 603 name[0] = defaultNS;
ohair@286 604 }
ohair@286 605 name[1] = qName.intern();
ohair@286 606 name[2] = name[1];
ohair@286 607 }
ohair@286 608
ohair@286 609 // Prefix
ohair@286 610 else {
ohair@286 611 String prefix = qName.substring(0, index);
ohair@286 612 String local = qName.substring(index + 1);
ohair@286 613 String uri;
ohair@286 614 if ("".equals(prefix)) {
ohair@286 615 uri = defaultNS;
ohair@286 616 } else {
ohair@286 617 uri = (String) prefixTable.get(prefix);
ohair@286 618 }
ohair@286 619 if (uri == null) {
ohair@286 620 return null;
ohair@286 621 }
ohair@286 622 name[0] = uri;
ohair@286 623 name[1] = local.intern();
ohair@286 624 name[2] = qName.intern();
ohair@286 625 }
ohair@286 626
ohair@286 627 // Save in the cache for future use.
ohair@286 628 table.put(name[2], name);
ohair@286 629 tablesDirty = true;
ohair@286 630 return name;
ohair@286 631 }
ohair@286 632
ohair@286 633 /**
ohair@286 634 * Look up the URI associated with a prefix in this context.
ohair@286 635 *
ohair@286 636 * @param prefix The prefix to look up.
ohair@286 637 * @return The associated Namespace URI, or null if none is
ohair@286 638 * declared.
ohair@286 639 * @see org.xml.sax.helpers.NamespaceSupport#getURI
ohair@286 640 */
ohair@286 641 String getURI(String prefix) {
ohair@286 642 if ("".equals(prefix)) {
ohair@286 643 return defaultNS;
ohair@286 644 } else if (prefixTable == null) {
ohair@286 645 return null;
ohair@286 646 } else {
ohair@286 647 return (String) prefixTable.get(prefix);
ohair@286 648 }
ohair@286 649 }
ohair@286 650
ohair@286 651 /**
ohair@286 652 * Look up one of the prefixes associated with a URI in this context.
ohair@286 653 *
ohair@286 654 * <p>Since many prefixes may be mapped to the same URI,
ohair@286 655 * the return value may be unreliable.</p>
ohair@286 656 *
ohair@286 657 * @param uri The URI to look up.
ohair@286 658 * @return The associated prefix, or null if none is declared.
ohair@286 659 * @see org.xml.sax.helpers.NamespaceSupport#getPrefix
ohair@286 660 */
ohair@286 661 String getPrefix(String uri) {
ohair@286 662 if (uriTable == null) {
ohair@286 663 return null;
ohair@286 664 } else {
ohair@286 665 return (String) uriTable.get(uri);
ohair@286 666 }
ohair@286 667 }
ohair@286 668
ohair@286 669 /**
ohair@286 670 * Return an enumeration of prefixes declared in this context.
ohair@286 671 *
ohair@286 672 * @return An enumeration of prefixes (possibly empty).
ohair@286 673 * @see org.xml.sax.helpers.NamespaceSupport#getDeclaredPrefixes
ohair@286 674 */
ohair@286 675 Iterable<String> getDeclaredPrefixes() {
ohair@286 676 if (declarations == null) {
ohair@286 677 return EMPTY_ENUMERATION;
ohair@286 678 } else {
ohair@286 679 return declarations;
ohair@286 680 }
ohair@286 681 }
ohair@286 682
ohair@286 683 /**
ohair@286 684 * Return an enumeration of all prefixes currently in force.
ohair@286 685 *
ohair@286 686 * <p>The default prefix, if in force, is <em>not</em>
ohair@286 687 * returned, and will have to be checked for separately.</p>
ohair@286 688 *
ohair@286 689 * @return An enumeration of prefixes (never empty).
ohair@286 690 * @see org.xml.sax.helpers.NamespaceSupport#getPrefixes
ohair@286 691 */
ohair@286 692 Iterable<String> getPrefixes() {
ohair@286 693 if (prefixTable == null) {
ohair@286 694 return EMPTY_ENUMERATION;
ohair@286 695 } else {
ohair@286 696 return prefixTable.keySet();
ohair@286 697 }
ohair@286 698 }
ohair@286 699
ohair@286 700 ////////////////////////////////////////////////////////////////
ohair@286 701 // Internal methods.
ohair@286 702 ////////////////////////////////////////////////////////////////
ohair@286 703
ohair@286 704 /**
ohair@286 705 * Copy on write for the internal tables in this context.
ohair@286 706 *
ohair@286 707 * <p>This class is optimized for the normal case where most
ohair@286 708 * elements do not contain Namespace declarations.</p>
ohair@286 709 */
ohair@286 710 private void copyTables() {
ohair@286 711 if (prefixTable != null) {
ohair@286 712 prefixTable = (HashMap) prefixTable.clone();
ohair@286 713 } else {
ohair@286 714 prefixTable = new HashMap();
ohair@286 715 }
ohair@286 716 if (uriTable != null) {
ohair@286 717 uriTable = (HashMap) uriTable.clone();
ohair@286 718 } else {
ohair@286 719 uriTable = new HashMap();
ohair@286 720 }
ohair@286 721 elementNameTable = new HashMap();
ohair@286 722 attributeNameTable = new HashMap();
ohair@286 723 tablesDirty = true;
ohair@286 724 }
ohair@286 725
ohair@286 726 ////////////////////////////////////////////////////////////////
ohair@286 727 // Protected state.
ohair@286 728 ////////////////////////////////////////////////////////////////
ohair@286 729
ohair@286 730 HashMap prefixTable;
ohair@286 731 HashMap uriTable;
ohair@286 732 // PBG May 6 2002 changed these two from Map to HashMap
ohair@286 733 HashMap elementNameTable;
ohair@286 734 HashMap attributeNameTable;
ohair@286 735 String defaultNS = null;
ohair@286 736
ohair@286 737 ////////////////////////////////////////////////////////////////
ohair@286 738 // Internal state.
ohair@286 739 ////////////////////////////////////////////////////////////////
ohair@286 740
ohair@286 741 // PBG May 6 2002 changed this from List to ArrayList
ohair@286 742 private ArrayList declarations = null;
ohair@286 743 private boolean tablesDirty = false;
ohair@286 744 private Context parent = null;
ohair@286 745 }
ohair@286 746 }

mercurial