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 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class Namespace extends AbstractNode { |
22 |
| |
23 |
| |
24 |
| protected static final NamespaceCache cache = new NamespaceCache(); |
25 |
| |
26 |
| |
27 |
| public static final Namespace XML_NAMESPACE |
28 |
| = cache.get("xml", "http://www.w3.org/XML/1998/namespace"); |
29 |
| |
30 |
| |
31 |
| public static final Namespace NO_NAMESPACE |
32 |
| = cache.get("", ""); |
33 |
| |
34 |
| |
35 |
| |
36 |
| private String prefix; |
37 |
| |
38 |
| |
39 |
| private String uri; |
40 |
| |
41 |
| |
42 |
| private int hashCode; |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
197688
| public static Namespace get(String prefix, String uri) {
|
51 |
197688
| return cache.get(prefix, uri);
|
52 |
| } |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
20
| public static Namespace get(String uri) {
|
60 |
20
| return cache.get(uri);
|
61 |
| } |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
20996
| public Namespace(String prefix, String uri) {
|
67 |
20996
| this.prefix = (prefix != null) ? prefix : "";
|
68 |
20996
| this.uri = (uri != null) ? uri : "";;
|
69 |
| } |
70 |
| |
71 |
| |
72 |
1080
| public short getNodeType() {
|
73 |
1080
| return NAMESPACE_NODE;
|
74 |
| } |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
82820
| public int hashCode() {
|
80 |
82820
| if ( hashCode == 0 ) {
|
81 |
470
| hashCode = createHashCode();
|
82 |
| } |
83 |
82820
| return hashCode;
|
84 |
| } |
85 |
| |
86 |
| |
87 |
470
| protected int createHashCode() {
|
88 |
470
| int hashCode = uri.hashCode() ^ prefix.hashCode();
|
89 |
470
| if ( hashCode == 0 ) {
|
90 |
12
| hashCode = 0xbabe;
|
91 |
| } |
92 |
470
| return hashCode;
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
30032
| public boolean equals(Object object) {
|
100 |
30032
| if ( this == object ) {
|
101 |
9566
| return true;
|
102 |
| } |
103 |
20466
| else if ( object instanceof Namespace ) {
|
104 |
20406
| Namespace that = (Namespace) object;
|
105 |
| |
106 |
| |
107 |
20406
| if ( hashCode() == that.hashCode() ) {
|
108 |
19450
| return uri.equals( that.getURI() )
|
109 |
| && prefix.equals( that.getPrefix() ); |
110 |
| } |
111 |
| } |
112 |
1016
| return false;
|
113 |
| } |
114 |
| |
115 |
0
| public String getText() {
|
116 |
0
| return uri;
|
117 |
| } |
118 |
| |
119 |
0
| public String getStringValue() {
|
120 |
0
| return uri;
|
121 |
| } |
122 |
| |
123 |
| |
124 |
| |
125 |
131604
| public String getPrefix() {
|
126 |
131604
| return prefix;
|
127 |
| } |
128 |
| |
129 |
| |
130 |
| |
131 |
669518
| public String getURI() {
|
132 |
669518
| return uri;
|
133 |
| } |
134 |
| |
135 |
| |
136 |
24
| public String getXPathNameStep() {
|
137 |
24
| if (prefix != null && !"".equals( prefix )) {
|
138 |
12
| return "namespace::" + prefix;
|
139 |
| } |
140 |
12
| return "namespace::*[name()='']";
|
141 |
| } |
142 |
| |
143 |
12
| public String getPath(Element context) {
|
144 |
12
| StringBuffer path = new StringBuffer(10);
|
145 |
12
| Element parent = getParent();
|
146 |
12
| if (parent != null && parent != context) {
|
147 |
0
| path.append( parent.getPath( context ) );
|
148 |
0
| path.append( '/' );
|
149 |
| } |
150 |
12
| path.append( getXPathNameStep() );
|
151 |
12
| return path.toString();
|
152 |
| } |
153 |
| |
154 |
12
| public String getUniquePath(Element context) {
|
155 |
12
| StringBuffer path = new StringBuffer(10);
|
156 |
12
| Element parent = getParent();
|
157 |
12
| if (parent != null && parent != context) {
|
158 |
0
| path.append( parent.getUniquePath( context ) );
|
159 |
0
| path.append( '/' );
|
160 |
| } |
161 |
12
| path.append( getXPathNameStep() );
|
162 |
12
| return path.toString();
|
163 |
| } |
164 |
| |
165 |
6
| public String toString() {
|
166 |
6
| return super.toString() + " [Namespace: prefix " + getPrefix()
|
167 |
| + " mapped to URI \"" + getURI() + "\"]"; |
168 |
| } |
169 |
| |
170 |
52
| public String asXML() {
|
171 |
52
| StringBuffer asxml = new StringBuffer(10);
|
172 |
52
| String prefix = getPrefix();
|
173 |
52
| if ( prefix != null && prefix.length() > 0 ) {
|
174 |
28
| asxml.append("xmlns:");
|
175 |
28
| asxml.append(prefix);
|
176 |
28
| asxml.append("=\"");
|
177 |
| } |
178 |
| else { |
179 |
24
| asxml.append("xmlns=\"");
|
180 |
| } |
181 |
52
| asxml.append(getURI());
|
182 |
52
| asxml.append("\"");
|
183 |
52
| return asxml.toString();
|
184 |
| } |
185 |
| |
186 |
0
| public void accept(Visitor visitor) {
|
187 |
0
| visitor.visit(this);
|
188 |
| } |
189 |
| |
190 |
152
| protected Node createXPathResult(Element parent) {
|
191 |
152
| 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 |
| |