1 /* Copyright 2002-2006 Elliotte Rusty Harold
2 
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6 
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    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@ibiblio.org. 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.2d1
38  */
39 class Namespaces {
40 
41     private HashMap   namespaces = new HashMap(1);
42     private ArrayList prefixes   = new ArrayList(1);
43 
44 
put(String prefix, String URI)45     void put(String prefix, String URI) {
46         namespaces.put(prefix, URI);
47         prefixes.remove(prefix);
48         prefixes.add(prefix);
49     }
50 
51 
remove(String prefix)52     void remove(String prefix) {
53         if (prefix == null) prefix = "";
54         namespaces.remove(prefix);
55         prefixes.remove(prefix);
56     }
57 
58 
59     /**
60      * <p>
61      * Return the URI associated with a prefix, as determined
62      * by the namespaces stored in this list.  This method
63      * returns null if the prefix is not found in the list.
64      * </p>
65      *
66      * @param prefix the prefix whose URI is desired
67      *
68      * @return the namespace URI for this prefix, or null if this
69      *      prefix is not not mapped to a URI by these namespace
70      *      declarations
71      */
getURI(String prefix)72     String getURI(String prefix) {
73         return (String) (namespaces.get(prefix));
74     }
75 
76 
77     // This violates encapsulation. Don't change the
78     // array returned.
getPrefixes()79     ArrayList getPrefixes() {
80         return this.prefixes;
81     }
82 
83 
copy()84     Namespaces copy() {
85 
86         Namespaces result = new Namespaces();
87         // shallow copies work here because these collections only
88         // contain immutable strings
89         result.namespaces = (HashMap)   this.namespaces.clone();
90         result.prefixes   = (ArrayList) this.prefixes.clone();
91         return result;
92 
93     }
94 
95 
size()96     int size() {
97         return prefixes.size();
98     }
99 
100 
getPrefix(int i)101     String getPrefix(int i) {
102         return (String) prefixes.get(i);
103     }
104 
105 }