1 package javax.xml.bind; 2 3 import javax.xml.bind.annotation.XmlRootElement; 4 import javax.xml.namespace.QName ; 5 import javax.xml.transform.Result ; 6 import javax.xml.transform.Source ; 7 import javax.xml.transform.stream.StreamResult ; 8 import javax.xml.transform.stream.StreamSource ; 9 import java.beans.Introspector ; 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.io.OutputStream ; 14 import java.io.Reader ; 15 import java.io.Writer ; 16 import java.lang.ref.WeakReference ; 17 import java.net.HttpURLConnection ; 18 import java.net.URI ; 19 import java.net.URISyntaxException ; 20 import java.net.URL ; 21 import java.net.URLConnection ; 22 23 72 public final class JAXB { 73 76 private JAXB() {} 77 78 81 private static final class Cache { 82 final Class type; 83 final JAXBContext context; 84 85 public Cache(Class type) throws JAXBException { 86 this.type = type; 87 this.context = JAXBContext.newInstance(type); 88 } 89 } 90 91 95 private static volatile WeakReference <Cache> cache; 96 97 105 private static <T> JAXBContext getContext(Class <T> type) throws JAXBException { 106 WeakReference <Cache> c = cache; 107 if(c!=null) { 108 Cache d = c.get(); 109 if(d!=null && d.type==type) 110 return d.context; 111 } 112 113 Cache d = new Cache(type); 115 cache = new WeakReference <Cache>(d); 116 117 return d.context; 118 } 119 120 126 public static <T> T unmarshal( File xml, Class <T> type ) { 127 try { 128 JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(new StreamSource (xml), type); 129 return item.getValue(); 130 } catch (JAXBException e) { 131 throw new DataBindingException(e); 132 } 133 } 134 135 141 public static <T> T unmarshal( URL xml, Class <T> type ) { 142 try { 143 JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type); 144 return item.getValue(); 145 } catch (JAXBException e) { 146 throw new DataBindingException(e); 147 } catch (IOException e) { 148 throw new DataBindingException(e); 149 } 150 } 151 152 159 public static <T> T unmarshal( URI xml, Class <T> type ) { 160 try { 161 JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type); 162 return item.getValue(); 163 } catch (JAXBException e) { 164 throw new DataBindingException(e); 165 } catch (IOException e) { 166 throw new DataBindingException(e); 167 } 168 } 169 170 178 public static <T> T unmarshal( String xml, Class <T> type ) { 179 try { 180 JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type); 181 return item.getValue(); 182 } catch (JAXBException e) { 183 throw new DataBindingException(e); 184 } catch (IOException e) { 185 throw new DataBindingException(e); 186 } 187 } 188 189 196 public static <T> T unmarshal( InputStream xml, Class <T> type ) { 197 try { 198 JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type); 199 return item.getValue(); 200 } catch (JAXBException e) { 201 throw new DataBindingException(e); 202 } catch (IOException e) { 203 throw new DataBindingException(e); 204 } 205 } 206 207 215 public static <T> T unmarshal( Reader xml, Class <T> type ) { 216 try { 217 JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type); 218 return item.getValue(); 219 } catch (JAXBException e) { 220 throw new DataBindingException(e); 221 } catch (IOException e) { 222 throw new DataBindingException(e); 223 } 224 } 225 226 232 public static <T> T unmarshal( Source xml, Class <T> type ) { 233 try { 234 JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type); 235 return item.getValue(); 236 } catch (JAXBException e) { 237 throw new DataBindingException(e); 238 } catch (IOException e) { 239 throw new DataBindingException(e); 240 } 241 } 242 243 244 245 249 private static Source toSource(Object xml) throws IOException { 250 if(xml==null) 251 throw new IllegalArgumentException ("no XML is given"); 252 253 if (xml instanceof String ) { 254 try { 255 xml=new URI ((String )xml); 256 } catch (URISyntaxException e) { 257 xml=new File ((String )xml); 258 } 259 } 260 if (xml instanceof File ) { 261 File file = (File ) xml; 262 return new StreamSource (file); 263 } 264 if (xml instanceof URI ) { 265 URI uri = (URI ) xml; 266 xml=uri.toURL(); 267 } 268 if (xml instanceof URL ) { 269 URL url = (URL ) xml; 270 return new StreamSource (url.toExternalForm()); 271 } 272 if (xml instanceof InputStream ) { 273 InputStream in = (InputStream ) xml; 274 return new StreamSource (in); 275 } 276 if (xml instanceof Reader ) { 277 Reader r = (Reader ) xml; 278 return new StreamSource (r); 279 } 280 if (xml instanceof Source ) { 281 return (Source ) xml; 282 } 283 throw new IllegalArgumentException ("I don't understand how to handle "+xml.getClass()); 284 } 285 286 306 public static void marshal( Object jaxbObject, File xml ) { 307 _marshal(jaxbObject,xml); 308 } 309 310 333 public static void marshal( Object jaxbObject, URL xml ) { 334 _marshal(jaxbObject,xml); 335 } 336 337 357 public static void marshal( Object jaxbObject, URI xml ) { 358 _marshal(jaxbObject,xml); 359 } 360 361 382 public static void marshal( Object jaxbObject, String xml ) { 383 _marshal(jaxbObject,xml); 384 } 385 386 406 public static void marshal( Object jaxbObject, OutputStream xml ) { 407 _marshal(jaxbObject,xml); 408 } 409 410 430 public static void marshal( Object jaxbObject, Writer xml ) { 431 _marshal(jaxbObject,xml); 432 } 433 434 453 public static void marshal( Object jaxbObject, Result xml ) { 454 _marshal(jaxbObject,xml); 455 } 456 457 529 private static void _marshal( Object jaxbObject, Object xml ) { 530 try { 531 JAXBContext context; 532 533 if(jaxbObject instanceof JAXBElement) { 534 context = getContext(((JAXBElement<?>)jaxbObject).getDeclaredType()); 535 } else { 536 Class <?> clazz = jaxbObject.getClass(); 537 XmlRootElement r = clazz.getAnnotation(XmlRootElement.class); 538 context = getContext(clazz); 539 if(r==null) { 540 jaxbObject = new JAXBElement(new QName (inferName(clazz)),clazz,jaxbObject); 542 } 543 } 544 545 Marshaller m = context.createMarshaller(); 546 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); 547 m.marshal(jaxbObject, toResult(xml)); 548 } catch (JAXBException e) { 549 throw new DataBindingException(e); 550 } catch (IOException e) { 551 throw new DataBindingException(e); 552 } 553 } 554 555 private static String inferName(Class clazz) { 556 return Introspector.decapitalize(clazz.getSimpleName()); 557 } 558 559 563 private static Result toResult(Object xml) throws IOException { 564 if(xml==null) 565 throw new IllegalArgumentException ("no XML is given"); 566 567 if (xml instanceof String ) { 568 try { 569 xml=new URI ((String )xml); 570 } catch (URISyntaxException e) { 571 xml=new File ((String )xml); 572 } 573 } 574 if (xml instanceof File ) { 575 File file = (File ) xml; 576 return new StreamResult (file); 577 } 578 if (xml instanceof URI ) { 579 URI uri = (URI ) xml; 580 xml=uri.toURL(); 581 } 582 if (xml instanceof URL ) { 583 URL url = (URL ) xml; 584 URLConnection con = url.openConnection(); 585 con.setDoOutput(true); 586 con.setDoInput(false); 587 con.connect(); 588 return new StreamResult (con.getOutputStream()); 589 } 590 if (xml instanceof OutputStream ) { 591 OutputStream os = (OutputStream ) xml; 592 return new StreamResult (os); 593 } 594 if (xml instanceof Writer ) { 595 Writer w = (Writer )xml; 596 return new StreamResult (w); 597 } 598 if (xml instanceof Result ) { 599 return (Result ) xml; 600 } 601 throw new IllegalArgumentException ("I don't understand how to handle "+xml.getClass()); 602 } 603 604 } 605 | Popular Tags |