org.springframework.oxm.jaxb
Class Jaxb2Marshaller

java.lang.Object
  extended by org.springframework.oxm.jaxb.Jaxb2Marshaller
All Implemented Interfaces:
BeanClassLoaderAware, InitializingBean, GenericMarshaller, GenericUnmarshaller, Marshaller, MimeMarshaller, MimeUnmarshaller, Unmarshaller

public class Jaxb2Marshaller
extends java.lang.Object
implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware, InitializingBean

Implementation of the Marshaller interface for JAXB 2.0.

The typical usage will be to set either the contextPath or the classesToBeBound property on this bean, possibly customize the marshaller and unmarshaller by setting properties, schemas, adapters, and listeners, and to refer to it.

Since:
3.0
Author:
Arjen Poutsma
See Also:
setContextPath(String), setClassesToBeBound(Class[]), setJaxbContextProperties(Map), setMarshallerProperties(Map), setUnmarshallerProperties(Map), setSchema(Resource), setSchemas(Resource[]), setMarshallerListener(javax.xml.bind.Marshaller.Listener), setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener), setAdapters(XmlAdapter[])

Nested Class Summary
private static class Jaxb2Marshaller.ByteArrayDataSource
          DataSource that wraps around a byte array.
private static class Jaxb2Marshaller.Jaxb2AttachmentMarshaller
           
private static class Jaxb2Marshaller.Jaxb2AttachmentUnmarshaller
           
 
Field Summary
private  javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] adapters
           
private  java.lang.ClassLoader beanClassLoader
           
private static java.lang.String CID
           
private  java.lang.Class<?>[] classesToBeBound
           
private  java.lang.String contextPath
           
private  javax.xml.bind.JAXBContext jaxbContext
           
private  java.util.Map<java.lang.String,?> jaxbContextProperties
           
private  boolean lazyInit
           
protected  Log logger
          Logger available to subclasses.
private  javax.xml.bind.Marshaller.Listener marshallerListener
           
private  java.util.Map<java.lang.String,?> marshallerProperties
           
private  boolean mtomEnabled
           
private  javax.xml.validation.Schema schema
           
private  java.lang.String schemaLanguage
           
private  Resource[] schemaResources
           
private  javax.xml.bind.Unmarshaller.Listener unmarshallerListener
           
private  java.util.Map<java.lang.String,?> unmarshallerProperties
           
private  javax.xml.bind.ValidationEventHandler validationEventHandler
           
 
Constructor Summary
Jaxb2Marshaller()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected  XmlMappingException convertJaxbException(javax.xml.bind.JAXBException ex)
          Convert the given JAXBException to an appropriate exception from the org.springframework.oxm hierarchy.
private  javax.xml.bind.JAXBContext createJaxbContextFromClasses()
           
private  javax.xml.bind.JAXBContext createJaxbContextFromContextPath()
           
protected  javax.xml.bind.Marshaller createMarshaller()
          Return a newly created JAXB marshaller.
protected  javax.xml.bind.Unmarshaller createUnmarshaller()
          Return a newly created JAXB unmarshaller.
 java.lang.Class<?>[] getClassesToBeBound()
          Return the list of Java classes to be recognized by a newly created JAXBContext.
 java.lang.String getContextPath()
          Return the JAXB context path.
protected  javax.xml.bind.JAXBContext getJaxbContext()
           
protected  void initJaxbMarshaller(javax.xml.bind.Marshaller marshaller)
          Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
protected  void initJaxbUnmarshaller(javax.xml.bind.Unmarshaller unmarshaller)
          Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
private  boolean isPrimitiveWrapper(java.lang.Class<?> clazz)
          Checks whether the given type is a primitive wrapper type.
private  boolean isStandardClass(java.lang.Class<?> clazz)
          Checks whether the given type is a standard class.
private  javax.xml.validation.Schema loadSchema(Resource[] resources, java.lang.String schemaLanguage)
           
 void marshal(java.lang.Object graph, javax.xml.transform.Result result)
          Marshals the object graph with the given root into the provided Result.
 void marshal(java.lang.Object graph, javax.xml.transform.Result result, MimeContainer mimeContainer)
          Marshals the object graph with the given root into the provided Result, writing binary data to a MimeContainer.
