1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.dom4j.tree; |
9 |
| |
10 |
| import java.util.ArrayList; |
11 |
| import java.util.HashMap; |
12 |
| import java.util.Map; |
13 |
| |
14 |
| import org.dom4j.DocumentFactory; |
15 |
| import org.dom4j.Namespace; |
16 |
| import org.dom4j.QName; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class NamespaceStack { |
27 |
| |
28 |
| private DocumentFactory documentFactory; |
29 |
| |
30 |
| |
31 |
| private ArrayList namespaceStack = new ArrayList(); |
32 |
| |
33 |
| |
34 |
| private ArrayList namespaceCacheList = new ArrayList(); |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| private Map currentNamespaceCache; |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| private Map rootNamespaceCache = new HashMap(); |
47 |
| |
48 |
| |
49 |
| private Namespace defaultNamespace; |
50 |
| |
51 |
2198
| public NamespaceStack() {
|
52 |
2198
| this.documentFactory = DocumentFactory.getInstance();
|
53 |
| } |
54 |
| |
55 |
5831
| public NamespaceStack(DocumentFactory documentFactory) {
|
56 |
5831
| this.documentFactory = documentFactory;
|
57 |
| } |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
8825
| public void push(Namespace namespace) {
|
67 |
8825
| namespaceStack.add(namespace);
|
68 |
8825
| namespaceCacheList.add(null);
|
69 |
8825
| currentNamespaceCache = null;
|
70 |
| |
71 |
8825
| String prefix = namespace.getPrefix();
|
72 |
| |
73 |
8825
| if ((prefix == null) || (prefix.length() == 0)) {
|
74 |
2374
| defaultNamespace = namespace;
|
75 |
| } |
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
744
| public Namespace pop() {
|
84 |
744
| return remove(namespaceStack.size() - 1);
|
85 |
| } |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
163293
| public int size() {
|
93 |
163293
| return namespaceStack.size();
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
11678
| public void clear() {
|
100 |
11678
| namespaceStack.clear();
|
101 |
11678
| namespaceCacheList.clear();
|
102 |
11678
| rootNamespaceCache.clear();
|
103 |
11678
| currentNamespaceCache = null;
|
104 |
| } |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
5884
| public Namespace getNamespace(int index) {
|
115 |
5884
| return (Namespace) namespaceStack.get(index);
|
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
2140
| public Namespace getNamespaceForPrefix(String prefix) {
|
128 |
2140
| if (prefix == null) {
|
129 |
0
| prefix = "";
|
130 |
| } |
131 |
| |
132 |
2140
| for (int i = namespaceStack.size() - 1; i >= 0; i--) {
|
133 |
2767
| Namespace namespace = (Namespace) namespaceStack.get(i);
|
134 |
| |
135 |
2767
| if (prefix.equals(namespace.getPrefix())) {
|
136 |
1532
| return namespace;
|
137 |
| } |
138 |
| } |
139 |
| |
140 |
608
| return null;
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
14
| public String getURI(String prefix) {
|
152 |
14
| Namespace namespace = getNamespaceForPrefix(prefix);
|
153 |
| |
154 |
14
| return (namespace != null) ? namespace.getURI() : null;
|
155 |
| } |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
20698
| public boolean contains(Namespace namespace) {
|
166 |
20698
| String prefix = namespace.getPrefix();
|
167 |
20698
| Namespace current = null;
|
168 |
| |
169 |
20698
| if ((prefix == null) || (prefix.length() == 0)) {
|
170 |
18575
| current = getDefaultNamespace();
|
171 |
| } else { |
172 |
2123
| current = getNamespaceForPrefix(prefix);
|
173 |
| } |
174 |
| |
175 |
20698
| if (current == null) {
|
176 |
622
| return false;
|
177 |
| } |
178 |
| |
179 |
20076
| if (current == namespace) {
|
180 |
19967
| return true;
|
181 |
| } |
182 |
| |
183 |
109
| return namespace.getURI().equals(current.getURI());
|
184 |
| } |
185 |
| |
186 |
94617
| public QName getQName(String namespaceURI, String localName,
|
187 |
| String qualifiedName) { |
188 |
94577
| if (localName == null) {
|
189 |
11
| localName = qualifiedName;
|
190 |
94606
| } else if (qualifiedName == null) {
|
191 |
0
| qualifiedName = localName;
|
192 |
| } |
193 |
| |
194 |
94617
| if (namespaceURI == null) {
|
195 |
7455
| namespaceURI = "";
|
196 |
| } |
197 |
| |
198 |
94577
| String prefix = "";
|
199 |
94617
| int index = qualifiedName.indexOf(":");
|
200 |
| |
201 |
94617
| if (index > 0) {
|
202 |
13475
| prefix = qualifiedName.substring(0, index);
|
203 |
| |
204 |
13475
| if (localName.trim().length() == 0) {
|
205 |
0
| localName = qualifiedName.substring(index + 1);
|
206 |
| } |
207 |
81142
| } else if (localName.trim().length() == 0) {
|
208 |
0
| localName = qualifiedName;
|
209 |
| } |
210 |
| |
211 |
94617
| Namespace namespace = createNamespace(prefix, namespaceURI);
|
212 |
| |
213 |
94617
| return pushQName(localName, qualifiedName, namespace, prefix);
|
214 |
| } |
215 |
| |
216 |
10455
| public QName getAttributeQName(String namespaceURI, String localName,
|
217 |
| String qualifiedName) { |
218 |
10455
| if (qualifiedName == null) {
|
219 |
0
| qualifiedName = localName;
|
220 |
| } |
221 |
| |
222 |
10455
| Map map = getNamespaceCache();
|
223 |
10455
| QName answer = (QName) map.get(qualifiedName);
|
224 |
| |
225 |
10455
| if (answer != null) {
|
226 |
9424
| return answer;
|
227 |
| } |
228 |
| |
229 |
1031
| if (localName == null) {
|
230 |
0
| localName = qualifiedName;
|
231 |
| } |
232 |
| |
233 |
1031
| if (namespaceURI == null) {
|
234 |
0
| namespaceURI = "";
|
235 |
| } |
236 |
| |
237 |
1031
| Namespace namespace = null;
|
238 |
1031
| String prefix = "";
|
239 |
1031
| int index = qualifiedName.indexOf(":");
|
240 |
| |
241 |
1031
| if (index > 0) {
|
242 |
99
| prefix = qualifiedName.substring(0, index);
|
243 |
99
| namespace = createNamespace(prefix, namespaceURI);
|
244 |
| |
245 |
99
| if (localName.trim().length() == 0) {
|
246 |
0
| localName = qualifiedName.substring(index + 1);
|
247 |
| } |
248 |
| } else { |
249 |
| |
250 |
932
| namespace = Namespace.NO_NAMESPACE;
|
251 |
| |
252 |
932
| if (localName.trim().length() == 0) {
|
253 |
0
| localName = qualifiedName;
|
254 |
| } |
255 |
| } |
256 |
| |
257 |
1031
| answer = pushQName(localName, qualifiedName, namespace, prefix);
|
258 |
1031
| map.put(qualifiedName, answer);
|
259 |
| |
260 |
1031
| return answer;
|
261 |
| } |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
5886
| public void push(String prefix, String uri) {
|
272 |
5886
| if (uri == null) {
|
273 |
0
| uri = "";
|
274 |
| } |
275 |
| |
276 |
5886
| Namespace namespace = createNamespace(prefix, uri);
|
277 |
5886
| push(namespace);
|
278 |
| } |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
52
| public Namespace addNamespace(String prefix, String uri) {
|
291 |
52
| Namespace namespace = createNamespace(prefix, uri);
|
292 |
52
| push(namespace);
|
293 |
| |
294 |
52
| return namespace;
|
295 |
| } |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
5884
| public Namespace pop(String prefix) {
|
306 |
5884
| if (prefix == null) {
|
307 |
0
| prefix = "";
|
308 |
| } |
309 |
| |
310 |
5884
| Namespace namespace = null;
|
311 |
| |
312 |
5986
| for (int i = namespaceStack.size() - 1; i >= 0; i--) {
|
313 |
5986
| Namespace ns = (Namespace) namespaceStack.get(i);
|
314 |
| |
315 |
5986
| if (prefix.equals(ns.getPrefix())) {
|
316 |
5884
| remove(i);
|
317 |
5884
| namespace = ns;
|
318 |
| |
319 |
5884
| break;
|
320 |
| } |
321 |
| } |
322 |
| |
323 |
5884
| if (namespace == null) {
|
324 |
0
| System.out.println("Warning: missing namespace prefix ignored: "
|
325 |
| + prefix); |
326 |
| } |
327 |
| |
328 |
5884
| return namespace;
|
329 |
| } |
330 |
| |
331 |
0
| public String toString() {
|
332 |
0
| return super.toString() + " Stack: " + namespaceStack.toString();
|
333 |
| } |
334 |
| |
335 |
0
| public DocumentFactory getDocumentFactory() {
|
336 |
0
| return documentFactory;
|
337 |
| } |
338 |
| |
339 |
0
| public void setDocumentFactory(DocumentFactory documentFactory) {
|
340 |
0
| this.documentFactory = documentFactory;
|
341 |
| } |
342 |
| |
343 |
18576
| public Namespace getDefaultNamespace() {
|
344 |
18576
| if (defaultNamespace == null) {
|
345 |
46
| defaultNamespace = findDefaultNamespace();
|
346 |
| } |
347 |
| |
348 |
18576
| return defaultNamespace;
|
349 |
| } |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
| |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
95648
| protected QName pushQName(String localName, String qualifiedName,
|
369 |
| Namespace namespace, String prefix) { |
370 |
95648
| if ((prefix == null) || (prefix.length() == 0)) {
|
371 |
82074
| this.defaultNamespace = null;
|
372 |
| } |
373 |
| |
374 |
95648
| return createQName(localName, qualifiedName, namespace);
|
375 |
| } |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
| |
386 |
| |
387 |
| |
388 |
| |
389 |
| |
390 |
95648
| protected QName createQName(String localName, String qualifiedName,
|
391 |
| Namespace namespace) { |
392 |
95648
| return documentFactory.createQName(localName, namespace);
|
393 |
| } |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
| |
399 |
| |
400 |
| |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
100654
| protected Namespace createNamespace(String prefix, String namespaceURI) {
|
407 |
100654
| return documentFactory.createNamespace(prefix, namespaceURI);
|
408 |
| } |
409 |
| |
410 |
| |
411 |
| |
412 |
| |
413 |
| |
414 |
| |
415 |
| |
416 |
46
| protected Namespace findDefaultNamespace() {
|
417 |
46
| for (int i = namespaceStack.size() - 1; i >= 0; i--) {
|
418 |
91
| Namespace namespace = (Namespace) namespaceStack.get(i);
|
419 |
| |
420 |
91
| if (namespace != null) {
|
421 |
91
| String prefix = namespace.getPrefix();
|
422 |
| |
423 |
91
| if ((prefix == null) || (namespace.getPrefix().length() == 0)) {
|
424 |
30
| return namespace;
|
425 |
| } |
426 |
| } |
427 |
| } |
428 |
| |
429 |
16
| return null;
|
430 |
| } |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
6628
| protected Namespace remove(int index) {
|
441 |
6628
| Namespace namespace = (Namespace) namespaceStack.remove(index);
|
442 |
6628
| namespaceCacheList.remove(index);
|
443 |
6628
| defaultNamespace = null;
|
444 |
6628
| currentNamespaceCache = null;
|
445 |
| |
446 |
6628
| return namespace;
|
447 |
| } |
448 |
| |
449 |
10455
| protected Map getNamespaceCache() {
|
450 |
10455
| if (currentNamespaceCache == null) {
|
451 |
320
| int index = namespaceStack.size() - 1;
|
452 |
| |
453 |
320
| if (index < 0) {
|
454 |
76
| currentNamespaceCache = rootNamespaceCache;
|
455 |
| } else { |
456 |
244
| currentNamespaceCache = (Map) namespaceCacheList.get(index);
|
457 |
| |
458 |
244
| if (currentNamespaceCache == null) {
|
459 |
212
| currentNamespaceCache = new HashMap();
|
460 |
212
| namespaceCacheList.set(index, currentNamespaceCache);
|
461 |
| } |
462 |
| } |
463 |
| } |
464 |
| |
465 |
10455
| return currentNamespaceCache;
|
466 |
| } |
467 |
| } |
468 |
| |
469 |
| |
470 |
| |
471 |
| |
472 |
| |
473 |
| |
474 |
| |
475 |
| |
476 |
| |
477 |
| |
478 |
| |
479 |
| |
480 |
| |
481 |
| |
482 |
| |
483 |
| |
484 |
| |
485 |
| |
486 |
| |
487 |
| |
488 |
| |
489 |
| |
490 |
| |
491 |
| |
492 |
| |
493 |
| |
494 |
| |
495 |
| |
496 |
| |
497 |
| |
498 |
| |
499 |
| |
500 |
| |
501 |
| |
502 |
| |
503 |
| |
504 |
| |