Class AbstractMarshaller
- All Implemented Interfaces:
Marshaller
,Unmarshaller
- Direct Known Subclasses:
XStreamMarshaller
Marshaller
and Unmarshaller
interface.
This implementation inspects the given Source
or Result
, and
delegates further handling to overridable template methods.- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected Document
Build a newDocument
from this marshaller'sDocumentBuilderFactory
, as a placeholder for a DOM node.protected DocumentBuilder
Create aDocumentBuilder
that this marshaller will use for creating DOM documents when passed an emptyDOMSource
.protected DocumentBuilderFactory
Create aDocumentBuilder
that this marshaller will use for creating DOM documents when passed an emptyDOMSource
.protected XMLReader
Create anXMLReader
that this marshaller will when passed an emptySAXSource
.protected String
Determine the default encoding to use for marshalling or unmarshalling from a byte stream, ornull
if none.boolean
Return whether XML external entities are allowed.boolean
Return whether DTD parsing is supported.final void
Marshals the object graph with the given root into the providedjavax.xml.transform.Result
.protected abstract void
marshalDomNode
(Object graph, Node node) Abstract template method for marshalling the given object graph to a DOMNode
.protected void
marshalDomResult
(Object graph, DOMResult domResult) Template method for handlingDOMResult
s.protected abstract void
marshalOutputStream
(Object graph, OutputStream outputStream) Abstract template method for marshalling the given object graph to aOutputStream
.protected abstract void
marshalSaxHandlers
(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) Abstract template method for marshalling the given object graph to a SAXContentHandler
.protected void
marshalSaxResult
(Object graph, SAXResult saxResult) Template method for handlingSAXResult
s.protected void
marshalStaxResult
(Object graph, Result staxResult) Template method for handlingStaxResult
s.protected void
marshalStreamResult
(Object graph, StreamResult streamResult) Template method for handlingStreamResult
s.protected abstract void
marshalWriter
(Object graph, Writer writer) Abstract template method for marshalling the given object graph to aWriter
.protected abstract void
marshalXmlEventWriter
(Object graph, XMLEventWriter eventWriter) Abstract template method for marshalling the given object to a StAXXMLEventWriter
.protected abstract void
marshalXmlStreamWriter
(Object graph, XMLStreamWriter streamWriter) Abstract template method for marshalling the given object to a StAXXMLStreamWriter
.void
setProcessExternalEntities
(boolean processExternalEntities) Indicate whether external XML entities are processed when unmarshalling.void
setSupportDtd
(boolean supportDtd) Indicate whether DTD parsing should be supported.final Object
Unmarshals the given providedjavax.xml.transform.Source
into an object graph.protected abstract Object
unmarshalDomNode
(Node node) Abstract template method for unmarshalling from a given DOMNode
.protected Object
unmarshalDomSource
(DOMSource domSource) Template method for handlingDOMSource
s.protected abstract Object
unmarshalInputStream
(InputStream inputStream) Abstract template method for unmarshalling from a givenInputStream
.protected abstract Object
unmarshalReader
(Reader reader) Abstract template method for unmarshalling from a givenReader
.protected abstract Object
unmarshalSaxReader
(XMLReader xmlReader, InputSource inputSource) Abstract template method for unmarshalling using a given SAXXMLReader
andInputSource
.protected Object
unmarshalSaxSource
(SAXSource saxSource) Template method for handlingSAXSource
s.protected Object
unmarshalStaxSource
(Source staxSource) Template method for handlingStaxSource
s.protected Object
unmarshalStreamSource
(StreamSource streamSource) Template method for handlingStreamSource
s.protected abstract Object
unmarshalXmlEventReader
(XMLEventReader eventReader) Abstract template method for unmarshalling from a given StaxXMLEventReader
.protected abstract Object
unmarshalXmlStreamReader
(XMLStreamReader streamReader) Abstract template method for unmarshalling from a given StaxXMLStreamReader
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.oxm.Marshaller
supports
Methods inherited from interface org.springframework.oxm.Unmarshaller
supports
-
Field Details
-
logger
Logger available to subclasses.
-
-
Constructor Details
-
AbstractMarshaller
public AbstractMarshaller()
-
-
Method Details
-
setSupportDtd
public void setSupportDtd(boolean supportDtd) Indicate whether DTD parsing should be supported.Default is
false
meaning that DTD is disabled. -
isSupportDtd
public boolean isSupportDtd()Return whether DTD parsing is supported. -
setProcessExternalEntities
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 theSource
passed tounmarshal(Source)
is aSAXSource
orStreamSource
. It has no effect forDOMSource
orStAXSource
instances.Note: setting this option to
true
also automatically setssetSupportDtd(boolean)
totrue
. -
isProcessExternalEntities
public boolean isProcessExternalEntities()Return whether XML external entities are allowed.- See Also:
-
buildDocument
Build a newDocument
from this marshaller'sDocumentBuilderFactory
, as a placeholder for a DOM node. -
createDocumentBuilderFactory
Create aDocumentBuilder
that this marshaller will use for creating DOM documents when passed an emptyDOMSource
.The resulting
DocumentBuilderFactory
is cached, so this method will only be called once.- Returns:
- the DocumentBuilderFactory
- Throws:
ParserConfigurationException
- if thrown by JAXP methods
-
createDocumentBuilder
protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException Create aDocumentBuilder
that this marshaller will use for creating DOM documents when passed an emptyDOMSource
.Can be overridden in subclasses, adding further initialization of the builder.
- Parameters:
factory
- theDocumentBuilderFactory
that the DocumentBuilder should be created with- Returns:
- the
DocumentBuilder
- Throws:
ParserConfigurationException
- if thrown by JAXP methods
-
createXmlReader
Create anXMLReader
that this marshaller will when passed an emptySAXSource
.- Returns:
- the XMLReader
- Throws:
SAXException
- if thrown by JAXP methodsParserConfigurationException
- if thrown by JAXP methods
-
getDefaultEncoding
Determine the default encoding to use for marshalling or unmarshalling from a byte stream, ornull
if none.The default implementation returns
null
. -
marshal
Marshals the object graph with the given root into the providedjavax.xml.transform.Result
.This implementation inspects the given result, and calls
marshalDomResult
,marshalSaxResult
, ormarshalStreamResult
.- Specified by:
marshal
in interfaceMarshaller
- Parameters:
graph
- the root of the object graph to marshalresult
- the result to marshal to- Throws:
IOException
- if an I/O exception occursXmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- ifresult
if neither aDOMResult
, aSAXResult
, nor aStreamResult
- See Also:
-
marshalDomResult
Template method for handlingDOMResult
s.This implementation delegates to
marshalDomNode
.- Parameters:
graph
- the root of the object graph to marshaldomResult
- theDOMResult
- Throws:
XmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- if thedomResult
is empty- See Also:
-
marshalStaxResult
Template method for handlingStaxResult
s.This implementation delegates to
marshalXMLSteamWriter
ormarshalXMLEventConsumer
, depending on what is contained in theStaxResult
.- Parameters:
graph
- the root of the object graph to marshalstaxResult
- a JAXP 1.4StAXSource
- Throws:
XmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- if thedomResult
is empty- See Also:
-
marshalSaxResult
Template method for handlingSAXResult
s.This implementation delegates to
marshalSaxHandlers
.- Parameters:
graph
- the root of the object graph to marshalsaxResult
- theSAXResult
- Throws:
XmlMappingException
- if the given object cannot be marshalled to the result- See Also:
-
marshalStreamResult
protected void marshalStreamResult(Object graph, StreamResult streamResult) throws XmlMappingException, IOException Template method for handlingStreamResult
s.This implementation delegates to
marshalOutputStream
ormarshalWriter
, depending on what is contained in theStreamResult
- Parameters:
graph
- the root of the object graph to marshalstreamResult
- theStreamResult
- Throws:
IOException
- if an I/O Exception occursXmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- ifstreamResult
does neither contain anOutputStream
nor aWriter
-
unmarshal
Unmarshals the given providedjavax.xml.transform.Source
into an object graph.This implementation inspects the given result, and calls
unmarshalDomSource
,unmarshalSaxSource
, orunmarshalStreamSource
.- Specified by:
unmarshal
in interfaceUnmarshaller
- Parameters:
source
- the source to marshal from- Returns:
- the object graph
- Throws:
IOException
- if an I/O Exception occursXmlMappingException
- if the given source cannot be mapped to an objectIllegalArgumentException
- ifsource
is neither aDOMSource
, aSAXSource
, nor aStreamSource
- See Also:
-
unmarshalDomSource
Template method for handlingDOMSource
s.This implementation delegates to
unmarshalDomNode
. If the given source is empty, an empty sourceDocument
will be created as a placeholder.- Parameters:
domSource
- theDOMSource
- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given source cannot be mapped to an objectIllegalArgumentException
- if thedomSource
is empty- See Also:
-
unmarshalStaxSource
Template method for handlingStaxSource
s.This implementation delegates to
unmarshalXmlStreamReader
orunmarshalXmlEventReader
.- Parameters:
staxSource
- theStaxSource
- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given source cannot be mapped to an object
-
unmarshalSaxSource
Template method for handlingSAXSource
s.This implementation delegates to
unmarshalSaxReader
.- Parameters:
saxSource
- theSAXSource
- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given source cannot be mapped to an objectIOException
- if an I/O Exception occurs- See Also:
-
unmarshalStreamSource
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException Template method for handlingStreamSource
s.This implementation delegates to
unmarshalInputStream
orunmarshalReader
.- Parameters:
streamSource
- theStreamSource
- Returns:
- the object graph
- Throws:
IOException
- if an I/O exception occursXmlMappingException
- if the given source cannot be mapped to an object
-
marshalDomNode
Abstract template method for marshalling the given object graph to a DOMNode
.In practice, node is be a
Document
node, aDocumentFragment
node, or aElement
node. In other words, a node that accepts children.- Parameters:
graph
- the root of the object graph to marshalnode
- the DOM node that will contain the result tree- Throws:
XmlMappingException
- if the given object cannot be marshalled to the DOM node- See Also:
-
marshalXmlEventWriter
protected abstract void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException Abstract template method for marshalling the given object to a StAXXMLEventWriter
.- Parameters:
graph
- the root of the object graph to marshaleventWriter
- theXMLEventWriter
to write to- Throws:
XmlMappingException
- if the given object cannot be marshalled to the DOM node
-
marshalXmlStreamWriter
protected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException Abstract template method for marshalling the given object to a StAXXMLStreamWriter
.- Parameters:
graph
- the root of the object graph to marshalstreamWriter
- theXMLStreamWriter
to write to- Throws:
XmlMappingException
- if the given object cannot be marshalled to the DOM node
-
marshalSaxHandlers
protected abstract void marshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler) throws XmlMappingException Abstract template method for marshalling the given object graph to a SAXContentHandler
.- Parameters:
graph
- the root of the object graph to marshalcontentHandler
- the SAXContentHandler
lexicalHandler
- the SAX2LexicalHandler
. Can benull
.- Throws:
XmlMappingException
- if the given object cannot be marshalled to the handlers
-
marshalOutputStream
protected abstract void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException Abstract template method for marshalling the given object graph to aOutputStream
.- Parameters:
graph
- the root of the object graph to marshaloutputStream
- theOutputStream
to write to- Throws:
XmlMappingException
- if the given object cannot be marshalled to the writerIOException
- if an I/O exception occurs
-
marshalWriter
protected abstract void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException Abstract template method for marshalling the given object graph to aWriter
.- Parameters:
graph
- the root of the object graph to marshalwriter
- theWriter
to write to- Throws:
XmlMappingException
- if the given object cannot be marshalled to the writerIOException
- if an I/O exception occurs
-
unmarshalDomNode
Abstract template method for unmarshalling from a given DOMNode
.- 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
-
unmarshalXmlEventReader
protected abstract Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException Abstract template method for unmarshalling from a given StaxXMLEventReader
.- Parameters:
eventReader
- theXMLEventReader
to read from- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given event reader cannot be converted to an object
-
unmarshalXmlStreamReader
protected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException Abstract template method for unmarshalling from a given StaxXMLStreamReader
.- Parameters:
streamReader
- theXMLStreamReader
to read from- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given stream reader cannot be converted to an object
-
unmarshalSaxReader
protected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException Abstract template method for unmarshalling using a given SAXXMLReader
andInputSource
.- Parameters:
xmlReader
- the SAXXMLReader
to parse withinputSource
- the input source to parse from- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given reader and input source cannot be converted to an objectIOException
- if an I/O exception occurs
-
unmarshalInputStream
protected abstract Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException Abstract template method for unmarshalling from a givenInputStream
.- Parameters:
inputStream
- theInputStreamStream
to read from- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given stream cannot be converted to an objectIOException
- if an I/O exception occurs
-
unmarshalReader
Abstract template method for unmarshalling from a givenReader
.- Parameters:
reader
- theReader
to read from- Returns:
- the object graph
- Throws:
XmlMappingException
- if the given reader cannot be converted to an objectIOException
- if an I/O exception occurs
-