src/share/jaxws_classes/com/sun/xml/internal/txw2/NamespaceSupport.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

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 // NamespaceSupport.java - generic Namespace support for SAX.
aoqi@0 27 // http://www.saxproject.org
aoqi@0 28 // Written by David Megginson
aoqi@0 29 // This class is in the Public Domain. NO WARRANTY!
aoqi@0 30
aoqi@0 31 package com.sun.xml.internal.txw2;
aoqi@0 32
aoqi@0 33 import java.util.EmptyStackException;
aoqi@0 34 import java.util.Enumeration;
aoqi@0 35 import java.util.Hashtable;
aoqi@0 36 import java.util.Vector;
aoqi@0 37
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * Encapsulate Namespace logic for use by applications using SAX,
aoqi@0 41 * or internally by SAX drivers.
aoqi@0 42 *
aoqi@0 43 * <blockquote>
aoqi@0 44 * <em>This module, both source code and documentation, is in the
aoqi@0 45 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
aoqi@0 46 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
aoqi@0 47 * for further information.
aoqi@0 48 * </blockquote>
aoqi@0 49 *
aoqi@0 50 * <p>This class encapsulates the logic of Namespace processing: it
aoqi@0 51 * tracks the declarations currently in force for each context and
aoqi@0 52 * automatically processes qualified XML names into their Namespace
aoqi@0 53 * parts; it can also be used in reverse for generating XML qnames
aoqi@0 54 * from Namespaces.</p>
aoqi@0 55 *
aoqi@0 56 * <p>Namespace support objects are reusable, but the reset method
aoqi@0 57 * must be invoked between each session.</p>
aoqi@0 58 *
aoqi@0 59 * <p>Here is a simple session:</p>
aoqi@0 60 *
aoqi@0 61 * <pre>
aoqi@0 62 * String parts[] = new String[3];
aoqi@0 63 * NamespaceSupport support = new NamespaceSupport();
aoqi@0 64 *
aoqi@0 65 * support.pushContext();
aoqi@0 66 * support.declarePrefix("", "http://www.w3.org/1999/xhtml");
aoqi@0 67 * support.declarePrefix("dc", "http://www.purl.org/dc#");
aoqi@0 68 *
aoqi@0 69 * parts = support.processName("p", parts, false);
aoqi@0 70 * System.out.println("Namespace URI: " + parts[0]);
aoqi@0 71 * System.out.println("Local name: " + parts[1]);
aoqi@0 72 * System.out.println("Raw name: " + parts[2]);
aoqi@0 73 *
aoqi@0 74 * parts = support.processName("dc:title", parts, false);
aoqi@0 75 * System.out.println("Namespace URI: " + parts[0]);
aoqi@0 76 * System.out.println("Local name: " + parts[1]);
aoqi@0 77 * System.out.println("Raw name: " + parts[2]);
aoqi@0 78 *
aoqi@0 79 * support.popContext();
aoqi@0 80 * </pre>
aoqi@0 81 *
aoqi@0 82 * <p>Note that this class is optimized for the use case where most
aoqi@0 83 * elements do not contain Namespace declarations: if the same
aoqi@0 84 * prefix/URI mapping is repeated for each context (for example), this
aoqi@0 85 * class will be somewhat less efficient.</p>
aoqi@0 86 *
aoqi@0 87 * <p>Although SAX drivers (parsers) may choose to use this class to
aoqi@0 88 * implement namespace handling, they are not required to do so.
aoqi@0 89 * Applications must track namespace information themselves if they
aoqi@0 90 * want to use namespace information.
aoqi@0 91 *
aoqi@0 92 * @since SAX 2.0
aoqi@0 93 * @author David Megginson
aoqi@0 94 * @version 2.0.1 (sax2r2)
aoqi@0 95 */
aoqi@0 96 final class NamespaceSupport
aoqi@0 97 {
aoqi@0 98
aoqi@0 99
aoqi@0 100 ////////////////////////////////////////////////////////////////////
aoqi@0 101 // Constants.
aoqi@0 102 ////////////////////////////////////////////////////////////////////
aoqi@0 103
aoqi@0 104
aoqi@0 105 /**
aoqi@0 106 * The XML Namespace URI as a constant.
aoqi@0 107 * The value is <code>http://www.w3.org/XML/1998/namespace</code>
aoqi@0 108 * as defined in the "Namespaces in XML" * recommendation.
aoqi@0 109 *
aoqi@0 110 * <p>This is the Namespace URI that is automatically mapped
aoqi@0 111 * to the "xml" prefix.</p>
aoqi@0 112 */
aoqi@0 113 public final static String XMLNS =
aoqi@0 114 "http://www.w3.org/XML/1998/namespace";
aoqi@0 115
aoqi@0 116
aoqi@0 117 /**
aoqi@0 118 * The namespace declaration URI as a constant.
aoqi@0 119 * The value is <code>http://www.w3.org/xmlns/2000/</code>, as defined
aoqi@0 120 * in a backwards-incompatible erratum to the "Namespaces in XML"
aoqi@0 121 * recommendation. Because that erratum postdated SAX2, SAX2 defaults
aoqi@0 122 * to the original recommendation, and does not normally use this URI.
aoqi@0 123 *
aoqi@0 124 *
aoqi@0 125 * <p>This is the Namespace URI that is optionally applied to
aoqi@0 126 * <em>xmlns</em> and <em>xmlns:*</em> attributes, which are used to
aoqi@0 127 * declare namespaces. </p>
aoqi@0 128 *
aoqi@0 129 * @since SAX 2.1alpha
aoqi@0 130 * @see #setNamespaceDeclUris
aoqi@0 131 * @see #isNamespaceDeclUris
aoqi@0 132 */
aoqi@0 133 public final static String NSDECL =
aoqi@0 134 "http://www.w3.org/xmlns/2000/";
aoqi@0 135
aoqi@0 136
aoqi@0 137 /**
aoqi@0 138 * An empty enumeration.
aoqi@0 139 */
aoqi@0 140 private final static Enumeration EMPTY_ENUMERATION =
aoqi@0 141 new Vector().elements();
aoqi@0 142
aoqi@0 143
aoqi@0 144 ////////////////////////////////////////////////////////////////////
aoqi@0 145 // Constructor.
aoqi@0 146 ////////////////////////////////////////////////////////////////////
aoqi@0 147
aoqi@0 148
aoqi@0 149 /**
aoqi@0 150 * Create a new Namespace support object.
aoqi@0 151 */
aoqi@0 152 public NamespaceSupport ()
aoqi@0 153 {
aoqi@0 154 reset();
aoqi@0 155 }
aoqi@0 156
aoqi@0 157
aoqi@0 158
aoqi@0 159 ////////////////////////////////////////////////////////////////////
aoqi@0 160 // Context management.
aoqi@0 161 ////////////////////////////////////////////////////////////////////
aoqi@0 162
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Reset this Namespace support object for reuse.
aoqi@0 166 *
aoqi@0 167 * <p>It is necessary to invoke this method before reusing the
aoqi@0 168 * Namespace support object for a new session. If namespace
aoqi@0 169 * declaration URIs are to be supported, that flag must also
aoqi@0 170 * be set to a non-default value.
aoqi@0 171 * </p>
aoqi@0 172 *
aoqi@0 173 * @see #setNamespaceDeclUris
aoqi@0 174 */
aoqi@0 175 public void reset ()
aoqi@0 176 {
aoqi@0 177 contexts = new Context[32];
aoqi@0 178 namespaceDeclUris = false;
aoqi@0 179 contextPos = 0;
aoqi@0 180 contexts[contextPos] = currentContext = new Context();
aoqi@0 181 currentContext.declarePrefix("xml", XMLNS);
aoqi@0 182 }
aoqi@0 183
aoqi@0 184
aoqi@0 185 /**
aoqi@0 186 * Start a new Namespace context.
aoqi@0 187 * The new context will automatically inherit
aoqi@0 188 * the declarations of its parent context, but it will also keep
aoqi@0 189 * track of which declarations were made within this context.
aoqi@0 190 *
aoqi@0 191 * <p>Event callback code should start a new context once per element.
aoqi@0 192 * This means being ready to call this in either of two places.
aoqi@0 193 * For elements that don't include namespace declarations, the
aoqi@0 194 * <em>ContentHandler.startElement()</em> callback is the right place.
aoqi@0 195 * For elements with such a declaration, it'd done in the first
aoqi@0 196 * <em>ContentHandler.startPrefixMapping()</em> callback.
aoqi@0 197 * A boolean flag can be used to
aoqi@0 198 * track whether a context has been started yet. When either of
aoqi@0 199 * those methods is called, it checks the flag to see if a new context
aoqi@0 200 * needs to be started. If so, it starts the context and sets the
aoqi@0 201 * flag. After <em>ContentHandler.startElement()</em>
aoqi@0 202 * does that, it always clears the flag.
aoqi@0 203 *
aoqi@0 204 * <p>Normally, SAX drivers would push a new context at the beginning
aoqi@0 205 * of each XML element. Then they perform a first pass over the
aoqi@0 206 * attributes to process all namespace declarations, making
aoqi@0 207 * <em>ContentHandler.startPrefixMapping()</em> callbacks.
aoqi@0 208 * Then a second pass is made, to determine the namespace-qualified
aoqi@0 209 * names for all attributes and for the element name.
aoqi@0 210 * Finally all the information for the
aoqi@0 211 * <em>ContentHandler.startElement()</em> callback is available,
aoqi@0 212 * so it can then be made.
aoqi@0 213 *
aoqi@0 214 * <p>The Namespace support object always starts with a base context
aoqi@0 215 * already in force: in this context, only the "xml" prefix is
aoqi@0 216 * declared.</p>
aoqi@0 217 *
aoqi@0 218 * @see org.xml.sax.ContentHandler
aoqi@0 219 * @see #popContext
aoqi@0 220 */
aoqi@0 221 public void pushContext ()
aoqi@0 222 {
aoqi@0 223 int max = contexts.length;
aoqi@0 224
aoqi@0 225 contextPos++;
aoqi@0 226
aoqi@0 227 // Extend the array if necessary
aoqi@0 228 if (contextPos >= max) {
aoqi@0 229 Context newContexts[] = new Context[max*2];
aoqi@0 230 System.arraycopy(contexts, 0, newContexts, 0, max);
aoqi@0 231 max *= 2;
aoqi@0 232 contexts = newContexts;
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 // Allocate the context if necessary.
aoqi@0 236 currentContext = contexts[contextPos];
aoqi@0 237 if (currentContext == null) {
aoqi@0 238 contexts[contextPos] = currentContext = new Context();
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 // Set the parent, if any.
aoqi@0 242 if (contextPos > 0) {
aoqi@0 243 currentContext.setParent(contexts[contextPos - 1]);
aoqi@0 244 }
aoqi@0 245 }
aoqi@0 246
aoqi@0 247
aoqi@0 248 /**
aoqi@0 249 * Revert to the previous Namespace context.
aoqi@0 250 *
aoqi@0 251 * <p>Normally, you should pop the context at the end of each
aoqi@0 252 * XML element. After popping the context, all Namespace prefix
aoqi@0 253 * mappings that were previously in force are restored.</p>
aoqi@0 254 *
aoqi@0 255 * <p>You must not attempt to declare additional Namespace
aoqi@0 256 * prefixes after popping a context, unless you push another
aoqi@0 257 * context first.</p>
aoqi@0 258 *
aoqi@0 259 * @see #pushContext
aoqi@0 260 */
aoqi@0 261 public void popContext ()
aoqi@0 262 {
aoqi@0 263 contexts[contextPos].clear();
aoqi@0 264 contextPos--;
aoqi@0 265 if (contextPos < 0) {
aoqi@0 266 throw new EmptyStackException();
aoqi@0 267 }
aoqi@0 268 currentContext = contexts[contextPos];
aoqi@0 269 }
aoqi@0 270
aoqi@0 271
aoqi@0 272
aoqi@0 273 ////////////////////////////////////////////////////////////////////
aoqi@0 274 // Operations within a context.
aoqi@0 275 ////////////////////////////////////////////////////////////////////
aoqi@0 276
aoqi@0 277
aoqi@0 278 /**
aoqi@0 279 * Declare a Namespace prefix. All prefixes must be declared
aoqi@0 280 * before they are referenced. For example, a SAX driver (parser)
aoqi@0 281 * would scan an element's attributes
aoqi@0 282 * in two passes: first for namespace declarations,
aoqi@0 283 * then a second pass using {@link #processName processName()} to
aoqi@0 284 * interpret prefixes against (potentially redefined) prefixes.
aoqi@0 285 *
aoqi@0 286 * <p>This method declares a prefix in the current Namespace
aoqi@0 287 * context; the prefix will remain in force until this context
aoqi@0 288 * is popped, unless it is shadowed in a descendant context.</p>
aoqi@0 289 *
aoqi@0 290 * <p>To declare the default element Namespace, use the empty string as
aoqi@0 291 * the prefix.</p>
aoqi@0 292 *
aoqi@0 293 * <p>Note that there is an asymmetry in this library: {@link
aoqi@0 294 * #getPrefix getPrefix} will not return the "" prefix,
aoqi@0 295 * even if you have declared a default element namespace.
aoqi@0 296 * To check for a default namespace,
aoqi@0 297 * you have to look it up explicitly using {@link #getURI getURI}.
aoqi@0 298 * This asymmetry exists to make it easier to look up prefixes
aoqi@0 299 * for attribute names, where the default prefix is not allowed.</p>
aoqi@0 300 *
aoqi@0 301 * @param prefix The prefix to declare, or the empty string to
aoqi@0 302 * indicate the default element namespace. This may never have
aoqi@0 303 * the value "xml" or "xmlns".
aoqi@0 304 * @param uri The Namespace URI to associate with the prefix.
aoqi@0 305 * @return true if the prefix was legal, false otherwise
aoqi@0 306 *
aoqi@0 307 * @see #processName
aoqi@0 308 * @see #getURI
aoqi@0 309 * @see #getPrefix
aoqi@0 310 */
aoqi@0 311 public boolean declarePrefix (String prefix, String uri)
aoqi@0 312 {
aoqi@0 313 if (prefix.equals("xml") || prefix.equals("xmlns")) {
aoqi@0 314 return false;
aoqi@0 315 } else {
aoqi@0 316 currentContext.declarePrefix(prefix, uri);
aoqi@0 317 return true;
aoqi@0 318 }
aoqi@0 319 }
aoqi@0 320
aoqi@0 321
aoqi@0 322 /**
aoqi@0 323 * Process a raw XML qualified name, after all declarations in the
aoqi@0 324 * current context have been handled by {@link #declarePrefix
aoqi@0 325 * declarePrefix()}.
aoqi@0 326 *
aoqi@0 327 * <p>This method processes a raw XML qualified name in the
aoqi@0 328 * current context by removing the prefix and looking it up among
aoqi@0 329 * the prefixes currently declared. The return value will be the
aoqi@0 330 * array supplied by the caller, filled in as follows:</p>
aoqi@0 331 *
aoqi@0 332 * <dl>
aoqi@0 333 * <dt>parts[0]</dt>
aoqi@0 334 * <dd>The Namespace URI, or an empty string if none is
aoqi@0 335 * in use.</dd>
aoqi@0 336 * <dt>parts[1]</dt>
aoqi@0 337 * <dd>The local name (without prefix).</dd>
aoqi@0 338 * <dt>parts[2]</dt>
aoqi@0 339 * <dd>The original raw name.</dd>
aoqi@0 340 * </dl>
aoqi@0 341 *
aoqi@0 342 * <p>All of the strings in the array will be internalized. If
aoqi@0 343 * the raw name has a prefix that has not been declared, then
aoqi@0 344 * the return value will be null.</p>
aoqi@0 345 *
aoqi@0 346 * <p>Note that attribute names are processed differently than
aoqi@0 347 * element names: an unprefixed element name will receive the
aoqi@0 348 * default Namespace (if any), while an unprefixed attribute name
aoqi@0 349 * will not.</p>
aoqi@0 350 *
aoqi@0 351 * @param qName The XML qualified name to be processed.
aoqi@0 352 * @param parts An array supplied by the caller, capable of
aoqi@0 353 * holding at least three members.
aoqi@0 354 * @param isAttribute A flag indicating whether this is an
aoqi@0 355 * attribute name (true) or an element name (false).
aoqi@0 356 * @return The supplied array holding three internalized strings
aoqi@0 357 * representing the Namespace URI (or empty string), the
aoqi@0 358 * local name, and the XML qualified name; or null if there
aoqi@0 359 * is an undeclared prefix.
aoqi@0 360 * @see #declarePrefix
aoqi@0 361 * @see java.lang.String#intern */
aoqi@0 362 public String [] processName (String qName, String parts[],
aoqi@0 363 boolean isAttribute)
aoqi@0 364 {
aoqi@0 365 String myParts[] = currentContext.processName(qName, isAttribute);
aoqi@0 366 if (myParts == null) {
aoqi@0 367 return null;
aoqi@0 368 } else {
aoqi@0 369 parts[0] = myParts[0];
aoqi@0 370 parts[1] = myParts[1];
aoqi@0 371 parts[2] = myParts[2];
aoqi@0 372 return parts;
aoqi@0 373 }
aoqi@0 374 }
aoqi@0 375
aoqi@0 376
aoqi@0 377 /**
aoqi@0 378 * Look up a prefix and get the currently-mapped Namespace URI.
aoqi@0 379 *
aoqi@0 380 * <p>This method looks up the prefix in the current context.
aoqi@0 381 * Use the empty string ("") for the default Namespace.</p>
aoqi@0 382 *
aoqi@0 383 * @param prefix The prefix to look up.
aoqi@0 384 * @return The associated Namespace URI, or null if the prefix
aoqi@0 385 * is undeclared in this context.
aoqi@0 386 * @see #getPrefix
aoqi@0 387 * @see #getPrefixes
aoqi@0 388 */
aoqi@0 389 public String getURI (String prefix)
aoqi@0 390 {
aoqi@0 391 return currentContext.getURI(prefix);
aoqi@0 392 }
aoqi@0 393
aoqi@0 394
aoqi@0 395 /**
aoqi@0 396 * Return an enumeration of all prefixes whose declarations are
aoqi@0 397 * active in the current context.
aoqi@0 398 * This includes declarations from parent contexts that have
aoqi@0 399 * not been overridden.
aoqi@0 400 *
aoqi@0 401 * <p><strong>Note:</strong> if there is a default prefix, it will not be
aoqi@0 402 * returned in this enumeration; check for the default prefix
aoqi@0 403 * using the {@link #getURI getURI} with an argument of "".</p>
aoqi@0 404 *
aoqi@0 405 * @return An enumeration of prefixes (never empty).
aoqi@0 406 * @see #getDeclaredPrefixes
aoqi@0 407 * @see #getURI
aoqi@0 408 */
aoqi@0 409 public Enumeration getPrefixes ()
aoqi@0 410 {
aoqi@0 411 return currentContext.getPrefixes();
aoqi@0 412 }
aoqi@0 413
aoqi@0 414
aoqi@0 415 /**
aoqi@0 416 * Return one of the prefixes mapped to a Namespace URI.
aoqi@0 417 *
aoqi@0 418 * <p>If more than one prefix is currently mapped to the same
aoqi@0 419 * URI, this method will make an arbitrary selection; if you
aoqi@0 420 * want all of the prefixes, use the {@link #getPrefixes}
aoqi@0 421 * method instead.</p>
aoqi@0 422 *
aoqi@0 423 * <p><strong>Note:</strong> this will never return the empty (default) prefix;
aoqi@0 424 * to check for a default prefix, use the {@link #getURI getURI}
aoqi@0 425 * method with an argument of "".</p>
aoqi@0 426 *
aoqi@0 427 * @param uri the namespace URI
aoqi@0 428 * @return one of the prefixes currently mapped to the URI supplied,
aoqi@0 429 * or null if none is mapped or if the URI is assigned to
aoqi@0 430 * the default namespace
aoqi@0 431 * @see #getPrefixes(java.lang.String)
aoqi@0 432 * @see #getURI
aoqi@0 433 */
aoqi@0 434 public String getPrefix (String uri)
aoqi@0 435 {
aoqi@0 436 return currentContext.getPrefix(uri);
aoqi@0 437 }
aoqi@0 438
aoqi@0 439
aoqi@0 440 /**
aoqi@0 441 * Return an enumeration of all prefixes for a given URI whose
aoqi@0 442 * declarations are active in the current context.
aoqi@0 443 * This includes declarations from parent contexts that have
aoqi@0 444 * not been overridden.
aoqi@0 445 *
aoqi@0 446 * <p>This method returns prefixes mapped to a specific Namespace
aoqi@0 447 * URI. The xml: prefix will be included. If you want only one
aoqi@0 448 * prefix that's mapped to the Namespace URI, and you don't care
aoqi@0 449 * which one you get, use the {@link #getPrefix getPrefix}
aoqi@0 450 * method instead.</p>
aoqi@0 451 *
aoqi@0 452 * <p><strong>Note:</strong> the empty (default) prefix is <em>never</em> included
aoqi@0 453 * in this enumeration; to check for the presence of a default
aoqi@0 454 * Namespace, use the {@link #getURI getURI} method with an
aoqi@0 455 * argument of "".</p>
aoqi@0 456 *
aoqi@0 457 * @param uri The Namespace URI.
aoqi@0 458 * @return An enumeration of prefixes (never empty).
aoqi@0 459 * @see #getPrefix
aoqi@0 460 * @see #getDeclaredPrefixes
aoqi@0 461 * @see #getURI
aoqi@0 462 */
aoqi@0 463 public Enumeration getPrefixes (String uri)
aoqi@0 464 {
aoqi@0 465 Vector prefixes = new Vector();
aoqi@0 466 Enumeration allPrefixes = getPrefixes();
aoqi@0 467 while (allPrefixes.hasMoreElements()) {
aoqi@0 468 String prefix = (String)allPrefixes.nextElement();
aoqi@0 469 if (uri.equals(getURI(prefix))) {
aoqi@0 470 prefixes.addElement(prefix);
aoqi@0 471 }
aoqi@0 472 }
aoqi@0 473 return prefixes.elements();
aoqi@0 474 }
aoqi@0 475
aoqi@0 476
aoqi@0 477 /**
aoqi@0 478 * Return an enumeration of all prefixes declared in this context.
aoqi@0 479 *
aoqi@0 480 * <p>The empty (default) prefix will be included in this
aoqi@0 481 * enumeration; note that this behaviour differs from that of
aoqi@0 482 * {@link #getPrefix} and {@link #getPrefixes}.</p>
aoqi@0 483 *
aoqi@0 484 * @return An enumeration of all prefixes declared in this
aoqi@0 485 * context.
aoqi@0 486 * @see #getPrefixes
aoqi@0 487 * @see #getURI
aoqi@0 488 */
aoqi@0 489 public Enumeration getDeclaredPrefixes ()
aoqi@0 490 {
aoqi@0 491 return currentContext.getDeclaredPrefixes();
aoqi@0 492 }
aoqi@0 493
aoqi@0 494 /**
aoqi@0 495 * Controls whether namespace declaration attributes are placed
aoqi@0 496 * into the {@link #NSDECL NSDECL} namespace
aoqi@0 497 * by {@link #processName processName()}. This may only be
aoqi@0 498 * changed before any contexts have been pushed.
aoqi@0 499 *
aoqi@0 500 * @since SAX 2.1alpha
aoqi@0 501 *
aoqi@0 502 * @exception IllegalStateException when attempting to set this
aoqi@0 503 * after any context has been pushed.
aoqi@0 504 */
aoqi@0 505 public void setNamespaceDeclUris (boolean value)
aoqi@0 506 {
aoqi@0 507 if (contextPos != 0)
aoqi@0 508 throw new IllegalStateException ();
aoqi@0 509 if (value == namespaceDeclUris)
aoqi@0 510 return;
aoqi@0 511 namespaceDeclUris = value;
aoqi@0 512 if (value)
aoqi@0 513 currentContext.declarePrefix ("xmlns", NSDECL);
aoqi@0 514 else {
aoqi@0 515 contexts[contextPos] = currentContext = new Context();
aoqi@0 516 currentContext.declarePrefix("xml", XMLNS);
aoqi@0 517 }
aoqi@0 518 }
aoqi@0 519
aoqi@0 520 /**
aoqi@0 521 * Returns true if namespace declaration attributes are placed into
aoqi@0 522 * a namespace. This behavior is not the default.
aoqi@0 523 *
aoqi@0 524 * @since SAX 2.1alpha
aoqi@0 525 */
aoqi@0 526 public boolean isNamespaceDeclUris ()
aoqi@0 527 { return namespaceDeclUris; }
aoqi@0 528
aoqi@0 529
aoqi@0 530
aoqi@0 531 ////////////////////////////////////////////////////////////////////
aoqi@0 532 // Internal state.
aoqi@0 533 ////////////////////////////////////////////////////////////////////
aoqi@0 534
aoqi@0 535 private Context contexts[];
aoqi@0 536 private Context currentContext;
aoqi@0 537 private int contextPos;
aoqi@0 538 private boolean namespaceDeclUris;
aoqi@0 539
aoqi@0 540
aoqi@0 541 ////////////////////////////////////////////////////////////////////
aoqi@0 542 // Internal classes.
aoqi@0 543 ////////////////////////////////////////////////////////////////////
aoqi@0 544
aoqi@0 545 /**
aoqi@0 546 * Internal class for a single Namespace context.
aoqi@0 547 *
aoqi@0 548 * <p>This module caches and reuses Namespace contexts,
aoqi@0 549 * so the number allocated
aoqi@0 550 * will be equal to the element depth of the document, not to the total
aoqi@0 551 * number of elements (i.e. 5-10 rather than tens of thousands).
aoqi@0 552 * Also, data structures used to represent contexts are shared when
aoqi@0 553 * possible (child contexts without declarations) to further reduce
aoqi@0 554 * the amount of memory that's consumed.
aoqi@0 555 * </p>
aoqi@0 556 */
aoqi@0 557 final class Context {
aoqi@0 558
aoqi@0 559 /**
aoqi@0 560 * Create the root-level Namespace context.
aoqi@0 561 */
aoqi@0 562 Context ()
aoqi@0 563 {
aoqi@0 564 copyTables();
aoqi@0 565 }
aoqi@0 566
aoqi@0 567
aoqi@0 568 /**
aoqi@0 569 * (Re)set the parent of this Namespace context.
aoqi@0 570 * The context must either have been freshly constructed,
aoqi@0 571 * or must have been cleared.
aoqi@0 572 *
aoqi@0 573 * @param context The parent Namespace context object.
aoqi@0 574 */
aoqi@0 575 void setParent (Context parent)
aoqi@0 576 {
aoqi@0 577 this.parent = parent;
aoqi@0 578 declarations = null;
aoqi@0 579 prefixTable = parent.prefixTable;
aoqi@0 580 uriTable = parent.uriTable;
aoqi@0 581 elementNameTable = parent.elementNameTable;
aoqi@0 582 attributeNameTable = parent.attributeNameTable;
aoqi@0 583 defaultNS = parent.defaultNS;
aoqi@0 584 declSeen = false;
aoqi@0 585 }
aoqi@0 586
aoqi@0 587 /**
aoqi@0 588 * Makes associated state become collectible,
aoqi@0 589 * invalidating this context.
aoqi@0 590 * {@link #setParent} must be called before
aoqi@0 591 * this context may be used again.
aoqi@0 592 */
aoqi@0 593 void clear ()
aoqi@0 594 {
aoqi@0 595 parent = null;
aoqi@0 596 prefixTable = null;
aoqi@0 597 uriTable = null;
aoqi@0 598 elementNameTable = null;
aoqi@0 599 attributeNameTable = null;
aoqi@0 600 defaultNS = "";
aoqi@0 601 }
aoqi@0 602
aoqi@0 603
aoqi@0 604 /**
aoqi@0 605 * Declare a Namespace prefix for this context.
aoqi@0 606 *
aoqi@0 607 * @param prefix The prefix to declare.
aoqi@0 608 * @param uri The associated Namespace URI.
aoqi@0 609 * @see org.xml.sax.helpers.NamespaceSupport#declarePrefix
aoqi@0 610 */
aoqi@0 611 void declarePrefix (String prefix, String uri)
aoqi@0 612 {
aoqi@0 613 // Lazy processing...
aoqi@0 614 // if (!declsOK)
aoqi@0 615 // throw new IllegalStateException (
aoqi@0 616 // "can't declare any more prefixes in this context");
aoqi@0 617 if (!declSeen) {
aoqi@0 618 copyTables();
aoqi@0 619 }
aoqi@0 620 if (declarations == null) {
aoqi@0 621 declarations = new Vector();
aoqi@0 622 }
aoqi@0 623
aoqi@0 624 prefix = prefix.intern();
aoqi@0 625 uri = uri.intern();
aoqi@0 626 if ("".equals(prefix)) {
aoqi@0 627 defaultNS = uri;
aoqi@0 628 } else {
aoqi@0 629 prefixTable.put(prefix, uri);
aoqi@0 630 uriTable.put(uri, prefix); // may wipe out another prefix
aoqi@0 631 }
aoqi@0 632 declarations.addElement(prefix);
aoqi@0 633 }
aoqi@0 634
aoqi@0 635
aoqi@0 636 /**
aoqi@0 637 * Process an XML qualified name in this context.
aoqi@0 638 *
aoqi@0 639 * @param qName The XML qualified name.
aoqi@0 640 * @param isAttribute true if this is an attribute name.
aoqi@0 641 * @return An array of three strings containing the
aoqi@0 642 * URI part (or empty string), the local part,
aoqi@0 643 * and the raw name, all internalized, or null
aoqi@0 644 * if there is an undeclared prefix.
aoqi@0 645 * @see org.xml.sax.helpers.NamespaceSupport#processName
aoqi@0 646 */
aoqi@0 647 String [] processName (String qName, boolean isAttribute)
aoqi@0 648 {
aoqi@0 649 String name[];
aoqi@0 650 Hashtable table;
aoqi@0 651
aoqi@0 652 // Select the appropriate table.
aoqi@0 653 if (isAttribute) {
aoqi@0 654 table = attributeNameTable;
aoqi@0 655 } else {
aoqi@0 656 table = elementNameTable;
aoqi@0 657 }
aoqi@0 658
aoqi@0 659 // Start by looking in the cache, and
aoqi@0 660 // return immediately if the name
aoqi@0 661 // is already known in this content
aoqi@0 662 name = (String[])table.get(qName);
aoqi@0 663 if (name != null) {
aoqi@0 664 return name;
aoqi@0 665 }
aoqi@0 666
aoqi@0 667 // We haven't seen this name in this
aoqi@0 668 // context before. Maybe in the parent
aoqi@0 669 // context, but we can't assume prefix
aoqi@0 670 // bindings are the same.
aoqi@0 671 name = new String[3];
aoqi@0 672 name[2] = qName.intern();
aoqi@0 673 int index = qName.indexOf(':');
aoqi@0 674
aoqi@0 675
aoqi@0 676 // No prefix.
aoqi@0 677 if (index == -1) {
aoqi@0 678 if (isAttribute) {
aoqi@0 679 if (qName == "xmlns" && namespaceDeclUris)
aoqi@0 680 name[0] = NSDECL;
aoqi@0 681 else
aoqi@0 682 name[0] = "";
aoqi@0 683 } else {
aoqi@0 684 name[0] = defaultNS;
aoqi@0 685 }
aoqi@0 686 name[1] = name[2];
aoqi@0 687 }
aoqi@0 688
aoqi@0 689 // Prefix
aoqi@0 690 else {
aoqi@0 691 String prefix = qName.substring(0, index);
aoqi@0 692 String local = qName.substring(index+1);
aoqi@0 693 String uri;
aoqi@0 694 if ("".equals(prefix)) {
aoqi@0 695 uri = defaultNS;
aoqi@0 696 } else {
aoqi@0 697 uri = (String)prefixTable.get(prefix);
aoqi@0 698 }
aoqi@0 699 if (uri == null
aoqi@0 700 || (!isAttribute && "xmlns".equals (prefix))) {
aoqi@0 701 return null;
aoqi@0 702 }
aoqi@0 703 name[0] = uri;
aoqi@0 704 name[1] = local.intern();
aoqi@0 705 }
aoqi@0 706
aoqi@0 707 // Save in the cache for future use.
aoqi@0 708 // (Could be shared with parent context...)
aoqi@0 709 table.put(name[2], name);
aoqi@0 710 return name;
aoqi@0 711 }
aoqi@0 712
aoqi@0 713
aoqi@0 714 /**
aoqi@0 715 * Look up the URI associated with a prefix in this context.
aoqi@0 716 *
aoqi@0 717 * @param prefix The prefix to look up.
aoqi@0 718 * @return The associated Namespace URI, or null if none is
aoqi@0 719 * declared.
aoqi@0 720 * @see org.xml.sax.helpers.NamespaceSupport#getURI
aoqi@0 721 */
aoqi@0 722 String getURI (String prefix)
aoqi@0 723 {
aoqi@0 724 if ("".equals(prefix)) {
aoqi@0 725 return defaultNS;
aoqi@0 726 } else if (prefixTable == null) {
aoqi@0 727 return null;
aoqi@0 728 } else {
aoqi@0 729 return (String)prefixTable.get(prefix);
aoqi@0 730 }
aoqi@0 731 }
aoqi@0 732
aoqi@0 733
aoqi@0 734 /**
aoqi@0 735 * Look up one of the prefixes associated with a URI in this context.
aoqi@0 736 *
aoqi@0 737 * <p>Since many prefixes may be mapped to the same URI,
aoqi@0 738 * the return value may be unreliable.</p>
aoqi@0 739 *
aoqi@0 740 * @param uri The URI to look up.
aoqi@0 741 * @return The associated prefix, or null if none is declared.
aoqi@0 742 * @see org.xml.sax.helpers.NamespaceSupport#getPrefix
aoqi@0 743 */
aoqi@0 744 String getPrefix (String uri) {
aoqi@0 745 if (uriTable != null) {
aoqi@0 746 String uriPrefix = (String)uriTable.get(uri);
aoqi@0 747 if (uriPrefix == null) return null;
aoqi@0 748 String verifyNamespace = (String) prefixTable.get(uriPrefix);
aoqi@0 749 if (uri.equals(verifyNamespace)) {
aoqi@0 750 return uriPrefix;
aoqi@0 751 }
aoqi@0 752 }
aoqi@0 753 return null;
aoqi@0 754 }
aoqi@0 755
aoqi@0 756
aoqi@0 757 /**
aoqi@0 758 * Return an enumeration of prefixes declared in this context.
aoqi@0 759 *
aoqi@0 760 * @return An enumeration of prefixes (possibly empty).
aoqi@0 761 * @see org.xml.sax.helpers.NamespaceSupport#getDeclaredPrefixes
aoqi@0 762 */
aoqi@0 763 Enumeration getDeclaredPrefixes ()
aoqi@0 764 {
aoqi@0 765 if (declarations == null) {
aoqi@0 766 return EMPTY_ENUMERATION;
aoqi@0 767 } else {
aoqi@0 768 return declarations.elements();
aoqi@0 769 }
aoqi@0 770 }
aoqi@0 771
aoqi@0 772
aoqi@0 773 /**
aoqi@0 774 * Return an enumeration of all prefixes currently in force.
aoqi@0 775 *
aoqi@0 776 * <p>The default prefix, if in force, is <em>not</em>
aoqi@0 777 * returned, and will have to be checked for separately.</p>
aoqi@0 778 *
aoqi@0 779 * @return An enumeration of prefixes (never empty).
aoqi@0 780 * @see org.xml.sax.helpers.NamespaceSupport#getPrefixes
aoqi@0 781 */
aoqi@0 782 Enumeration getPrefixes ()
aoqi@0 783 {
aoqi@0 784 if (prefixTable == null) {
aoqi@0 785 return EMPTY_ENUMERATION;
aoqi@0 786 } else {
aoqi@0 787 return prefixTable.keys();
aoqi@0 788 }
aoqi@0 789 }
aoqi@0 790
aoqi@0 791
aoqi@0 792
aoqi@0 793 ////////////////////////////////////////////////////////////////
aoqi@0 794 // Internal methods.
aoqi@0 795 ////////////////////////////////////////////////////////////////
aoqi@0 796
aoqi@0 797
aoqi@0 798 /**
aoqi@0 799 * Copy on write for the internal tables in this context.
aoqi@0 800 *
aoqi@0 801 * <p>This class is optimized for the normal case where most
aoqi@0 802 * elements do not contain Namespace declarations.</p>
aoqi@0 803 */
aoqi@0 804 private void copyTables ()
aoqi@0 805 {
aoqi@0 806 if (prefixTable != null) {
aoqi@0 807 prefixTable = (Hashtable)prefixTable.clone();
aoqi@0 808 } else {
aoqi@0 809 prefixTable = new Hashtable();
aoqi@0 810 }
aoqi@0 811 if (uriTable != null) {
aoqi@0 812 uriTable = (Hashtable)uriTable.clone();
aoqi@0 813 } else {
aoqi@0 814 uriTable = new Hashtable();
aoqi@0 815 }
aoqi@0 816 elementNameTable = new Hashtable();
aoqi@0 817 attributeNameTable = new Hashtable();
aoqi@0 818 declSeen = true;
aoqi@0 819 }
aoqi@0 820
aoqi@0 821
aoqi@0 822
aoqi@0 823 ////////////////////////////////////////////////////////////////
aoqi@0 824 // Protected state.
aoqi@0 825 ////////////////////////////////////////////////////////////////
aoqi@0 826
aoqi@0 827 Hashtable prefixTable;
aoqi@0 828 Hashtable uriTable;
aoqi@0 829 Hashtable elementNameTable;
aoqi@0 830 Hashtable attributeNameTable;
aoqi@0 831 String defaultNS = "";
aoqi@0 832
aoqi@0 833
aoqi@0 834
aoqi@0 835 ////////////////////////////////////////////////////////////////
aoqi@0 836 // Internal state.
aoqi@0 837 ////////////////////////////////////////////////////////////////
aoqi@0 838
aoqi@0 839 private Vector declarations = null;
aoqi@0 840 private boolean declSeen = false;
aoqi@0 841 private Context parent = null;
aoqi@0 842 }
aoqi@0 843 }
aoqi@0 844
aoqi@0 845 // end of NamespaceSupport.java

mercurial