1 7 8 package org.dom4j.jaxb; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.FileOutputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.io.OutputStream ; 18 import java.io.Reader ; 19 import java.io.Writer ; 20 import java.net.URL ; 21 import java.nio.charset.Charset ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import org.dom4j.Document; 27 import org.dom4j.DocumentException; 28 import org.dom4j.io.ElementModifier; 29 import org.dom4j.io.OutputFormat; 30 import org.dom4j.io.SAXModifier; 31 import org.dom4j.io.XMLWriter; 32 33 import org.xml.sax.InputSource ; 34 35 45 public class JAXBModifier extends JAXBSupport { 46 private SAXModifier modifier; 47 48 private XMLWriter xmlWriter; 49 50 private boolean pruneElements; 51 52 private OutputFormat outputFormat; 53 54 private HashMap modifiers = new HashMap (); 55 56 66 public JAXBModifier(String contextPath) { 67 super(contextPath); 68 this.outputFormat = new OutputFormat(); 69 } 70 71 84 public JAXBModifier(String contextPath, ClassLoader classloader) { 85 super(contextPath, classloader); 86 this.outputFormat = new OutputFormat(); 87 } 88 89 101 public JAXBModifier(String contextPath, OutputFormat outputFormat) { 102 super(contextPath); 103 this.outputFormat = outputFormat; 104 } 105 106 120 public JAXBModifier(String contextPath, ClassLoader classloader, 121 OutputFormat outputFormat) { 122 super(contextPath, classloader); 123 this.outputFormat = outputFormat; 124 } 125 126 140 public Document modify(File source) throws DocumentException, IOException { 141 return installModifier().modify(source); 142 } 143 144 161 public Document modify(File source, Charset charset) 162 throws DocumentException, IOException { 163 try { 164 Reader reader = new InputStreamReader (new FileInputStream (source), 165 charset); 166 167 return installModifier().modify(reader); 168 } catch (JAXBRuntimeException ex) { 169 Throwable cause = ex.getCause(); 170 throw new DocumentException(cause.getMessage(), cause); 171 } catch (FileNotFoundException ex) { 172 throw new DocumentException(ex.getMessage(), ex); 173 } 174 } 175 176 190 public Document modify(InputSource source) throws DocumentException, 191 IOException { 192 try { 193 return installModifier().modify(source); 194 } catch (JAXBRuntimeException ex) { 195 Throwable cause = ex.getCause(); 196 throw new DocumentException(cause.getMessage(), cause); 197 } 198 } 199 200 214 public Document modify(InputStream source) throws DocumentException, 215 IOException { 216 try { 217 return installModifier().modify(source); 218 } catch (JAXBRuntimeException ex) { 219 Throwable cause = ex.getCause(); 220 throw new DocumentException(cause.getMessage(), cause); 221 } 222 } 223 224 240 public Document modify(InputStream source, String systemId) 241 throws DocumentException, IOException { 242 try { 243 return installModifier().modify(source); 244 } catch (JAXBRuntimeException ex) { 245 Throwable cause = ex.getCause(); 246 throw new DocumentException(cause.getMessage(), cause); 247 } 248 } 249 250 264 public Document modify(Reader r) throws DocumentException, IOException { 265 try { 266 return installModifier().modify(r); 267 } catch (JAXBRuntimeException ex) { 268 Throwable cause = ex.getCause(); 269 throw new DocumentException(cause.getMessage(), cause); 270 } 271 } 272 273 289 public Document modify(Reader source, String systemId) 290 throws DocumentException, IOException { 291 try { 292 return installModifier().modify(source); 293 } catch (JAXBRuntimeException ex) { 294 Throwable cause = ex.getCause(); 295 throw new DocumentException(cause.getMessage(), cause); 296 } 297 } 298 299 313 public Document modify(String url) throws DocumentException, IOException { 314 try { 315 return installModifier().modify(url); 316 } catch (JAXBRuntimeException ex) { 317 Throwable cause = ex.getCause(); 318 throw new DocumentException(cause.getMessage(), cause); 319 } 320 } 321 322 336 public Document modify(URL source) throws DocumentException, IOException { 337 try { 338 return installModifier().modify(source); 339 } catch (JAXBRuntimeException ex) { 340 Throwable cause = ex.getCause(); 341 throw new DocumentException(cause.getMessage(), cause); 342 } 343 } 344 345 354 public void setOutput(File file) throws IOException { 355 createXMLWriter().setOutputStream(new FileOutputStream (file)); 356 } 357 358 367 public void setOutput(OutputStream outputStream) throws IOException { 368 createXMLWriter().setOutputStream(outputStream); 369 } 370 371 380 public void setOutput(Writer writer) throws IOException { 381 createXMLWriter().setWriter(writer); 382 } 383 384 393 public void addObjectModifier(String path, JAXBObjectModifier mod) { 394 modifiers.put(path, mod); 395 } 396 397 404 public void removeObjectModifier(String path) { 405 modifiers.remove(path); 406 getModifier().removeModifier(path); 407 } 408 409 413 public void resetObjectModifiers() { 414 modifiers.clear(); 415 getModifier().resetModifiers(); 416 } 417 418 424 public boolean isPruneElements() { 425 return pruneElements; 426 } 427 428 435 public void setPruneElements(boolean pruneElements) { 436 this.pruneElements = pruneElements; 437 } 438 439 private SAXModifier installModifier() throws IOException { 440 modifier = new SAXModifier(isPruneElements()); 441 442 modifier.resetModifiers(); 443 444 Iterator modifierIt = modifiers.entrySet().iterator(); 445 446 while (modifierIt.hasNext()) { 447 Map.Entry entry = (Map.Entry ) modifierIt.next(); 448 ElementModifier mod = new JAXBElementModifier(this, 449 (JAXBObjectModifier) entry.getValue()); 450 getModifier().addModifier((String ) entry.getKey(), mod); 451 } 452 453 modifier.setXMLWriter(getXMLWriter()); 454 455 return modifier; 456 } 457 458 private SAXModifier getModifier() { 459 if (this.modifier == null) { 460 modifier = new SAXModifier(isPruneElements()); 461 } 462 463 return modifier; 464 } 465 466 private XMLWriter getXMLWriter() { 467 return xmlWriter; 468 } 469 470 private XMLWriter createXMLWriter() throws IOException { 471 if (this.xmlWriter == null) { 472 xmlWriter = new XMLWriter(outputFormat); 473 } 474 475 return xmlWriter; 476 } 477 478 private class JAXBElementModifier implements ElementModifier { 479 private JAXBModifier jaxbModifier; 480 481 private JAXBObjectModifier objectModifier; 482 483 public JAXBElementModifier(JAXBModifier jaxbModifier, 484 JAXBObjectModifier objectModifier) { 485 this.jaxbModifier = jaxbModifier; 486 this.objectModifier = objectModifier; 487 } 488 489 public org.dom4j.Element modifyElement(org.dom4j.Element element) 490 throws Exception { 491 javax.xml.bind.Element originalObject = jaxbModifier 492 .unmarshal(element); 493 javax.xml.bind.Element modifiedObject = objectModifier 494 .modifyObject(originalObject); 495 496 return jaxbModifier.marshal(modifiedObject); 497 } 498 } 499 } 500 501 537 | Popular Tags |