|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.io; |
|
9 |
| |
|
10 |
| import java.io.BufferedReader; |
|
11 |
| import java.io.CharArrayReader; |
|
12 |
| import java.io.File; |
|
13 |
| import java.io.FileReader; |
|
14 |
| import java.io.IOException; |
|
15 |
| import java.io.InputStream; |
|
16 |
| import java.io.InputStreamReader; |
|
17 |
| import java.io.Reader; |
|
18 |
| import java.net.URL; |
|
19 |
| |
|
20 |
| import org.dom4j.Document; |
|
21 |
| import org.dom4j.DocumentException; |
|
22 |
| import org.dom4j.DocumentFactory; |
|
23 |
| import org.dom4j.Element; |
|
24 |
| import org.dom4j.ElementHandler; |
|
25 |
| import org.dom4j.QName; |
|
26 |
| |
|
27 |
| import org.xmlpull.v1.XmlPullParser; |
|
28 |
| import org.xmlpull.v1.XmlPullParserException; |
|
29 |
| import org.xmlpull.v1.XmlPullParserFactory; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| public class XPP3Reader { |
|
43 |
| |
|
44 |
| private DocumentFactory factory; |
|
45 |
| |
|
46 |
| |
|
47 |
| private XmlPullParser xppParser; |
|
48 |
| |
|
49 |
| |
|
50 |
| private XmlPullParserFactory xppFactory; |
|
51 |
| |
|
52 |
| |
|
53 |
| private DispatchHandler dispatchHandler; |
|
54 |
| |
|
55 |
2
| public XPP3Reader() {
|
|
56 |
| } |
|
57 |
| |
|
58 |
0
| public XPP3Reader(DocumentFactory factory) {
|
|
59 |
0
| this.factory = factory;
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
2
| public Document read(File file) throws DocumentException, IOException,
|
|
80 |
| XmlPullParserException { |
|
81 |
2
| String systemID = file.getAbsolutePath();
|
|
82 |
| |
|
83 |
2
| return read(new BufferedReader(new FileReader(file)), systemID);
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
0
| public Document read(URL url) throws DocumentException, IOException,
|
|
104 |
| XmlPullParserException { |
|
105 |
0
| String systemID = url.toExternalForm();
|
|
106 |
| |
|
107 |
0
| return read(createReader(url.openStream()), systemID);
|
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
0
| public Document read(String systemID) throws DocumentException,
|
|
136 |
| IOException, XmlPullParserException { |
|
137 |
0
| if (systemID.indexOf(':') >= 0) {
|
|
138 |
| |
|
139 |
0
| return read(new URL(systemID));
|
|
140 |
| } else { |
|
141 |
| |
|
142 |
0
| return read(new File(systemID));
|
|
143 |
| } |
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
0
| public Document read(InputStream in) throws DocumentException, IOException,
|
|
164 |
| XmlPullParserException { |
|
165 |
0
| return read(createReader(in));
|
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
2
| public Document read(Reader reader) throws DocumentException, IOException,
|
|
186 |
| XmlPullParserException { |
|
187 |
2
| getXPPParser().setInput(reader);
|
|
188 |
| |
|
189 |
2
| return parseDocument();
|
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
0
| public Document read(char[] text) throws DocumentException, IOException,
|
|
210 |
| XmlPullParserException { |
|
211 |
0
| getXPPParser().setInput(new CharArrayReader(text));
|
|
212 |
| |
|
213 |
0
| return parseDocument();
|
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
0
| public Document read(InputStream in, String systemID)
|
|
236 |
| throws DocumentException, IOException, XmlPullParserException { |
|
237 |
0
| return read(createReader(in), systemID);
|
|
238 |
| } |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
2
| public Document read(Reader reader, String systemID)
|
|
260 |
| throws DocumentException, IOException, XmlPullParserException { |
|
261 |
2
| Document document = read(reader);
|
|
262 |
2
| document.setName(systemID);
|
|
263 |
| |
|
264 |
2
| return document;
|
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
4
| public XmlPullParser getXPPParser() throws XmlPullParserException {
|
|
270 |
4
| if (xppParser == null) {
|
|
271 |
2
| xppParser = getXPPFactory().newPullParser();
|
|
272 |
| } |
|
273 |
| |
|
274 |
4
| return xppParser;
|
|
275 |
| } |
|
276 |
| |
|
277 |
2
| public XmlPullParserFactory getXPPFactory() throws XmlPullParserException {
|
|
278 |
2
| if (xppFactory == null) {
|
|
279 |
2
| xppFactory = XmlPullParserFactory.newInstance();
|
|
280 |
| } |
|
281 |
| |
|
282 |
2
| xppFactory.setNamespaceAware(true);
|
|
283 |
| |
|
284 |
2
| return xppFactory;
|
|
285 |
| } |
|
286 |
| |
|
287 |
0
| public void setXPPFactory(XmlPullParserFactory xPPfactory) {
|
|
288 |
0
| this.xppFactory = xPPfactory;
|
|
289 |
| } |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
2
| public DocumentFactory getDocumentFactory() {
|
|
298 |
2
| if (factory == null) {
|
|
299 |
2
| factory = DocumentFactory.getInstance();
|
|
300 |
| } |
|
301 |
| |
|
302 |
2
| return factory;
|
|
303 |
| } |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
0
| public void setDocumentFactory(DocumentFactory documentFactory) {
|
|
317 |
0
| this.factory = documentFactory;
|
|
318 |
| } |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
0
| public void addHandler(String path, ElementHandler handler) {
|
|
331 |
0
| getDispatchHandler().addHandler(path, handler);
|
|
332 |
| } |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
0
| public void removeHandler(String path) {
|
|
342 |
0
| getDispatchHandler().removeHandler(path);
|
|
343 |
| } |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
0
| public void setDefaultHandler(ElementHandler handler) {
|
|
355 |
0
| getDispatchHandler().setDefaultHandler(handler);
|
|
356 |
| } |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
2
| protected Document parseDocument() throws DocumentException, IOException,
|
|
361 |
| XmlPullParserException { |
|
362 |
2
| DocumentFactory df = getDocumentFactory();
|
|
363 |
2
| Document document = df.createDocument();
|
|
364 |
2
| Element parent = null;
|
|
365 |
2
| XmlPullParser pp = getXPPParser();
|
|
366 |
2
| pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
|
367 |
| |
|
368 |
2
| while (true) {
|
|
369 |
72
| int type = pp.nextToken();
|
|
370 |
| |
|
371 |
72
| switch (type) {
|
|
372 |
0
| case XmlPullParser.PROCESSING_INSTRUCTION: {
|
|
373 |
0
| String text = pp.getText();
|
|
374 |
0
| int loc = text.indexOf(" ");
|
|
375 |
| |
|
376 |
0
| if (loc >= 0) {
|
|
377 |
0
| String target = text.substring(0, loc);
|
|
378 |
0
| String txt = text.substring(loc + 1);
|
|
379 |
0
| document.addProcessingInstruction(target, txt);
|
|
380 |
| } else { |
|
381 |
0
| document.addProcessingInstruction(text, "");
|
|
382 |
| } |
|
383 |
| |
|
384 |
0
| break;
|
|
385 |
| } |
|
386 |
| |
|
387 |
0
| case XmlPullParser.COMMENT: {
|
|
388 |
0
| if (parent != null) {
|
|
389 |
0
| parent.addComment(pp.getText());
|
|
390 |
| } else { |
|
391 |
0
| document.addComment(pp.getText());
|
|
392 |
| } |
|
393 |
| |
|
394 |
0
| break;
|
|
395 |
| } |
|
396 |
| |
|
397 |
0
| case XmlPullParser.CDSECT: {
|
|
398 |
0
| if (parent != null) {
|
|
399 |
0
| parent.addCDATA(pp.getText());
|
|
400 |
| } else { |
|
401 |
0
| String msg = "Cannot have text content outside of the "
|
|
402 |
| + "root document"; |
|
403 |
0
| throw new DocumentException(msg);
|
|
404 |
| } |
|
405 |
| |
|
406 |
0
| break;
|
|
407 |
| } |
|
408 |
| |
|
409 |
2
| case XmlPullParser.ENTITY_REF:
|
|
410 |
2
| break;
|
|
411 |
| |
|
412 |
2
| case XmlPullParser.END_DOCUMENT:
|
|
413 |
2
| return document;
|
|
414 |
| |
|
415 |
16
| case XmlPullParser.START_TAG: {
|
|
416 |
16
| QName qname = (pp.getPrefix() == null) ? df.createQName(pp
|
|
417 |
| .getName(), pp.getNamespace()) : df.createQName(pp |
|
418 |
| .getName(), pp.getPrefix(), pp.getNamespace()); |
|
419 |
16
| Element newElement = df.createElement(qname);
|
|
420 |
16
| int nsStart = pp.getNamespaceCount(pp.getDepth() - 1);
|
|
421 |
16
| int nsEnd = pp.getNamespaceCount(pp.getDepth());
|
|
422 |
| |
|
423 |
16
| for (int i = nsStart; i < nsEnd; i++) {
|
|
424 |
0
| if (pp.getNamespacePrefix(i) != null) {
|
|
425 |
0
| newElement.addNamespace(pp.getNamespacePrefix(i),
|
|
426 |
| pp.getNamespaceUri(i)); |
|
427 |
| } |
|
428 |
| } |
|
429 |
| |
|
430 |
16
| for (int i = 0; i < pp.getAttributeCount(); i++) {
|
|
431 |
0
| QName qa = (pp.getAttributePrefix(i) == null) ? df
|
|
432 |
| .createQName(pp.getAttributeName(i)) : df |
|
433 |
| .createQName(pp.getAttributeName(i), pp |
|
434 |
| .getAttributePrefix(i), pp |
|
435 |
| .getAttributeNamespace(i)); |
|
436 |
0
| newElement.addAttribute(qa, pp.getAttributeValue(i));
|
|
437 |
| } |
|
438 |
| |
|
439 |
16
| if (parent != null) {
|
|
440 |
14
| parent.add(newElement);
|
|
441 |
| } else { |
|
442 |
2
| document.add(newElement);
|
|
443 |
| } |
|
444 |
| |
|
445 |
16
| parent = newElement;
|
|
446 |
| |
|
447 |
16
| break;
|
|
448 |
| } |
|
449 |
| |
|
450 |
16
| case XmlPullParser.END_TAG: {
|
|
451 |
16
| if (parent != null) {
|
|
452 |
16
| parent = parent.getParent();
|
|
453 |
| } |
|
454 |
| |
|
455 |
16
| break;
|
|
456 |
| } |
|
457 |
| |
|
458 |
32
| case XmlPullParser.TEXT: {
|
|
459 |
32
| String text = pp.getText();
|
|
460 |
| |
|
461 |
32
| if (parent != null) {
|
|
462 |
32
| parent.addText(text);
|
|
463 |
| } else { |
|
464 |
0
| String msg = "Cannot have text content outside of the "
|
|
465 |
| + "root document"; |
|
466 |
0
| throw new DocumentException(msg);
|
|
467 |
| } |
|
468 |
| |
|
469 |
32
| break;
|
|
470 |
| } |
|
471 |
| |
|
472 |
4
| default:
|
|
473 |
4
| break;
|
|
474 |
| } |
|
475 |
| } |
|
476 |
| } |
|
477 |
| |
|
478 |
0
| protected DispatchHandler getDispatchHandler() {
|
|
479 |
0
| if (dispatchHandler == null) {
|
|
480 |
0
| dispatchHandler = new DispatchHandler();
|
|
481 |
| } |
|
482 |
| |
|
483 |
0
| return dispatchHandler;
|
|
484 |
| } |
|
485 |
| |
|
486 |
0
| protected void setDispatchHandler(DispatchHandler dispatchHandler) {
|
|
487 |
0
| this.dispatchHandler = dispatchHandler;
|
|
488 |
| } |
|
489 |
| |
|
490 |
| |
|
491 |
| |
|
492 |
| |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
| |
|
497 |
| |
|
498 |
| |
|
499 |
| |
|
500 |
| |
|
501 |
0
| protected Reader createReader(InputStream in) throws IOException {
|
|
502 |
0
| return new BufferedReader(new InputStreamReader(in));
|
|
503 |
| } |
|
504 |
| } |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
| |
|
515 |
| |
|
516 |
| |
|
517 |
| |
|
518 |
| |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
| |
|
524 |
| |
|
525 |
| |
|
526 |
| |
|
527 |
| |
|
528 |
| |
|
529 |
| |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
| |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
| |