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