1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import org.dom4j.tree.AbstractNode;
11 import org.dom4j.tree.DefaultNamespace;
12 import org.dom4j.tree.NamespaceCache;
13
14 /***
15 * <p>
16 * <code>Namespace</code> is a Flyweight Namespace that can be shared amongst
17 * nodes.
18 * </p>
19 *
20 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
21 * @version $Revision: 1.22 $
22 */
23 public class Namespace extends AbstractNode {
24 /*** Cache of Namespace instances */
25 protected static final NamespaceCache CACHE = new NamespaceCache();
26
27 /*** XML Namespace */
28 public static final Namespace XML_NAMESPACE = CACHE.get("xml",
29 "http://www.w3.org/XML/1998/namespace");
30
31 /*** No Namespace present */
32 public static final Namespace NO_NAMESPACE = CACHE.get("", "");
33
34 /*** The prefix mapped to this namespace */
35 private String prefix;
36
37 /*** The URI for this namespace */
38 private String uri;
39
40 /*** A cached version of the hashcode for efficiency */
41 private int hashCode;
42
43 /***
44 * DOCUMENT ME!
45 *
46 * @param prefix
47 * is the prefix for this namespace
48 * @param uri
49 * is the URI for this namespace
50 */
51 public Namespace(String prefix, String uri) {
52 this.prefix = (prefix != null) ? prefix : "";
53 this.uri = (uri != null) ? uri : "";
54 }
55
56 /***
57 * A helper method to return the Namespace instance for the given prefix and
58 * URI
59 *
60 * @param prefix
61 * DOCUMENT ME!
62 * @param uri
63 * DOCUMENT ME!
64 *
65 * @return an interned Namespace object
66 */
67 public static Namespace get(String prefix, String uri) {
68 return CACHE.get(prefix, uri);
69 }
70
71 /***
72 * A helper method to return the Namespace instance for no prefix and the
73 * URI
74 *
75 * @param uri
76 * DOCUMENT ME!
77 *
78 * @return an interned Namespace object
79 */
80 public static Namespace get(String uri) {
81 return CACHE.get(uri);
82 }
83
84 public short getNodeType() {
85 return NAMESPACE_NODE;
86 }
87
88 /***
89 * DOCUMENT ME!
90 *
91 * @return the hash code based on the qualified name and the URI of the
92 * namespace.
93 */
94 public int hashCode() {
95 if (hashCode == 0) {
96 hashCode = createHashCode();
97 }
98
99 return hashCode;
100 }
101
102 /***
103 * Factory method to create the hashcode allowing derived classes to change
104 * the behaviour
105 *
106 * @return DOCUMENT ME!
107 */
108 protected int createHashCode() {
109 int result = uri.hashCode() ^ prefix.hashCode();
110
111 if (result == 0) {
112 result = 0xbabe;
113 }
114
115 return result;
116 }
117
118 /***
119 * Checks whether this Namespace equals the given Namespace. Two Namespaces
120 * are equals if their URI and prefix are equal.
121 *
122 * @param object
123 * DOCUMENT ME!
124 *
125 * @return DOCUMENT ME!
126 */
127 public boolean equals(Object object) {
128 if (this == object) {
129 return true;
130 } else if (object instanceof Namespace) {
131 Namespace that = (Namespace) object;
132
133
134 if (hashCode() == that.hashCode()) {
135 return uri.equals(that.getURI())
136 && prefix.equals(that.getPrefix());
137 }
138 }
139
140 return false;
141 }
142
143 public String getText() {
144 return uri;
145 }
146
147 public String getStringValue() {
148 return uri;
149 }
150
151 /***
152 * DOCUMENT ME!
153 *
154 * @return the prefix for this <code>Namespace</code>.
155 */
156 public String getPrefix() {
157 return prefix;
158 }
159
160 /***
161 * DOCUMENT ME!
162 *
163 * @return the URI for this <code>Namespace</code>.
164 */
165 public String getURI() {
166 return uri;
167 }
168
169 public String getXPathNameStep() {
170 if ((prefix != null) && !"".equals(prefix)) {
171 return "namespace::" + prefix;
172 }
173
174 return "namespace::*[name()='']";
175 }
176
177 public String getPath(Element context) {
178 StringBuffer path = new StringBuffer(10);
179 Element parent = getParent();
180
181 if ((parent != null) && (parent != context)) {
182 path.append(parent.getPath(context));
183 path.append('/');
184 }
185
186 path.append(getXPathNameStep());
187
188 return path.toString();
189 }
190
191 public String getUniquePath(Element context) {
192 StringBuffer path = new StringBuffer(10);
193 Element parent = getParent();
194
195 if ((parent != null) && (parent != context)) {
196 path.append(parent.getUniquePath(context));
197 path.append('/');
198 }
199
200 path.append(getXPathNameStep());
201
202 return path.toString();
203 }
204
205 public String toString() {
206 return super.toString() + " [Namespace: prefix " + getPrefix()
207 + " mapped to URI \"" + getURI() + "\"]";
208 }
209
210 public String asXML() {
211 StringBuffer asxml = new StringBuffer(10);
212 String pref = getPrefix();
213
214 if ((pref != null) && (pref.length() > 0)) {
215 asxml.append("xmlns:");
216 asxml.append(pref);
217 asxml.append("=\"");
218 } else {
219 asxml.append("xmlns=\"");
220 }
221
222 asxml.append(getURI());
223 asxml.append("\"");
224
225 return asxml.toString();
226 }
227
228 public void accept(Visitor visitor) {
229 visitor.visit(this);
230 }
231
232 protected Node createXPathResult(Element parent) {
233 return new DefaultNamespace(parent, getPrefix(), getURI());
234 }
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272