org.springframework.oxm.jibx
Class JibxMarshaller

java.lang.Object
  extended by org.springframework.oxm.support.AbstractMarshaller
      extended by org.springframework.oxm.jibx.JibxMarshaller
All Implemented Interfaces:
InitializingBean, Marshaller, Unmarshaller

public class JibxMarshaller
extends AbstractMarshaller
implements InitializingBean

Implementation of the Marshaller and Unmarshaller interfaces for JiBX.

The typical usage will be to set the targetClass and optionally the bindingName property on this bean.

Since:
3.0
Author:
Arjen Poutsma
See Also:
org.jibx.runtime.IMarshallingContext, org.jibx.runtime.IUnmarshallingContext

Field Summary
private  IBindingFactory bindingFactory
           
private  java.lang.String bindingName
           
private  java.lang.String docTypeInternalSubset
           
private  java.lang.String docTypePublicId
           
private  java.lang.String docTypeRootElementName
           
private  java.lang.String docTypeSystemId
           
private  java.lang.String encoding
           
private  int indent
           
private  java.lang.Boolean standalone
           
private  java.lang.Class<?> targetClass
           
private  javax.xml.transform.TransformerFactory transformerFactory
           
 
Fields inherited from class org.springframework.oxm.support.AbstractMarshaller
logger
 
Constructor Summary
JibxMarshaller()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
 XmlMappingException convertJibxException(JiBXException ex, boolean marshalling)
          Convert the given JiBXException to an appropriate exception from the org.springframework.oxm hierarchy.
protected  IMarshallingContext createMarshallingContext()
          Create a new IMarshallingContext, configured with the correct indentation.
protected  IUnmarshallingContext createUnmarshallingContext()
          Create a new IUnmarshallingContext.
private  void marshalDocument(IMarshallingContext marshallingContext, java.lang.Object graph)
           
protected  void marshalDomNode(java.lang.Object graph, org.w3c.dom.Node node)
          Abstract template method for marshalling the given object graph to a DOM Node.
protected  void marshalOutputStream(java.lang.Object graph, java.io.OutputStream outputStream)
          Abstract template method for marshalling the given object graph to a OutputStream.
protected  void marshalSaxHandlers(java.lang.Object graph, org.xml.sax.ContentHandler contentHandler, org.xml.sax.ext.LexicalHandler lexicalHandler)
          Abstract template method for marshalling the given object graph to a SAX ContentHandler.
protected  void marshalWriter(java.lang.Object graph, java.io.Writer writer)
          Abstract template method for marshalling the given object graph to a Writer.
protected  void marshalXmlEventWriter(java.lang.Object graph, javax.xml.stream.XMLEventWriter eventWriter)
          Abstract template method for marshalling the given object to a StAX XMLEventWriter.
protected  void marshalXmlStreamWriter(java.lang.Object graph, javax.xml.stream.XMLStreamWriter streamWriter)
          Abstract template method for marshalling the given object to a StAX XMLStreamWriter.
 void setBindingName(java.lang.String bindingName)
          Set the optional binding name for this instance.
 void setDocTypeInternalSubset(java.lang.String docTypeInternalSubset)
          Sets the internal subset Id for the DTD declaration written when marshalling.
 void setDocTypePublicId(java.lang.String docTypePublicId)
          Sets the public Id for the DTD declaration written when marshalling.
 void setDocTypeRootElementName(java.lang.String docTypeRootElementName)
          Sets the root element name for the DTD declaration written when marshalling.
 void setDocTypeSystemId(java.lang.String docTypeSystemId)
          Sets the system Id for the DTD declaration written when marshalling.
 void setEncoding(java.lang.String encoding)
          Set the document encoding using for marshalling.
 void setIndent(int indent)
          Set the number of nesting indent spaces.
 void setStandalone(java.lang.Boolean standalone)
          Set the document standalone flag for marshalling.
 void setTargetClass(java.lang.Class<?> targetClass)
          Set the target class for this instance.
 boolean supports(java.lang.Class<?> clazz)
          Indicates whether this marshaller can marshal instances of the supplied type.
