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