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 SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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.protected StringDetermine 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, 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 java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.oxm.MarshallersupportsMethods inherited from interface org.springframework.oxm.Unmarshallersupports
- 
Field Details- 
loggerLogger available to subclasses.
 
- 
- 
Constructor Details- 
AbstractMarshallerpublic AbstractMarshaller()
 
- 
- 
Method Details- 
setSupportDtdpublic void setSupportDtd(boolean supportDtd) Indicate whether DTD parsing should be supported.Default is falsemeaning that DTD is disabled.
- 
isSupportDtdpublic boolean isSupportDtd()Return whether DTD parsing is supported.
- 
setProcessExternalEntitiespublic 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.
- 
isProcessExternalEntitiespublic boolean isProcessExternalEntities()Return whether XML external entities are allowed.- See Also:
 
- 
buildDocumentBuild a newDocumentfrom this marshaller'sDocumentBuilderFactory, as a placeholder for a DOM node.
- 
createDocumentBuilderFactoryCreate 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
 
- 
createDocumentBuilderprotected 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- the- DocumentBuilderFactorythat the DocumentBuilder should be created with
- Returns:
- the DocumentBuilder
- Throws:
- ParserConfigurationException- if thrown by JAXP methods
 
- 
createXmlReaderCreate anXMLReaderthat this marshaller will when passed an emptySAXSource.- Returns:
- the XMLReader
- Throws:
- SAXException- if thrown by JAXP methods
- ParserConfigurationException- if thrown by JAXP methods
 
- 
getDefaultEncodingDetermine the default encoding to use for marshalling or unmarshalling from a byte stream, ornullif none.The default implementation returns null.
- 
marshalMarshals 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 interface- Marshaller
- Parameters:
- graph- the root of the object graph to marshal
- result- the result to marshal to
- Throws:
- IOException- if an I/O exception occurs
- XmlMappingException- if the given object cannot be marshalled to the result
- IllegalArgumentException- if- resultif neither a- DOMResult, a- SAXResult, nor a- StreamResult
- See Also:
 
- 
marshalDomResultTemplate method for handlingDOMResults.This implementation delegates to marshalDomNode.- Parameters:
- graph- the root of the object graph to marshal
- domResult- the- DOMResult
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the result
- IllegalArgumentException- if the- domResultis empty
- See Also:
 
- 
marshalStaxResultTemplate method for handlingStaxResults.This implementation delegates to marshalXMLSteamWriterormarshalXMLEventConsumer, depending on what is contained in theStaxResult.- Parameters:
- graph- the root of the object graph to marshal
- staxResult- a JAXP 1.4- StAXSource
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the result
- IllegalArgumentException- if the- domResultis empty
- See Also:
 
- 
marshalSaxResultTemplate method for handlingSAXResults.This implementation delegates to marshalSaxHandlers.- Parameters:
- graph- the root of the object graph to marshal
- saxResult- the- SAXResult
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the result
- See Also:
 
- 
marshalStreamResultprotected 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 marshal
- streamResult- the- StreamResult
- Throws:
- IOException- if an I/O Exception occurs
- XmlMappingException- if the given object cannot be marshalled to the result
- IllegalArgumentException- if- streamResultdoes neither contain an- OutputStreamnor a- Writer
 
- 
unmarshalUnmarshals the given providedjavax.xml.transform.Sourceinto an object graph.This implementation inspects the given result, and calls unmarshalDomSource,unmarshalSaxSource, orunmarshalStreamSource.- Specified by:
- unmarshalin interface- Unmarshaller
- Parameters:
- source- the source to marshal from
- Returns:
- the object graph
- Throws:
- IOException- if an I/O Exception occurs
- XmlMappingException- if the given source cannot be mapped to an object
- IllegalArgumentException- if- sourceis neither a- DOMSource, a- SAXSource, nor a- StreamSource
- See Also:
 
- 
unmarshalDomSourceTemplate method for handlingDOMSources.This implementation delegates to unmarshalDomNode. If the given source is empty, an empty sourceDocumentwill be created as a placeholder.- Parameters:
- domSource- the- DOMSource
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given source cannot be mapped to an object
- IllegalArgumentException- if the- domSourceis empty
- See Also:
 
- 
unmarshalStaxSourceTemplate method for handlingStaxSources.This implementation delegates to unmarshalXmlStreamReaderorunmarshalXmlEventReader.- Parameters:
- staxSource- the- StaxSource
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given source cannot be mapped to an object
 
- 
unmarshalSaxSourceTemplate method for handlingSAXSources.This implementation delegates to unmarshalSaxReader.- Parameters:
- saxSource- the- SAXSource
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given source cannot be mapped to an object
- IOException- if an I/O Exception occurs
- See Also:
 
- 
unmarshalStreamSourceprotected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException Template method for handlingStreamSources.This implementation delegates to unmarshalInputStreamorunmarshalReader.- Parameters:
- streamSource- the- StreamSource
- Returns:
- the object graph
- Throws:
- IOException- if an I/O exception occurs
- XmlMappingException- if the given source cannot be mapped to an object
 
- 
marshalDomNodeAbstract 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 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:
 
- 
marshalXmlEventWriterprotected 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 marshal
- eventWriter- the- XMLEventWriterto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the DOM node
 
- 
marshalXmlStreamWriterprotected 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 marshal
- streamWriter- the- XMLStreamWriterto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the DOM node
 
- 
marshalSaxHandlersprotected 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 marshal
- contentHandler- the SAX- ContentHandler
- lexicalHandler- the SAX2- LexicalHandler. Can be- null.
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the handlers
 
- 
marshalOutputStreamprotected 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 marshal
- outputStream- the- OutputStreamto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the writer
- IOException- if an I/O exception occurs
 
- 
marshalWriterprotected 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 marshal
- writer- the- Writerto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the writer
- IOException- if an I/O exception occurs
 
- 
unmarshalDomNodeAbstract 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
 
- 
unmarshalXmlEventReaderprotected abstract Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException Abstract template method for unmarshalling from a given StaxXMLEventReader.- Parameters:
- eventReader- the- XMLEventReaderto read from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given event reader cannot be converted to an object
 
- 
unmarshalXmlStreamReaderprotected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException Abstract template method for unmarshalling from a given StaxXMLStreamReader.- Parameters:
- streamReader- the- XMLStreamReaderto read from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given stream reader cannot be converted to an object
 
- 
unmarshalSaxReaderprotected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException Abstract template method for unmarshalling using a given SAXXMLReaderandInputSource.- Parameters:
- xmlReader- the SAX- XMLReaderto 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
- IOException- if an I/O exception occurs
 
- 
unmarshalInputStreamprotected abstract Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException Abstract template method for unmarshalling from a givenInputStream.- Parameters:
- inputStream- the- InputStreamStreamto read from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given stream cannot be converted to an object
- IOException- if an I/O exception occurs
 
- 
unmarshalReaderAbstract template method for unmarshalling from a givenReader.- Parameters:
- reader- the- Readerto read from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given reader cannot be converted to an object
- IOException- if an I/O exception occurs
 
 
-