annotate NGSrich_0.5.5/src/org/jdom/Namespace.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 /*--
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 $Id: Namespace.java,v 1.44 2008/12/17 23:22:48 jhunter Exp $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 All rights reserved.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 Redistribution and use in source and binary forms, with or without
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 modification, are permitted provided that the following conditions
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 are met:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 1. Redistributions of source code must retain the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 notice, this list of conditions, and the following disclaimer.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 2. Redistributions in binary form must reproduce the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 notice, this list of conditions, and the disclaimer that follows
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 these conditions in the documentation and/or other materials
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 provided with the distribution.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 3. The name "JDOM" must not be used to endorse or promote products
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 derived from this software without prior written permission. For
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 written permission, please contact <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 4. Products derived from this software may not be called "JDOM", nor
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 may "JDOM" appear in their name, without prior written permission
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 In addition, we request (but do not require) that you include in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 end-user documentation provided with the redistribution and/or in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 software itself an acknowledgement equivalent to the following:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 "This product includes software developed by the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 JDOM Project (http://www.jdom.org/)."
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 Alternatively, the acknowledgment may be graphical using the logos
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 available at http://www.jdom.org/images/logos.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 SUCH DAMAGE.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 This software consists of voluntary contributions made by many
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 individuals on behalf of the JDOM Project and was originally
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 on the JDOM Project, please see <http://www.jdom.org/>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 package org.jdom;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 import java.util.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 * An XML namespace representation, as well as a factory for creating XML
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 * namespace objects. Namespaces are not Serializable, however objects that use
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 * namespaces have special logic to handle serialization manually. These classes
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 * call the getNamespace() method on deserialization to ensure there is one
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 * unique Namespace object for any unique prefix/uri pair.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 * @version $Revision: 1.44 $, $Date: 2008/12/17 23:22:48 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 * @author Brett McLaughlin
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 * @author Elliotte Rusty Harold
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 * @author Jason Hunter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 * @author Wesley Biggs
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 public final class Namespace {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76 // XXX May want to use weak references to keep the maps from growing
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 // large with extended use
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 private static final String CVS_ID =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 "@(#) $RCSfile: Namespace.java,v $ $Revision: 1.44 $ $Date: 2008/12/17 23:22:48 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 * Factory list of namespaces.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 * Keys are <i>prefix</i>&amp;<i>URI</i>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 * Values are Namespace objects
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 private static HashMap namespaces;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89 /** Define a <code>Namespace</code> for when <i>not</i> in a namespace */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 public static final Namespace NO_NAMESPACE = new Namespace("", "");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92 /** Define a <code>Namespace</code> for the standard xml prefix. */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 public static final Namespace XML_NAMESPACE =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 new Namespace("xml", "http://www.w3.org/XML/1998/namespace");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 /** The prefix mapped to this namespace */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 private String prefix;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 /** The URI for this namespace */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 private String uri;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 * This static initializer acts as a factory contructor.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 * It sets up storage and required initial values.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 static {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 namespaces = new HashMap(16);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 // Add the "empty" namespace
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 namespaces.put(new NamespaceKey(NO_NAMESPACE), NO_NAMESPACE);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 namespaces.put(new NamespaceKey(XML_NAMESPACE), XML_NAMESPACE);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 * This will retrieve (if in existence) or create (if not) a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 * <code>Namespace</code> for the supplied prefix and URI.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 * @param prefix <code>String</code> prefix to map to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 * <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 * @param uri <code>String</code> URI of new <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 * @return <code>Namespace</code> - ready to use namespace.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 * @throws IllegalNameException if the given prefix and uri make up
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 * an illegal namespace name.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 public static Namespace getNamespace(String prefix, String uri) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 // Sanity checking
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 if ((prefix == null) || (prefix.trim().equals(""))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 // Short-cut out for common case of no namespace
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 if ((uri == null) || (uri.trim().equals(""))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 return NO_NAMESPACE;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 prefix = "";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 else if ((uri == null) || (uri.trim().equals(""))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 uri = "";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138 // Return existing namespace if found. The preexisting namespaces
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 // should all be legal. In other words, an illegal namespace won't
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 // have been placed in this. Thus we can do this test before
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 // verifying the URI and prefix.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 NamespaceKey lookup = new NamespaceKey(prefix, uri);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 Namespace preexisting;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 synchronized (namespaces) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 preexisting = (Namespace) namespaces.get(lookup);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 if (preexisting != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 return preexisting;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 // Ensure proper naming
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 String reason;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 if ((reason = Verifier.checkNamespacePrefix(prefix)) != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 throw new IllegalNameException(prefix, "Namespace prefix", reason);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 if ((reason = Verifier.checkNamespaceURI(uri)) != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 throw new IllegalNameException(uri, "Namespace URI", reason);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 // Unless the "empty" Namespace (no prefix and no URI), require a URI
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 if ((!prefix.equals("")) && (uri.equals(""))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 throw new IllegalNameException("", "namespace",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 "Namespace URIs must be non-null and non-empty Strings");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 // Handle XML namespace mislabels. If the user requested the correct
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 // namespace and prefix -- xml, http://www.w3.org/XML/1998/namespace
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168 // -- then it was already returned from the preexisting namespaces.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 // Thus any use of the xml prefix or the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 // http://www.w3.org/XML/1998/namespace URI at this point must be
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171 // incorrect.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 if (prefix.equals("xml")) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 throw new IllegalNameException(prefix, "Namespace prefix",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 "The xml prefix can only be bound to " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 "http://www.w3.org/XML/1998/namespace");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 // The erratum to Namespaces in XML 1.0 that suggests this
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 // next check is controversial. Not everyone accepts it.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 if (uri.equals("http://www.w3.org/XML/1998/namespace")) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 throw new IllegalNameException(uri, "Namespace URI",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 "The http://www.w3.org/XML/1998/namespace must be bound to " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 "the xml prefix.");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 // Finally, store and return
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 Namespace ns = new Namespace(prefix, uri);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 synchronized (namespaces) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 namespaces.put(lookup, ns);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 return ns;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 * This will retrieve (if in existence) or create (if not) a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196 * <code>Namespace</code> for the supplied URI, and make it usable
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197 * as a default namespace, as no prefix is supplied.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 * @param uri <code>String</code> URI of new <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200 * @return <code>Namespace</code> - ready to use namespace.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 public static Namespace getNamespace(String uri) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203 return getNamespace("", uri);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207 * This constructor handles creation of a <code>Namespace</code> object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208 * with a prefix and URI; it is intentionally left <code>private</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 * so that it cannot be invoked by external programs/code.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211 * @param prefix <code>String</code> prefix to map to this namespace.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 * @param uri <code>String</code> URI for namespace.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214 private Namespace(String prefix, String uri) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 this.prefix = prefix;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216 this.uri = uri;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220 * This returns the prefix mapped to this <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222 * @return <code>String</code> - prefix for this <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224 public String getPrefix() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 return prefix;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229 * This returns the namespace URI for this <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231 * @return <code>String</code> - URI for this <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233 public String getURI() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 return uri;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238 * This tests for equality - Two <code>Namespaces</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 * are equal if and only if their URIs are byte-for-byte equals.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 * @param ob <code>Object</code> to compare to this <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242 * @return <code>boolean</code> - whether the supplied object is equal to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243 * this <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 public boolean equals(Object ob) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246 if (this == ob) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 return true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 if (ob instanceof Namespace) { // instanceof returns false if null
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 return uri.equals(((Namespace)ob).uri);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 return false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256 * This returns a <code>String</code> representation of this
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 * <code>Namespace</code>, suitable for use in debugging.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259 * @return <code>String</code> - information about this instance.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 public String toString() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262 return "[Namespace: prefix \"" + prefix + "\" is mapped to URI \"" +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263 uri + "\"]";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267 * This returns a probably unique hash code for the <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 * If two namespaces have the same URI, they are equal and have the same
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269 * hash code, even if they have different prefixes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 * @return <code>int</code> - hash code for this <code>Namespace</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 public int hashCode() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 return uri.hashCode();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276 }