Top Description Fields Constructors Methods
org.springframework.oxm.support

public abstract Class AbstractMarshaller

extends Object
implements Marshaller, Unmarshaller
Class Inheritance
All Implemented Interfaces
org.springframework.oxm.Unmarshaller, org.springframework.oxm.Marshaller
Known Direct Subclasses
org.springframework.oxm.xstream.XStreamMarshaller
Imports
java.io.IOException, .InputStream, .OutputStream, .Reader, .StringReader, .Writer, javax.xml.parsers.DocumentBuilder, .DocumentBuilderFactory, .ParserConfigurationException, .SAXParser, .SAXParserFactory, javax.xml.stream.XMLEventReader, .XMLEventWriter, .XMLStreamReader, .XMLStreamWriter, javax.xml.transform.Result, .Source, javax.xml.transform.dom.DOMResult, .DOMSource, javax.xml.transform.sax.SAXResult, .SAXSource, javax.xml.transform.stax.StAXSource, javax.xml.transform.stream.StreamResult, .StreamSource, org.apache.commons.logging.Log, .LogFactory, org.w3c.dom.Document, .Node, org.xml.sax.ContentHandler, .EntityResolver, .InputSource, .SAXException, .XMLReader, org.xml.sax.ext.LexicalHandler, org.springframework.lang.Nullable, org.springframework.oxm.Marshaller, .Unmarshaller, .UnmarshallingFailureException, .XmlMappingException, org.springframework.util.Assert, org.springframework.util.xml.StaxUtils

Abstract implementation of the Marshaller and Unmarshaller interface. This implementation inspects the given Source or Result, and delegates further handling to overridable template methods.
Authors
Arjen Poutsma, Juergen Hoeller
Since
3.0

Field Summary

Modifier and TypeField and Description
private DocumentBuilderFactory
private final Object
protected final Log
logger

Logger available to subclasses.

private static final EntityResolver
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Document
buildDocument()

Build a new Document from this marshaller's DocumentBuilderFactory, as a placeholder for a DOM node.

protected DocumentBuilder

Returns:

the DocumentBuilder
createDocumentBuilder
(DocumentBuilderFactory
the DocumentBuilderFactory that the DocumentBuilder should be created with
factory
)

Create a DocumentBuilder that this marshaller will use for creating DOM documents when passed an empty DOMSource.

protected DocumentBuilderFactory

Returns:

the DocumentBuilderFactory
createDocumentBuilderFactory
()

Create a DocumentBuilder that this marshaller will use for creating DOM documents when passed an empty DOMSource.

protected XMLReader

Returns:

the XMLReader
createXmlReader
()

Create an XMLReader that this marshaller will when passed an empty SAXSource.

protected String
getDefaultEncoding()

Determine the default encoding to use for marshalling or unmarshalling from a byte stream, or null if none.

public boolean
isProcessExternalEntities()

Return whether XML external entities are allowed.

public boolean
isSupportDtd()

Return whether DTD parsing is supported.

public final void
marshal(Object
the root of the object graph to marshal
graph
,
Result
the result to marshal to
result
)

Implements org.springframework.oxm.Marshaller.marshal.

Marshals the object graph with the given root into the provided javax.xml.transform.Result.
protected abstract void
marshalDomNode(Object
the root of the object graph to marshal
graph
,
Node
the DOM node that will contain the result tree
node
)

Abstract template method for marshalling the given object graph to a DOM Node.

protected void
marshalDomResult(Object
the root of the object graph to marshal
graph
,
DOMResult
the DOMResult
domResult
)

Template method for handling DOMResults.

protected abstract void
marshalOutputStream(Object
the root of the object graph to marshal
graph
,
OutputStream
the OutputStream to write to
outputStream
)

Abstract template method for marshalling the given object graph to a OutputStream.

