T
- is the generic type of the JAXB bean.public class XmlBeanMapper<T> extends AbstractLoggableComponent implements ValidationEventHandler
read
or write
the XML for a single JAXB
annotated java bean. IDValidator
to validate duplicate or unresolved XmlID
s
on un-marshaling
. This is unfortunately not the default for JAXB and also NOT part
of the JAXB-API. So this feature depends on the actual JAXB implementation you are using. We support the default
implementation build into the JRE (com.sun.internal.xml.bind). However if jaxb-impl
(com.sun.xml.bind) is NOT
on your classpath it will replace the default implementation. For this reason we also support jaxb-impl
as
fallback. Other implementations are NOT supported (everything should work but ID-validation is turned off then).Modifier and Type | Field and Description |
---|---|
private JAXBContext |
jaxbContext |
private BrowsableResourceFactory |
resourceFactory |
private StaxUtil |
staxUtil |
private boolean |
xIncludeAware |
private Class<T> |
xmlBeanClass |
Constructor and Description |
---|
XmlBeanMapper(Class<T> xmlBeanClass)
The constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
doInitialize()
This method performs the actual
initialization . |
protected JAXBContext |
getJaxbContext() |
protected Marshaller |
getOrCreateMarshaller()
This method gets a
Marshaller instance. |
protected Unmarshaller |
getOrCreateUnmarshaller()
This method gets a
Marshaller instance. |
protected DataResourceFactory |
getResourceFactory() |
protected StaxUtil |
getStaxUtil() |
boolean |
handleEvent(ValidationEvent event) |
protected boolean |
isIgnoreUndefinedNodes() |
boolean |
isXIncludeAware() |
T |
loadXml(DataResource resource)
This method loads the JAXB-bean as XML from the given
resource . |
T |
loadXml(File file)
This method loads the JAXB-bean as XML from the given
inputStream . |
T |
loadXml(InputStream inputStream,
Object source)
This method loads the JAXB-bean as XML from the given
inputStream . |
T |
loadXml(String locationUrl)
This method loads the JAXB-bean as XML from the given
location . |
void |
saveXml(T jaxbBean,
DataResource resource)
This method saves the given
jaxbBean as XML to the given outputStream . |
void |
saveXml(T jaxbBean,
OutputStream outputStream)
This method saves the given
jaxbBean as XML to the given outputStream . |
void |
saveXml(T jaxbBean,
String locationUrl)
This method saves the given
jaxbBean as XML to the given locationUrl . |
void |
setResourceFactory(BrowsableResourceFactory browsableResourceFactory) |
void |
setStaxUtil(StaxUtil staxUtil) |
void |
setXIncludeAware(boolean isXIncludeAware) |
protected void |
validate(T jaxbBean)
This method is invoked after the
jaxbBean has been loaded and before it is saved. |
createLogger, getLogger
doInitialized, getInitializationState, initialize
private final JAXBContext jaxbContext
private final Class<T> xmlBeanClass
loadXml(InputStream, Object)
private BrowsableResourceFactory resourceFactory
private StaxUtil staxUtil
private boolean xIncludeAware
protected StaxUtil getStaxUtil()
@Inject public void setStaxUtil(StaxUtil staxUtil)
staxUtil
- is the staxUtil to setprotected DataResourceFactory getResourceFactory()
@Inject public void setResourceFactory(BrowsableResourceFactory browsableResourceFactory)
browsableResourceFactory
- is the BrowsableResourceFactory
to set.protected void doInitialize()
AbstractComponent
initialization
. It is called when AbstractComponent.initialize()
is
invoked for the first time. super.
AbstractComponent.doInitialize()
.doInitialize
in class AbstractLoggableComponent
protected boolean isIgnoreUndefinedNodes()
true
if undefined tags and attributes should be ignored (may be useful for compatibility),
false
if they shall cause an exception (default is false
).public boolean isXIncludeAware()
true
if loadXml(DataResource)
should support XIncludes when reading the XML, false
otherwise.public void setXIncludeAware(boolean isXIncludeAware)
isXIncludeAware
- is the new value of isXIncludeAware()
.protected Marshaller getOrCreateMarshaller()
Marshaller
instance. This method potentially allows reusing the Marshaller
(if
it is thread-safe).Marshaller
.protected Unmarshaller getOrCreateUnmarshaller()
Marshaller
instance. This method potentially allows reusing the Marshaller
(if
it is thread-safe).Unmarshaller
.protected JAXBContext getJaxbContext()
protected void validate(T jaxbBean)
jaxbBean
has been loaded and before it is saved. It does nothing by
default but can be overridden to implement custom validation logic.jaxbBean
- is the JAXB bean to validate.public T loadXml(InputStream inputStream, Object source)
inputStream
.inputStream
- is the InputStream
with the XML to parse.source
- describes the source of the invalid XML. Typically this will be the filename where the XML was read
from. It is used in in the exception message. This will help to find the problem easier.public T loadXml(String locationUrl)
location
.locationUrl
- is the location URL for the resource
pointing to the XML to parse.public T loadXml(File file)
inputStream
.file
- is the File
with the XML to parse.public T loadXml(DataResource resource)
resource
.resource
- is the DataResource
with the XML to parse.public void saveXml(T jaxbBean, DataResource resource)
jaxbBean
as XML to the given outputStream
. close
the outputStream
.jaxbBean
- is the JAXB-bean to save as XML.resource
- is the DataResource
where to write
to.public void saveXml(T jaxbBean, OutputStream outputStream)
jaxbBean
as XML to the given outputStream
. close
the outputStream
.jaxbBean
- is the JAXB-bean to save as XML.outputStream
- is the OutputStream
where to write the XML to.public void saveXml(T jaxbBean, String locationUrl)
jaxbBean
as XML to the given locationUrl
. jaxbBean
- is the JAXB-bean to save as XML.locationUrl
- is the location URL for the resource
where to write the XML to.
Typically a file-URL.public boolean handleEvent(ValidationEvent event)
handleEvent
in interface ValidationEventHandler
Copyright © 2001–2016 mmm-Team. All rights reserved.