|
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 |
|