private  void transformAndMarshal(java.lang.Object graph, javax.xml.transform.Result result)
           
private  java.lang.Object transformAndUnmarshal(javax.xml.transform.Source source)
           
protected  java.lang.Object unmarshalDomNode(org.w3c.dom.Node node)
          Abstract template method for unmarshalling from a given DOM Node.
protected  java.lang.Object unmarshalInputStream(java.io.InputStream inputStream)
          Abstract template method for unmarshalling from a given InputStream.
protected  java.lang.Object unmarshalReader(java.io.Reader reader)
          Abstract template method for unmarshalling from a given Reader.
protected  java.lang.Object unmarshalSaxReader(org.xml.sax.XMLReader xmlReader, org.xml.sax.InputSource inputSource)
          Abstract template method for unmarshalling using a given SAX XMLReader and InputSource.
protected  java.lang.Object unmarshalXmlEventReader(javax.xml.stream.XMLEventReader eventReader)
          Abstract template method for unmarshalling from a given Stax XMLEventReader.
protected  java.lang.Object unmarshalXmlStreamReader(javax.xml.stream.XMLStreamReader streamReader)
          Abstract template method for unmarshalling from a given Stax XMLStreamReader.
 
Methods inherited from class org.springframework.oxm.support.AbstractMarshaller
createDocumentBuilder, createDocumentBuilderFactory, createXmlReader, marshal, marshalDomResult, marshalSaxResult, marshalStaxResult, marshalStreamResult, unmarshal, unmarshalDomSource, unmarshalSaxSource, unmarshalStaxSource, unmarshalStreamSource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

targetClass

private java.lang.Class<?> targetClass

bindingName

private java.lang.String bindingName

indent

private int indent

encoding

private java.lang.String encoding

standalone

private java.lang.Boolean standalone

docTypeRootElementName

private java.lang.String docTypeRootElementName

docTypeSystemId

private java.lang.String docTypeSystemId

docTypePublicId

private java.lang.String docTypePublicId

docTypeInternalSubset

private java.lang.String docTypeInternalSubset

bindingFactory

private IBindingFactory bindingFactory

transformerFactory

private javax.xml.transform.TransformerFactory transformerFactory
Constructor Detail

JibxMarshaller

public JibxMarshaller()
Method Detail

setTargetClass

public void setTargetClass(java.lang.Class<?> targetClass)
Set the target class for this instance. This property is required.


setBindingName

public void setBindingName(java.lang.String bindingName)
Set the optional binding name for this instance.


setIndent

public void setIndent(int indent)
Set the number of nesting indent spaces. Default is -1, i.e. no indentation.


setEncoding

public void setEncoding(java.lang.String encoding)
Set the document encoding using for marshalling. Default is UTF-8.


setStandalone

public void setStandalone(java.lang.Boolean standalone)
Set the document standalone flag for marshalling. By default, this flag is not present.


setDocTypeRootElementName

public void setDocTypeRootElementName(java.lang.String docTypeRootElementName)
Sets the root element name for the DTD declaration written when marshalling. By default, this is null (i.e. no DTD declaration is written). If set to a value, the system ID or public ID also need to be set.

See Also:
setDocTypeSystemId(String), setDocTypePublicId(String)

setDocTypeSystemId

public void setDocTypeSystemId(java.lang.String docTypeSystemId)
Sets the system Id for the DTD declaration written when marshalling. By default, this is null. Only used when the root element also has been set. Set either this property or docTypePublicId, not both.

See Also:
setDocTypeRootElementName(String)

setDocTypePublicId

public void setDocTypePublicId(java.lang.String docTypePublicId)
Sets the public Id for the DTD declaration written when marshalling. By default, this is null. Only used when the root element also has been set. Set either this property or docTypeSystemId, not both.

See Also:
setDocTypeRootElementName(String)

setDocTypeInternalSubset

public void setDocTypeInternalSubset(java.lang.String docTypeInternalSubset)
Sets the internal subset Id for the DTD declaration written when marshalling. By default, this is null. Only used when the root element also has been set.

