JAXBpublic final class JAXB extends Object Class that defines convenience methods for common, simple use of JAXB.
Methods defined in this class are convenience methods that combine several basic operations
in the {@link JAXBContext}, {@link Unmarshaller}, and {@link Marshaller}.
They are designed
to be the prefered methods for developers new to JAXB. They have
the following characterstics:
- Generally speaking, the performance is not necessarily optimal.
It is expected that people who need to write performance
critical code will use the rest of the JAXB API directly.
- Errors that happen during the processing is wrapped into
{@link DataBindingException} (which will have {@link JAXBException}
as its {@link Throwable#getCause() cause}. It is expected that
people who prefer the checked exception would use
the rest of the JAXB API directly.
In addition, the unmarshal methods have the following characteristic:
- Schema validation is not performed on the input XML.
The processing will try to continue even if there
are errors in the XML, as much as possible. Only as
the last resort, this method fails with {@link DataBindingException}.
Similarly, the marshal methods have the following characteristic:
- The processing will try to continue even if the Java object tree
does not meet the validity requirement. Only as
the last resort, this method fails with {@link DataBindingException}.
All the methods on this class require non-null arguments to all parameters.
The unmarshal methods either fail with an exception or return
a non-null value. |
Fields Summary |
---|
private static volatile WeakReference | cacheCache. We don't want to prevent the {@link Cache#type} from GC-ed,
hence {@link WeakReference}. |
Constructors Summary |
---|
private JAXB()No instanciation is allowed.
|
Methods Summary |
---|
private static void | _marshal(java.lang.Object jaxbObject, java.lang.Object xml)Writes a Java object tree to XML and store it to the specified location.
This method is a convenience method that combines several basic operations
in the {@link JAXBContext} and {@link Marshaller}. This method is designed
to be the prefered method for developers new to JAXB. This method
has the following characterstics:
- Generally speaking, the performance is not necessarily optimal.
It is expected that those people who need to write performance
critical code will use the rest of the JAXB API directly.
- Errors that happen during the processing is wrapped into
{@link DataBindingException} (which will have {@link JAXBException}
as its {@link Throwable#getCause() cause}. It is expected that
those people who prefer the checked exception would use
the rest of the JAXB API directly.
try {
JAXBContext context;
if(jaxbObject instanceof JAXBElement) {
context = getContext(((JAXBElement<?>)jaxbObject).getDeclaredType());
} else {
Class<?> clazz = jaxbObject.getClass();
XmlRootElement r = clazz.getAnnotation(XmlRootElement.class);
context = getContext(clazz);
if(r==null) {
// we need to infer the name
jaxbObject = new JAXBElement(new QName(inferName(clazz)),clazz,jaxbObject);
}
}
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
m.marshal(jaxbObject, toResult(xml));
} catch (JAXBException e) {
throw new DataBindingException(e);
} catch (IOException e) {
throw new DataBindingException(e);
}
| private static javax.xml.bind.JAXBContext | getContext(java.lang.Class type)Obtains the {@link JAXBContext} from the given type,
by using the cache if possible.
We don't use locks to control access to {@link #cache}, but this code
should be thread-safe thanks to the immutable {@link Cache} and {@code volatile}.
WeakReference<Cache> c = cache;
if(c!=null) {
Cache d = c.get();
if(d!=null && d.type==type)
return d.context;
}
// overwrite the cache
Cache d = new Cache(type);
cache = new WeakReference<Cache>(d);
return d.context;
| private static java.lang.String | inferName(java.lang.Class clazz)
return Introspector.decapitalize(clazz.getSimpleName());
| public static void | marshal(java.lang.Object jaxbObject, java.io.File xml)Writes a Java object tree to XML and store it to the specified location.
_marshal(jaxbObject,xml);
| public static void | marshal(java.lang.Object jaxbObject, java.net.URL xml)Writes a Java object tree to XML and store it to the specified location.
_marshal(jaxbObject,xml);
| public static void | marshal(java.lang.Object jaxbObject, java.net.URI xml)Writes a Java object tree to XML and store it to the specified location.
_marshal(jaxbObject,xml);
| public static void | marshal(java.lang.Object jaxbObject, java.lang.String xml)Writes a Java object tree to XML and store it to the specified location.
_marshal(jaxbObject,xml);
| public static void | marshal(java.lang.Object jaxbObject, java.io.OutputStream xml)Writes a Java object tree to XML and store it to the specified location.
_marshal(jaxbObject,xml);
| public static void | marshal(java.lang.Object jaxbObject, java.io.Writer xml)Writes a Java object tree to XML and store it to the specified location.
_marshal(jaxbObject,xml);
| public static void | marshal(java.lang.Object jaxbObject, javax.xml.transform.Result xml)Writes a Java object tree to XML and store it to the specified location.
_marshal(jaxbObject,xml);
| private static javax.xml.transform.Result | toResult(java.lang.Object xml)Creates {@link Result} from various XML representation.
See {@link #_marshal(Object,Object)} for the conversion rules.
if(xml==null)
throw new IllegalArgumentException("no XML is given");
if (xml instanceof String) {
try {
xml=new URI((String)xml);
} catch (URISyntaxException e) {
xml=new File((String)xml);
}
}
if (xml instanceof File) {
File file = (File) xml;
return new StreamResult(file);
}
if (xml instanceof URI) {
URI uri = (URI) xml;
xml=uri.toURL();
}
if (xml instanceof URL) {
URL url = (URL) xml;
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(false);
con.connect();
return new StreamResult(con.getOutputStream());
}
if (xml instanceof OutputStream) {
OutputStream os = (OutputStream) xml;
return new StreamResult(os);
}
if (xml instanceof Writer) {
Writer w = (Writer)xml;
return new StreamResult(w);
}
if (xml instanceof Result) {
return (Result) xml;
}
throw new IllegalArgumentException("I don't understand how to handle "+xml.getClass());
| private static javax.xml.transform.Source | toSource(java.lang.Object xml)Creates {@link Source} from various XML representation.
See {@link #unmarshal} for the conversion rules.
if(xml==null)
throw new IllegalArgumentException("no XML is given");
if (xml instanceof String) {
try {
xml=new URI((String)xml);
} catch (URISyntaxException e) {
xml=new File((String)xml);
}
}
if (xml instanceof File) {
File file = (File) xml;
return new StreamSource(file);
}
if (xml instanceof URI) {
URI uri = (URI) xml;
xml=uri.toURL();
}
if (xml instanceof URL) {
URL url = (URL) xml;
return new StreamSource(url.toExternalForm());
}
if (xml instanceof InputStream) {
InputStream in = (InputStream) xml;
return new StreamSource(in);
}
if (xml instanceof Reader) {
Reader r = (Reader) xml;
return new StreamSource(r);
}
if (xml instanceof Source) {
return (Source) xml;
}
throw new IllegalArgumentException("I don't understand how to handle "+xml.getClass());
| public static T | unmarshal(java.io.File xml, java.lang.Class type)Reads in a Java object tree from the given XML input.
try {
JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(new StreamSource(xml), type);
return item.getValue();
} catch (JAXBException e) {
throw new DataBindingException(e);
}
| public static T | unmarshal(java.net.URL xml, java.lang.Class type)Reads in a Java object tree from the given XML input.
try {
JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type);
return item.getValue();
} catch (JAXBException e) {
throw new DataBindingException(e);
} catch (IOException e) {
throw new DataBindingException(e);
}
| public static T | unmarshal(java.net.URI xml, java.lang.Class type)Reads in a Java object tree from the given XML input.
try {
JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type);
return item.getValue();
} catch (JAXBException e) {
throw new DataBindingException(e);
} catch (IOException e) {
throw new DataBindingException(e);
}
| public static T | unmarshal(java.lang.String xml, java.lang.Class type)Reads in a Java object tree from the given XML input.
try {
JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type);
return item.getValue();
} catch (JAXBException e) {
throw new DataBindingException(e);
} catch (IOException e) {
throw new DataBindingException(e);
}
| public static T | unmarshal(java.io.InputStream xml, java.lang.Class type)Reads in a Java object tree from the given XML input.
try {
JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type);
return item.getValue();
} catch (JAXBException e) {
throw new DataBindingException(e);
} catch (IOException e) {
throw new DataBindingException(e);
}
| public static T | unmarshal(java.io.Reader xml, java.lang.Class type)Reads in a Java object tree from the given XML input.
try {
JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type);
return item.getValue();
} catch (JAXBException e) {
throw new DataBindingException(e);
} catch (IOException e) {
throw new DataBindingException(e);
}
| public static T | unmarshal(javax.xml.transform.Source xml, java.lang.Class type)Reads in a Java object tree from the given XML input.
try {
JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type);
return item.getValue();
} catch (JAXBException e) {
throw new DataBindingException(e);
} catch (IOException e) {
throw new DataBindingException(e);
}
|
|