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 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| class SAXModifyContentHandler extends SAXContentHandler { |
30 |
| |
31 |
| private XMLWriter xmlWriter; |
32 |
| |
33 |
0
| public SAXModifyContentHandler() {
|
34 |
| } |
35 |
| |
36 |
0
| public SAXModifyContentHandler(DocumentFactory documentFactory) {
|
37 |
0
| super(documentFactory);
|
38 |
| } |
39 |
| |
40 |
0
| public SAXModifyContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler) {
|
41 |
0
| super(documentFactory, elementHandler);
|
42 |
| } |
43 |
| |
44 |
0
| public SAXModifyContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler,
|
45 |
| ElementStack elementStack) { |
46 |
0
| super(documentFactory, elementHandler, elementStack);
|
47 |
| } |
48 |
| |
49 |
0
| public void setXMLWriter(XMLWriter xmlWriter) {
|
50 |
0
| this.xmlWriter = xmlWriter;
|
51 |
| } |
52 |
| |
53 |
0
| public void startCDATA() throws SAXException {
|
54 |
0
| super.startCDATA();
|
55 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
56 |
0
| xmlWriter.startCDATA();
|
57 |
| } |
58 |
| } |
59 |
| |
60 |
0
| public void startDTD(String name, String publicId, String systemId) throws SAXException {
|
61 |
0
| super.startDTD(name, publicId, systemId);
|
62 |
0
| if (xmlWriter != null) {
|
63 |
0
| xmlWriter.startDTD(name, publicId, systemId);
|
64 |
| } |
65 |
| } |
66 |
| |
67 |
0
| public void endDTD() throws org.xml.sax.SAXException {
|
68 |
0
| super.endDTD();
|
69 |
0
| if (xmlWriter != null) {
|
70 |
0
| xmlWriter.endDTD();
|
71 |
| } |
72 |
| } |
73 |
| |
74 |
0
| public void comment(char[] parm1, int parm2, int parm3) throws SAXException {
|
75 |
0
| super.comment(parm1, parm2, parm3);
|
76 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
77 |
0
| xmlWriter.comment(parm1, parm2, parm3);
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
0
| public void startEntity(String name) throws SAXException {
|
82 |
0
| super.startEntity(name);
|
83 |
0
| if (xmlWriter != null) {
|
84 |
0
| xmlWriter.startEntity(name);
|
85 |
| } |
86 |
| } |
87 |
| |
88 |
0
| public void endCDATA() throws org.xml.sax.SAXException {
|
89 |
0
| super.endCDATA();
|
90 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
91 |
0
| xmlWriter.endCDATA();
|
92 |
| } |
93 |
| } |
94 |
| |
95 |
0
| public void endEntity(String name) throws SAXException {
|
96 |
0
| super.endEntity(name);
|
97 |
0
| if (xmlWriter != null) {
|
98 |
0
| xmlWriter.endEntity(name);
|
99 |
| } |
100 |
| } |
101 |
| |
102 |
0
| public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName)
|
103 |
| throws SAXException { |
104 |
0
| super.unparsedEntityDecl(name, publicId, systemId, notationName);
|
105 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
106 |
0
| xmlWriter.unparsedEntityDecl(name, publicId, systemId, notationName);
|
107 |
| } |
108 |
| } |
109 |
| |
110 |
0
| public void notationDecl(String name, String publicId, String systemId) throws SAXException {
|
111 |
0
| super.notationDecl(name, publicId, systemId);
|
112 |
0
| if (xmlWriter != null) {
|
113 |
0
| xmlWriter.notationDecl(name, publicId, systemId);
|
114 |
| } |
115 |
| } |
116 |
| |
117 |
0
| public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
|
118 |
0
| super.startElement(uri, localName, qName, atts);
|
119 |
| |
120 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
121 |
0
| xmlWriter.startElement(uri, localName, qName, atts);
|
122 |
| } |
123 |
| } |
124 |
| |
125 |
0
| public void startDocument() throws SAXException {
|
126 |
0
| super.startDocument();
|
127 |
0
| if (xmlWriter != null) {
|
128 |
0
| xmlWriter.startDocument();
|
129 |
| } |
130 |
| } |
131 |
| |
132 |
0
| public void ignorableWhitespace(char[] parm1, int parm2, int parm3) throws SAXException {
|
133 |
0
| super.ignorableWhitespace(parm1, parm2, parm3);
|
134 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
135 |
0
| xmlWriter.ignorableWhitespace(parm1, parm2, parm3);
|
136 |
| } |
137 |
| } |
138 |
| |
139 |
0
| public void processingInstruction(String target, String data) throws SAXException {
|
140 |
0
| super.processingInstruction(target, data);
|
141 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
142 |
0
| xmlWriter.processingInstruction(target, data);
|
143 |
| } |
144 |
| } |
145 |
| |
146 |
0
| public void setDocumentLocator(Locator locator) {
|
147 |
0
| super.setDocumentLocator(locator);
|
148 |
0
| if (xmlWriter != null) {
|
149 |
0
| xmlWriter.setDocumentLocator(locator);
|
150 |
| } |
151 |
| } |
152 |
| |
153 |
0
| public void skippedEntity(String name) throws SAXException {
|
154 |
0
| super.skippedEntity(name);
|
155 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
156 |
0
| xmlWriter.skippedEntity(name);
|
157 |
| } |
158 |
| } |
159 |
| |
160 |
0
| public void endDocument() throws SAXException {
|
161 |
0
| super.endDocument();
|
162 |
0
| if (xmlWriter != null) {
|
163 |
0
| xmlWriter.endDocument();
|
164 |
| } |
165 |
| } |
166 |
| |
167 |
0
| public void startPrefixMapping(String prefix, String uri) throws SAXException {
|
168 |
0
| super.startPrefixMapping(prefix, uri);
|
169 |
0
| if (xmlWriter != null) {
|
170 |
0
| xmlWriter.startPrefixMapping(prefix, uri);
|
171 |
| } |
172 |
| } |
173 |
| |
174 |
0
| public void endElement(String uri, String localName, String qName) throws SAXException {
|
175 |
0
| ElementHandler currentHandler = getElementStack().getDispatchHandler().getHandler(getElementStack().getPath());
|
176 |
| |
177 |
0
| super.endElement(uri, localName, qName);
|
178 |
| |
179 |
0
| if (!activeHandlers()) {
|
180 |
0
| if (xmlWriter != null) {
|
181 |
0
| if (currentHandler == null) {
|
182 |
0
| xmlWriter.endElement(uri, localName, qName);
|
183 |
| } |
184 |
0
| else if (currentHandler instanceof SAXModifyElementHandler) {
|
185 |
0
| SAXModifyElementHandler modifyHandler = (SAXModifyElementHandler) currentHandler;
|
186 |
0
| Element modifiedElement = modifyHandler.getModifiedElement();
|
187 |
0
| try {
|
188 |
0
| xmlWriter.write(modifiedElement);
|
189 |
| } |
190 |
| catch (IOException ex) { |
191 |
0
| throw new SAXModifyException(ex);
|
192 |
| } |
193 |
| } |
194 |
| } |
195 |
| } |
196 |
| } |
197 |
| |
198 |
0
| public void endPrefixMapping(String prefix) throws SAXException {
|
199 |
0
| super.endPrefixMapping(prefix);
|
200 |
0
| if (xmlWriter != null) {
|
201 |
0
| xmlWriter.endPrefixMapping(prefix);
|
202 |
| } |
203 |
| } |
204 |
| |
205 |
0
| public void characters(char[] parm1, int parm2, int parm3) throws SAXException {
|
206 |
0
| super.characters(parm1, parm2, parm3);
|
207 |
0
| if (!activeHandlers() && xmlWriter != null) {
|
208 |
0
| xmlWriter.characters(parm1, parm2, parm3);
|
209 |
| } |
210 |
| } |
211 |
| |
212 |
0
| protected XMLWriter getXMLWriter() {
|
213 |
0
| return this.xmlWriter;
|
214 |
| } |
215 |
| |
216 |
0
| private boolean activeHandlers() {
|
217 |
0
| 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 |
| |