|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.io; |
|
11 |
| |
|
12 |
| import java.io.File; |
|
13 |
| import java.io.FileInputStream; |
|
14 |
| import java.io.FileNotFoundException; |
|
15 |
| import java.io.InputStream; |
|
16 |
| import java.io.Reader; |
|
17 |
| import java.io.Serializable; |
|
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.ElementHandler; |
|
24 |
| import org.xml.sax.EntityResolver; |
|
25 |
| import org.xml.sax.ErrorHandler; |
|
26 |
| import org.xml.sax.InputSource; |
|
27 |
| import org.xml.sax.SAXException; |
|
28 |
| import org.xml.sax.SAXParseException; |
|
29 |
| import org.xml.sax.XMLFilter; |
|
30 |
| import org.xml.sax.XMLReader; |
|
31 |
| import org.xml.sax.helpers.DefaultHandler; |
|
32 |
| import org.xml.sax.helpers.XMLReaderFactory; |
|
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 |
| public class SAXReader { |
|
70 |
| |
|
71 |
| |
|
72 |
| private DocumentFactory factory; |
|
73 |
| |
|
74 |
| |
|
75 |
| private XMLReader xmlReader; |
|
76 |
| |
|
77 |
| |
|
78 |
| private boolean validating; |
|
79 |
| |
|
80 |
| |
|
81 |
| private DispatchHandler dispatchHandler; |
|
82 |
| |
|
83 |
| |
|
84 |
| private ErrorHandler errorHandler; |
|
85 |
| |
|
86 |
| |
|
87 |
| private EntityResolver entityResolver; |
|
88 |
| |
|
89 |
| |
|
90 |
| private boolean stringInternEnabled = true; |
|
91 |
| |
|
92 |
| |
|
93 |
| private boolean includeInternalDTDDeclarations = false; |
|
94 |
| |
|
95 |
| |
|
96 |
| private boolean includeExternalDTDDeclarations = false; |
|
97 |
| |
|
98 |
| |
|
99 |
| private boolean mergeAdjacentText = false; |
|
100 |
| |
|
101 |
| |
|
102 |
| private boolean stripWhitespaceText = false; |
|
103 |
| |
|
104 |
| |
|
105 |
| private boolean ignoreComments = false; |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| private XMLFilter xmlFilter; |
|
113 |
| |
|
114 |
| |
|
115 |
11438
| public SAXReader() {
|
|
116 |
| } |
|
117 |
| |
|
118 |
0
| public SAXReader(boolean validating) {
|
|
119 |
0
| this.validating = validating;
|
|
120 |
| } |
|
121 |
| |
|
122 |
106
| public SAXReader(DocumentFactory factory) {
|
|
123 |
106
| this.factory = factory;
|
|
124 |
| } |
|
125 |
| |
|
126 |
0
| public SAXReader(DocumentFactory factory, boolean validating) {
|
|
127 |
0
| this.factory = factory;
|
|
128 |
0
| this.validating = validating;
|
|
129 |
| } |
|
130 |
| |
|
131 |
0
| public SAXReader(XMLReader xmlReader) {
|
|
132 |
0
| this.xmlReader = xmlReader;
|
|
133 |
| } |
|
134 |
| |
|
135 |
0
| public SAXReader(XMLReader xmlReader, boolean validating) {
|
|
136 |
0
| this.xmlReader = xmlReader;
|
|
137 |
0
| this.validating = validating;
|
|
138 |
| } |
|
139 |
| |
|
140 |
22
| public SAXReader(String xmlReaderClassName) throws SAXException {
|
|
141 |
22
| if (xmlReaderClassName != null) {
|
|
142 |
22
| this.xmlReader = XMLReaderFactory.createXMLReader(xmlReaderClassName);
|
|
143 |
| } |
|
144 |
| } |
|
145 |
| |
|
146 |
2
| public SAXReader(String xmlReaderClassName, boolean validating) throws SAXException {
|
|
147 |
2
| if (xmlReaderClassName != null) {
|
|
148 |
2
| this.xmlReader = XMLReaderFactory.createXMLReader(xmlReaderClassName);
|
|
149 |
| } |
|
150 |
2
| this.validating = validating;
|
|
151 |
| } |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
0
| public void setProperty(String name, Object value) throws SAXException {
|
|
169 |
0
| getXMLReader().setProperty(name, value);
|
|
170 |
| } |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
0
| public void setFeature(String name, boolean value) throws SAXException {
|
|
186 |
0
| getXMLReader().setFeature(name, value);
|
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
60
| public Document read(File file) throws DocumentException {
|
|
197 |
60
| try {
|
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
60
| InputSource source = new InputSource(new FileInputStream(file));
|
|
207 |
60
| String path = file.getAbsolutePath();
|
|
208 |
60
| if (path != null) {
|
|
209 |
| |
|
210 |
| |
|
211 |
60
| StringBuffer sb = new StringBuffer("file://");
|
|
212 |
| |
|
213 |
60
| if (!path.startsWith(File.separator)) {
|
|
214 |
60
| sb.append("/");
|
|
215 |
| } |
|
216 |
| |
|
217 |
60
| path = path.replace('\\', '/');
|
|
218 |
60
| sb.append(path);
|
|
219 |
| |
|
220 |
60
| source.setSystemId(sb.toString());
|
|
221 |
| } |
|
222 |
60
| return read(source);
|
|
223 |
| } catch (FileNotFoundException e) { |
|
224 |
0
| throw new DocumentException(e.getMessage(), e);
|
|
225 |
| } |
|
226 |
| } |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
338
| public Document read(URL url) throws DocumentException {
|
|
235 |
338
| String systemID = url.toExternalForm();
|
|
236 |
338
| return read(new InputSource(systemID));
|
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
2
| public Document read(String systemId) throws DocumentException {
|
|
254 |
2
| return read(new InputSource(systemId));
|
|
255 |
| } |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
8
| public Document read(InputStream in) throws DocumentException {
|
|
264 |
8
| return read(new InputSource(in));
|
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
52
| public Document read(Reader reader) throws DocumentException {
|
|
274 |
52
| return read(new InputSource(reader));
|
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
0
| public Document read(InputStream in, String systemId) throws DocumentException {
|
|
285 |
0
| InputSource source = new InputSource(in);
|
|
286 |
0
| source.setSystemId(systemId);
|
|
287 |
0
| return read(source);
|
|
288 |
| } |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
0
| public Document read(Reader reader, String systemId) throws DocumentException {
|
|
298 |
0
| InputSource source = new InputSource(reader);
|
|
299 |
0
| source.setSystemId(systemId);
|
|
300 |
0
| return read(source);
|
|
301 |
| } |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
11560
| public Document read(InputSource in) throws DocumentException {
|
|
310 |
11560
| try {
|
|
311 |
11560
| XMLReader xmlReader = getXMLReader();
|
|
312 |
| |
|
313 |
11560
| xmlReader = installXMLFilter(xmlReader);
|
|
314 |
| |
|
315 |
11560
| EntityResolver thatEntityResolver = this.entityResolver;
|
|
316 |
11560
| if (thatEntityResolver==null) {
|
|
317 |
11496
| thatEntityResolver = createDefaultEntityResolver( in.getSystemId() );
|
|
318 |
11496
| this.entityResolver=thatEntityResolver;
|
|
319 |
| } |
|
320 |
11559
| xmlReader.setEntityResolver( thatEntityResolver );
|
|
321 |
| |
|
322 |
11560
| SAXContentHandler contentHandler = createContentHandler(xmlReader);
|
|
323 |
11560
| contentHandler.setEntityResolver( thatEntityResolver );
|
|
324 |
11560
| contentHandler.setInputSource( in );
|
|
325 |
11560
| contentHandler.setIncludeInternalDTDDeclarations( isIncludeInternalDTDDeclarations() );
|
|
326 |
11560
| contentHandler.setIncludeExternalDTDDeclarations( isIncludeExternalDTDDeclarations() );
|
|
327 |
11560
| contentHandler.setMergeAdjacentText( isMergeAdjacentText() );
|
|
328 |
11560
| contentHandler.setStripWhitespaceText( isStripWhitespaceText() );
|
|
329 |
11560
| contentHandler.setIgnoreComments( isIgnoreComments() );
|
|
330 |
11560
| xmlReader.setContentHandler(contentHandler);
|
|
331 |
| |
|
332 |
11560
| configureReader(xmlReader, contentHandler);
|
|
333 |
| |
|
334 |
11558
| xmlReader.parse(in);
|
|
335 |
11558
| return contentHandler.getDocument();
|
|
336 |
| } |
|
337 |
| catch (Exception e) { |
|
338 |
2
| if (e instanceof SAXParseException) {
|
|
339 |
| |
|
340 |
0
| SAXParseException parseException = (SAXParseException) e;
|
|
341 |
0
| String systemId = parseException.getSystemId();
|
|
342 |
0
| if ( systemId == null ) {
|
|
343 |
0
| systemId = "";
|
|
344 |
| } |
|
345 |
0
| String message = "Error on line "
|
|
346 |
| + parseException.getLineNumber() |
|
347 |
| + " of document " + systemId |
|
348 |
| + " : " + parseException.getMessage(); |
|
349 |
| |
|
350 |
0
| throw new DocumentException(message, e);
|
|
351 |
| } |
|
352 |
| else { |
|
353 |
2
| throw new DocumentException(e.getMessage(), e);
|
|
354 |
| } |
|
355 |
| } |
|
356 |
| } |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
23040
| public boolean isValidating() {
|
|
367 |
23040
| return validating;
|
|
368 |
| } |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
0
| public void setValidation(boolean validating) {
|
|
375 |
0
| this.validating = validating;
|
|
376 |
| } |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
11560
| public boolean isIncludeInternalDTDDeclarations() {
|
|
382 |
11560
| return includeInternalDTDDeclarations;
|
|
383 |
| } |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
10
| public void setIncludeInternalDTDDeclarations(boolean includeInternalDTDDeclarations) {
|
|
392 |
10
| this.includeInternalDTDDeclarations = includeInternalDTDDeclarations;
|
|
393 |
| } |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
11560
| public boolean isIncludeExternalDTDDeclarations() {
|
|
399 |
11560
| return includeExternalDTDDeclarations;
|
|
400 |
| } |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
8
| public void setIncludeExternalDTDDeclarations(boolean includeExternalDTDDeclarations) {
|
|
409 |
8
| this.includeExternalDTDDeclarations = includeExternalDTDDeclarations;
|
|
410 |
| } |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
11560
| public boolean isStringInternEnabled() {
|
|
417 |
11560
| return stringInternEnabled;
|
|
418 |
| } |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
0
| public void setStringInternEnabled(boolean stringInternEnabled) {
|
|
424 |
0
| this.stringInternEnabled = stringInternEnabled;
|
|
425 |
| } |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
11560
| public boolean isMergeAdjacentText() {
|
|
431 |
11560
| return mergeAdjacentText;
|
|
432 |
| } |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
14
| public void setMergeAdjacentText(boolean mergeAdjacentText) {
|
|
439 |
14
| this.mergeAdjacentText = mergeAdjacentText;
|
|
440 |
| } |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
11560
| public boolean isStripWhitespaceText() {
|
|
447 |
11560
| return stripWhitespaceText;
|
|
448 |
| } |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
0
| public void setStripWhitespaceText(boolean stripWhitespaceText) {
|
|
455 |
0
| this.stripWhitespaceText = stripWhitespaceText;
|
|
456 |
| } |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
11560
| public boolean isIgnoreComments() {
|
|
463 |
11560
| return ignoreComments;
|
|
464 |
| } |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
0
| public void setIgnoreComments(boolean ignoreComments) {
|
|
471 |
0
| this.ignoreComments = ignoreComments;
|
|
472 |
| } |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
11560
| public DocumentFactory getDocumentFactory() {
|
|
478 |
11560
| if (factory == null) {
|
|
479 |
11378
| factory = DocumentFactory.getInstance();
|
|
480 |
| } |
|
481 |
11560
| return factory;
|
|
482 |
| } |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
18
| public void setDocumentFactory(DocumentFactory factory) {
|
|
491 |
18
| this.factory = factory;
|
|
492 |
| } |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
0
| public ErrorHandler getErrorHandler() {
|
|
497 |
0
| return errorHandler;
|
|
498 |
| } |
|
499 |
| |
|
500 |
| |
|
501 |
| |
|
502 |
| |
|
503 |
| |
|
504 |
| |
|
505 |
0
| public void setErrorHandler(ErrorHandler errorHandler) {
|
|
506 |
0
| this.errorHandler = errorHandler;
|
|
507 |
| } |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
0
| public EntityResolver getEntityResolver() {
|
|
512 |
0
| return entityResolver;
|
|
513 |
| } |
|
514 |
| |
|
515 |
| |
|
516 |
| |
|
517 |
6
| public void setEntityResolver(EntityResolver entityResolver) {
|
|
518 |
6
| this.entityResolver = entityResolver;
|
|
519 |
| } |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
11560
| public XMLReader getXMLReader() throws SAXException {
|
|
524 |
11560
| if (xmlReader == null) {
|
|
525 |
11478
| xmlReader = createXMLReader();
|
|
526 |
| } |
|
527 |
11560
| return xmlReader;
|
|
528 |
| } |
|
529 |
| |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
0
| public void setXMLReader(XMLReader xmlReader) {
|
|
535 |
0
| this.xmlReader = xmlReader;
|
|
536 |
| } |
|
537 |
| |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
| |
|
543 |
| |
|
544 |
0
| public void setXMLReaderClassName(String xmlReaderClassName) throws SAXException {
|
|
545 |
0
| setXMLReader( XMLReaderFactory.createXMLReader(xmlReaderClassName) );
|
|
546 |
| } |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
42
| public void addHandler(String path, ElementHandler handler) {
|
|
557 |
42
| getDispatchHandler().addHandler(path, handler);
|
|
558 |
| } |
|
559 |
| |
|
560 |
| |
|
561 |
| |
|
562 |
| |
|
563 |
| |
|
564 |
| |
|
565 |
0
| public void removeHandler(String path) {
|
|
566 |
0
| getDispatchHandler().removeHandler(path);
|
|
567 |
| } |
|
568 |
| |
|
569 |
| |
|
570 |
| |
|
571 |
| |
|
572 |
| |
|
573 |
| |
|
574 |
| |
|
575 |
| |
|
576 |
0
| public void setDefaultHandler(ElementHandler handler) {
|
|
577 |
0
| getDispatchHandler().setDefaultHandler(handler);
|
|
578 |
| } |
|
579 |
| |
|
580 |
| |
|
581 |
| |
|
582 |
| |
|
583 |
| |
|
584 |
| |
|
585 |
0
| public void resetHandlers() {
|
|
586 |
0
| getDispatchHandler().resetHandlers();
|
|
587 |
| } |
|
588 |
| |
|
589 |
| |
|
590 |
| |
|
591 |
| |
|
592 |
| |
|
593 |
11560
| public XMLFilter getXMLFilter() {
|
|
594 |
11560
| return xmlFilter;
|
|
595 |
| } |
|
596 |
| |
|
597 |
| |
|
598 |
| |
|
599 |
| |
|
600 |
| |
|
601 |
0
| public void setXMLFilter(XMLFilter xmlFilter) {
|
|
602 |
0
| this.xmlFilter = xmlFilter;
|
|
603 |
| } |
|
604 |
| |
|
605 |
| |
|
606 |
| |
|
607 |
| |
|
608 |
| |
|
609 |
| |
|
610 |
| |
|
611 |
| |
|
612 |
| |
|
613 |
| |
|
614 |
11560
| protected XMLReader installXMLFilter(XMLReader xmlReader) {
|
|
615 |
11560
| XMLFilter xmlFilter = getXMLFilter();
|
|
616 |
11560
| if ( xmlFilter != null ) {
|
|
617 |
| |
|
618 |
0
| XMLFilter root = xmlFilter;
|
|
619 |
0
| while (true) {
|
|
620 |
0
| XMLReader parent = root.getParent();
|
|
621 |
0
| if ( parent instanceof XMLFilter ) {
|
|
622 |
0
| root = (XMLFilter) parent;
|
|
623 |
| } |
|
624 |
| else { |
|
625 |
0
| break;
|
|
626 |
| } |
|
627 |
| } |
|
628 |
0
| root.setParent(xmlReader);
|
|
629 |
0
| return xmlFilter;
|
|
630 |
| } |
|
631 |
11560
| return xmlReader;
|
|
632 |
| } |
|
633 |
| |
|
634 |
| |
|
635 |
42
| protected DispatchHandler getDispatchHandler() {
|
|
636 |
42
| if (dispatchHandler == null) {
|
|
637 |
42
| dispatchHandler = new DispatchHandler();
|
|
638 |
| } |
|
639 |
42
| return dispatchHandler;
|
|
640 |
| } |
|
641 |
| |
|
642 |
0
| protected void setDispatchHandler(DispatchHandler dispatchHandler) {
|
|
643 |
0
| this.dispatchHandler = dispatchHandler;
|
|
644 |
| } |
|
645 |
| |
|
646 |
| |
|
647 |
| |
|
648 |
| |
|
649 |
11478
| protected XMLReader createXMLReader() throws SAXException {
|
|
650 |
11478
| return SAXHelper.createXMLReader( isValidating() );
|
|
651 |
| } |
|
652 |
| |
|
653 |
| |
|
654 |
11560
| protected void configureReader(XMLReader reader, DefaultHandler contentHandler) throws DocumentException {
|
|
655 |
| |
|
656 |
11560
| SAXHelper.setParserProperty(
|
|
657 |
| reader, |
|
658 |
| "http://xml.org/sax/handlers/LexicalHandler", |
|
659 |
| contentHandler |
|
660 |
| ); |
|
661 |
| |
|
662 |
| |
|
663 |
11560
| SAXHelper.setParserProperty(
|
|
664 |
| reader, |
|
665 |
| "http://xml.org/sax/properties/lexical-handler", |
|
666 |
| contentHandler |
|
667 |
| ); |
|
668 |
| |
|
669 |
| |
|
670 |
11560
| if ( includeInternalDTDDeclarations || includeExternalDTDDeclarations ) {
|
|
671 |
12
| SAXHelper.setParserProperty(
|
|
672 |
| reader, |
|
673 |
| "http://xml.org/sax/properties/declaration-handler", |
|
674 |
| contentHandler |
|
675 |
| ); |
|
676 |
| } |
|
677 |
| |
|
678 |
| |
|
679 |
11560
| SAXHelper.setParserFeature(
|
|
680 |
| reader, |
|
681 |
| "http://xml.org/sax/features/namespaces", |
|
682 |
| true |
|
683 |
| ); |
|
684 |
| |
|
685 |
11560
| SAXHelper.setParserFeature(
|
|
686 |
| reader, |
|
687 |
| "http://xml.org/sax/features/namespace-prefixes", |
|
688 |
| false |
|
689 |
| ); |
|
690 |
| |
|
691 |
| |
|
692 |
11560
| SAXHelper.setParserFeature(
|
|
693 |
| reader, |
|
694 |
| "http://xml.org/sax/features/string-interning", |
|
695 |
| isStringInternEnabled() |
|
696 |
| ); |
|
697 |
| |
|
698 |
| |
|
699 |
| |
|
700 |
| |
|
701 |
| |
|
702 |
| |
|
703 |
| |
|
704 |
| |
|
705 |
| |
|
706 |
| |
|
707 |
| |
|
708 |
| |
|
709 |
| |
|
710 |
| |
|
711 |
| |
|
712 |
11560
| SAXHelper.setParserFeature(
|
|
713 |
| reader, |
|
714 |
| "http://xml.org/sax/features/use-locator2", |
|
715 |
| true |
|
716 |
| ); |
|
717 |
| |
|
718 |
11560
| try {
|
|
719 |
| |
|
720 |
11560
| reader.setFeature(
|
|
721 |
| "http://xml.org/sax/features/validation", |
|
722 |
| isValidating() |
|
723 |
| ); |
|
724 |
11558
| if (errorHandler != null) {
|
|
725 |
0
| reader.setErrorHandler(errorHandler);
|
|
726 |
| } |
|
727 |
| else { |
|
728 |
11558
| reader.setErrorHandler(contentHandler);
|
|
729 |
| } |
|
730 |
| } |
|
731 |
| catch (Exception e) { |
|
732 |
2
| if (isValidating()) {
|
|
733 |
2
| throw new DocumentException(
|
|
734 |
| "Validation not supported for XMLReader: " + reader, |
|
735 |
| e |
|
736 |
| ); |
|
737 |
| } |
|
738 |
| |
|
739 |
| } |
|
740 |
| } |
|
741 |
| |
|
742 |
| |
|
743 |
| |
|
744 |
11560
| protected SAXContentHandler createContentHandler(XMLReader reader) {
|
|
745 |
11560
| return new SAXContentHandler(
|
|
746 |
| getDocumentFactory(), dispatchHandler |
|
747 |
| ); |
|
748 |
| } |
|
749 |
| |
|
750 |
11496
| protected EntityResolver createDefaultEntityResolver( String documentSystemId ) {
|
|
751 |
11496
| String prefix = null;
|
|
752 |
11496
| if ( documentSystemId != null && documentSystemId.length() > 0 ) {
|
|
753 |
342
| int idx = documentSystemId.lastIndexOf( '/' );
|
|
754 |
342
| if ( idx > 0 ) {
|
|
755 |
340
| prefix = documentSystemId.substring(0, idx+1);
|
|
756 |
| |
|
757 |
| } |
|
758 |
| } |
|
759 |
11496
| return new SAXEntityResolver(prefix);
|
|
760 |
| } |
|
761 |
| |
|
762 |
| protected static class SAXEntityResolver implements EntityResolver, Serializable { |
|
763 |
| String uriPrefix; |
|
764 |
| |
|
765 |
11496
| public SAXEntityResolver(String uriPrefix) {
|
|
766 |
11496
| this.uriPrefix = uriPrefix;
|
|
767 |
| } |
|
768 |
| |
|
769 |
10
| public InputSource resolveEntity(String publicId, String systemId) {
|
|
770 |
| |
|
771 |
10
| if ( systemId != null && systemId.length() > 0 ) {
|
|
772 |
10
| if ( uriPrefix != null && systemId.indexOf( ':' ) <= 0 ) {
|
|
773 |
4
| systemId = uriPrefix + systemId;
|
|
774 |
| } |
|
775 |
| } |
|
776 |
10
| return new InputSource(systemId);
|
|
777 |
| } |
|
778 |
| } |
|
779 |
| |
|
780 |
| |
|
781 |
| |
|
782 |
| } |
|
783 |
| |
|
784 |
| |
|
785 |
| |
|
786 |
| |
|
787 |
| |
|
788 |
| |
|
789 |
| |
|
790 |
| |
|
791 |
| |
|
792 |
| |
|
793 |
| |
|
794 |
| |
|
795 |
| |
|
796 |
| |
|
797 |
| |
|
798 |
| |
|
799 |
| |
|
800 |
| |
|
801 |
| |
|
802 |
| |
|
803 |
| |
|
804 |
| |
|
805 |
| |
|
806 |
| |
|
807 |
| |
|
808 |
| |
|
809 |
| |
|
810 |
| |
|
811 |
| |
|
812 |
| |
|
813 |
| |
|
814 |
| |
|
815 |
| |
|
816 |
| |
|
817 |
| |
|
818 |
| |
|
819 |
| |
|
820 |
| |
|
821 |
| |
|
822 |
| |
|
823 |
| |
|
824 |
| |
|
825 |
| |
|
826 |
| |
|
827 |
| |
|
828 |
| |
|
829 |
| |
|
830 |
| |