|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.io; |
|
9 |
| |
|
10 |
| import java.io.InputStream; |
|
11 |
| import java.io.Reader; |
|
12 |
| import java.util.Iterator; |
|
13 |
| |
|
14 |
| import javax.xml.namespace.QName; |
|
15 |
| import javax.xml.stream.XMLEventReader; |
|
16 |
| import javax.xml.stream.XMLInputFactory; |
|
17 |
| import javax.xml.stream.XMLStreamConstants; |
|
18 |
| import javax.xml.stream.XMLStreamException; |
|
19 |
| import javax.xml.stream.events.Attribute; |
|
20 |
| import javax.xml.stream.events.Characters; |
|
21 |
| import javax.xml.stream.events.Comment; |
|
22 |
| import javax.xml.stream.events.EndElement; |
|
23 |
| import javax.xml.stream.events.EntityReference; |
|
24 |
| import javax.xml.stream.events.Namespace; |
|
25 |
| import javax.xml.stream.events.ProcessingInstruction; |
|
26 |
| import javax.xml.stream.events.StartDocument; |
|
27 |
| import javax.xml.stream.events.StartElement; |
|
28 |
| import javax.xml.stream.events.XMLEvent; |
|
29 |
| |
|
30 |
| import org.dom4j.CharacterData; |
|
31 |
| import org.dom4j.Document; |
|
32 |
| import org.dom4j.DocumentFactory; |
|
33 |
| import org.dom4j.Element; |
|
34 |
| import org.dom4j.Entity; |
|
35 |
| import org.dom4j.Node; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| public class STAXEventReader { |
|
44 |
| |
|
45 |
| private DocumentFactory factory; |
|
46 |
| |
|
47 |
| |
|
48 |
| private XMLInputFactory inputFactory = XMLInputFactory.newInstance(); |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
1
| public STAXEventReader() {
|
|
55 |
1
| this.factory = DocumentFactory.getInstance();
|
|
56 |
| } |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
0
| public STAXEventReader(DocumentFactory factory) {
|
|
67 |
0
| if (factory != null) {
|
|
68 |
0
| this.factory = factory;
|
|
69 |
| } else { |
|
70 |
0
| this.factory = DocumentFactory.getInstance();
|
|
71 |
| } |
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
0
| public void setDocumentFactory(DocumentFactory documentFactory) {
|
|
82 |
0
| if (documentFactory != null) {
|
|
83 |
0
| this.factory = documentFactory;
|
|
84 |
| } else { |
|
85 |
0
| this.factory = DocumentFactory.getInstance();
|
|
86 |
| } |
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
0
| public Document readDocument(InputStream is) throws XMLStreamException {
|
|
102 |
0
| return readDocument(is, null);
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
1
| public Document readDocument(Reader reader) throws XMLStreamException {
|
|
118 |
1
| return readDocument(reader, null);
|
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
0
| public Document readDocument(InputStream is, String systemId)
|
|
136 |
| throws XMLStreamException { |
|
137 |
0
| XMLEventReader eventReader = inputFactory.createXMLEventReader(
|
|
138 |
| systemId, is); |
|
139 |
| |
|
140 |
0
| try {
|
|
141 |
0
| return readDocument(eventReader);
|
|
142 |
| } finally { |
|
143 |
0
| eventReader.close();
|
|
144 |
| } |
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
1
| public Document readDocument(Reader reader, String systemId)
|
|
162 |
| throws XMLStreamException { |
|
163 |
1
| XMLEventReader eventReader = inputFactory.createXMLEventReader(
|
|
164 |
| systemId, reader); |
|
165 |
| |
|
166 |
1
| try {
|
|
167 |
1
| return readDocument(eventReader);
|
|
168 |
| } finally { |
|
169 |
1
| eventReader.close();
|
|
170 |
| } |
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
24
| public Node readNode(XMLEventReader reader) throws XMLStreamException {
|
|
195 |
24
| XMLEvent event = reader.peek();
|
|
196 |
| |
|
197 |
24
| if (event.isStartElement()) {
|
|
198 |
8
| return readElement(reader);
|
|
199 |
16
| } else if (event.isCharacters()) {
|
|
200 |
16
| return readCharacters(reader);
|
|
201 |
0
| } else if (event.isStartDocument()) {
|
|
202 |
0
| return readDocument(reader);
|
|
203 |
0
| } else if (event.isProcessingInstruction()) {
|
|
204 |
0
| return readProcessingInstruction(reader);
|
|
205 |
0
| } else if (event.isEntityReference()) {
|
|
206 |
0
| return readEntityReference(reader);
|
|
207 |
0
| } else if (event.isAttribute()) {
|
|
208 |
0
| return readAttribute(reader);
|
|
209 |
0
| } else if (event.isNamespace()) {
|
|
210 |
0
| return readNamespace(reader);
|
|
211 |
| } else { |
|
212 |
0
| throw new XMLStreamException("Unsupported event: " + event);
|
|
213 |
| } |
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
1
| public Document readDocument(XMLEventReader reader)
|
|
230 |
| throws XMLStreamException { |
|
231 |
1
| Document doc = null;
|
|
232 |
| |
|
233 |
1
| while (reader.hasNext()) {
|
|
234 |
5
| XMLEvent nextEvent = reader.peek();
|
|
235 |
5
| int type = nextEvent.getEventType();
|
|
236 |
| |
|
237 |
5
| switch (type) {
|
|
238 |
1
| case XMLStreamConstants.START_DOCUMENT:
|
|
239 |
| |
|
240 |
1
| StartDocument event = (StartDocument) reader.nextEvent();
|
|
241 |
| |
|
242 |
1
| if (doc == null) {
|
|
243 |
| |
|
244 |
1
| if (event.encodingSet()) {
|
|
245 |
1
| String encodingScheme = event
|
|
246 |
| .getCharacterEncodingScheme(); |
|
247 |
1
| doc = factory.createDocument(encodingScheme);
|
|
248 |
| } else { |
|
249 |
0
| doc = factory.createDocument();
|
|
250 |
| } |
|
251 |
| } else { |
|
252 |
| |
|
253 |
0
| String msg = "Unexpected StartDocument event";
|
|
254 |
0
| throw new XMLStreamException(msg, event.getLocation());
|
|
255 |
| } |
|
256 |
| |
|
257 |
1
| break;
|
|
258 |
| |
|
259 |
1
| case XMLStreamConstants.END_DOCUMENT:
|
|
260 |
0
| case XMLStreamConstants.SPACE:
|
|
261 |
2
| case XMLStreamConstants.CHARACTERS:
|
|
262 |
| |
|
263 |
| |
|
264 |
3
| reader.nextEvent();
|
|
265 |
| |
|
266 |
3
| break;
|
|
267 |
| |
|
268 |
1
| default:
|
|
269 |
| |
|
270 |
1
| if (doc == null) {
|
|
271 |
| |
|
272 |
0
| doc = factory.createDocument();
|
|
273 |
| } |
|
274 |
| |
|
275 |
1
| Node n = readNode(reader);
|
|
276 |
1
| doc.add(n);
|
|
277 |
| } |
|
278 |
| } |
|
279 |
| |
|
280 |
1
| return doc;
|
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
8
| public Element readElement(XMLEventReader eventReader)
|
|
299 |
| throws XMLStreamException { |
|
300 |
8
| XMLEvent event = eventReader.peek();
|
|
301 |
| |
|
302 |
8
| if (event.isStartElement()) {
|
|
303 |
| |
|
304 |
8
| StartElement startTag = eventReader.nextEvent().asStartElement();
|
|
305 |
8
| Element elem = createElement(startTag);
|
|
306 |
| |
|
307 |
| |
|
308 |
8
| while (true) {
|
|
309 |
31
| if (!eventReader.hasNext()) {
|
|
310 |
0
| String msg = "Unexpected end of stream while reading"
|
|
311 |
| + " element content"; |
|
312 |
0
| throw new XMLStreamException(msg);
|
|
313 |
| } |
|
314 |
| |
|
315 |
31
| XMLEvent nextEvent = eventReader.peek();
|
|
316 |
| |
|
317 |
31
| if (nextEvent.isEndElement()) {
|
|
318 |
8
| EndElement endElem = eventReader.nextEvent().asEndElement();
|
|
319 |
| |
|
320 |
8
| if (!endElem.getName().equals(startTag.getName())) {
|
|
321 |
0
| throw new XMLStreamException("Expected "
|
|
322 |
| + startTag.getName() + " end-tag, but found" |
|
323 |
| + endElem.getName()); |
|
324 |
| } |
|
325 |
| |
|
326 |
8
| break;
|
|
327 |
| } |
|
328 |
| |
|
329 |
23
| Node child = readNode(eventReader);
|
|
330 |
23
| elem.add(child);
|
|
331 |
| } |
|
332 |
| |
|
333 |
8
| return elem;
|
|
334 |
| } else { |
|
335 |
0
| throw new XMLStreamException("Expected Element event, found: "
|
|
336 |
| + event); |
|
337 |
| } |
|
338 |
| } |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
0
| public org.dom4j.Attribute readAttribute(XMLEventReader reader)
|
|
354 |
| throws XMLStreamException { |
|
355 |
0
| XMLEvent event = reader.peek();
|
|
356 |
| |
|
357 |
0
| if (event.isAttribute()) {
|
|
358 |
0
| Attribute attr = (Attribute) reader.nextEvent();
|
|
359 |
| |
|
360 |
0
| return createAttribute(null, attr);
|
|
361 |
| } else { |
|
362 |
0
| throw new XMLStreamException("Expected Attribute event, found: "
|
|
363 |
| + event); |
|
364 |
| } |
|
365 |
| } |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
0
| public org.dom4j.Namespace readNamespace(XMLEventReader reader)
|
|
381 |
| throws XMLStreamException { |
|
382 |
0
| XMLEvent event = reader.peek();
|
|
383 |
| |
|
384 |
0
| if (event.isNamespace()) {
|
|
385 |
0
| Namespace ns = (Namespace) reader.nextEvent();
|
|
386 |
| |
|
387 |
0
| return createNamespace(ns);
|
|
388 |
| } else { |
|
389 |
0
| throw new XMLStreamException("Expected Namespace event, found: "
|
|
390 |
| + event); |
|
391 |
| } |
|
392 |
| } |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
16
| public CharacterData readCharacters(XMLEventReader reader)
|
|
408 |
| throws XMLStreamException { |
|
409 |
16
| XMLEvent event = reader.peek();
|
|
410 |
| |
|
411 |
16
| if (event.isCharacters()) {
|
|
412 |
16
| Characters characters = reader.nextEvent().asCharacters();
|
|
413 |
| |
|
414 |
16
| return createCharacterData(characters);
|
|
415 |
| } else { |
|
416 |
0
| throw new XMLStreamException("Expected Characters event, found: "
|
|
417 |
| + event); |
|
418 |
| } |
|
419 |
| } |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
0
| public org.dom4j.Comment readComment(XMLEventReader reader)
|
|
435 |
| throws XMLStreamException { |
|
436 |
0
| XMLEvent event = reader.peek();
|
|
437 |
| |
|
438 |
0
| if (event instanceof Comment) {
|
|
439 |
0
| return createComment((Comment) reader.nextEvent());
|
|
440 |
| } else { |
|
441 |
0
| throw new XMLStreamException("Expected Comment event, found: "
|
|
442 |
| + event); |
|
443 |
| } |
|
444 |
| } |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
0
| public Entity readEntityReference(XMLEventReader reader)
|
|
462 |
| throws XMLStreamException { |
|
463 |
0
| XMLEvent event = reader.peek();
|
|
464 |
| |
|
465 |
0
| if (event.isEntityReference()) {
|
|
466 |
0
| EntityReference entityRef = (EntityReference) reader.nextEvent();
|
|
467 |
| |
|
468 |
0
| return createEntity(entityRef);
|
|
469 |
| } else { |
|
470 |
0
| throw new XMLStreamException("Expected EntityRef event, found: "
|
|
471 |
| + event); |
|
472 |
| } |
|
473 |
| } |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
| |
|
479 |
| |
|
480 |
| |