01: /* Copyright 2002-2005 Elliotte Rusty Harold
02:
03: This library is free software; you can redistribute it and/or modify
04: it under the terms of version 2.1 of the GNU Lesser General Public
05: License as published by the Free Software Foundation.
06:
07: This library is distributed in the hope that it will be useful,
08: but WITHOUT ANY WARRANTY; without even the implied warranty of
09: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: GNU Lesser General Public License for more details.
11:
12: You should have received a copy of the GNU Lesser General Public
13: License along with this library; if not, write to the
14: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15: Boston, MA 02111-1307 USA
16:
17: You can contact Elliotte Rusty Harold by sending e-mail to
18: elharo@metalab.unc.edu. Please include the word "XOM" in the
19: subject line. The XOM home page is located at http://www.xom.nu/
20: */
21:
22: package nu.xom;
23:
24: import java.util.ArrayList;
25: import java.util.HashMap;
26:
27: /**
28: * <p>
29: * The <code>Namespaces</code> container is a read-only list
30: * used to hold the additional namespace declarations of an
31: * element. It provides indexed access for convenience,
32: * but the order is neither predictable nor reproducible,
33: * and has no meaning.
34: * </p>
35: *
36: * @author Elliotte Rusty Harold
37: * @version 1.1b3
38: */
39: class Namespaces {
40:
41: private HashMap namespaces = new HashMap(1);
42: private ArrayList prefixes = new ArrayList(1);
43:
44: void put(String prefix, String URI) {
45: namespaces.put(prefix, URI);
46: prefixes.remove(prefix);
47: prefixes.add(prefix);
48: }
49:
50: void remove(String prefix) {
51: if (prefix == null)
52: prefix = "";
53: namespaces.remove(prefix);
54: prefixes.remove(prefix);
55: }
56:
57: /**
58: * <p>
59: * Return the URI associated with a prefix, as determined
60: * by the namespaces stored in this list. This method
61: * returns null if the prefix is not found in the list.
62: * </p>
63: *
64: * @param prefix the prefix whose URI is desired
65: *
66: * @return the namespace URI for this prefix, or null if this
67: * prefix is not not mapped to a URI by these namespace
68: * declarations
69: */
70: String getURI(String prefix) {
71: return (String) (namespaces.get(prefix));
72: }
73:
74: // This violates encapsulation. Don't change the
75: // array returned.
76: ArrayList getPrefixes() {
77: return this .prefixes;
78: }
79:
80: Namespaces copy() {
81:
82: Namespaces result = new Namespaces();
83: // shallow copies work here because these collections only
84: // contain immutable strings
85: result.namespaces = (HashMap) this .namespaces.clone();
86: result.prefixes = (ArrayList) this .prefixes.clone();
87: return result;
88:
89: }
90:
91: int size() {
92: return prefixes.size();
93: }
94:
95: String getPrefix(int i) {
96: return (String) prefixes.get(i);
97: }
98:
99: }
|