|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.io; |
|
9 |
| |
|
10 |
| import java.io.File; |
|
11 |
| import java.io.FileInputStream; |
|
12 |
| import java.io.FileNotFoundException; |
|
13 |
| import java.io.InputStream; |
|
14 |
| import java.io.Reader; |
|
15 |
| import java.io.Serializable; |
|
16 |
| import java.net.URL; |
|
17 |
| |
|
18 |
| import org.dom4j.Document; |
|
19 |
| import org.dom4j.DocumentException; |
|
20 |
| import org.dom4j.DocumentFactory; |
|
21 |
| import org.dom4j.ElementHandler; |
|
22 |
| |
|
23 |
| import org.xml.sax.EntityResolver; |
|
24 |
| import org.xml.sax.ErrorHandler; |
|
25 |
| import org.xml.sax.InputSource; |
|
26 |
| import org.xml.sax.SAXException; |
|
27 |
| import org.xml.sax.SAXParseException; |
|
28 |
| import org.xml.sax.XMLFilter; |
|
29 |
| import org.xml.sax.XMLReader; |
|
30 |
| import org.xml.sax.helpers.DefaultHandler; |
|
31 |
| import org.xml.sax.helpers.XMLReaderFactory; |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| public class SAXReader { |
|
80 |
| private static final String SAX_STRING_INTERNING = |
|
81 |
| "http://xml.org/sax/features/string-interning"; |
|
82 |
| private static final String SAX_NAMESPACE_PREFIXES = |
|
83 |
| "http://xml.org/sax/features/namespace-prefixes"; |
|
84 |
| private static final String SAX_NAMESPACES = |
|
85 |
| "http://xml.org/sax/features/namespaces"; |
|
86 |
| private static final String SAX_DECL_HANDLER = |
|
87 |
| "http://xml.org/sax/properties/declaration-handler"; |
|
88 |
| private static final String SAX_LEXICAL_HANDLER = |
|
89 |
| "http://xml.org/sax/properties/lexical-handler"; |
|
90 |
| private static final String SAX_LEXICALHANDLER = |
|
91 |
| "http://xml.org/sax/handlers/LexicalHandler"; |
|
92 |
| |
|
93 |
| |
|
94 |
| private DocumentFactory factory; |
|
95 |
| |
|
96 |
| |
|
97 |
| private XMLReader xmlReader; |
|
98 |
| |
|
99 |
| |
|
100 |
| private boolean validating; |
|
101 |
| |
|
102 |
| |
|
103 |
| private DispatchHandler dispatchHandler; |
|
104 |
| |
|
105 |
| |
|
106 |
| private ErrorHandler errorHandler; |
|
107 |
| |
|
108 |
| |
|
109 |
| private EntityResolver entityResolver; |
|
110 |
| |
|
111 |
| |
|
112 |
| private boolean stringInternEnabled = true; |
|
113 |
| |
|
114 |
| |
|
115 |
| private boolean includeInternalDTDDeclarations = false; |
|
116 |
| |
|
117 |
| |
|
118 |
| private boolean includeExternalDTDDeclarations = false; |
|
119 |
| |
|
120 |
| |
|
121 |
| private boolean mergeAdjacentText = false; |
|
122 |
| |
|
123 |
| |
|
124 |
| private boolean stripWhitespaceText = false; |
|
125 |
| |
|
126 |
| |
|
127 |
| private boolean ignoreComments = false; |
|
128 |
| |
|
129 |
| |
|
130 |
| private String encoding = null; |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| private XMLFilter xmlFilter; |
|
137 |
| |
|
138 |
5741
| public SAXReader() {
|
|
139 |
| } |
|
140 |
| |
|
141 |
1
| public SAXReader(boolean validating) {
|
|
142 |
1
| this.validating = validating;
|
|
143 |
| } |
|
144 |
| |
|
145 |
55
| public SAXReader(DocumentFactory factory) {
|
|
146 |
55
| this.factory = factory;
|
|
147 |
| } |
|
148 |
| |
|
149 |
0
| public SAXReader(DocumentFactory factory, boolean validating) {
|
|
150 |
0
| this.factory = factory;
|
|
151 |
0
| this.validating = validating;
|
|
152 |
| } |
|
153 |
| |
|
154 |
0
| public SAXReader(XMLReader xmlReader) {
|
|
155 |
0
| this.xmlReader = xmlReader;
|
|
156 |
| } |
|
157 |
| |
|
158 |
0
| public SAXReader(XMLReader xmlReader, boolean validating) {
|
|
159 |
0
| this.xmlReader = xmlReader;
|
|
160 |
0
| this.validating = validating;
|
|
161 |
| } |
|
162 |
| |
|
163 |
1
| public SAXReader(String xmlReaderClassName) throws SAXException {
|
|
164 |
1
| if (xmlReaderClassName != null) {
|
|
165 |
1
| this.xmlReader = XMLReaderFactory
|
|
166 |
| .createXMLReader(xmlReaderClassName); |
|
167 |
| } |
|
168 |
| } |
|
169 |
| |
|
170 |
0
| public SAXReader(String xmlReaderClassName, boolean validating)
|
|
171 |
| throws SAXException { |
|
172 |
0
| if (xmlReaderClassName != null) {
|
|
173 |
0
| this.xmlReader = XMLReaderFactory
|
|
174 |
| .createXMLReader(xmlReaderClassName); |
|
175 |
| } |
|
176 |
| |
|
177 |
0
| this.validating = validating;
|
|
178 |
| } |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
0
| public void setProperty(String name, Object value) throws SAXException {
|
|
198 |
0
| getXMLReader().setProperty(name, value);
|
|
199 |
| } |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
0
| public void setFeature(String name, boolean value) throws SAXException {
|
|
218 |
0
| getXMLReader().setFeature(name, value);
|
|
219 |
| } |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
204
| public Document read(File file) throws DocumentException {
|
|
235 |
204
| try {
|
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
204
| InputSource source = new InputSource(new FileInputStream(file));
|
|
244 |
204
| if (this.encoding != null) {
|
|
245 |
0
| source.setEncoding(this.encoding);
|
|
246 |
| } |
|
247 |
204
| String path = file.getAbsolutePath();
|
|
248 |
| |
|
249 |
204
| if (path != null) {
|
|
250 |
| |
|
251 |
204
| StringBuffer sb = new StringBuffer("file://");
|
|
252 |
| |
|
253 |
| |
|
254 |
204
| if (!path.startsWith(File.separator)) {
|
|
255 |
204
| sb.append("/");
|
|
256 |
| } |
|
257 |
| |
|
258 |
204
| path = path.replace('\\', '/');
|
|
259 |
204
| sb.append(path);
|
|
260 |
| |
|
261 |
204
| source.setSystemId(sb.toString());
|
|
262 |
| } |
|
263 |
| |
|
264 |
204
| return read(source);
|
|
265 |
| } catch (FileNotFoundException e) { |
|
266 |
0
| throw new DocumentException(e.getMessage(), e);
|
|
267 |
| } |
|
268 |
| } |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
0
| public Document read(URL url) throws DocumentException {
|
|
284 |
0
| String systemID = url.toExternalForm();
|
|
285 |
| |
|
286 |
0
| InputSource source = new InputSource(systemID);
|
|
287 |
0
| if (this.encoding != null) {
|
|
288 |
0
| source.setEncoding(this.encoding);
|
|
289 |
| } |
|
290 |
| |
|
291 |
0
| return read(source);
|
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
1
| public Document read(String systemId) throws DocumentException {
|
|
316 |
1
| InputSource source = new InputSource(systemId);
|
|
317 |
1
| if (this.encoding != null) {
|
|
318 |
0
| source.setEncoding(this.encoding);
|
|
319 |
| } |
|
320 |
| |
|
321 |
1
| return read(source);
|
|
322 |
| } |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
1
| public Document read(InputStream in) throws DocumentException {
|
|
338 |
1
| InputSource source = new InputSource(in);
|
|
339 |
1
| if (this.encoding != null) {
|
|
340 |
0
| source.setEncoding(this.encoding);
|
|
341 |
| } |
|
342 |
| |
|
343 |
1
| return read(source);
|
|
344 |
| } |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
31
| public Document read(Reader reader) throws DocumentException {
|
|
360 |
31
| InputSource source = new InputSource(reader);
|
|
361 |
31
| if (this.encoding != null) {
|
|
362 |
1
| source.setEncoding(this.encoding);
|
|
363 |
| } |
|
364 |
| |
|
365 |
31
| return read(source);
|
|
366 |
| } |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
0
| public Document read(InputStream in, String systemId)
|
|
384 |
| throws DocumentException { |
|
385 |
0
| InputSource source = new InputSource(in);
|
|
386 |
0
| source.setSystemId(systemId);
|
|
387 |
0
| if (this.encoding != null) {
|
|
388 |
0
| source.setEncoding(this.encoding);
|
|
389 |
| } |
|
390 |
| |
|
391 |
0
| return read(source);
|
|
392 |
| } |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
0
| public Document read(Reader reader, String systemId)
|
|
410 |
| throws DocumentException { |
|
411 |
0
| InputSource source = new InputSource(reader);
|
|
412 |
0
| source.setSystemId(systemId);
|
|
413 |
0
| if (this.encoding != null) {
|
|
414 |
0
| source.setEncoding(this.encoding);
|
|
415 |
| } |
|
416 |
| |
|
417 |
0
| return read(source);
|
|
418 |
| } |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
5792
| public Document read(InputSource in) throws DocumentException {
|
|
434 |
5792
| try {
|
|
435 |
5792
| XMLReader reader = getXMLReader();
|
|
436 |
| |
|
437 |
5792
| reader = installXMLFilter(reader);
|
|
438 |
| |
|
439 |
5792
| EntityResolver thatEntityResolver = this.entityResolver;
|
|
440 |
| |
|
441 |
5792
| if (thatEntityResolver == null) {
|
|
442 |
5762
| thatEntityResolver = createDefaultEntityResolver(in
|
|
443 |
| .getSystemId()); |
|
444 |
5762
| this.entityResolver = thatEntityResolver;
|
|
445 |
| } |
|
446 |
| |
|
447 |
5792
| reader.setEntityResolver(thatEntityResolver);
|
|
448 |
| |
|
449 |
5792
| SAXContentHandler contentHandler = createContentHandler(reader);
|
|
450 |
5792
| contentHandler.setEntityResolver(thatEntityResolver);
|
|
451 |
5792
| contentHandler.setInputSource(in);
|
|
452 |
| |
|
453 |
5792
| boolean internal = isIncludeInternalDTDDeclarations();
|
|
454 |
5792
| boolean external = isIncludeExternalDTDDeclarations();
|
|
455 |
| |
|
456 |
5792
| contentHandler.setIncludeInternalDTDDeclarations(internal);
|
|
457 |
5792
| contentHandler.setIncludeExternalDTDDeclarations(external);
|
|
458 |
5792
| contentHandler.setMergeAdjacentText(isMergeAdjacentText());
|
|
459 |
5792
| contentHandler.setStripWhitespaceText(isStripWhitespaceText());
|
|
460 |
5792
| contentHandler.setIgnoreComments(isIgnoreComments());
|
|
461 |
5792
| reader.setContentHandler(contentHandler);
|
|
462 |
| |
|
463 |
5792
| configureReader(reader, contentHandler);
|
|
464 |
| |
|
465 |
5792
| reader.parse(in);
|
|
466 |
| |
|
467 |
5791
| return contentHandler.getDocument();
|
|
468 |
| } catch (Exception e) { |
|
469 |
1
| if (e instanceof SAXParseException) {
|
|
470 |
| |
|
471 |
0
| SAXParseException parseException = (SAXParseException) e;
|
|
472 |
0
| String systemId = parseException.getSystemId();
|
|
473 |
| |
|
474 |
0
| if (systemId == null) {
|
|
475 |
0
| systemId = "";
|
|
476 |
| } |
|
477 |
| |
|
478 |
0
| String message = "Error on line "
|
|
479 |
| + parseException.getLineNumber() + " of document " |
|
480 |
| + systemId + " : " + parseException.getMessage(); |
|
481 |
| |
|
482 |
0
| throw new DocumentException(message, e);
|
|
483 |
| } else { |
|
484 |
1
| throw new DocumentException(e.getMessage(), e);
|
|
485 |
| |