public class XStreamMarshaller extends AbstractMarshaller implements InitializingBean, BeanClassLoaderAware
Marshaller
interface for XStream.
By default, XStream does not require any further configuration and can (un)marshal
any class on the classpath. As such, it is not recommended to use the
XStreamMarshaller
to unmarshal XML from external sources (i.e. the Web),
as this can result in security vulnerabilities. If you do use the
XStreamMarshaller
to unmarshal external XML, set the
supportedClasses
and
converters
properties (possibly using a
CatchAllConverter
as the last converter in the list) or override the
customizeXStream(XStream)
method to make sure it only accepts the classes
you want it to support.
Due to XStream's API, it is required to set the encoding used for writing to OutputStreams.
It defaults to UTF-8
.
NOTE: XStream is an XML serialization library, not a data binding library. Therefore, it has limited namespace support. As such, it is rather unsuitable for usage within Web Services.
This marshaller is compatible with XStream 1.3 and 1.4.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ENCODING
The default encoding used for stream access: UTF-8.
|
logger
Constructor and Description |
---|
XStreamMarshaller() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected XmlMappingException |
convertXStreamException(Exception ex,
boolean marshalling)
Convert the given XStream exception to an appropriate exception from the
org.springframework.oxm hierarchy. |
protected void |
customizeXStream(com.thoughtworks.xstream.XStream xstream)
Template to allow for customizing of the given
XStream . |
com.thoughtworks.xstream.XStream |
getXStream()
Return the XStream instance used by this marshaller.
|
protected void |
marshalDomNode(Object graph,
Node node)
Abstract template method for marshalling the given object graph to a DOM
Node . |
protected void |
marshalOutputStream(Object graph,
OutputStream outputStream)
Abstract template method for marshalling the given object graph to a
OutputStream . |
protected void |
marshalSaxHandlers(Object graph,
ContentHandler contentHandler,
LexicalHandler lexicalHandler)
Abstract template method for marshalling the given object graph to a SAX
ContentHandler . |
protected void |
marshalWriter(Object graph,
Writer writer)
Abstract template method for marshalling the given object graph to a
Writer . |
protected void |
marshalXmlEventWriter(Object graph,
XMLEventWriter eventWriter)
Abstract template method for marshalling the given object to a StAX
XMLEventWriter . |
protected void |
marshalXmlStreamWriter(Object graph,
XMLStreamWriter streamWriter)
Abstract template method for marshalling the given object to a StAX
XMLStreamWriter . |
void |
setAliases(Map<String,?> aliases)
Set an alias/type map, consisting of String aliases mapped to classes.
|
void |
setAliasesByType(Map<String,?> aliases)
Set the aliases by type map, consisting of String aliases mapped to classes.
|
void |
setAnnotatedClass(Class<?> annotatedClass)
Deprecated.
in favor of
setAnnotatedClasses(java.lang.Class<?>...) with varargs |
void |
setAnnotatedClasses(Class<?>... annotatedClasses)
Set annotated classes for which aliases will be read from class-level annotation metadata.
|
void |
setAutodetectAnnotations(boolean autodetectAnnotations)
Activate XStream's autodetection mode.
|
void |
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean
class loader to
a bean instance. |
void |
setConverters(com.thoughtworks.xstream.converters.ConverterMatcher... converters)
Set the
Converters or SingleValueConverters to be registered
with the XStream instance. |
void |
setEncoding(String encoding)
Set the encoding to be used for stream access.
|
void |
setFieldAliases(Map<String,String> aliases)
Set a field alias/type map, consisting of field names.
|
void |
setImplicitCollections(Map<Class<?>,String> implicitCollections)
Specify implicit collection fields, as a Map consisting of
Class instances
mapped to comma separated collection field names. |
void |
setMode(int mode)
Set the XStream mode.
|
void |
setOmittedFields(Map<Class<?>,String> omittedFields)
Specify omitted fields, as a Map consisting of
Class instances
mapped to comma separated field names. |
void |
setStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver)
Set the XStream hierarchical stream driver to be used with stream readers and writers.
|
void |
setSupportedClasses(Class<?>... supportedClasses)
Set the classes supported by this marshaller.
|
void |
setUseAttributeFor(Map<?,?> attributes)
Set the types to use XML attributes for.
|
void |
setUseAttributeForTypes(Class<?>... types)
Set types to use XML attributes for.
|
boolean |
supports(Class<?> clazz)
Indicates whether this marshaller can marshal instances of the supplied type.
|
protected Object |
unmarshalDomNode(Node node)
Abstract template method for unmarshalling from a given DOM
Node . |
protected Object |
unmarshalInputStream(InputStream inputStream)
Abstract template method for unmarshalling from a given
InputStream . |
protected Object |
unmarshalReader(Reader reader)
Abstract template method for unmarshalling from a given
Reader . |
protected Object |
unmarshalSaxReader(XMLReader xmlReader,
InputSource inputSource)
Abstract template method for unmarshalling using a given SAX
XMLReader
and InputSource . |
protected Object |
unmarshalXmlEventReader(XMLEventReader eventReader)
Abstract template method for unmarshalling from a given Stax
XMLEventReader . |
protected Object |
unmarshalXmlStreamReader(XMLStreamReader streamReader)
Abstract template method for unmarshalling from a given Stax
XMLStreamReader . |
createDocumentBuilder, createDocumentBuilderFactory, createXmlReader, marshal, marshalDomResult, marshalSaxResult, marshalStaxResult, marshalStreamResult, unmarshal, unmarshalDomSource, unmarshalSaxSource, unmarshalStaxSource, unmarshalStreamSource
public static final String DEFAULT_ENCODING
public com.thoughtworks.xstream.XStream getXStream()
NOTE: While this method can be overridden in Spring 3.x, it wasn't originally meant to be. As of Spring 4.0, it will be marked as final, with all of XStream 1.4's configurable strategies to be exposed on XStreamMarshaller itself.
public void setMode(int mode)
XStream#XPATH_REFERENCES
,
XStream.ID_REFERENCES
,
XStream.NO_REFERENCES
public void setConverters(com.thoughtworks.xstream.converters.ConverterMatcher... converters)
Converters
or SingleValueConverters
to be registered
with the XStream
instance.Converter
,
SingleValueConverter
public void setAliases(Map<String,?> aliases) throws ClassNotFoundException
Keys are aliases; values are either Class objects or String class names.
ClassNotFoundException
XStream.alias(String, Class)
public void setAliasesByType(Map<String,?> aliases) throws ClassNotFoundException
Any class that is assignable to this type will be aliased to the same name. Keys are aliases; values are either Class objects or String class names.
ClassNotFoundException
XStream.aliasType(String, Class)
public void setFieldAliases(Map<String,String> aliases) throws ClassNotFoundException, NoSuchFieldException
ClassNotFoundException
NoSuchFieldException
XStream.aliasField(String, Class, String)
public void setUseAttributeForTypes(Class<?>... types)
XStream.useAttributeFor(Class)
public void setUseAttributeFor(Map<?,?> attributes)
<String, Class>
pairs, in which case
XStream.useAttributeFor(String, Class)
is called.
Alternatively, the map can contain <Class, String>
or <Class, List<String>>
pairs, which results in
XStream.useAttributeFor(Class, String)
calls.public void setImplicitCollections(Map<Class<?>,String> implicitCollections)
Class
instances
mapped to comma separated collection field names.XStream.addImplicitCollection(Class, String)
public void setOmittedFields(Map<Class<?>,String> omittedFields)
Class
instances
mapped to comma separated field names.XStream.omitField(Class, String)
@Deprecated public void setAnnotatedClass(Class<?> annotatedClass)
setAnnotatedClasses(java.lang.Class<?>...)
with varargsXStream.processAnnotations(Class)
public void setAnnotatedClasses(Class<?>... annotatedClasses)
XStream.processAnnotations(Class[])
public void setAutodetectAnnotations(boolean autodetectAnnotations)
Note: Autodetection implies that the XStream instance is being configured while it is processing the XML streams, and thus introduces a potential concurrency problem.
XStream.autodetectAnnotations(boolean)
public void setStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver)
public void setEncoding(String encoding)
DEFAULT_ENCODING
public void setSupportedClasses(Class<?>... supportedClasses)
If this property is empty (the default), all classes are supported.
supports(Class)
public void setBeanClassLoader(ClassLoader classLoader)
BeanClassLoaderAware
class loader
to
a bean instance.
Invoked after the population of normal bean properties but
before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.
setBeanClassLoader
in interface BeanClassLoaderAware
classLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
public final void afterPropertiesSet() throws Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.protected void customizeXStream(com.thoughtworks.xstream.XStream xstream)
XStream
.
The default implementation is empty.
xstream
- the XStream
instancepublic boolean supports(Class<?> clazz)
Marshaller
supports
in interface Marshaller
supports
in interface Unmarshaller
clazz
- the class that this marshaller is being asked if it can marshaltrue
if this marshaller can indeed marshal instances of the supplied class;
false
otherwiseprotected void marshalDomNode(Object graph, Node node) throws XmlMappingException
AbstractMarshaller
Node
.
In practice, node is be a Document
node, a DocumentFragment
node,
or a Element
node. In other words, a node that accepts children.
marshalDomNode
in class AbstractMarshaller
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 void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException
AbstractMarshaller
XMLEventWriter
.marshalXmlEventWriter
in class AbstractMarshaller
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 void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException
AbstractMarshaller
XMLStreamWriter
.marshalXmlStreamWriter
in class AbstractMarshaller
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 void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException
AbstractMarshaller
OutputStream
.marshalOutputStream
in class AbstractMarshaller
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 void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException
AbstractMarshaller
ContentHandler
.marshalSaxHandlers
in class AbstractMarshaller
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 void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException
AbstractMarshaller
Writer
.marshalWriter
in class AbstractMarshaller
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 Object unmarshalDomNode(Node node) throws XmlMappingException
AbstractMarshaller
Node
.unmarshalDomNode
in class AbstractMarshaller
node
- the DOM node that contains the objects to be unmarshalledXmlMappingException
- if the given DOM node cannot be mapped to an objectprotected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException
AbstractMarshaller
XMLEventReader
.unmarshalXmlEventReader
in class AbstractMarshaller
eventReader
- the XMLEventReader
to read fromXmlMappingException
- if the given event reader cannot be converted to an objectprotected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException
AbstractMarshaller
XMLStreamReader
.unmarshalXmlStreamReader
in class AbstractMarshaller
streamReader
- the XMLStreamReader
to read fromXmlMappingException
- if the given stream reader cannot be converted to an objectprotected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException
AbstractMarshaller
InputStream
.unmarshalInputStream
in class AbstractMarshaller
inputStream
- the InputStreamStream
to read fromXmlMappingException
- if the given stream cannot be converted to an objectIOException
- if an I/O exception occursprotected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException
AbstractMarshaller
Reader
.unmarshalReader
in class AbstractMarshaller
reader
- the Reader
to read fromXmlMappingException
- if the given reader cannot be converted to an objectIOException
- if an I/O exception occursprotected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException
AbstractMarshaller
XMLReader
and InputSource
.unmarshalSaxReader
in class AbstractMarshaller
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 XmlMappingException convertXStreamException(Exception ex, boolean marshalling)
org.springframework.oxm
hierarchy.
A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since XStream itself does not make this distinction in its exception hierarchy.
ex
- XStream exception that occuredmarshalling
- indicates whether the exception occurs during marshalling (true
),
or unmarshalling (false
)XmlMappingException