protected abstract void
marshalSaxHandlers(Object
the root of the object graph to marshal
graph
,
ContentHandler
the SAX ContentHandler
contentHandler
,
LexicalHandler
the SAX2 LexicalHandler. Can be null.
lexicalHandler
)

Abstract template method for marshalling the given object graph to a SAX ContentHandler.

protected void
marshalSaxResult(Object
the root of the object graph to marshal
graph
,
SAXResult
the SAXResult
saxResult
)

Template method for handling SAXResults.

protected void
marshalStaxResult(Object
the root of the object graph to marshal
graph
,
Result
a JAXP 1.4 StAXSource
staxResult
)

Template method for handling StaxResults.

protected void
marshalStreamResult(Object
the root of the object graph to marshal
graph
,
StreamResult
the StreamResult
streamResult
)

Template method for handling StreamResults.

protected abstract void
marshalWriter(Object
the root of the object graph to marshal
graph
,
Writer
the Writer to write to
writer
)

Abstract template method for marshalling the given object graph to a Writer.

protected abstract void
marshalXmlEventWriter(Object
the root of the object graph to marshal
graph
,
XMLEventWriter
the XMLEventWriter to write to
eventWriter
)

Abstract template method for marshalling the given object to a StAX XMLEventWriter.

protected abstract void
marshalXmlStreamWriter(Object
the root of the object graph to marshal
graph
,
XMLStreamWriter
the XMLStreamWriter to write to
streamWriter
)

Abstract template method for marshalling the given object to a StAX XMLStreamWriter.

public void
setProcessExternalEntities(boolean processExternalEntities)

Indicate whether external XML entities are processed when unmarshalling.

public void
setSupportDtd(boolean supportDtd)

Indicate whether DTD parsing should be supported.

public final Object

Returns:

the object graph
unmarshal
(Source
the source to marshal from
source
)

Implements org.springframework.oxm.Unmarshaller.unmarshal.

Unmarshals the given provided javax.xml.transform.Source into an object graph.
protected abstract Object

Returns:

the object graph
unmarshalDomNode
(Node
the DOM node that contains the objects to be unmarshalled
node
)

Abstract template method for unmarshalling from a given DOM Node.

protected Object

Returns:

the object graph
unmarshalDomSource
(DOMSource
the DOMSource
domSource
)

Template method for handling DOMSources.

protected abstract Object

Returns:

the object graph
unmarshalInputStream
(InputStream
the InputStreamStream to read from
inputStream
)

Abstract template method for unmarshalling from a given InputStream.

protected abstract Object

Returns:

the object graph
unmarshalReader
(Reader
the Reader to read from
reader
)

Abstract template method for unmarshalling from a given Reader.

protected abstract Object

Returns:

the object graph
unmarshalSaxReader
(XMLReader
the SAX XMLReader to parse with
xmlReader
,
InputSource
the input source to parse from
inputSource
)

Abstract template method for unmarshalling using a given SAX XMLReader and InputSource.

protected Object

Returns:

the object graph
unmarshalSaxSource
(SAXSource
the SAXSource
saxSource
)

Template method for handling SAXSources.

protected Object

Returns:

the object graph
unmarshalStaxSource
(Source
the StaxSource
staxSource
)

Template method for handling StaxSources.

protected Object

Returns:

the object graph
unmarshalStreamSource
(StreamSource
the StreamSource
streamSource
)

Template method for handling StreamSources.

protected abstract Object

Returns:

the object graph
unmarshalXmlEventReader
(XMLEventReader
the XMLEventReader to read from
eventReader
)

Abstract template method for unmarshalling from a given Stax XMLEventReader.

protected abstract Object

Returns:

the object graph
unmarshalXmlStreamReader
(XMLStreamReader
the XMLStreamReader to read from
streamReader
)

Abstract template method for unmarshalling from a given Stax XMLStreamReader.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

documentBuilderFactoryback to summary
private DocumentBuilderFactory documentBuilderFactory
Annotations
@Nullable
documentBuilderFactoryMonitorback to summary
private final Object documentBuilderFactoryMonitor
loggerback to summary
protected final Log logger