private  void marshalStaxResult(javax.xml.bind.Marshaller jaxbMarshaller, java.lang.Object graph, javax.xml.transform.Result staxResult)
           
 void setAdapters(javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] adapters)
          Specify the XmlAdapters to be registered with the JAXB Marshaller and Unmarshaller
 void setBeanClassLoader(java.lang.ClassLoader classLoader)
          Callback that supplies the bean class loader to a bean instance.
 void setClassesToBeBound(java.lang.Class<?>... classesToBeBound)
          Set the list of Java classes to be recognized by a newly created JAXBContext.
 void setContextPath(java.lang.String contextPath)
          Set a JAXB context path.
 void setContextPaths(java.lang.String... contextPaths)
          Set multiple JAXB context paths.
 void setJaxbContextProperties(java.util.Map<java.lang.String,?> jaxbContextProperties)
          Set the JAXBContext properties.
 void setLazyInit(boolean lazyInit)
          Set whether to lazily initialize the JAXBContext for this marshaller.
 void setMarshallerListener(javax.xml.bind.Marshaller.Listener marshallerListener)
          Specify the Marshaller.Listener to be registered with the JAXB Marshaller.
 void setMarshallerProperties(java.util.Map<java.lang.String,?> properties)
          Set the JAXB Marshaller properties.
 void setMtomEnabled(boolean mtomEnabled)
          Specify whether MTOM support should be enabled or not.
 void setSchema(Resource schemaResource)
          Set the schema resource to use for validation.
 void setSchemaLanguage(java.lang.String schemaLanguage)
          Set the schema language.
 void setSchemas(Resource[] schemaResources)
          Set the schema resources to use for validation.
 void setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener unmarshallerListener)
          Set the Unmarshaller.Listener to be registered with the JAXB Unmarshaller.
 void setUnmarshallerProperties(java.util.Map<java.lang.String,?> properties)
          Set the JAXB Unmarshaller properties.
 void setValidationEventHandler(javax.xml.bind.ValidationEventHandler validationEventHandler)
          Set the JAXB validation event handler.
 boolean supports(java.lang.Class<?> clazz)
          Indicates whether this marshaller can marshal instances of the supplied type.
 boolean supports(java.lang.reflect.Type genericType)
          Indicates whether this marshaller can marshal instances of the supplied generic type.
private  boolean supportsInternal(java.lang.Class<?> clazz, boolean checkForXmlRootElement)
           
 java.lang.Object unmarshal(javax.xml.transform.Source source)
          Unmarshals the given Source into an object graph.
 java.lang.Object unmarshal(javax.xml.transform.Source source, MimeContainer mimeContainer)
          Unmarshals the given provided Source into an object graph, reading binary attachments from a MimeContainer.
private  java.lang.Object unmarshalStaxSource(javax.xml.bind.Unmarshaller jaxbUnmarshaller, javax.xml.transform.Source staxSource)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CID

private static final java.lang.String CID
See Also:
Constant Field Values

logger

protected final Log logger
Logger available to subclasses.


contextPath

private java.lang.String contextPath

classesToBeBound

private java.lang.Class<?>[] classesToBeBound

jaxbContextProperties

private java.util.Map<java.lang.String,?> jaxbContextProperties

marshallerProperties

private java.util.Map<java.lang.String,?> marshallerProperties

unmarshallerProperties

private java.util.Map<java.lang.String,?> unmarshallerProperties

marshallerListener

private javax.xml.bind.Marshaller.Listener marshallerListener

unmarshallerListener

private javax.xml.bind.Unmarshaller.Listener unmarshallerListener

validationEventHandler

private javax.xml.bind.ValidationEventHandler validationEventHandler

adapters

private javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] adapters

schemaResources

private Resource[] schemaResources

schemaLanguage

private java.lang.String schemaLanguage

mtomEnabled

private boolean mtomEnabled

beanClassLoader

private java.lang.ClassLoader beanClassLoader

jaxbContext

private javax.xml.bind.JAXBContext jaxbContext

schema

private javax.xml.validation.Schema schema

lazyInit

private boolean lazyInit
Constructor Detail

Jaxb2Marshaller

public Jaxb2Marshaller()
Method Detail

setContextPaths

public void setContextPaths(java.lang.String... contextPaths)
Set multiple JAXB context paths. The given array of context paths is converted to a colon-delimited string, as supported by JAXB.


setContextPath

public void setContextPath(java.lang.String contextPath)
Set a JAXB context path.


getContextPath

public java.lang.String getContextPath()
Return the JAXB context path.


setClassesToBeBound

public void setClassesToBeBound(java.lang.Class<?>... classesToBeBound)
Set the list of Java classes to be recognized by a newly created JAXBContext. Setting this property or "contextPath" is required.


getClassesToBeBound

public java.lang.Class<?>[] getClassesToBeBound()
Return the list of Java classes to be recognized by a newly created JAXBContext.


