public abstract class AbstractMarshaller extends Object implements Marshaller, Unmarshaller
Marshaller
and Unmarshaller
interface.
This implementation inspects the given Source
or Result
, and
delegates further handling to overridable template methods.Modifier and Type | Field and Description |
---|---|
protected Log |
logger
Logger available to subclasses.
|
Constructor and Description |
---|
AbstractMarshaller() |
Modifier and Type | Method and Description |
---|---|
protected Document |
buildDocument()
Build a new
Document from this marshaller's DocumentBuilderFactory ,
as a placeholder for a DOM node. |
protected DocumentBuilder |
createDocumentBuilder(DocumentBuilderFactory factory)
Create a
DocumentBuilder that this marshaller will use for creating
DOM documents when passed an empty DOMSource . |
protected DocumentBuilderFactory |
createDocumentBuilderFactory()
Create a
DocumentBuilder that this marshaller will use for creating
DOM documents when passed an empty DOMSource . |
protected 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. |
boolean |
isProcessExternalEntities()
Return whether XML external entities are allowed.
|
boolean |
isSupportDtd()
Return whether DTD parsing is supported.
|
void |
marshal(Object graph,
Result result)
Marshals the object graph with the given root into the provided
javax.xml.transform.Result . |
protected abstract void |
marshalDomNode(Object graph,
Node node)
Abstract template method for marshalling the given object graph to a DOM
Node . |
protected void |
marshalDomResult(Object graph,
DOMResult domResult)
Template method for handling
DOMResult s. |
protected abstract void |
marshalOutputStream(Object graph,
OutputStream outputStream)
Abstract template method for marshalling the given object graph to a
OutputStream . |
protected abstract void |
marshalSaxHandlers(Object graph,
ContentHandler contentHandler,
LexicalHandler lexicalHandler)
Abstract template method for marshalling the given object graph to a SAX
ContentHandler . |
protected void |
marshalSaxResult(Object graph,
SAXResult saxResult)
Template method for handling
SAXResult s. |
protected void |
marshalStaxResult(Object graph,
Result staxResult)
Template method for handling
StaxResult s. |
protected void |
marshalStreamResult(Object graph,
StreamResult streamResult)
Template method for handling
StreamResult s. |
protected abstract void |
marshalWriter(Object graph,
Writer writer)
Abstract template method for marshalling the given object graph to a
Writer . |
protected abstract void |
marshalXmlEventWriter(Object graph,
XMLEventWriter eventWriter)
Abstract template method for marshalling the given object to a StAX
XMLEventWriter . |
protected abstract void |
marshalXmlStreamWriter(Object graph,
XMLStreamWriter streamWriter)
Abstract template method for marshalling the given object to a StAX
XMLStreamWriter . |
void |
setProcessExternalEntities(boolean processExternalEntities)
Indicate whether external XML entities are processed when unmarshalling.
|
void |
setSupportDtd(boolean supportDtd)
Indicate whether DTD parsing should be supported.
|
Object |
unmarshal(Source source)
Unmarshals the given provided
javax.xml.transform.Source into an object graph. |
protected abstract Object |
unmarshalDomNode(Node node)
Abstract template method for unmarshalling from a given DOM
Node . |
protected Object |
unmarshalDomSource(DOMSource domSource)
Template method for handling
DOMSource s. |
protected abstract Object |
unmarshalInputStream(InputStream inputStream)
Abstract template method for unmarshalling from a given
InputStream . |
protected abstract Object |
unmarshalReader(Reader reader)
Abstract template method for unmarshalling from a given
Reader . |
protected abstract Object |
unmarshalSaxReader(XMLReader xmlReader,
InputSource inputSource)
Abstract template method for unmarshalling using a given SAX
XMLReader
and InputSource . |
protected Object |
unmarshalSaxSource(SAXSource saxSource)
Template method for handling
SAXSource s. |
protected Object |
unmarshalStaxSource(Source staxSource)
Template method for handling
StaxSource s. |
protected Object |
unmarshalStreamSource(StreamSource streamSource)
Template method for handling
StreamSource s. |
protected abstract Object |
unmarshalXmlEventReader(XMLEventReader eventReader)
Abstract template method for unmarshalling from a given Stax
XMLEventReader . |
protected abstract Object |
unmarshalXmlStreamReader(XMLStreamReader streamReader)
Abstract template method for unmarshalling from a given Stax
XMLStreamReader . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
supports
supports
protected final Log logger
public void setSupportDtd(boolean supportDtd)
Default is false
meaning that DTD is disabled.
public boolean isSupportDtd()
public void setProcessExternalEntities(boolean processExternalEntities)
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(boolean)
to true
.
public boolean isProcessExternalEntities()
createXmlReader()
protected Document buildDocument()
Document
from this marshaller's DocumentBuilderFactory
,
as a placeholder for a DOM node.protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException
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.
ParserConfigurationException
- if thrown by JAXP methodsprotected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException
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.
factory
- the DocumentBuilderFactory
that the DocumentBuilder should be created withDocumentBuilder
ParserConfigurationException
- if thrown by JAXP methodsprotected XMLReader createXmlReader() throws SAXException
XMLReader
that this marshaller will when passed an empty SAXSource
.SAXException
- if thrown by JAXP methods@Nullable protected String getDefaultEncoding()
null
if none.
The default implementation returns null
.
public final void marshal(Object graph, Result result) throws IOException, XmlMappingException
javax.xml.transform.Result
.
This implementation inspects the given result, and calls marshalDomResult
,
marshalSaxResult
, or marshalStreamResult
.
marshal
in interface Marshaller
graph
- the root of the object graph to marshalresult
- the result to marshal toIOException
- if an I/O exception occursXmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- if result
if neither a DOMResult
,
a SAXResult
, nor a StreamResult
marshalDomResult(Object, javax.xml.transform.dom.DOMResult)
,
marshalSaxResult(Object, javax.xml.transform.sax.SAXResult)
,
marshalStreamResult(Object, javax.xml.transform.stream.StreamResult)
protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException
DOMResult
s.
This implementation delegates to marshalDomNode
.
graph
- the root of the object graph to marshaldomResult
- the DOMResult
XmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- if the domResult
is emptymarshalDomNode(Object, org.w3c.dom.Node)
protected void marshalStaxResult(Object graph, Result staxResult) throws XmlMappingException
StaxResult
s.
This implementation delegates to marshalXMLSteamWriter
or
marshalXMLEventConsumer
, depending on what is contained in the
StaxResult
.
graph
- the root of the object graph to marshalstaxResult
- a JAXP 1.4 StAXSource
XmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- if the domResult
is emptymarshalDomNode(Object, org.w3c.dom.Node)
protected void marshalSaxResult(Object graph, SAXResult saxResult) throws XmlMappingException
SAXResult
s.
This implementation delegates to marshalSaxHandlers
.
graph
- the root of the object graph to marshalsaxResult
- the SAXResult
XmlMappingException
- if the given object cannot be marshalled to the resultmarshalSaxHandlers(Object, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler)
protected void marshalStreamResult(Object graph, StreamResult streamResult) throws XmlMappingException, IOException
StreamResult
s.
This implementation delegates to marshalOutputStream
or marshalWriter
,
depending on what is contained in the StreamResult
graph
- the root of the object graph to marshalstreamResult
- the StreamResult
IOException
- if an I/O Exception occursXmlMappingException
- if the given object cannot be marshalled to the resultIllegalArgumentException
- if streamResult
does neither
contain an OutputStream
nor a Writer
public final Object unmarshal(Source source) throws IOException, XmlMappingException
javax.xml.transform.Source
into an object graph.
This implementation inspects the given result, and calls unmarshalDomSource
,
unmarshalSaxSource
, or unmarshalStreamSource
.
unmarshal
in interface Unmarshaller
source
- the source to marshal fromIOException
- if an I/O Exception occursXmlMappingException
- if the given source cannot be mapped to an objectIllegalArgumentException
- if source
is neither a DOMSource
,
a SAXSource
, nor a StreamSource
unmarshalDomSource(javax.xml.transform.dom.DOMSource)
,
unmarshalSaxSource(javax.xml.transform.sax.SAXSource)
,
unmarshalStreamSource(javax.xml.transform.stream.StreamSource)
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException
DOMSource
s.
This implementation delegates to unmarshalDomNode
.
If the given source is empty, an empty source Document
will be created as a placeholder.
domSource
- the DOMSource
XmlMappingException
- if the given source cannot be mapped to an objectIllegalArgumentException
- if the domSource
is emptyunmarshalDomNode(org.w3c.dom.Node)
protected Object unmarshalStaxSource(Source staxSource) throws XmlMappingException
StaxSource
s.
This implementation delegates to unmarshalXmlStreamReader
or
unmarshalXmlEventReader
.
staxSource
- the StaxSource
XmlMappingException
- if the given source cannot be mapped to an objectprotected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingException, IOException
SAXSource
s.
This implementation delegates to unmarshalSaxReader
.
saxSource
- the SAXSource
XmlMappingException
- if the given source cannot be mapped to an objectIOException
- if an I/O Exception occursunmarshalSaxReader(org.xml.sax.XMLReader, org.xml.sax.InputSource)
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException
StreamSource
s.
This implementation delegates to unmarshalInputStream
or unmarshalReader
.
streamSource
- the StreamSource
IOException
- if an I/O exception occursXmlMappingException
- if the given source cannot be mapped to an objectprotected abstract void marshalDomNode(Object graph, Node node) throws XmlMappingException
Node
.
In practice, node
is a Document
node, a DocumentFragment
node,
or a Element
node. In other words, a node that accepts children.
graph
- the root of the object graph to marshalnode
- the DOM node that will contain the result treeXmlMappingException
- if the given object cannot be marshalled to the DOM nodeDocument
,
DocumentFragment
,
Element
protected abstract void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException
XMLEventWriter
.graph
- the root of the object graph to marshaleventWriter
- the XMLEventWriter
to write toXmlMappingException
- if the given object cannot be marshalled to the DOM nodeprotected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException
XMLStreamWriter
.graph
- the root of the object graph to marshalstreamWriter
- the XMLStreamWriter
to write toXmlMappingException
- if the given object cannot be marshalled to the DOM nodeprotected abstract void marshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler) throws XmlMappingException
ContentHandler
.graph
- the root of the object graph to marshalcontentHandler
- the SAX ContentHandler
lexicalHandler
- the SAX2 LexicalHandler
. Can be null
.XmlMappingException
- if the given object cannot be marshalled to the handlersprotected abstract void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException
OutputStream
.graph
- the root of the object graph to marshaloutputStream
- the OutputStream
to write toXmlMappingException
- if the given object cannot be marshalled to the writerIOException
- if an I/O exception occursprotected abstract void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException
Writer
.graph
- the root of the object graph to marshalwriter
- the Writer
to write toXmlMappingException
- if the given object cannot be marshalled to the writerIOException
- if an I/O exception occursprotected abstract Object unmarshalDomNode(Node node) throws XmlMappingException
Node
.node
- the DOM node that contains the objects to be unmarshalledXmlMappingException
- if the given DOM node cannot be mapped to an objectprotected abstract Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException
XMLEventReader
.eventReader
- the XMLEventReader
to read fromXmlMappingException
- if the given event reader cannot be converted to an objectprotected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException
XMLStreamReader
.streamReader
- the XMLStreamReader
to read fromXmlMappingException
- if the given stream reader cannot be converted to an objectprotected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException
XMLReader
and InputSource
.xmlReader
- the SAX XMLReader
to parse withinputSource
- the input source to parse fromXmlMappingException
- if the given reader and input source cannot be converted to an objectIOException
- if an I/O exception occursprotected abstract Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException
InputStream
.inputStream
- the InputStreamStream
to read fromXmlMappingException
- if the given stream cannot be converted to an objectIOException
- if an I/O exception occursprotected abstract Object unmarshalReader(Reader reader) throws XmlMappingException, IOException
Reader
.reader
- the Reader
to read fromXmlMappingException
- if the given reader cannot be converted to an objectIOException
- if an I/O exception occurs