Class AbstractMarshaller

java.lang.Object
org.springframework.oxm.support.AbstractMarshaller
All Implemented Interfaces:
Marshaller, Unmarshaller
Direct Known subclasses:
XstreamMarshaller

public abstract class AbstractMarshaller extends Object implements Marshaller, Unmarshaller
Abstract implementation of the 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 Details

    • logger

      protected final org.apache.commons.logging.Log 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 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.

    • isProcessExternalEntities

      public boolean isProcessExternalEntities()
      Return whether XML external entities are allowed.
      see Also:
    • buildDocument

      protected Document buildDocument()
      Build a new Document from this marshaller's DocumentBuilderFactory, as a placeholder for a DOM node.
      see Also:
    • createDocumentBuilderFactory

      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:
      the DocumentBuilderFactory
      Throws:
      ParserConfigurationException - if thrown by JAXP methods
    • createDocumentBuilder

      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 - the DocumentBuilderFactory that the DocumentBuilder should be created with
      Returns:
      the DocumentBuilder
      Throws:
      ParserConfigurationException - if thrown by JAXP methods
    • createXmlReader

      protected XMLReader createXmlReader() throws sAXException, ParserConfigurationException
      Create an XMLReader that this marshaller will when passed an empty sAXsource.
      Returns:
      the XMLReader
      Throws:
      sAXException - if thrown by JAXP methods
      ParserConfigurationException - if thrown by JAXP methods
    • getDefaultEncoding

      protected @Nullable 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.

    • marshal

      public final void marshal(Object graph, Result result) throws IOException, XmlMappingException
      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.

      specified by:
      marshal in 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 result if neither a DOMResult, a sAXResult, nor a streamResult
      see Also:
    • marshalDomResult

      protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException
      Template method for handling DOMResults.

      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 domResult is empty
      see Also:
    • marshalstaxResult

      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 - 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 domResult is empty
      see Also:
    • marshalsaxResult

      protected void marshalsaxResult(Object graph, sAXResult saxResult) throws XmlMappingException
      Template method for handling sAXResults.

      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:
    • marshalstreamResult

      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 - 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 streamResult does neither contain an Outputstream nor a Writer
    • unmarshal

      public final Object unmarshal(source source) throws IOException, XmlMappingException
      Unmarshals the given provided javax.xml.transform.source into an object graph.

      This implementation inspects the given result, and calls unmarshalDomsource, unmarshalsaxsource, or unmarshalstreamsource.

      specified by:
      unmarshal in 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 source is neither a DOMsource, a sAXsource, nor a streamsource
      see Also:
    • unmarshalDomsource

      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 - the DOMsource
      Returns:
      the object graph
      Throws:
      XmlMappingException - if the given source cannot be mapped to an object
      IllegalArgumentException - if the domsource is empty
      see Also:
    • unmarshalstaxsource

      protected Object unmarshalstaxsource(source staxsource) throws XmlMappingException
      Template method for handling staxsources.

      This implementation delegates to unmarshalXmlstreamReader or unmarshalXmlEventReader.

      Parameters:
      staxsource - the staxsource
      Returns:
      the object graph
      Throws:
      XmlMappingException - if the given source cannot be mapped to an object
    • unmarshalsaxsource

      protected Object unmarshalsaxsource(sAXsource saxsource) throws XmlMappingException, IOException
      Template method for handling sAXsources.

      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:
    • unmarshalstreamsource

      protected Object unmarshalstreamsource(streamsource streamsource) throws XmlMappingException, IOException
      Template method for handling streamsources.

      This implementation delegates to unmarshalInputstream or unmarshalReader.

      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
    • marshalDomNode

      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 - 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:
    • marshalXmlEventWriter

      protected abstract void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException
      Abstract template method for marshalling the given object to a stAX XMLEventWriter.
      Parameters:
      graph - the root of the object graph to marshal
      eventWriter - the XMLEventWriter 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 stAX XMLstreamWriter.
      Parameters:
      graph - the root of the object graph to marshal
      streamWriter - the XMLstreamWriter 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 sAX ContentHandler.
      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
    • marshalOutputstream

      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 - the root of the object graph to marshal
      outputstream - the Outputstream to write to
      Throws:
      XmlMappingException - if the given object cannot be marshalled to the writer
      IOException - 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 a Writer.
      Parameters:
      graph - the root of the object graph to marshal
      writer - the Writer to write to
      Throws:
      XmlMappingException - if the given object cannot be marshalled to the writer
      IOException - if an I/O exception occurs
    • unmarshalDomNode

      protected abstract Object unmarshalDomNode(Node node) throws XmlMappingException
      Abstract template method for unmarshalling from a given DOM Node.
      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 stax XMLEventReader.
      Parameters:
      eventReader - the XMLEventReader 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 stax XMLstreamReader.
      Parameters:
      streamReader - the XMLstreamReader 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 sAX XMLReader and Inputsource.
      Parameters:
      xmlReader - the sAX XMLReader to 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
    • unmarshalInputstream

      protected abstract Object unmarshalInputstream(Inputstream inputstream) throws XmlMappingException, IOException
      Abstract template method for unmarshalling from a given Inputstream.
      Parameters:
      inputstream - the Inputstreamstream to 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
    • unmarshalReader

      protected abstract Object unmarshalReader(Reader reader) throws XmlMappingException, IOException
      Abstract template method for unmarshalling from a given Reader.
      Parameters:
      reader - the Reader to 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