|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.io; |
|
9 |
| |
|
10 |
| import java.io.IOException; |
|
11 |
| import java.util.HashMap; |
|
12 |
| import java.util.Iterator; |
|
13 |
| import java.util.List; |
|
14 |
| import java.util.Map; |
|
15 |
| |
|
16 |
| import org.dom4j.Attribute; |
|
17 |
| import org.dom4j.Branch; |
|
18 |
| import org.dom4j.CDATA; |
|
19 |
| import org.dom4j.CharacterData; |
|
20 |
| import org.dom4j.Comment; |
|
21 |
| import org.dom4j.Document; |
|
22 |
| import org.dom4j.DocumentType; |
|
23 |
| import org.dom4j.Element; |
|
24 |
| import org.dom4j.Entity; |
|
25 |
| import org.dom4j.Namespace; |
|
26 |
| import org.dom4j.Node; |
|
27 |
| import org.dom4j.ProcessingInstruction; |
|
28 |
| import org.dom4j.Text; |
|
29 |
| import org.dom4j.tree.NamespaceStack; |
|
30 |
| |
|
31 |
| import org.xml.sax.Attributes; |
|
32 |
| import org.xml.sax.ContentHandler; |
|
33 |
| import org.xml.sax.DTDHandler; |
|
34 |
| import org.xml.sax.EntityResolver; |
|
35 |
| import org.xml.sax.ErrorHandler; |
|
36 |
| import org.xml.sax.InputSource; |
|
37 |
| import org.xml.sax.SAXException; |
|
38 |
| import org.xml.sax.SAXNotRecognizedException; |
|
39 |
| import org.xml.sax.SAXNotSupportedException; |
|
40 |
| import org.xml.sax.XMLReader; |
|
41 |
| import org.xml.sax.ext.LexicalHandler; |
|
42 |
| import org.xml.sax.helpers.AttributesImpl; |
|
43 |
| import org.xml.sax.helpers.LocatorImpl; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| public class SAXWriter implements XMLReader { |
|
54 |
| protected static final String[] LEXICAL_HANDLER_NAMES = { |
|
55 |
| "http://xml.org/sax/properties/lexical-handler", |
|
56 |
| "http://xml.org/sax/handlers/LexicalHandler" }; |
|
57 |
| |
|
58 |
| protected static final String FEATURE_NAMESPACE_PREFIXES |
|
59 |
| = "http://xml.org/sax/features/namespace-prefixes"; |
|
60 |
| |
|
61 |
| protected static final String FEATURE_NAMESPACES |
|
62 |
| = "http://xml.org/sax/features/namespaces"; |
|
63 |
| |
|
64 |
| |
|
65 |
| private ContentHandler contentHandler; |
|
66 |
| |
|
67 |
| |
|
68 |
| private DTDHandler dtdHandler; |
|
69 |
| |
|
70 |
| |
|
71 |
| private EntityResolver entityResolver; |
|
72 |
| |
|
73 |
| private ErrorHandler errorHandler; |
|
74 |
| |
|
75 |
| |
|
76 |
| private LexicalHandler lexicalHandler; |
|
77 |
| |
|
78 |
| |
|
79 |
| private AttributesImpl attributes = new AttributesImpl(); |
|
80 |
| |
|
81 |
| |
|
82 |
| private Map features = new HashMap(); |
|
83 |
| |
|
84 |
| |
|
85 |
| private Map properties = new HashMap(); |
|
86 |
| |
|
87 |
| |
|
88 |
| private boolean declareNamespaceAttributes; |
|
89 |
| |
|
90 |
20
| public SAXWriter() {
|
|
91 |
20
| properties.put(FEATURE_NAMESPACE_PREFIXES, Boolean.FALSE);
|
|
92 |
20
| properties.put(FEATURE_NAMESPACE_PREFIXES, Boolean.TRUE);
|
|
93 |
| } |
|
94 |
| |
|
95 |
0
| public SAXWriter(ContentHandler contentHandler) {
|
|
96 |
0
| this();
|
|
97 |
0
| this.contentHandler = contentHandler;
|
|
98 |
| } |
|
99 |
| |
|
100 |
0
| public SAXWriter(ContentHandler contentHandler,
|
|
101 |
| LexicalHandler lexicalHandler) { |
|
102 |
0
| this();
|
|
103 |
0
| this.contentHandler = contentHandler;
|
|
104 |
0
| this.lexicalHandler = lexicalHandler;
|
|
105 |
| } |
|
106 |
| |
|
107 |
12
| public SAXWriter(ContentHandler contentHandler,
|
|
108 |
| LexicalHandler lexicalHandler, EntityResolver entityResolver) { |
|
109 |
12
| this();
|
|
110 |
12
| this.contentHandler = contentHandler;
|
|
111 |
12
| this.lexicalHandler = lexicalHandler;
|
|
112 |
12
| this.entityResolver = entityResolver;
|
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
39
| public void write(Node node) throws SAXException {
|
|
125 |
39
| int nodeType = node.getNodeType();
|
|
126 |
| |
|
127 |
39
| switch (nodeType) {
|
|
128 |
0
| case Node.ELEMENT_NODE:
|
|
129 |
0
| write((Element) node);
|
|
130 |
| |
|
131 |
0
| break;
|
|
132 |
| |
|
133 |
0
| case Node.ATTRIBUTE_NODE:
|
|
134 |
0
| write((Attribute) node);
|
|
135 |
| |
|
136 |
0
| break;
|
|
137 |
| |
|
138 |
0
| case Node.TEXT_NODE:
|
|
139 |
0
| write(node.getText());
|
|
140 |
| |
|
141 |
0
| break;
|
|
142 |
| |
|
143 |
0
| case Node.CDATA_SECTION_NODE:
|
|
144 |
0
| write((CDATA) node);
|
|
145 |
| |
|
146 |
0
| break;
|
|
147 |
| |
|
148 |
0
| case Node.ENTITY_REFERENCE_NODE:
|
|
149 |
0
| write((Entity) node);
|
|
150 |
| |
|
151 |
0
| break;
|
|
152 |
| |
|
153 |
0
| case Node.PROCESSING_INSTRUCTION_NODE:
|
|
154 |
0
| write((ProcessingInstruction) node);
|
|
155 |
| |
|
156 |
0
| break;
|
|
157 |
| |
|
158 |
0
| case Node.COMMENT_NODE:
|
|
159 |
0
| write((Comment) node);
|
|
160 |
| |
|
161 |
0
| break;
|
|
162 |
| |
|
163 |
0
| case Node.DOCUMENT_NODE:
|
|
164 |
0
| write((Document) node);
|
|
165 |
| |
|
166 |
0
| break;
|
|
167 |
| |
|
168 |
0
| case Node.DOCUMENT_TYPE_NODE:
|
|
169 |
0
| write((DocumentType) node);
|
|
170 |
| |
|
171 |
0
| break;
|
|
172 |
| |
|
173 |
39
| case Node.NAMESPACE_NODE:
|
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
39
| break;
|
|
178 |
| |
|
179 |
0
| default:
|
|
180 |
0
| throw new SAXException("Invalid node type: " + node);
|
|
181 |
| } |
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
20
| public void write(Document document) throws SAXException {
|
|
194 |
20
| if (document != null) {
|
|
195 |
20
| checkForNullHandlers();
|
|
196 |
| |
|
197 |
20
| documentLocator(document);
|
|
198 |
20
| startDocument();
|
|
199 |
20
| entityResolver(document);
|
|
200 |
20
| dtdHandler(document);
|
|
201 |
| |
|
202 |
20
| writeContent(document, new NamespaceStack());
|
|
203 |
20
| endDocument();
|
|
204 |
| } |
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
0
| public void write(Element element) throws SAXException {
|
|
217 |
0
| write(element, new NamespaceStack());
|
|
218 |
| } |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
0
| public void writeOpen(Element element) throws SAXException {
|
|
233 |
0
| startElement(element, null);
|
|
234 |
| } |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
0
| public void writeClose(Element element) throws SAXException {
|
|
248 |
0
| endElement(element);
|
|
249 |
| } |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
12162
| public void write(String text) throws SAXException {
|
|
261 |
12162
| if (text != null) {
|
|
262 |
12162
| char[] chars = text.toCharArray();
|
|
263 |
12162
| contentHandler.characters(chars, 0, chars.length);
|
|
264 |
| } |
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
30
| public void write(CDATA cdata) throws SAXException {
|
|
277 |
30
| String text = cdata.getText();
|
|
278 |
| |
|
279 |
30
| if (lexicalHandler != null) {
|
|
280 |
30
| lexicalHandler.startCDATA();
|
|
281 |
30
| write(text);
|
|
282 |
30
| lexicalHandler.endCDATA();
|
|
283 |
| } else { |
|
284 |
0
| write(text);
|
|
285 |
| } |
|
286 |
| } |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
71
| public void write(Comment comment) throws SAXException {
|
|
298 |
71
| if (lexicalHandler != null) {
|
|
299 |
71
| String text = comment.getText();
|
|
300 |
71
| char[] chars = text.toCharArray();
|
|
301 |
71
| lexicalHandler.comment(chars, 0, chars.length);
|
|
302 |
| } |
|
303 |
| } |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
0
| public void write(Entity entity) throws SAXException {
|
|
315 |
0
| String text = entity.getText();
|
|
316 |
| |
|
317 |
0
| if (lexicalHandler != null) {
|
|
318 |
0
| String name = entity.getName();
|
|
319 |
0
| lexicalHandler.startEntity(name);
|
|
320 |
0
| write(text);
|
|
321 |
0
| lexicalHandler.endEntity(name);
|
|
322 |
| } else { |
|
323 |
0
| write(text);
|
|
324 |
| } |
|
325 |
| } |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
2
| public void write(ProcessingInstruction pi) throws SAXException {
|
|
337 |
2
| String target = pi.getTarget();
|
|
338 |
2
| String text = pi.getText();
|
|
339 |
2
| contentHandler.processingInstruction(target, text);
|
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
0
| public boolean isDeclareNamespaceAttributes() {
|
|
351 |
0
| return declareNamespaceAttributes;
|
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
8
| public void setDeclareNamespaceAttributes(boolean declareNamespaceAttrs) {
|
|
363 |
8
| this.declareNamespaceAttributes = declareNamespaceAttrs;
|
|
364 |
| } |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
0
| public ContentHandler getContentHandler() {
|
|
376 |
0
| return contentHandler;
|
|
377 |
| } |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
8
| public void setContentHandler(ContentHandler contentHandler) {
|
|
387 |
8
| this.contentHandler = contentHandler;
|
|
388 |
| } |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
0
| public DTDHandler getDTDHandler() {
|
|
396 |
0
| return dtdHandler;
|
|
397 |
| } |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
8
| public void setDTDHandler(DTDHandler handler) {
|
|
406 |
8
| this.dtdHandler = handler;
|
|
407 |
| } |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
1
| public ErrorHandler getErrorHandler() {
|
|
415 |
1
| return errorHandler;
|
|
416 |
| } |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
1
| public void setErrorHandler(ErrorHandler errorHandler) {
|
|
425 |
1
| this.errorHandler = errorHandler;
|
|
426 |
| } |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
0
| public EntityResolver getEntityResolver() {
|
|
435 |
0
| return entityResolver;
|
|
436 |
| } |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
0
| public void setEntityResolver(EntityResolver entityResolver) {
|
|
445 |
0
| this.entityResolver = entityResolver;
|
|
446 |
| } |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
0
| public LexicalHandler getLexicalHandler() {
|
|
455 |
0
| return lexicalHandler;
|
|
456 |
| } |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
15
| public void setLexicalHandler(LexicalHandler lexicalHandler) {
|
|
465 |
15
| this.lexicalHandler = lexicalHandler;
|
|
466 |
| } |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
0
| public void setXMLReader(XMLReader xmlReader) {
|
|
475 |
0
| setContentHandler(xmlReader.getContentHandler());
|
|
476 |
0
| setDTDHandler(xmlReader.getDTDHandler());
|
|
477 |
0
| setEntityResolver(xmlReader.getEntityResolver());
|
|
478 |
0
| setErrorHandler(xmlReader.getErrorHandler());
|
|
479 |
| } |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
| |
|
491 |
| |
|
492 |
| |
|
493 |
| |
|
494 |
0
| public boolean getFeature(String name) throws SAXNotRecognizedException,
|
|
495 |
| SAXNotSupportedException { |
|
496 |
0
| Boolean answer = (Boolean) features.get(name);
|
|
497 |
| |
|
498 |
0
| return (answer != null) && answer.booleanValue();
|
|
499 |
| } |
|
500 |
| |
|
501 |
| |
|
502 |
| |
|
503 |
| |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
| |
|
515 |
8
| public void setFeature(String name, boolean value)
|
|
516 |
| throws SAXNotRecognizedException, SAXNotSupportedException { |
|
517 |
8
| if (FEATURE_NAMESPACE_PREFIXES.equals(name)) {
|
|
518 |
8
| setDeclareNamespaceAttributes(value);
|
|
519 |
0
| } else if (FEATURE_NAMESPACE_PREFIXES.equals(name)) {
|
|
520 |
0
| if (!value) {
|
|
521 |
0
| String msg = "Namespace feature is always supported in dom4j";
|
|
522 |
0
| throw new SAXNotSupportedException(msg);
|
|
523 |
| } |
|
524 |
| } |
|
525 |
| |
|
526 |
8
| features.put(name, (value) ? Boolean.TRUE : Boolean.FALSE);
|
|
527 |
| } |
|
528 |
| |
|
529 |
| |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
29
| public void setProperty(String name, Object value) {
|
|
538 |
29
| for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
|
|
539 |
50
| if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
|
|
540 |
15
| setLexicalHandler((LexicalHandler) value);
|
|
541 |
| |
|
542 |
15
| return;
|
|
543 |
| } |
|
544 |
| } |
|
545 |
| |
|
546 |
14
| properties.put(name, value);
|
|
547 |
| } |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
| |
|
558 |
| |
|
559 |
| |
|
560 |
| |
|
561 |
| |
|
562 |
0
| public Object getProperty(String name) throws SAXNotRecognizedException,
|
|
563 |
| SAXNotSupportedException { |
|
564 |
0
| for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
|
|
565 |
0
| if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
|
|
566 |
0
| return getLexicalHandler();
|
|
567 |
| } |
|
568 |
| } |
|
569 |
| |
|
570 |
0
| return properties.get(name);
|
|
571 |
| } |
|
572 |
| |
|
573 |
| |
|
574 |
| |
|
575 |
| |
|
576 |
| |
|
577 |
| |
|
578 |
| |
|
579 |
| |
|
580 |
| |
|
581 |
| |
|
582 |
0
| public void parse(String systemId) throws SAXNotSupportedException {
|
|
583 |
0
| throw new SAXNotSupportedException("This XMLReader can only accept"
|
|
584 |
| + " <dom4j> InputSource objects"); |
|
585 |
| } |
|
586 |
| |
|
587 |
| |
|
588 |
| |
|
589 |
| |
|
590 |
| |
|
591 |
| |
|
592 |
| |
|
593 |
| |
|
594 |
| |
|
595 |
| |
|
596 |
| |
|
597 |
| |
|
598 |
| |
|
599 |
8
| public void parse(InputSource input) throws SAXException {
|
|
600 |
8
| if (input instanceof DocumentInputSource) {
|
|
601 |
8
| DocumentInputSource documentInput = (DocumentInputSource) input;
|
|
602 |
8
| Document document = documentInput.getDocument();
|
|
603 |
8
| write(document);
|
|
604 |
| } else { |
|
605 |
0
| throw new SAXNotSupportedException(
|
|
606 |
| "This XMLReader can only accept " |
|
607 |
| + "<dom4j> InputSource objects"); |
|
608 |
| } |
|
609 |
| } |
|
610 |
| |
|
611 |
| |
|
612 |
| |
|
613 |
4973
| protected void writeContent(Branch branch, NamespaceStack namespaceStack)
|
|
614 |
| throws SAXException { |
|
615 |
4973
| for (Iterator iter = branch.nodeIterator(); iter.hasNext();) {
|
|
616 |
17227
| Object object = iter.next();
|
|
617 |
| |
|
618 |
17227
| if (object instanceof Element) {
|
|
619 |
4953
| write((Element) object, namespaceStack);
|
|
620 |
12274
| } else if (object instanceof CharacterData) {
|
|
621 |
12233
| if (object instanceof Text) {
|
|
622 |
12132
| Text text = (Text) object;
|
|
623 |
12132
| write(text.getText());
|
|
624 |
101
| } else if (object instanceof CDATA) {
|
|
625 |
30
| write((CDATA) object);
|
|
626 |
71
| } else if (object instanceof Comment) {
|
|
627 |
71
| write((Comment) object);
|
|
628 |
| } else { |
|
629 |
0
| throw new SAXException("Invalid Node in DOM4J content: "
|
|
630 |
| + object + " of type: " + object.getClass()); |
|
631 |
| } |
|
632 |
41
| } else if (object instanceof String) {
|
|
633 |
0
| write((String) object);
|
|
634 |
41
| } else if (object instanceof Entity) {
|
|
635 |
0
| write((Entity) object);
|
|
636 |
41
| } else if (object instanceof ProcessingInstruction) {
|
|
637 |
2
| write((ProcessingInstruction) object);
|
|
638 |
39
| } else if (object instanceof Namespace) {
|
|
639 |
39
| write((Namespace) object);
|
|
640 |
| } else { |
|
641 |
0
| throw new SAXException("Invalid Node in DOM4J content: "
|
|
642 |
| + object); |
|
643 |
| } |
|
644 |
| } |
|
645 |
| } |
|
646 |
| |
|
647 |
| |
|
648 |
| |
|
649 |
| |
|
650 |
| |
|
651 |
| |
|
652 |
| |
|
653 |
| |
|
654 |
| |
|
655 |
| |
|
656 |
| |
|
657 |
| |
|
658 |
| |
|
659 |
| |
|
660 |
20
| protected void documentLocator(Document document) throws SAXException {
|
|
661 |
20
| LocatorImpl locator = new LocatorImpl();
|
|
662 |
| |
|
663 |
20
| String publicID = null;
|
|
664 |
20
| String systemID = null;
|
|
665 |
20
| DocumentType docType = document.getDocType();
|
|
666 |
| |
|
667 |
20
| if (docType != null) {
|
|
668 |
0
| publicID = docType.getPublicID();
|
|
669 |
0
| systemID = docType.getSystemID();
|
|
670 |
| } |
|
671 |
| |
|
672 |
20
| if (publicID != null) {
|
|
673 |
0
| locator.setPublicId(publicID);
|
|
674 |
| } |
|
675 |
| |
|
676 |
20
| if (systemID != null) {
|
|
677 |
0
| locator.setSystemId(systemID);
|
|
678 |
| } |
|
679 |
| |
|
680 |
20
| locator.setLineNumber(-1);
|
|
681 |
20
| locator.setColumnNumber(-1);
|
|
682 |
| |
|
683 |
20
| contentHandler.setDocumentLocator(locator);
|
|
684 |
| } |
|
685 |
| |
|
686 |
20
| protected void entityResolver(Document document) throws SAXException {
|
|
687 |
20
| if (entityResolver != null) {
|
|
688 |
12
| DocumentType docType = document.getDocType();
|
|
689 |
| |
|
690 |
12
| if (docType != null) {
|
|
691 |
0
| String publicID = docType.getPublicID();
|
|
692 |
0
| String systemID = docType.getSystemID();
|
|
693 |
| |
|
694 |
0
| if ((publicID != null) || (systemID != null)) {
|
|
695 |
0
| try {
|
|
696 |
0
| entityResolver.resolveEntity(publicID, systemID);
|
|
697 |
| } catch (IOException e) { |
|
698 |
0
| throw new SAXException("Could not resolve publicID: "
|
|
699 |
| + publicID + " systemID: " + systemID, e); |
|
700 |
| } |
|
701 |
| } |
|
702 |
| } |
|
703 |
| } |
|
704 |
| } |
|
705 |
| |
|
706 |
| |
|
707 |
| |
|
708 |
| |
|
709 |
| |
|
710 |
| |
|
711 |
| |
|
712 |
| |
|
713 |
| |
|
714 |
| |
|
715 |
| |
|
716 |
20
| protected void dtdHandler(Document document) throws SAXException {
|
|
717 |
| } |
|
718 |
| |
|
719 |
20
| protected void startDocument() throws SAXException {
|
|
720 |
20
| contentHandler.startDocument();
|
|
721 |
| } |
|
722 |
| |
|
723 |
20
| protected void endDocument() throws SAXException {
|
|
724 |
20
| contentHandler.endDocument();
|
|
725 |
| } |
|
726 |
| |
|
727 |
4953
| protected void write(Element element, NamespaceStack namespaceStack)
|
|
728 |
| throws SAXException { |
|
729 |
4953
| int stackSize = namespaceStack.size();
|
|
730 |
4953
| AttributesImpl namespaceAttributes = startPrefixMapping(element,
|
|
731 |
| namespaceStack); |
|
732 |
4953
| startElement(element, namespaceAttributes);
|
|
733 |
4953
| writeContent(element, namespaceStack);
|
|
734 |
4953
| endElement(element);
|
|
735 |
4953
| endPrefixMapping(namespaceStack, stackSize);
|
|
736 |
| } |
|
737 |
| |
|
738 |
| |
|
739 |
| |
|
740 |
| |
|
741 |
| |
|
742 |
| |
|
743 |
| |
|
744 |
| |
|
745 |
| |
|
746 |
| |
|
747 |
| |
|
748 |
| |
|
749 |
| |
|
750 |
| |
|
751 |
| |
|
752 |
4953
| protected AttributesImpl startPrefixMapping(Element element,
|
|
753 |
| NamespaceStack namespaceStack) throws SAXException { |
|
754 |
4953
| AttributesImpl namespaceAttributes = null;
|
|
755 |
| |
|
756 |
| |
|
757 |
4953
| Namespace elementNamespace = element.getNamespace();
|
|
758 |
| |
|
759 |
4953
| if ((elementNamespace != null)
|
|
760 |
| && !isIgnoreableNamespace(elementNamespace, namespaceStack)) { |
|
761 |
36
| namespaceStack.push(elementNamespace);
|
|
762 |
36
| contentHandler.startPrefixMapping(elementNamespace.getPrefix(),
|
|
763 |
| elementNamespace.getURI()); |
|
764 |
36
| namespaceAttributes = addNamespaceAttribute(namespaceAttributes,
|
|
765 |
| elementNamespace); |
|
766 |
| } |
|
767 |
| |
|
768 |
4953
| List declaredNamespaces = element.declaredNamespaces();
|
|
769 |
| |
|
770 |
4953
| for (int i = 0, size = declaredNamespaces.size(); i < size; i++) {
|
|
771 |
39
| Namespace namespace = (Namespace) declaredNamespaces.get(i);
|
|
772 |
| |
|
773 |
39
| if (!isIgnoreableNamespace(namespace, namespaceStack)) {
|
|
774 |
3
| namespaceStack.push(namespace);
|
|
775 |
3
| contentHandler.startPrefixMapping(namespace.getPrefix(),
|
|
776 |
| namespace.getURI()); |
|
777 |
3
| namespaceAttributes = addNamespaceAttribute(
|
|
778 |
| namespaceAttributes, namespace); |
|
779 |
| } |
|
780 |
| } |
|
781 |
| |
|
782 |
4953
| return namespaceAttributes;
|
|
783 |
| } |
|
784 |
| |
|
785 |
| |
|
786 |
| |
|
787 |
| |
|
788 |
| |
|
789 |
| |
|
790 |
| |
|
791 |
| |
|
792 |
| |
|
793 |
| |
|
794 |
| |
|
795 |
| |
|
796 |
| |
|
797 |
4953
| protected void endPrefixMapping(NamespaceStack stack, int stackSize)
|
|
798 |
| throws SAXException { |
|
799 |
4953
| while (stack.size() > stackSize) {
|
|
800 |
39
| Namespace namespace = stack.pop();
|
|
801 |
| |
|
802 |
39
| if (namespace != null) {
|
|
803 |
39
| contentHandler.endPrefixMapping(namespace.getPrefix());
|
|
804 |
| } |
|
805 |
| } |
|
806 |
| } |
|
807 |
| |
|
808 |
4953
| protected void startElement(Element element,
|
|
809 |
| AttributesImpl namespaceAttributes) throws SAXException { |
|
810 |
4953
| contentHandler.startElement(element.getNamespaceURI(), element
|
|
811 |
| .getName(), element.getQualifiedName(), createAttributes( |
|
812 |
| element, namespaceAttributes)); |
|
813 |
| } |
|
814 |
| |
|
815 |
4953
| protected void endElement(Element element) throws SAXException {
|
|
816 |
4953
| contentHandler.endElement(element.getNamespaceURI(), element.getName(),
|
|
817 |
| element.getQualifiedName()); |
|
818 |
| } |
|
819 |
| |
|
820 |
4953
| protected Attributes createAttributes(Element element,
|
|
821 |
| Attributes namespaceAttributes) throws SAXException { |
|
822 |
4953
| attributes.clear();
|
|
823 |
| |
|
824 |
4953
| if (namespaceAttributes != null) {
|
|
825 |
12
| attributes.setAttributes(namespaceAttributes);
|
|
826 |
| } |
|
827 |
| |
|
828 |
4953
| for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
|
|
829 |
2609
| Attribute attribute = (Attribute) iter.next();
|
|
830 |
2609
| attributes.addAttribute(attribute.getNamespaceURI(), attribute
|
|
831 |
| .getName(), attribute.getQualifiedName(), "CDATA", |
|
832 |
| attribute.getValue()); |
|
833 |
| } |
|
834 |
| |
|
835 |
4953
| return attributes;
|
|
836 |
| } |
|
837 |
| |
|
838 |
| |
|
839 |
| |
|
840 |
| |
|
841 |
| |
|
842 |
| |
|
843 |
| |
|
844 |
| |
|
845 |
| |
|
846 |
| |
|
847 |
| |
|
848 |
| |
|
849 |
| |
|
850 |
39
| protected AttributesImpl addNamespaceAttribute(AttributesImpl attrs,
|
|
851 |
| Namespace namespace) { |
|
852 |
39
| if (declareNamespaceAttributes) {
|
|
853 |
13
| if (attrs == null) {
|
|
854 |
12
| attrs = new AttributesImpl();
|
|
855 |
| } |
|
856 |
| |
|
857 |
13
| String prefix = namespace.getPrefix();
|
|
858 |
13
| String qualifiedName = "xmlns";
|
|
859 |
| |
|
860 |
13
| if ((prefix != null) && (prefix.length() > 0)) {
|
|
861 |
8
| qualifiedName = "xmlns:" + prefix;
|
|
862 |
| } |
|
863 |
| |
|
864 |
13
| String uri = "";
|
|
865 |
13
| String localName = prefix;
|
|
866 |
13
| String type = "CDATA";
|
|
867 |
13
| String value = namespace.getURI();
|
|
868 |
| |
|
869 |
13
| attrs.addAttribute(uri, localName, qualifiedName, type, value);
|
|
870 |
| } |
|
871 |
| |
|
872 |
39
| return attrs;
|
|
873 |
| } |
|
874 |
| |
|
875 |
| |
|
876 |
| |
|
877 |
| |
|
878 |
| |
|
879 |
| |
|
880 |
| |
|
881 |
| |
|
882 |
| |
|
883 |
| |
|
884 |
| |
|
885 |
| |
|
886 |
| |
|
887 |
4992
| protected boolean isIgnoreableNamespace(Namespace namespace,
|
|
888 |
| NamespaceStack namespaceStack) { |
|
889 |
4992
| if (namespace.equals(Namespace.NO_NAMESPACE)
|
|
890 |
| || namespace.equals(Namespace.XML_NAMESPACE)) { |
|
891 |
4761
| return true;
|
|
892 |
| } |
|
893 |
| |
|
894 |
231
| String uri = namespace.getURI();
|
|
895 |
| |
|
896 |
231
| if ((uri == null) || (uri.length() <= 0)) {
|
|
897 |
0
| return true;
|
|
898 |
| } |
|
899 |
| |
|
900 |
231
| return namespaceStack.contains(namespace);
|
|
901 |
| } |
|
902 |
| |
|
903 |
| |
|
904 |
| |
|
905 |
| |
|
906 |
20
| protected void checkForNullHandlers() {
|
|
907 |
| } |
|
908 |
| } |
|
909 |
| |
|
910 |
| |
|
911 |
| |
|
912 |
| |
|
913 |
| |
|
914 |
| |
|
915 |
| |
|
916 |
| |
|
917 |
| |
|
918 |
| |
|
919 |
| |
|
920 |
| |
|
921 |
| |
|
922 |
| |
|
923 |
| |
|
924 |
| |
|
925 |
| |
|
926 |
| |
|
927 |
| |
|
928 |
| |
|
929 |
| |
|
930 |
| |
|
931 |
| |
|
932 |
| |
|
933 |
| |
|
934 |
| |
|
935 |
| |
|
936 |
| |
|
937 |
| |
|
938 |
| |
|
939 |
| |
|
940 |
| |
|
941 |
| |
|
942 |
| |
|
943 |
| |
|
944 |
| |
|
945 |
| |