See Also:
setDocTypeRootElementName(String)

afterPropertiesSet

public void afterPropertiesSet()
                        throws JiBXException
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:
JiBXException

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

marshalOutputStream

protected void marshalOutputStream(java.lang.Object graph,
                                   java.io.OutputStream outputStream)
                            throws XmlMappingException,
                                   java.io.IOException
Description copied from class: AbstractMarshaller
Abstract template method for marshalling the given object graph to a OutputStream.

Specified by:
marshalOutputStream in class AbstractMarshaller
Parameters:
graph - the root of the object graph to marshal
outputStream - the OutputStream to write to
Throws:
XmlMappingException - if the given object cannot be marshalled to the writer
java.io.IOException - if an I/O exception occurs

marshalWriter

protected void marshalWriter(java.lang.Object graph,
                             java.io.Writer writer)
                      throws XmlMappingException,
                             java.io.IOException
Description copied from class: AbstractMarshaller
Abstract template method for marshalling the given object graph to a Writer.

Specified by:
marshalWriter in class AbstractMarshaller
Parameters:
graph - the root of the object graph to marshal
writer - the Writer to write to
Throws:
XmlMappingException - if the given object cannot be marshalled to the writer
java.io.IOException - if an I/O exception occurs

marshalDocument

private void marshalDocument(IMarshallingContext marshallingContext,
                             java.lang.Object graph)
                      throws java.io.IOException,
                             JiBXException
Throws:
java.io.IOException
JiBXException

marshalXmlStreamWriter

protected void marshalXmlStreamWriter(java.lang.Object graph,
                                      javax.xml.stream.XMLStreamWriter streamWriter)
                               throws XmlMappingException
Description copied from class: AbstractMarshaller
Abstract template method for marshalling the given object to a StAX XMLStreamWriter.

Specified by:
marshalXmlStreamWriter in class AbstractMarshaller
Parameters:
graph - the root of the object graph to marshal
streamWriter - the XMLStreamWriter to write to
Throws:
XmlMappingException - if the given object cannot be marshalled to the DOM node

marshalDomNode

protected void marshalDomNode(java.lang.Object graph,
                              org.w3c.dom.Node node)
                       throws XmlMappingException
Description copied from class: AbstractMarshaller
Abstract template method for marshalling the given object graph to a DOM Node.

In practice, node is be a Document node, a DocumentFragment node, or a Element node. In other words, a node that accepts children.

Specified by:
marshalDomNode in class AbstractMarshaller
Parameters:
graph - the root of the object graph to marshal
node - the DOM node that will contain the result tree
Throws:
XmlMappingException - if the given object cannot be marshalled to the DOM node
See Also:
Document, DocumentFragment, Element

marshalSaxHandlers

protected void marshalSaxHandlers(java.lang.Object graph,
                                  org.xml.sax.ContentHandler contentHandler,
                                  org.xml.sax.ext.LexicalHandler lexicalHandler)
                           throws XmlMappingException
Description copied from class: AbstractMarshaller
Abstract template method for marshalling the given object graph to a SAX ContentHandler.

Specified by:
marshalSaxHandlers in class AbstractMarshaller
Parameters:
graph - the root of the object graph to marshal
contentHandler - the SAX ContentHandler
lexicalHandler - the SAX2 LexicalHandler. Can be null.
Throws:
XmlMappingException - if the given object cannot be marshalled to the handlers

transformAndMarshal

private void transformAndMarshal(java.lang.Object graph,
                                 javax.xml.transform.Result result)
                          throws java.io.IOException
Throws:
java.io.IOException

marshalXmlEventWriter

protected void marshalXmlEventWriter(java.lang.Object graph,
                                     javax.xml.stream.XMLEventWriter eventWriter)
Description copied from class: AbstractMarshaller
Abstract template method for marshalling the given object to a StAX XMLEventWriter.

Specified by:
marshalXmlEventWriter in class AbstractMarshaller
Parameters:
graph - the root of the object graph to marshal
eventWriter - the XMLEventWriter to write to

unmarshalInputStream

