public final class JAXB
extends java.lang.Object
Methods defined in this class are convenience methods that combine several basic operations
in the JAXBContext
, Unmarshaller
, and Marshaller
.
They are designed
to be the prefered methods for developers new to JAXB. They have
the following characterstics:
DataBindingException
(which will have JAXBException
as its 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:
DataBindingException
.
Similarly, the marshal methods have the following characteristic:
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.
Modifier and Type | Class and Description |
---|---|
private static class |
JAXB.Cache
To improve the performance, we'll cache the last
JAXBContext used. |
Modifier and Type | Field and Description |
---|---|
private static java.lang.ref.WeakReference<JAXB.Cache> |
cache
Cache.
|
Modifier | Constructor and Description |
---|---|
private |
JAXB()
No instanciation is allowed.
|
Modifier and Type | Method and Description |
---|---|
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.
|
private static <T> JAXBContext |
getContext(java.lang.Class<T> type)
Obtains the
JAXBContext from the given type,
by using the cache if possible. |
private static java.lang.String |
inferName(java.lang.Class clazz) |
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
private static javax.xml.transform.Result |
toResult(java.lang.Object xml)
Creates
Result from various XML representation. |
private static javax.xml.transform.Source |
toSource(java.lang.Object xml)
Creates
Source from various XML representation. |
static <T> T |
unmarshal(java.io.File xml,
java.lang.Class<T> type)
Reads in a Java object tree from the given XML input.
|
static <T> T |
unmarshal(java.io.InputStream xml,
java.lang.Class<T> type)
Reads in a Java object tree from the given XML input.
|
static <T> T |
unmarshal(java.io.Reader xml,
java.lang.Class<T> type)
Reads in a Java object tree from the given XML input.
|
static <T> T |
unmarshal(javax.xml.transform.Source xml,
java.lang.Class<T> type)
Reads in a Java object tree from the given XML input.
|
static <T> T |
unmarshal(java.lang.String xml,
java.lang.Class<T> type)
Reads in a Java object tree from the given XML input.
|
static <T> T |
unmarshal(java.net.URI xml,
java.lang.Class<T> type)
Reads in a Java object tree from the given XML input.
|
static <T> T |
unmarshal(java.net.URL xml,
java.lang.Class<T> type)
Reads in a Java object tree from the given XML input.
|
private static volatile java.lang.ref.WeakReference<JAXB.Cache> cache
JAXB.Cache.type
from GC-ed,
hence WeakReference
.private static <T> JAXBContext getContext(java.lang.Class<T> type) throws JAXBException
JAXBContext
from the given type,
by using the cache if possible.
We don't use locks to control access to cache
, but this code
should be thread-safe thanks to the immutable JAXB.Cache
and volatile
.
JAXBException
public static <T> T unmarshal(java.io.File xml, java.lang.Class<T> type)
xml
- Reads the entire file as XML.public static <T> T unmarshal(java.net.URL xml, java.lang.Class<T> type)
xml
- The resource pointed by the URL is read in its entirety.public static <T> T unmarshal(java.net.URI xml, java.lang.Class<T> type)
xml
- The URI is turned into URL
and then
follows the handling of URL.public static <T> T unmarshal(java.lang.String xml, java.lang.Class<T> type)
xml
- The string is first interpreted as an absolute URI.
If it's not a valid absolute URI
,
then it's interpreted as a Filepublic static <T> T unmarshal(java.io.InputStream xml, java.lang.Class<T> type)
xml
- The entire stream is read as an XML infoset.
Upon a successful completion, the stream will be closed by this method.public static <T> T unmarshal(java.io.Reader xml, java.lang.Class<T> type)
xml
- The character stream is read as an XML infoset.
The encoding declaration in the XML will be ignored.
Upon a successful completion, the stream will be closed by this method.public static <T> T unmarshal(javax.xml.transform.Source xml, java.lang.Class<T> type)
xml
- The XML infoset that the Source
represents is read.private static javax.xml.transform.Source toSource(java.lang.Object xml) throws java.io.IOException
Source
from various XML representation.
See unmarshal(java.io.File, java.lang.Class<T>)
for the conversion rules.java.io.IOException
public static void marshal(java.lang.Object jaxbObject, java.io.File xml)
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- XML will be written to this file. If it already exists,
it will be overwritten.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(java.lang.Object jaxbObject, java.net.URL xml)
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- The XML will be sent
to the
resource pointed by this URL. Note that not all URLs support
such operation, and exact semantics depends on the URL
implementations. In case of HTTP URLs
,
this will perform HTTP POST.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(java.lang.Object jaxbObject, java.net.URI xml)
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- The URI is turned into URL
and then
follows the handling of URL. See above.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(java.lang.Object jaxbObject, java.lang.String xml)
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- The string is first interpreted as an absolute URI.
If it's not a valid absolute URI
,
then it's interpreted as a FileDataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(java.lang.Object jaxbObject, java.io.OutputStream xml)
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- The XML will be sent to the given OutputStream
.
Upon a successful completion, the stream will be closed by this method.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(java.lang.Object jaxbObject, java.io.Writer xml)
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- The XML will be sent as a character stream to the given Writer
.
Upon a successful completion, the stream will be closed by this method.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(java.lang.Object jaxbObject, javax.xml.transform.Result xml)
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- The XML will be sent to the Result
object.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.private static void _marshal(java.lang.Object jaxbObject, java.lang.Object xml)
This method is a convenience method that combines several basic operations
in the JAXBContext
and Marshaller
. This method is designed
to be the prefered method for developers new to JAXB. This method
has the following characterstics:
DataBindingException
(which will have JAXBException
as its cause
. It is expected that
those people who prefer the checked exception would use
the rest of the JAXB API directly.
jaxbObject
- The Java object to be marshalled into XML. If this object is
a JAXBElement
, it will provide the root tag name and
the body. If this object has XmlRootElement
on its class definition, that will be used as the root tag name
and the given object will provide the body. Otherwise,
the root tag name is infered
from
the short class name
.
This parameter must not be null.xml
- Represents the receiver of XML. Objects of the following types are allowed.
Type | Operation |
---|---|
File |
XML will be written to this file. If it already exists, it will be overwritten. |
URL |
The XML will be sent to the
resource pointed by this URL. Note that not all URLs support
such operation, and exact semantics depends on the URL
implementations. In case of HTTP URLs ,
this will perform HTTP POST. |
URI |
The URI is turned into URL and then
follows the handling of URL. See above. |
String |
The string is first interpreted as an absolute URI.
If it's not a valid absolute URI ,
then it's interpreted as a File |
OutputStream |
The XML will be sent to the given OutputStream .
Upon a successful completion, the stream will be closed by this method. |
Writer |
The XML will be sent as a character stream to the given Writer .
Upon a successful completion, the stream will be closed by this method. |
Result |
The XML will be sent to the Result object. |
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.private static java.lang.String inferName(java.lang.Class clazz)
private static javax.xml.transform.Result toResult(java.lang.Object xml) throws java.io.IOException
Result
from various XML representation.
See _marshal(Object,Object)
for the conversion rules.java.io.IOException