Logger available to subclasses.

NO_OP_ENTITY_RESOLVERback to summary
private static final EntityResolver NO_OP_ENTITY_RESOLVER
processExternalEntitiesback to summary
private boolean processExternalEntities
supportDtdback to summary
private boolean supportDtd

Constructor Detail

AbstractMarshallerback to summary
public AbstractMarshaller()

Method Detail

buildDocumentback to summary
protected Document buildDocument()

Build a new Document from this marshaller's DocumentBuilderFactory, as a placeholder for a DOM node.

See Also
createDocumentBuilderFactory(), createDocumentBuilder(DocumentBuilderFactory)
createDocumentBuilderback to summary
protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException

Create a DocumentBuilder that this marshaller will use for creating DOM documents when passed an empty DOMSource.

Can be overridden in subclasses, adding further initialization of the builder.

Parameters
factory:DocumentBuilderFactory

the DocumentBuilderFactory that the DocumentBuilder should be created with

Returns:DocumentBuilder

the DocumentBuilder

Exceptions
ParserConfigurationException:
if thrown by JAXP methods
createDocumentBuilderFactoryback to summary
protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException

Create a DocumentBuilder that this marshaller will use for creating DOM documents when passed an empty DOMSource.

The resulting DocumentBuilderFactory is cached, so this method will only be called once.

Returns:DocumentBuilderFactory

the DocumentBuilderFactory

Exceptions
ParserConfigurationException:
if thrown by JAXP methods
createXmlReaderback to summary
protected XMLReader createXmlReader() throws SAXException, ParserConfigurationException

Create an XMLReader that this marshaller will when passed an empty SAXSource.

Returns:XMLReader

the XMLReader

Exceptions
SAXException:
if thrown by JAXP methods
ParserConfigurationException:
if thrown by JAXP methods
getDefaultEncodingback to summary
protected String getDefaultEncoding()

Determine the default encoding to use for marshalling or unmarshalling from a byte stream, or null if none.

The default implementation returns null.

Annotations
@Nullable
isProcessExternalEntitiesback to summary
public boolean isProcessExternalEntities()

Return whether XML external entities are allowed.

See Also
createXmlReader()
isSupportDtdback to summary
public boolean isSupportDtd()

Return whether DTD parsing is supported.

marshalback to summary
public final void marshal(Object graph, Result result) throws IOException, XmlMappingException

Implements org.springframework.oxm.Marshaller.marshal.

Marshals the object graph with the given root into the provided javax.xml.transform.Result.

This implementation inspects the given result, and calls marshalDomResult, marshalSaxResult, or marshalStreamResult.

Parameters
graph:Object

the root of the object graph to marshal

result:Result

the result to marshal to

Annotations
@Override
Exceptions
IOException:
if an I/O exception occurs
XmlMappingException:
if the given object cannot be marshalled to the result
IllegalArgumentException:
if result if neither a DOMResult, a SAXResult, nor a StreamResult
See Also
marshalDomResult(Object, javax.xml.transform.dom.DOMResult), marshalSaxResult(Object, javax.xml.transform.sax.SAXResult), marshalStreamResult(Object, javax.xml.transform.stream.StreamResult)
marshalDomNodeback to summary
protected abstract void marshalDomNode(Object graph, Node node) throws XmlMappingException

Abstract template method for marshalling the given object graph to a DOM Node.

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

Parameters
graph:Object

the root of the object graph to marshal

node:Node

the DOM node that will contain the result tree

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the DOM node
See Also
org.w3c.dom.Document, org.w3c.dom.DocumentFragment, org.w3c.dom.Element
marshalDomResultback to summary
protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException

Template method for handling DOMResults.

This implementation delegates to marshalDomNode.

Parameters
graph:Object

the root of the object graph to marshal

domResult:DOMResult

the DOMResult

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the result
IllegalArgumentException:
if the domResult is empty
See Also
marshalDomNode(Object, org.w3c.dom.Node)
marshalOutputStreamback to summary
protected abstract void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException

Abstract template method for marshalling the given object graph to a OutputStream.

Parameters
graph:Object

the root of the object graph to marshal

outputStream:OutputStream

the OutputStream to write to

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the writer
IOException:
if an I/O exception occurs
marshalSaxHandlersback to summary
protected abstract void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException

Abstract template method for marshalling the given object graph to a SAX ContentHandler.

Parameters
graph:Object

the root of the object graph to marshal

contentHandler:ContentHandler

the SAX ContentHandler

lexicalHandler:LexicalHandler

the SAX2 LexicalHandler. Can be null.

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the handlers
marshalSaxResultback to summary
protected void marshalSaxResult(Object graph, SAXResult saxResult) throws XmlMappingException

Template method for handling SAXResults.

This implementation delegates to marshalSaxHandlers.

Parameters
graph:Object

the root of the object graph to marshal

saxResult:SAXResult

the SAXResult

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the result
See Also
marshalSaxHandlers(Object, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler)
marshalStaxResultback to summary
protected void marshalStaxResult(Object graph, Result staxResult) throws XmlMappingException

Template method for handling StaxResults.

This implementation delegates to marshalXMLSteamWriter or marshalXMLEventConsumer, depending on what is contained in the StaxResult.

Parameters
graph:Object

the root of the object graph to marshal

staxResult:Result

a JAXP 1.4 StAXSource

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the result
IllegalArgumentException:
if the domResult is empty
See Also
marshalDomNode(Object, org.w3c.dom.Node)
marshalStreamResultback to summary
protected void marshalStreamResult(Object graph, StreamResult streamResult) throws XmlMappingException, IOException

Template method for handling StreamResults.

This implementation delegates to marshalOutputStream or marshalWriter, depending on what is contained in the StreamResult

Parameters
graph:Object

the root of the object graph to marshal

streamResult:StreamResult

the StreamResult

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the result
IOException:
if an I/O Exception occurs
IllegalArgumentException:
if streamResult does neither contain an OutputStream nor a Writer
marshalWriterback to summary
protected abstract void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException

Abstract template method for marshalling the given object graph to a Writer.

Parameters
graph:Object

the root of the object graph to marshal

writer:Writer

the Writer to write to

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the writer
IOException:
if an I/O exception occurs
marshalXmlEventWriterback to summary
protected abstract void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException

Abstract template method for marshalling the given object to a StAX XMLEventWriter.

Parameters
graph:Object

the root of the object graph to marshal

eventWriter:XMLEventWriter

the XMLEventWriter to write to

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the DOM node
marshalXmlStreamWriterback to summary
protected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException

Abstract template method for marshalling the given object to a StAX XMLStreamWriter.

Parameters
graph:Object

the root of the object graph to marshal

streamWriter:XMLStreamWriter

the XMLStreamWriter to write to

Exceptions
XmlMappingException:
if the given object cannot be marshalled to the DOM node
setProcessExternalEntitiesback to summary
public void setProcessExternalEntities(boolean processExternalEntities)

Indicate whether external XML entities are processed when unmarshalling.

Default is false, meaning that external entities are not resolved. Note that processing of external entities will only be enabled/disabled when the Source passed to unmarshal(Source) is a SAXSource or StreamSource. It has no effect for DOMSource or StAXSource instances.

Note

setting this option to true also automatically sets setSupportDtd to true.

setSupportDtdback to summary
public void setSupportDtd(boolean supportDtd)

Indicate whether DTD parsing should be supported.

Default is false meaning that DTD is disabled.

unmarshalback to summary
public final Object unmarshal(Source source) throws IOException, XmlMappingException

Implements org.springframework.oxm.Unmarshaller.unmarshal.

