|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.tree; |
|
11 |
| |
|
12 |
| import java.util.ArrayList; |
|
13 |
| import java.util.HashMap; |
|
14 |
| import java.util.Map; |
|
15 |
| |
|
16 |
| import org.dom4j.DocumentFactory; |
|
17 |
| import org.dom4j.Namespace; |
|
18 |
| import org.dom4j.QName; |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| public class NamespaceStack { |
|
29 |
| |
|
30 |
| |
|
31 |
| private DocumentFactory documentFactory; |
|
32 |
| |
|
33 |
| |
|
34 |
| private ArrayList namespaceStack = new ArrayList(); |
|
35 |
| |
|
36 |
| |
|
37 |
| private ArrayList namespaceCacheList = new ArrayList(); |
|
38 |
| |
|
39 |
| |
|
40 |
| private Map currentNamespaceCache; |
|
41 |
| |
|
42 |
| |
|
43 |
| private Map rootNamespaceCache = new HashMap(); |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| private Namespace defaultNamespace; |
|
48 |
| |
|
49 |
| |
|
50 |
4410
| public NamespaceStack() {
|
|
51 |
4410
| this.documentFactory = DocumentFactory.getInstance();
|
|
52 |
| } |
|
53 |
| |
|
54 |
11638
| public NamespaceStack(DocumentFactory documentFactory) {
|
|
55 |
11638
| this.documentFactory = documentFactory;
|
|
56 |
| } |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
17592
| public void push(Namespace namespace) {
|
|
64 |
17592
| namespaceStack.add( namespace );
|
|
65 |
17592
| namespaceCacheList.add( null );
|
|
66 |
17592
| currentNamespaceCache = null;
|
|
67 |
17592
| String prefix = namespace.getPrefix();
|
|
68 |
17592
| if ( prefix == null || prefix.length() == 0 ) {
|
|
69 |
4762
| defaultNamespace = namespace;
|
|
70 |
| } |
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
1446
| public Namespace pop() {
|
|
79 |
1446
| return remove( namespaceStack.size() - 1 );
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
312382
| public int size() {
|
|
85 |
312382
| return namespaceStack.size();
|
|
86 |
| } |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
23300
| public void clear() {
|
|
91 |
23300
| namespaceStack.clear();
|
|
92 |
23300
| namespaceCacheList.clear();
|
|
93 |
23300
| rootNamespaceCache.clear();
|
|
94 |
23300
| currentNamespaceCache = null;
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
11738
| public Namespace getNamespace( int index ) {
|
|
100 |
11738
| return (Namespace) namespaceStack.get( index );
|
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
4212
| public Namespace getNamespaceForPrefix( String prefix ) {
|
|
107 |
4212
| if ( prefix == null ) {
|
|
108 |
0
| prefix = "";
|
|
109 |
| } |
|
110 |
4212
| for ( int i = namespaceStack.size() - 1; i >= 0; i-- ) {
|
|
111 |
5430
| Namespace namespace = (Namespace) namespaceStack.get(i);
|
|
112 |
5430
| if ( prefix.equals( namespace.getPrefix() ) ) {
|
|
113 |
3014
| return namespace;
|
|
114 |
| } |
|
115 |
| } |
|
116 |
1198
| return null;
|
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
24
| public String getURI( String prefix ) {
|
|
123 |
24
| Namespace namespace = getNamespaceForPrefix( prefix );
|
|
124 |
24
| return ( namespace != null ) ? namespace.getURI() : null;
|
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
36464
| public boolean contains( Namespace namespace ) {
|
|
130 |
36464
| String prefix = namespace.getPrefix();
|
|
131 |
36464
| Namespace current = null;
|
|
132 |
36464
| if ( prefix == null || prefix.length() == 0 ) {
|
|
133 |
32282
| current = getDefaultNamespace();
|
|
134 |
| } |
|
135 |
| else { |
|
136 |
4182
| current = getNamespaceForPrefix( prefix );
|
|
137 |
| } |
|
138 |
36464
| if ( current == null ) {
|
|
139 |
1226
| return false;
|
|
140 |
| } |
|
141 |
35238
| if ( current == namespace ) {
|
|
142 |
35078
| return true;
|
|
143 |
| } |
|
144 |
160
| return namespace.getURI().equals( current.getURI() );
|
|
145 |
| } |
|
146 |
| |
|
147 |
184534
| public QName getQName( String namespaceURI, String localName, String qualifiedName ) {
|
|
148 |
184534
| if ( localName == null ) {
|
|
149 |
22
| localName = qualifiedName;
|
|
150 |
| } |
|
151 |
184512
| else if ( qualifiedName == null ) {
|
|
152 |
0
| qualifiedName = localName;
|
|
153 |
| } |
|
154 |
184534
| if ( namespaceURI == null ) {
|
|
155 |
14910
| namespaceURI = "";
|
|
156 |
| } |
|
157 |
184534
| String prefix = "";
|
|
158 |
184534
| int index = qualifiedName.indexOf(":");
|
|
159 |
184534
| if (index > 0) {
|
|
160 |
26864
| prefix = qualifiedName.substring(0, index);
|
|
161 |
26864
| if (localName.trim().length() == 0) {
|
|
162 |
0
| localName = qualifiedName.substring(index+1);
|
|
163 |
| } |
|
164 |
157670
| } else if (localName.trim().length() == 0) {
|
|
165 |
0
| localName = qualifiedName;
|
|
166 |
| } |
|
167 |
184534
| Namespace namespace = createNamespace( prefix, namespaceURI );
|
|
168 |
184534
| return pushQName( localName, qualifiedName, namespace, prefix );
|
|
169 |
| } |
|
170 |
| |
|
171 |
18522
| public QName getAttributeQName( String namespaceURI, String localName, String qualifiedName ) {
|
|
172 |
18522
| if ( qualifiedName == null ) {
|
|
173 |
0
| qualifiedName = localName;
|
|
174 |
| } |
|
175 |
18522
| Map map = getNamespaceCache();
|
|
176 |
18522
| QName answer = (QName) map.get( qualifiedName );
|
|
177 |
18522
| if ( answer != null ) {
|
|
178 |
16544
| return answer;
|
|
179 |
| } |
|
180 |
1978
| if ( localName == null ) {
|
|
181 |
0
| localName = qualifiedName;
|
|
182 |
| } |
|
183 |
1978
| if ( namespaceURI == null ) {
|
|
184 |
0
| namespaceURI = "";
|
|
185 |
| } |
|
186 |
1978
| Namespace namespace = null;
|
|
187 |
1978
| String prefix = "";
|
|
188 |
1978
| int index = qualifiedName.indexOf(":");
|
|
189 |
1978
| if (index > 0) {
|
|
190 |
192
| prefix = qualifiedName.substring(0, index);
|
|
191 |
192
| namespace = createNamespace( prefix, namespaceURI );
|
|
192 |
192
| if ( localName.trim().length() == 0) {
|
|
193 |
0
| localName = qualifiedName.substring(index+1);
|
|
194 |
| } |
|
195 |
| } |
|
196 |
| else { |
|
197 |
| |
|
198 |
1786
| namespace = Namespace.NO_NAMESPACE;
|
|
199 |
1786
| if ( localName.trim().length() == 0) {
|
|
200 |
0
| localName = qualifiedName;
|
|
201 |
| } |
|
202 |
| } |
|
203 |
1978
| answer = pushQName( localName, qualifiedName, namespace, prefix );
|
|
204 |
1978
| map.put( qualifiedName, answer );
|
|
205 |
1978
| return answer;
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
11742
| public void push( String prefix, String uri ) {
|
|
210 |
11742
| if ( uri == null ) {
|
|
211 |
0
| uri = "";
|
|
212 |
| } |
|
213 |
11742
| Namespace namespace = createNamespace( prefix, uri );
|
|
214 |
11742
| push( namespace );
|
|
215 |
| } |
|
216 |
| |
|
217 |
| |
|
218 |
104
| public Namespace addNamespace( String prefix, String uri ) {
|
|
219 |
104
| Namespace namespace = createNamespace( prefix, uri );
|
|
220 |
104
| push( namespace );
|
|
221 |
104
| return namespace;
|
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
11738
| public Namespace pop( String prefix ) {
|
|
226 |
11738
| if ( prefix == null ) {
|
|
227 |
0
| prefix = "";
|
|
228 |
| } |
|
229 |
11738
| Namespace namespace = null;
|
|
230 |
11738
| for (int i = namespaceStack.size() - 1; i >= 0; i-- ) {
|
|
231 |
11990
| Namespace ns = (Namespace) namespaceStack.get(i);
|
|
232 |
11990
| if ( prefix.equals( ns.getPrefix() ) ) {
|
|
233 |
11714
| remove(i);
|
|
234 |
11714
| namespace = ns;
|
|
235 |
11714
| break;
|
|
236 |
| } |
|
237 |
| } |
|
238 |
11738
| if ( namespace == null ) {
|
|
239 |
24
| System.out.println( "Warning: missing namespace prefix ignored: " + prefix );
|
|
240 |
| } |
|
241 |
11738
| return namespace;
|
|
242 |
| } |
|
243 |
| |
|
244 |
0
| public String toString() {
|
|
245 |
0
| return super.toString() + " Stack: " + namespaceStack.toString();
|
|
246 |
| } |
|
247 |
| |
|
248 |
0
| public DocumentFactory getDocumentFactory() {
|
|
249 |
0
| return documentFactory;
|
|
250 |
| } |
|
251 |
| |
|
252 |
0
| public void setDocumentFactory(DocumentFactory documentFactory) {
|
|
253 |
0
| this.documentFactory = documentFactory;
|
|
254 |
| } |
|
255 |
| |
|
256 |
32284
| public Namespace getDefaultNamespace() {
|
|
257 |
32284
| if ( defaultNamespace == null ) {
|
|
258 |
80
| defaultNamespace = findDefaultNamespace();
|
|
259 |
| } |
|
260 |
32284
| return defaultNamespace;
|
|
261 |
| } |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
186512
| protected QName pushQName( String localName, String qualifiedName, Namespace namespace, String prefix ) {
|
|
269 |
186512
| if ( prefix == null || prefix.length() == 0 ) {
|
|
270 |
159456
| this.defaultNamespace = null;
|
|
271 |
| } |
|
272 |
186512
| return createQName( localName, qualifiedName, namespace );
|
|
273 |
| } |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
186512
| protected QName createQName( String localName, String qualifiedName, Namespace namespace ) {
|
|
279 |
186512
| return documentFactory.createQName( localName, namespace );
|
|
280 |
| } |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
196572
| protected Namespace createNamespace( String prefix, String namespaceURI ) {
|
|
286 |
196572
| return documentFactory.createNamespace( prefix, namespaceURI );
|
|
287 |
| } |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
80
| protected Namespace findDefaultNamespace() {
|
|
293 |
80
| for ( int i = namespaceStack.size() - 1; i >= 0; i-- ) {
|
|
294 |
154
| Namespace namespace = (Namespace) namespaceStack.get(i);
|
|
295 |
154
| if ( namespace != null ) {
|
|
296 |
154
| String prefix = namespace.getPrefix();
|
|
297 |
154
| if ( prefix == null || namespace.getPrefix().length() == 0 ) {
|
|
298 |
48
| return namespace;
|
|
299 |
| } |
|
300 |
| } |
|
301 |
| } |
|
302 |
32
| return null;
|
|
303 |
| } |
|
304 |
| |
|
305 |
| |
|
306 |
13160
| protected Namespace remove(int index) {
|
|
307 |
13160
| Namespace namespace = (Namespace) namespaceStack.remove(index);
|
|
308 |
13160
| namespaceCacheList.remove(index);
|
|
309 |
13160
| defaultNamespace = null;
|
|
310 |
13160
| currentNamespaceCache = null;
|
|
311 |
13160
| return namespace;
|
|
312 |
| } |
|
313 |
| |
|
314 |
18522
| protected Map getNamespaceCache() {
|
|
315 |
18522
| if ( currentNamespaceCache == null ) {
|
|
316 |
616
| int index = namespaceStack.size() - 1;
|
|
317 |
616
| if ( index < 0 ) {
|
|
318 |
146
| currentNamespaceCache = rootNamespaceCache;
|
|
319 |
| } |
|
320 |
| else { |
|
321 |
470
| currentNamespaceCache = (Map) namespaceCacheList.get(index);
|
|
322 |
470
| if ( currentNamespaceCache == null ) {
|
|
323 |
410
| currentNamespaceCache = new HashMap();
|
|
324 |
410
| namespaceCacheList.set(index, currentNamespaceCache);
|
|
325 |
| } |
|
326 |
| } |
|
327 |
| } |
|
328 |
18522
| return currentNamespaceCache;
|
|
329 |
| } |
|
330 |
| } |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |