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