setJaxbContextProperties

public void setJaxbContextProperties(java.util.Map<java.lang.String,?> jaxbContextProperties)
Set the JAXBContext properties. These implementation-specific properties will be set on the underlying JAXBContext.


setMarshallerProperties

public void setMarshallerProperties(java.util.Map<java.lang.String,?> properties)
Set the JAXB Marshaller properties. These properties will be set on the underlying JAXB Marshaller, and allow for features such as indentation.

Parameters:
properties - the properties
See Also:
Marshaller.setProperty(String,Object), Marshaller.JAXB_ENCODING, Marshaller.JAXB_FORMATTED_OUTPUT, Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, Marshaller.JAXB_SCHEMA_LOCATION

setUnmarshallerProperties

public void setUnmarshallerProperties(java.util.Map<java.lang.String,?> properties)
Set the JAXB Unmarshaller properties. These properties will be set on the underlying JAXB Unmarshaller.

Parameters:
properties - the properties
See Also:
Unmarshaller.setProperty(String,Object)

setMarshallerListener

public void setMarshallerListener(javax.xml.bind.Marshaller.Listener marshallerListener)
Specify the Marshaller.Listener to be registered with the JAXB Marshaller.


setUnmarshallerListener

public void setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener unmarshallerListener)
Set the Unmarshaller.Listener to be registered with the JAXB Unmarshaller.


setValidationEventHandler

public void setValidationEventHandler(javax.xml.bind.ValidationEventHandler validationEventHandler)
Set the JAXB validation event handler. This event handler will be called by JAXB if any validation errors are encountered during calls to any of the marshal APIs.


setAdapters

public void setAdapters(javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] adapters)
Specify the XmlAdapters to be registered with the JAXB Marshaller and Unmarshaller


setSchema

public void setSchema(Resource schemaResource)
Set the schema resource to use for validation.


setSchemas

public void setSchemas(Resource[] schemaResources)
Set the schema resources to use for validation.


setSchemaLanguage

public void setSchemaLanguage(java.lang.String schemaLanguage)
Set the schema language. Default is the W3C XML Schema: http://www.w3.org/2001/XMLSchema".

See Also:
XMLConstants.W3C_XML_SCHEMA_NS_URI, XMLConstants.RELAXNG_NS_URI

setMtomEnabled

public void setMtomEnabled(boolean mtomEnabled)
Specify whether MTOM support should be enabled or not. Default is false: marshalling using XOP/MTOM not being enabled.


setLazyInit

public void setLazyInit(boolean lazyInit)
Set whether to lazily initialize the JAXBContext for this marshaller. Default is false to initialize on startup; can be switched to true.

Early initialization just applies if afterPropertiesSet() is called.


setBeanClassLoader

public void setBeanClassLoader(java.lang.ClassLoader classLoader)
Description copied from interface: BeanClassLoaderAware
Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's InitializingBean.afterPropertiesSet() method or a custom init-method.

Specified by:
setBeanClassLoader in interface BeanClassLoaderAware
Parameters:
classLoader - the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via ClassUtils.getDefaultClassLoader()

afterPropertiesSet

public final void afterPropertiesSet()
                              throws java.lang.Exception
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

getJaxbContext

protected javax.xml.bind.JAXBContext getJaxbContext()

createJaxbContextFromContextPath

private javax.xml.bind.JAXBContext createJaxbContextFromContextPath()
                                                             throws javax.xml.bind.JAXBException
Throws:
javax.xml.bind.JAXBException

createJaxbContextFromClasses

private javax.xml.bind.JAXBContext createJaxbContextFromClasses()
                                                         throws javax.xml.bind.JAXBException
Throws:
javax.xml.bind.JAXBException

loadSchema

private javax.xml.validation.Schema loadSchema(Resource[] resources,
                                               java.lang.String schemaLanguage)
                                        throws java.io.IOException,
                                               org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

supports

public boolean supports(java.lang.Class<?> clazz)
Description copied from interface: Marshaller
Indicates whether this marshaller can marshal instances of the supplied type.

Specified by:
supports in interface Marshaller
Specified by:
supports in interface Unmarshaller
Parameters:
clazz - the class that this marshaller is being asked if it can marshal
Returns:
true if this marshaller can indeed marshal instances of the supplied class; false otherwise

supports

public boolean supports(java.lang.reflect.Type genericType)
Description copied from interface: GenericMarshaller
Indicates whether this marshaller can marshal instances of the supplied generic type.

