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