1
2
3
4
5
6
7
8
9
10 package org.dom4j.io;
11
12 import java.io.IOException;
13
14 import org.dom4j.DocumentFactory;
15 import org.dom4j.Element;
16 import org.dom4j.ElementHandler;
17 import org.xml.sax.Attributes;
18 import org.xml.sax.Locator;
19 import org.xml.sax.SAXException;
20
21 /***
22 * This extension of the SAXContentHandler writes SAX events immediately to the provided XMLWriter,
23 * unless some {@link org.dom4.ElementHandler} is still handling the current Element.
24 *
25 * @see org.dom4j.io.SAXContentHandler
26 *
27 * @author Wonne Keysers (Realsoftware.be)
28 */
29 class SAXModifyContentHandler extends SAXContentHandler {
30
31 private XMLWriter xmlWriter;
32
33 public SAXModifyContentHandler() {
34 }
35
36 public SAXModifyContentHandler(DocumentFactory documentFactory) {
37 super(documentFactory);
38 }
39
40 public SAXModifyContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler) {
41 super(documentFactory, elementHandler);
42 }
43
44 public SAXModifyContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler,
45 ElementStack elementStack) {
46 super(documentFactory, elementHandler, elementStack);
47 }
48
49 public void setXMLWriter(XMLWriter xmlWriter) {
50 this.xmlWriter = xmlWriter;
51 }
52
53 public void startCDATA() throws SAXException {
54 super.startCDATA();
55 if (!activeHandlers() && xmlWriter != null) {
56 xmlWriter.startCDATA();
57 }
58 }
59
60 public void startDTD(String name, String publicId, String systemId) throws SAXException {
61 super.startDTD(name, publicId, systemId);
62 if (xmlWriter != null) {
63 xmlWriter.startDTD(name, publicId, systemId);
64 }
65 }
66
67 public void endDTD() throws org.xml.sax.SAXException {
68 super.endDTD();
69 if (xmlWriter != null) {
70 xmlWriter.endDTD();
71 }
72 }
73
74 public void comment(char[] parm1, int parm2, int parm3) throws SAXException {
75 super.comment(parm1, parm2, parm3);
76 if (!activeHandlers() && xmlWriter != null) {
77 xmlWriter.comment(parm1, parm2, parm3);
78 }
79 }
80
81 public void startEntity(String name) throws SAXException {
82 super.startEntity(name);
83 if (xmlWriter != null) {
84 xmlWriter.startEntity(name);
85 }
86 }
87
88 public void endCDATA() throws org.xml.sax.SAXException {
89 super.endCDATA();
90 if (!activeHandlers() && xmlWriter != null) {
91 xmlWriter.endCDATA();
92 }
93 }
94
95 public void endEntity(String name) throws SAXException {
96 super.endEntity(name);
97 if (xmlWriter != null) {
98 xmlWriter.endEntity(name);
99 }
100 }
101
102 public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName)
103 throws SAXException {
104 super.unparsedEntityDecl(name, publicId, systemId, notationName);
105 if (!activeHandlers() && xmlWriter != null) {
106 xmlWriter.unparsedEntityDecl(name, publicId, systemId, notationName);
107 }
108 }
109
110 public void notationDecl(String name, String publicId, String systemId) throws SAXException {
111 super.notationDecl(name, publicId, systemId);
112 if (xmlWriter != null) {
113 xmlWriter.notationDecl(name, publicId, systemId);
114 }
115 }
116
117 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
118 super.startElement(uri, localName, qName, atts);
119
120 if (!activeHandlers() && xmlWriter != null) {
121 xmlWriter.startElement(uri, localName, qName, atts);
122 }
123 }
124
125 public void startDocument() throws SAXException {
126 super.startDocument();
127 if (xmlWriter != null) {
128 xmlWriter.startDocument();
129 }
130 }
131
132 public void ignorableWhitespace(char[] parm1, int parm2, int parm3) throws SAXException {
133 super.ignorableWhitespace(parm1, parm2, parm3);
134 if (!activeHandlers() && xmlWriter != null) {
135 xmlWriter.ignorableWhitespace(parm1, parm2, parm3);
136 }
137 }
138
139 public void processingInstruction(String target, String data) throws SAXException {
140 super.processingInstruction(target, data);
141 if (!activeHandlers() && xmlWriter != null) {
142 xmlWriter.processingInstruction(target, data);
143 }
144 }
145
146 public void setDocumentLocator(Locator locator) {
147 super.setDocumentLocator(locator);
148 if (xmlWriter != null) {
149 xmlWriter.setDocumentLocator(locator);
150 }
151 }
152
153 public void skippedEntity(String name) throws SAXException {
154 super.skippedEntity(name);
155 if (!activeHandlers() && xmlWriter != null) {
156 xmlWriter.skippedEntity(name);
157 }
158 }
159
160 public void endDocument() throws SAXException {
161 super.endDocument();
162 if (xmlWriter != null) {
163 xmlWriter.endDocument();
164 }
165 }
166
167 public void startPrefixMapping(String prefix, String uri) throws SAXException {
168 super.startPrefixMapping(prefix, uri);
169 if (xmlWriter != null) {
170 xmlWriter.startPrefixMapping(prefix, uri);
171 }
172 }
173
174 public void endElement(String uri, String localName, String qName) throws SAXException {
175 ElementHandler currentHandler = getElementStack().getDispatchHandler().getHandler(getElementStack().getPath());
176
177 super.endElement(uri, localName, qName);
178
179 if (!activeHandlers()) {
180 if (xmlWriter != null) {
181 if (currentHandler == null) {
182 xmlWriter.endElement(uri, localName, qName);
183 }
184 else if (currentHandler instanceof SAXModifyElementHandler) {
185 SAXModifyElementHandler modifyHandler = (SAXModifyElementHandler) currentHandler;
186 Element modifiedElement = modifyHandler.getModifiedElement();
187 try {
188 xmlWriter.write(modifiedElement);
189 }
190 catch (IOException ex) {
191 throw new SAXModifyException(ex);
192 }
193 }
194 }
195 }
196 }
197
198 public void endPrefixMapping(String prefix) throws SAXException {
199 super.endPrefixMapping(prefix);
200 if (xmlWriter != null) {
201 xmlWriter.endPrefixMapping(prefix);
202 }
203 }
204
205 public void characters(char[] parm1, int parm2, int parm3) throws SAXException {
206 super.characters(parm1, parm2, parm3);
207 if (!activeHandlers() && xmlWriter != null) {
208 xmlWriter.characters(parm1, parm2, parm3);
209 }
210 }
211
212 protected XMLWriter getXMLWriter() {
213 return this.xmlWriter;
214 }
215
216 private boolean activeHandlers() {
217 return getElementStack().getDispatchHandler().getActiveHandlerCount() > 0;
218 }
219
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268