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
FieldsModifier and TypeFieldDescriptionprotected final org.apache.commons.logging.LogLogger available to subclasses. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected DocumentBuild a newDocumentfrom this marshaller'sDocumentBuilderFactory, as a placeholder for a DOM node.protected DocumentBuilderCreate aDocumentBuilderthat this marshaller will use for creating DOM documents when passed an emptyDOMSource.protected DocumentBuilderFactoryCreate aDocumentBuilderthat this marshaller will use for creating DOM documents when passed an emptyDOMSource.protected XMLReaderCreate anXMLReaderthat this marshaller will when passed an emptySAXSource.Determine the default encoding to use for marshalling or unmarshalling from a byte stream, ornullif none.booleanReturn whether XML external entities are allowed.booleanReturn whether DTD parsing is supported.final voidMarshals the object graph with the given root into the providedjavax.xml.transform.Result.protected abstract voidmarshalDomNode(Object graph, Node node) Abstract template method for marshalling the given object graph to a DOMNode.protected voidmarshalDomResult(Object graph, DOMResult domResult) Template method for handlingDOMResults.protected abstract voidmarshalOutputStream(Object graph, OutputStream outputStream) Abstract template method for marshalling the given object graph to aOutputStream.protected abstract voidmarshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler) Abstract template method for marshalling the given object graph to a SAXContentHandler.protected voidmarshalSaxResult(Object graph, SAXResult saxResult) Template method for handlingSAXResults.protected voidmarshalStaxResult(Object graph, Result staxResult) Template method for handlingStaxResults.protected voidmarshalStreamResult(Object graph, StreamResult streamResult) Template method for handlingStreamResults.protected abstract voidmarshalWriter(Object graph, Writer writer) Abstract template method for marshalling the given object graph to aWriter.protected abstract voidmarshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) Abstract template method for marshalling the given object to a StAXXMLEventWriter.protected abstract voidmarshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) Abstract template method for marshalling the given object to a StAXXMLStreamWriter.voidsetProcessExternalEntities(boolean processExternalEntities) Indicate whether external XML entities are processed when unmarshalling.voidsetSupportDtd(boolean supportDtd) Indicate whether DTD parsing should be supported.final ObjectUnmarshals the given providedjavax.xml.transform.Sourceinto an object graph.protected abstract ObjectunmarshalDomNode(Node node) Abstract template method for unmarshalling from a given DOMNode.protected ObjectunmarshalDomSource(DOMSource domSource) Template method for handlingDOMSources.protected abstract ObjectunmarshalInputStream(InputStream inputStream) Abstract template method for unmarshalling from a givenInputStream.protected abstract ObjectunmarshalReader(Reader reader) Abstract template method for unmarshalling from a givenReader.protected abstract ObjectunmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) Abstract template method for unmarshalling using a given SAXXMLReaderandInputSource.protected ObjectunmarshalSaxSource(SAXSource saxSource) Template method for handlingSAXSources.protected ObjectunmarshalStaxSource(Source staxSource) Template method for handlingStaxSources.protected ObjectunmarshalStreamSource(StreamSource streamSource) Template method for handlingStreamSources.protected abstract ObjectunmarshalXmlEventReader(XMLEventReader eventReader) Abstract template method for unmarshalling from a given StaxXMLEventReader.protected abstract ObjectunmarshalXmlStreamReader(XMLStreamReader streamReader) Abstract template method for unmarshalling from a given StaxXMLStreamReader.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Marshaller
supportsMethods inherited from interface Unmarshaller
supports
-
Field Details
-
logger
protected final org.apache.commons.logging.Log loggerLogger 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
falsemeaning 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 theSourcepassed tounmarshal(Source)is aSAXSourceorStreamSource. It has no effect forDOMSourceorStAXSourceinstances.Note: setting this option to
truealso automatically setssetSupportDtd(boolean)totrue. -
isProcessExternalEntities
public boolean isProcessExternalEntities()Return whether XML external entities are allowed.- See Also:
-
buildDocument
Build a newDocumentfrom this marshaller'sDocumentBuilderFactory, as a placeholder for a DOM node.- See Also:
-
createDocumentBuilderFactory
Create aDocumentBuilderthat this marshaller will use for creating DOM documents when passed an emptyDOMSource.The resulting
DocumentBuilderFactoryis 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 aDocumentBuilderthat 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- theDocumentBuilderFactorythat the DocumentBuilder should be created with- Returns:
- the
DocumentBuilder - Throws:
ParserConfigurationException- if thrown by JAXP methods
-
createXmlReader
Create anXMLReaderthat this marshaller will when passed an emptySAXSource.- Returns:
- the XMLReader
- Throws:
SAXException- if thrown by JAXP methodsParserConfigurationException- if thrown by JAXP methods
-
getDefaultEncoding
-
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:
marshalin 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- ifresultif neither aDOMResult, aSAXResult, nor aStreamResult- See Also:
-
marshalDomResult
Template method for handlingDOMResults.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 thedomResultis empty- See Also:
-
marshalStaxResult
Template method for handlingStaxResults.This implementation delegates to
marshalXMLSteamWriterormarshalXMLEventConsumer, 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 thedomResultis empty- See Also:
-
marshalSaxResult
Template method for handlingSAXResults.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 handlingStreamResults.This implementation delegates to
marshalOutputStreamormarshalWriter, 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- ifstreamResultdoes neither contain anOutputStreamnor aWriter
-
unmarshal
Unmarshals the given providedjavax.xml.transform.Sourceinto an object graph.This implementation inspects the given result, and calls
unmarshalDomSource,unmarshalSaxSource, orunmarshalStreamSource.- Specified by:
unmarshalin 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- ifsourceis neither aDOMSource, aSAXSource, nor aStreamSource- See Also:
-
unmarshalDomSource
Template method for handlingDOMSources.This implementation delegates to
unmarshalDomNode. If the given source is empty, an empty sourceDocumentwill 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 thedomSourceis empty- See Also:
-
unmarshalStaxSource
Template method for handlingStaxSources.This implementation delegates to
unmarshalXmlStreamReaderorunmarshalXmlEventReader.- Parameters:
staxSource- theStaxSource- Returns:
- the object graph
- Throws:
XmlMappingException- if the given source cannot be mapped to an object
-
unmarshalSaxSource
Template method for handlingSAXSources.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 handlingStreamSources.This implementation delegates to
unmarshalInputStreamorunmarshalReader.- 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,
nodeis aDocumentnode, aDocumentFragmentnode, or aElementnode. 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- theXMLEventWriterto 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- theXMLStreamWriterto 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 SAXContentHandlerlexicalHandler- 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- theOutputStreamto 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- theWriterto 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- theXMLEventReaderto 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- theXMLStreamReaderto 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 SAXXMLReaderandInputSource.- Parameters:
xmlReader- the SAXXMLReaderto 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- theInputStreamStreamto 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- theReaderto read from- Returns:
- the object graph
- Throws:
XmlMappingException- if the given reader cannot be converted to an objectIOException- if an I/O exception occurs
-