net.sf.mmm.util.xml.base.jaxb
Class XmlBeanMapper<T>

java.lang.Object
  extended by net.sf.mmm.util.component.base.AbstractComponent
      extended by net.sf.mmm.util.component.base.AbstractLoggableComponent
          extended by net.sf.mmm.util.xml.base.jaxb.XmlBeanMapper<T>
Type Parameters:
T - is the generic type of the JAXB bean.

public class XmlBeanMapper<T>
extends AbstractLoggableComponent

This class is a little helper for the simple but common use of JAXB where you simply want to read or write the XML for a single JAXB annotated java bean.
ATTENTION:
This class uses an IDValidator to validate duplicate or unresolved XmlIDs 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).

Since:
2.0.0
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Field Summary
private  javax.xml.bind.JAXBContext jaxbContext
           
private  BrowsableResourceFactory resourceFactory
           
private  Class<T> xmlBeanClass
           
 
Constructor Summary
XmlBeanMapper(Class<T> xmlBeanClass)
          The constructor.
 
Method Summary
protected  void doInitialize()
          This method performs the actual initialization.
protected  javax.xml.bind.JAXBContext getJaxbContext()
           
protected  javax.xml.bind.Marshaller getOrCreateMarshaller()
          This method gets a Marshaller instance.
protected  javax.xml.bind.Unmarshaller getOrCreateUnmarshaller()
          This method gets a Marshaller instance.
protected  DataResourceFactory getResourceFactory()
           
 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)
           
protected  void validate(T jaxbBean)
          This method is invoked after the jaxbBean has been loaded and before it is saved.
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent
getLogger, setLogger
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent
doInitialized, getInitializationState, initialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

jaxbContext

private final javax.xml.bind.JAXBContext jaxbContext
See Also:
getJaxbContext()

xmlBeanClass

private final Class<T> xmlBeanClass
See Also:
loadXml(InputStream, Object)

resourceFactory

private BrowsableResourceFactory resourceFactory
See Also:
getResourceFactory()
Constructor Detail

XmlBeanMapper

public XmlBeanMapper(Class<T> xmlBeanClass)
The constructor.

Parameters:
xmlBeanClass - is the JAXB-annotated-Class that should be mapped.
Method Detail

getResourceFactory

protected DataResourceFactory getResourceFactory()
Returns:
the browsableResourceFactory

setResourceFactory

@Inject
public void setResourceFactory(BrowsableResourceFactory browsableResourceFactory)
Parameters:
browsableResourceFactory - is the BrowsableResourceFactory to set.

doInitialize

protected void doInitialize()
This method performs the actual initialization. It is called when AbstractComponent.initialize() is invoked for the first time.
ATTENTION:
When you override this method from a sub-class you need to do a super.AbstractComponent.doInitialize().

Overrides:
doInitialize in class AbstractLoggableComponent

getOrCreateMarshaller

protected javax.xml.bind.Marshaller getOrCreateMarshaller()
This method gets a Marshaller instance. This method potentially allows reusing the Marshaller (if it is thread-safe).

Returns:
the Marshaller.

getOrCreateUnmarshaller

protected javax.xml.bind.Unmarshaller getOrCreateUnmarshaller()
This method gets a Marshaller instance. This method potentially allows reusing the Marshaller (if it is thread-safe).

Returns:
the Unmarshaller.

getJaxbContext

protected javax.xml.bind.JAXBContext getJaxbContext()
Returns:
the jaxbContext

validate

protected void validate(T jaxbBean)
This method is invoked after the jaxbBean has been loaded and before it is saved. It does nothing by default but can be overridden to implement custom validation logic.

Parameters:
jaxbBean - is the JAXB bean to validate.

loadXml

public T loadXml(InputStream inputStream,
                 Object source)
This method loads the JAXB-bean as XML from the given inputStream.

Parameters:
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.
Returns:
the parsed XML converted to the according JAXB-bean.

loadXml

public T loadXml(String locationUrl)
This method loads the JAXB-bean as XML from the given location .

Parameters:
locationUrl - is the location URL for the resource pointing to the XML to parse.
Returns:
the parsed XML converted to the according JAXB-bean.

loadXml

public T loadXml(File file)
This method loads the JAXB-bean as XML from the given inputStream.

Parameters:
file - is the File with the XML to parse.
Returns:
the parsed XML converted to the according JAXB-bean.

loadXml

public T loadXml(DataResource resource)
This method loads the JAXB-bean as XML from the given resource .

Parameters:
resource - is the DataResource with the XML to parse.
Returns:
the parsed XML converted to the according JAXB-bean.

saveXml

public void saveXml(T jaxbBean,
                    DataResource resource)
This method saves the given jaxbBean as XML to the given outputStream.
ATTENTION:
The caller of this method has to close the outputStream.

Parameters:
jaxbBean - is the JAXB-bean to save as XML.
resource - is the DataResource where to write to.

saveXml

public void saveXml(T jaxbBean,
                    OutputStream outputStream)
This method saves the given jaxbBean as XML to the given outputStream.
ATTENTION:
The caller of this method has to close the outputStream.

Parameters:
jaxbBean - is the JAXB-bean to save as XML.
outputStream - is the OutputStream where to write the XML to.

saveXml

public void saveXml(T jaxbBean,
                    String locationUrl)
This method saves the given jaxbBean as XML to the given locationUrl.

Parameters:
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.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.