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