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.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.io.Reader ; 16 import java.net.URL ; 17 import java.nio.charset.Charset ; 18 19 import org.dom4j.Document; 20 import org.dom4j.DocumentException; 21 import org.dom4j.Element; 22 import org.dom4j.ElementHandler; 23 import org.dom4j.ElementPath; 24 import org.dom4j.io.SAXReader; 25 26 import org.xml.sax.InputSource ; 27 28 39 public class JAXBReader extends JAXBSupport { 40 private SAXReader reader; 41 42 private boolean pruneElements; 43 44 54 public JAXBReader(String contextPath) { 55 super(contextPath); 56 } 57 58 71 public JAXBReader(String contextPath, ClassLoader classloader) { 72 super(contextPath, classloader); 73 } 74 75 86 public Document read(File source) throws DocumentException { 87 return getReader().read(source); 88 } 89 90 104 public Document read(File file, Charset charset) throws DocumentException { 105 try { 106 Reader xmlReader = new InputStreamReader (new FileInputStream (file), 107 charset); 108 109 return getReader().read(xmlReader); 110 } catch (JAXBRuntimeException ex) { 111 Throwable cause = ex.getCause(); 112 throw new DocumentException(cause.getMessage(), cause); 113 } catch (FileNotFoundException ex) { 114 throw new DocumentException(ex.getMessage(), ex); 115 } 116 } 117 118 129 public Document read(InputSource source) throws DocumentException { 130 try { 131 return getReader().read(source); 132 } catch (JAXBRuntimeException ex) { 133 Throwable cause = ex.getCause(); 134 throw new DocumentException(cause.getMessage(), cause); 135 } 136 } 137 138 149 public Document read(InputStream source) throws DocumentException { 150 try { 151 return getReader().read(source); 152 } catch (JAXBRuntimeException ex) { 153 Throwable cause = ex.getCause(); 154 throw new DocumentException(cause.getMessage(), cause); 155 } 156 } 157 158 171 public Document read(InputStream source, String systemId) 172 throws DocumentException { 173 try { 174 return getReader().read(source); 175 } catch (JAXBRuntimeException ex) { 176 Throwable cause = ex.getCause(); 177 throw new DocumentException(cause.getMessage(), cause); 178 } 179 } 180 181 192 public Document read(Reader source) throws DocumentException { 193 try { 194 return getReader().read(source); 195 } catch (JAXBRuntimeException ex) { 196 Throwable cause = ex.getCause(); 197 throw new DocumentException(cause.getMessage(), cause); 198 } 199 } 200 201 214 public Document read(Reader source, String systemId) 215 throws DocumentException { 216 try { 217 return getReader().read(source); 218 } catch (JAXBRuntimeException ex) { 219 Throwable cause = ex.getCause(); 220 throw new DocumentException(cause.getMessage(), cause); 221 } 222 } 223 224 235 public Document read(String source) throws DocumentException { 236 try { 237 return getReader().read(source); 238 } catch (JAXBRuntimeException ex) { 239 Throwable cause = ex.getCause(); 240 throw new DocumentException(cause.getMessage(), cause); 241 } 242 } 243 244 255 public Document read(URL source) throws DocumentException { 256 try { 257 return getReader().read(source); 258 } catch (JAXBRuntimeException ex) { 259 Throwable cause = ex.getCause(); 260 throw new DocumentException(cause.getMessage(), cause); 261 } 262 } 263 264 274 public void addObjectHandler(String path, JAXBObjectHandler handler) { 275 ElementHandler eHandler = new UnmarshalElementHandler(this, handler); 276 getReader().addHandler(path, eHandler); 277 } 278 279 286 public void removeObjectHandler(String path) { 287 getReader().removeHandler(path); 288 } 289 290 300 public void addHandler(String path, ElementHandler handler) { 301 getReader().addHandler(path, handler); 302 } 303 304 311 public void removeHandler(String path) { 312 getReader().removeHandler(path); 313 } 314 315 319 public void resetHandlers() { 320 getReader().resetHandlers(); 321 } 322 323 328 public boolean isPruneElements() { 329 return pruneElements; 330 } 331 332 338 public void setPruneElements(boolean pruneElements) { 339 this.pruneElements = pruneElements; 340 341 if (pruneElements) { 342 getReader().setDefaultHandler(new PruningElementHandler()); 343 } 344 } 345 346 private SAXReader getReader() { 347 if (reader == null) { 348 reader = new SAXReader(); 349 } 350 351 return reader; 352 } 353 354 private class UnmarshalElementHandler implements ElementHandler { 355 private JAXBReader jaxbReader; 356 357 private JAXBObjectHandler handler; 358 359 public UnmarshalElementHandler(JAXBReader documentReader, 360 JAXBObjectHandler handler) { 361 this.jaxbReader = documentReader; 362 this.handler = handler; 363 } 364 365 public void onStart(ElementPath elementPath) { 366 } 367 368 public void onEnd(ElementPath elementPath) { 369 try { 370 org.dom4j.Element elem = elementPath.getCurrent(); 371 372 javax.xml.bind.Element jaxbObject 373 = (javax.xml.bind.Element) jaxbReader.unmarshal(elem); 374 375 if (jaxbReader.isPruneElements()) { 376 elem.detach(); 377 } 378 379 handler.handleObject(jaxbObject); 380 } catch (Exception ex) { 381 throw new JAXBRuntimeException(ex); 382 } 383 } 384 } 385 386 private class PruningElementHandler implements ElementHandler { 387 public PruningElementHandler() { 388 } 389 390 public void onStart(ElementPath parm1) { 391 } 392 393 public void onEnd(ElementPath elementPath) { 394 Element elem = elementPath.getCurrent(); 395 elem.detach(); 396 elem = null; 397 } 398 } 399 } 400 401 437 | Popular Tags |