1 7 8 package org.dom4j.io; 9 10 import java.io.File ; 11 import java.io.InputStream ; 12 import java.io.Reader ; 13 import java.net.URL ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.dom4j.Document; 19 import org.dom4j.DocumentException; 20 import org.dom4j.DocumentFactory; 21 22 import org.xml.sax.InputSource ; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.XMLReader ; 25 26 44 public class SAXModifier { 45 private XMLWriter xmlWriter; 46 47 private XMLReader xmlReader; 48 49 private boolean pruneElements; 50 51 private SAXModifyReader modifyReader; 52 53 private HashMap modifiers = new HashMap (); 54 55 61 public SAXModifier() { 62 } 63 64 74 public SAXModifier(boolean pruneElements) { 75 this.pruneElements = pruneElements; 76 } 77 78 85 public SAXModifier(XMLReader xmlReader) { 86 this.xmlReader = xmlReader; 87 } 88 89 99 public SAXModifier(XMLReader xmlReader, boolean pruneElements) { 100 this.xmlReader = xmlReader; 101 } 102 103 117 public Document modify(File source) throws DocumentException { 118 try { 119 return installModifyReader().read(source); 120 } catch (SAXModifyException ex) { 121 Throwable cause = ex.getCause(); 122 throw new DocumentException(cause.getMessage(), cause); 123 } 124 } 125 126 140 public Document modify(InputSource source) throws DocumentException { 141 try { 142 return installModifyReader().read(source); 143 } catch (SAXModifyException ex) { 144 Throwable cause = ex.getCause(); 145 throw new DocumentException(cause.getMessage(), cause); 146 } 147 } 148 149 163 public Document modify(InputStream source) throws DocumentException { 164 try { 165 return installModifyReader().read(source); 166 } catch (SAXModifyException ex) { 167 Throwable cause = ex.getCause(); 168 throw new DocumentException(cause.getMessage(), cause); 169 } 170 } 171 172 188 public Document modify(InputStream source, String systemId) 189 throws DocumentException { 190 try { 191 return installModifyReader().read(source); 192 } catch (SAXModifyException ex) { 193 Throwable cause = ex.getCause(); 194 throw new DocumentException(cause.getMessage(), cause); 195 } 196 } 197 198 212 public Document modify(Reader source) throws DocumentException { 213 try { 214 return installModifyReader().read(source); 215 } catch (SAXModifyException ex) { 216 Throwable cause = ex.getCause(); 217 throw new DocumentException(cause.getMessage(), cause); 218 } 219 } 220 221 237 public Document modify(Reader source, String systemId) 238 throws DocumentException { 239 try { 240 return installModifyReader().read(source); 241 } catch (SAXModifyException ex) { 242 Throwable cause = ex.getCause(); 243 throw new DocumentException(cause.getMessage(), cause); 244 } 245 } 246 247 261 public Document modify(URL source) throws DocumentException { 262 try { 263 return installModifyReader().read(source); 264 } catch (SAXModifyException ex) { 265 Throwable cause = ex.getCause(); 266 throw new DocumentException(cause.getMessage(), cause); 267 } 268 } 269 270 284 public Document modify(String source) throws DocumentException { 285 try { 286 return installModifyReader().read(source); 287 } catch (SAXModifyException ex) { 288 Throwable cause = ex.getCause(); 289 throw new DocumentException(cause.getMessage(), cause); 290 } 291 } 292 293 303 public void addModifier(String path, ElementModifier modifier) { 304 this.modifiers.put(path, modifier); 305 } 306 307 311 public void resetModifiers() { 312 this.modifiers.clear(); 313 getSAXModifyReader().resetHandlers(); 314 } 315 316 323 public void removeModifier(String path) { 324 this.modifiers.remove(path); 325 getSAXModifyReader().removeHandler(path); 326 } 327 328 334 public DocumentFactory getDocumentFactory() { 335 return getSAXModifyReader().getDocumentFactory(); 336 } 337 338 345 public void setDocumentFactory(DocumentFactory factory) { 346 getSAXModifyReader().setDocumentFactory(factory); 347 } 348 349 354 public XMLWriter getXMLWriter() { 355 return this.xmlWriter; 356 } 357 358 364 public void setXMLWriter(XMLWriter writer) { 365 this.xmlWriter = writer; 366 } 367 368 374 public boolean isPruneElements() { 375 return pruneElements; 376 } 377 378 private SAXReader installModifyReader() throws DocumentException { 379 try { 380 SAXModifyReader reader = getSAXModifyReader(); 381 382 if (isPruneElements()) { 383 modifyReader.setDispatchHandler(new PruningDispatchHandler()); 384 } 385 386 reader.resetHandlers(); 387 388 Iterator modifierIt = this.modifiers.entrySet().iterator(); 389 390 while (modifierIt.hasNext()) { 391 Map.Entry entry = (Map.Entry ) modifierIt.next(); 392 393 SAXModifyElementHandler handler = new SAXModifyElementHandler( 394 (ElementModifier) entry.getValue()); 395 reader.addHandler((String ) entry.getKey(), handler); 396 } 397 398 reader.setXMLWriter(getXMLWriter()); 399 reader.setXMLReader(getXMLReader()); 400 401 return reader; 402 } catch (SAXException ex) { 403 throw new DocumentException(ex.getMessage(), ex); 404 } 405 } 406 407 private XMLReader getXMLReader() throws SAXException { 408 if (this.xmlReader == null) { 409 xmlReader = SAXHelper.createXMLReader(false); 410 } 411 412 return this.xmlReader; 413 } 414 415 private SAXModifyReader getSAXModifyReader() { 416 if (modifyReader == null) { 417 modifyReader = new SAXModifyReader(); 418 } 419 420 return modifyReader; 421 } 422 } 423 424 460 | Popular Tags |