KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > Namespaces


1 /* Copyright 2002-2004 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@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 JavaDoc;
25 import java.util.HashMap JavaDoc;
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.0
38  */

39 class Namespaces {
40     
41     private HashMap JavaDoc namespaces = new HashMap JavaDoc(1);
42     private ArrayList JavaDoc prefixes = new ArrayList JavaDoc(1);
43     
44
45     void put(String JavaDoc prefix, String JavaDoc URI) {
46         namespaces.put(prefix, URI);
47         prefixes.remove(prefix);
48         prefixes.add(prefix);
49     }
50
51     
52     void remove(String JavaDoc 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      */

72     String JavaDoc getURI(String JavaDoc prefix) {
73         return (String JavaDoc) (namespaces.get(prefix));
74     }
75     
76     
77     // This violates encapsulation. Don't change the
78
// array returned.
79
ArrayList JavaDoc getPrefixes() {
80         return this.prefixes;
81     }
82     
83     
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 JavaDoc) this.namespaces.clone();
90         result.prefixes = (ArrayList JavaDoc) this.prefixes.clone();
91         return result;
92         
93     }
94
95 }
Popular Tags