Unmarshals the given provided javax.xml.transform.Source into an object graph.

This implementation inspects the given result, and calls unmarshalDomSource, unmarshalSaxSource, or unmarshalStreamSource.

Parameters
source:Source

the source to marshal from

Returns:Object

the object graph

Annotations
@Override
Exceptions
IOException:
if an I/O Exception occurs
XmlMappingException:
if the given source cannot be mapped to an object
IllegalArgumentException:
if source is neither a DOMSource, a SAXSource, nor a StreamSource
See Also
unmarshalDomSource(javax.xml.transform.dom.DOMSource), unmarshalSaxSource(javax.xml.transform.sax.SAXSource), unmarshalStreamSource(javax.xml.transform.stream.StreamSource)
unmarshalDomNodeback to summary
protected abstract Object unmarshalDomNode(Node node) throws XmlMappingException

Abstract template method for unmarshalling from a given DOM Node.

Parameters
node:Node

the DOM node that contains the objects to be unmarshalled

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given DOM node cannot be mapped to an object
unmarshalDomSourceback to summary
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException

Template method for handling DOMSources.

This implementation delegates to unmarshalDomNode. If the given source is empty, an empty source Document will be created as a placeholder.

Parameters
domSource:DOMSource

the DOMSource

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given source cannot be mapped to an object
IllegalArgumentException:
if the domSource is empty
See Also
unmarshalDomNode(org.w3c.dom.Node)
unmarshalInputStreamback to summary
protected abstract Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException

Abstract template method for unmarshalling from a given InputStream.

Parameters
inputStream:InputStream

the InputStreamStream to read from

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given stream cannot be converted to an object
IOException:
if an I/O exception occurs
unmarshalReaderback to summary
protected abstract Object unmarshalReader(Reader reader) throws XmlMappingException, IOException

Abstract template method for unmarshalling from a given Reader.

Parameters
reader:Reader

the Reader to read from

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given reader cannot be converted to an object
IOException:
if an I/O exception occurs
unmarshalSaxReaderback to summary
protected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException

Abstract template method for unmarshalling using a given SAX XMLReader and InputSource.

Parameters
xmlReader:XMLReader

the SAX XMLReader to parse with

inputSource:InputSource

the input source to parse from

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given reader and input source cannot be converted to an object
IOException:
if an I/O exception occurs
unmarshalSaxSourceback to summary
protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingException, IOException

Template method for handling SAXSources.

This implementation delegates to unmarshalSaxReader.

Parameters
saxSource:SAXSource

the SAXSource

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given source cannot be mapped to an object
IOException:
if an I/O Exception occurs
See Also
unmarshalSaxReader(org.xml.sax.XMLReader, org.xml.sax.InputSource)
unmarshalStaxSourceback to summary
protected Object unmarshalStaxSource(Source staxSource) throws XmlMappingException

Template method for handling StaxSources.

This implementation delegates to unmarshalXmlStreamReader or unmarshalXmlEventReader.

Parameters
staxSource:Source

the StaxSource

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given source cannot be mapped to an object
unmarshalStreamSourceback to summary
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException

Template method for handling StreamSources.

This implementation delegates to unmarshalInputStream or unmarshalReader.

Parameters
streamSource:StreamSource

the StreamSource

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given source cannot be mapped to an object
IOException:
if an I/O exception occurs
unmarshalXmlEventReaderback to summary
protected abstract Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException

Abstract template method for unmarshalling from a given Stax XMLEventReader.

Parameters
eventReader:XMLEventReader

the XMLEventReader to read from

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given event reader cannot be converted to an object
unmarshalXmlStreamReaderback to summary
protected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException

Abstract template method for unmarshalling from a given Stax XMLStreamReader.

Parameters
streamReader:XMLStreamReader

the XMLStreamReader to read from

Returns:Object

the object graph

Exceptions
XmlMappingException:
if the given stream reader cannot be converted to an object