Specified by:
supports in interface GenericMarshaller
Specified by:
supports in interface GenericUnmarshaller
Parameters:
genericType - the type that this marshaller is being asked if it can marshal
Returns:
true if this marshaller can indeed marshal instances of the supplied type; false otherwise

supportsInternal

private boolean supportsInternal(java.lang.Class<?> clazz,
                                 boolean checkForXmlRootElement)

isPrimitiveWrapper

private boolean isPrimitiveWrapper(java.lang.Class<?> clazz)
Checks whether the given type is a primitive wrapper type. Compare section 8.5.1 of the JAXB2 spec.


isStandardClass

private boolean isStandardClass(java.lang.Class<?> clazz)
Checks whether the given type is a standard class. Compare section 8.5.2 of the JAXB2 spec.


marshal

public void marshal(java.lang.Object graph,
                    javax.xml.transform.Result result)
             throws XmlMappingException
Description copied from interface: Marshaller
Marshals the object graph with the given root into the provided Result.

Specified by:
marshal in interface Marshaller
Parameters:
graph - the root of the object graph to marshal
result - the result to marshal to
Throws:
XmlMappingException - if the given object cannot be marshalled to the result

marshal

public void marshal(java.lang.Object graph,
                    javax.xml.transform.Result result,
                    MimeContainer mimeContainer)
             throws XmlMappingException
Description copied from interface: MimeMarshaller
Marshals the object graph with the given root into the provided Result, writing binary data to a MimeContainer.

Specified by:
marshal in interface MimeMarshaller
Parameters:
graph - the root of the object graph to marshal
result - the result to marshal to
mimeContainer - the MIME container to write extracted binary content to
Throws:
XmlMappingException - if the given object cannot be marshalled to the result

marshalStaxResult

private void marshalStaxResult(javax.xml.bind.Marshaller jaxbMarshaller,
                               java.lang.Object graph,
                               javax.xml.transform.Result staxResult)
                        throws javax.xml.bind.JAXBException
Throws:
javax.xml.bind.JAXBException

createMarshaller

protected javax.xml.bind.Marshaller createMarshaller()
Return a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe.


initJaxbMarshaller

protected void initJaxbMarshaller(javax.xml.bind.Marshaller marshaller)
                           throws javax.xml.bind.JAXBException
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets called after creation of JAXB Marshaller, and after the respective properties have been set.

The default implementation sets the defined properties, the validation event handler, the schemas, listener, and adapters.

Throws:
javax.xml.bind.JAXBException

unmarshal

public java.lang.Object unmarshal(javax.xml.transform.Source source)
                           throws XmlMappingException
Description copied from interface: Unmarshaller
Unmarshals the given Source into an object graph.

Specified by:
unmarshal in interface Unmarshaller
Parameters:
source - the source to marshal from
Returns:
the object graph
Throws:
XmlMappingException - if the given source cannot be mapped to an object

unmarshal

public java.lang.Object unmarshal(javax.xml.transform.Source source,
                                  MimeContainer mimeContainer)
                           throws XmlMappingException
Description copied from interface: MimeUnmarshaller
Unmarshals the given provided Source into an object graph, reading binary attachments from a MimeContainer.

Specified by:
unmarshal in interface MimeUnmarshaller
Parameters:
source - the source to marshal from
mimeContainer - the MIME container to read extracted binary content from
Returns:
the object graph
Throws:
XmlMappingException - if the given source cannot be mapped to an object

unmarshalStaxSource

private java.lang.Object unmarshalStaxSource(javax.xml.bind.Unmarshaller jaxbUnmarshaller,
                                             javax.xml.transform.Source staxSource)
                                      throws javax.xml.bind.JAXBException
Throws:
javax.xml.bind.JAXBException

createUnmarshaller

protected javax.xml.bind.Unmarshaller createUnmarshaller()
Return a newly created JAXB unmarshaller. JAXB unmarshallers are not necessarily thread safe.


initJaxbUnmarshaller

protected void initJaxbUnmarshaller(javax.xml.bind.Unmarshaller unmarshaller)
                             throws javax.xml.bind.JAXBException
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets called after creation of JAXB Marshaller, and after the respective properties have been set.

The default implementation sets the defined properties, the validation event handler, the schemas, listener, and adapters.

Throws:
javax.xml.bind.JAXBException

convertJaxbException

protected XmlMappingException convertJaxbException(javax.xml.bind.JAXBException ex)
Convert the given JAXBException to an appropriate exception from the org.springframework.oxm hierarchy.

Parameters:
ex - JAXBException that occured
Returns:
the corresponding XmlMappingException