protected java.lang.Object unmarshalInputStream(java.io.InputStream inputStream)
                                         throws XmlMappingException,
                                                java.io.IOException
Description copied from class: AbstractMarshaller
Abstract template method for unmarshalling from a given InputStream.

Specified by:
unmarshalInputStream in class AbstractMarshaller
Parameters:
inputStream - the InputStreamStream to read from
Returns:
the object graph
Throws:
XmlMappingException - if the given stream cannot be converted to an object
java.io.IOException - if an I/O exception occurs

unmarshalReader

protected java.lang.Object unmarshalReader(java.io.Reader reader)
                                    throws XmlMappingException,
                                           java.io.IOException
Description copied from class: AbstractMarshaller
Abstract template method for unmarshalling from a given Reader.

Specified by:
unmarshalReader in class AbstractMarshaller
Parameters:
reader - the Reader to read from
Returns:
the object graph
Throws:
XmlMappingException - if the given reader cannot be converted to an object
java.io.IOException - if an I/O exception occurs

unmarshalXmlStreamReader

protected java.lang.Object unmarshalXmlStreamReader(javax.xml.stream.XMLStreamReader streamReader)
Description copied from class: AbstractMarshaller
Abstract template method for unmarshalling from a given Stax XMLStreamReader.

Specified by:
unmarshalXmlStreamReader in class AbstractMarshaller
Parameters:
streamReader - the XMLStreamReader to read from
Returns:
the object graph

unmarshalXmlEventReader

protected java.lang.Object unmarshalXmlEventReader(javax.xml.stream.XMLEventReader eventReader)
Description copied from class: AbstractMarshaller
Abstract template method for unmarshalling from a given Stax XMLEventReader.

Specified by:
unmarshalXmlEventReader in class AbstractMarshaller
Parameters:
eventReader - the XMLEventReader to read from
Returns:
the object graph

unmarshalDomNode

protected java.lang.Object unmarshalDomNode(org.w3c.dom.Node node)
                                     throws XmlMappingException
Description copied from class: AbstractMarshaller
Abstract template method for unmarshalling from a given DOM Node.

Specified by:
unmarshalDomNode in class AbstractMarshaller
Parameters:
node - the DOM node that contains the objects to be unmarshalled
Returns:
the object graph
Throws:
XmlMappingException - if the given DOM node cannot be mapped to an object

unmarshalSaxReader

protected java.lang.Object unmarshalSaxReader(org.xml.sax.XMLReader xmlReader,
                                              org.xml.sax.InputSource inputSource)
                                       throws XmlMappingException,
                                              java.io.IOException
Description copied from class: AbstractMarshaller
Abstract template method for unmarshalling using a given SAX XMLReader and InputSource.

Specified by:
unmarshalSaxReader in class AbstractMarshaller
Parameters:
xmlReader - the SAX XMLReader to parse with
inputSource - the input source to parse from
Returns:
the object graph
Throws:
XmlMappingException - if the given reader and input source cannot be converted to an object
java.io.IOException - if an I/O exception occurs

transformAndUnmarshal

private java.lang.Object transformAndUnmarshal(javax.xml.transform.Source source)
                                        throws java.io.IOException
Throws:
java.io.IOException

createMarshallingContext

protected IMarshallingContext createMarshallingContext()
                                                throws JiBXException
Create a new IMarshallingContext, configured with the correct indentation.

Returns:
the created marshalling context
Throws:
JiBXException - in case of errors

createUnmarshallingContext

protected IUnmarshallingContext createUnmarshallingContext()
                                                    throws JiBXException
Create a new IUnmarshallingContext.

Returns:
the created unmarshalling context
Throws:
JiBXException - in case of errors

convertJibxException

public XmlMappingException convertJibxException(JiBXException ex,
                                                boolean marshalling)
Convert the given JiBXException to an appropriate exception from the org.springframework.oxm hierarchy.

A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since JiBX itself does not make this distinction in its exception hierarchy.

Parameters:
ex - JiBXException that occured
marshalling - indicates whether the exception occurs during marshalling (true), or unmarshalling (false)
Returns:
the corresponding XmlMappingException