public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLoaderAware, InitializingBean
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
) 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 requires XStream 1.4.7 or higher, as of Spring 5.2.17.
Note that XStream
construction has been reworked in 4.0, with the
stream driver and the class loader getting passed into XStream itself now.
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 the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
protected com.thoughtworks.xstream.XStream |
buildXStream()
Build the native XStream delegate to be used by this marshaller,
delegating to
constructXStream() , configureXStream(com.thoughtworks.xstream.XStream) ,
and customizeXStream(com.thoughtworks.xstream.XStream) . |
protected void |
configureXStream(com.thoughtworks.xstream.XStream xstream)
Configure the XStream instance with this marshaller's bean properties.
|
protected com.thoughtworks.xstream.XStream |
constructXStream()
Construct an XStream instance, either using one of the
standard constructors or creating a custom subclass.
|
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 the given
XStream . |
protected String |
getDefaultEncoding()
Determine the default encoding to use for marshalling or unmarshalling from
a byte stream, or
null if none. |
com.thoughtworks.xstream.XStream |
getXStream()
Return the native XStream delegate used by this marshaller.
|
protected void |
marshalDomNode(Object graph,
Node node)
Abstract template method for marshalling the given object graph to a DOM
Node . |
void |
marshalOutputStream(Object graph,
OutputStream outputStream)
Abstract template method for marshalling the given object graph to a
OutputStream . |
void |
marshalOutputStream(Object graph,
OutputStream outputStream,
com.thoughtworks.xstream.converters.DataHolder dataHolder) |
protected void |
marshalSaxHandlers(Object graph,
ContentHandler contentHandler,
LexicalHandler lexicalHandler)
Abstract template method for marshalling the given object graph to a SAX
ContentHandler . |
void |
marshalWriter(Object graph,
Writer writer)
Abstract template method for marshalling the given object graph to a
Writer . |
void |
marshalWriter(Object graph,
Writer writer,
com.thoughtworks.xstream.converters.DataHolder dataHolder) |
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 the alias/type map, consisting of string aliases mapped to classes.
|
void |
setAliasesByType(Map<String,?> aliasesByType)
Set the aliases by type map, consisting of string aliases mapped to classes.
|
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 |
setConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup converterLookup)
Set a custom XStream
ConverterLookup to use. |
void |
setConverterRegistry(com.thoughtworks.xstream.converters.ConverterRegistry converterRegistry)
Set a custom XStream
ConverterRegistry to use. |
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> fieldAliases)
Set the 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 |
setMapper(com.thoughtworks.xstream.mapper.Mapper mapper)
Set a custom XStream
Mapper to use. |
void |
setMapperWrappers(Class<? extends com.thoughtworks.xstream.mapper.MapperWrapper>... mapperWrappers)
Set one or more custom XStream
MapperWrapper classes. |
void |
setMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy)
Set a custom XStream
MarshallingStrategy to use. |
void |
setMode(int mode)
Set the XStream mode to use.
|
void |
setNameCoder(com.thoughtworks.xstream.io.naming.NameCoder nameCoder)
Set a custom XStream
NameCoder to use. |
void |
setOmittedFields(Map<Class<?>,String> omittedFields)
Specify omitted fields, as a Map consisting of
Class instances
mapped to comma separated field names. |
void |
setReflectionProvider(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider)
Set a custom XStream
ReflectionProvider to use. |
void |
setStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver)
Set a XStream
HierarchicalStreamDriver to be used for readers and writers. |
void |
setSupportedClasses(Class<?>... supportedClasses)
Set the classes supported by this marshaller.
|
void |
setTypePermissions(com.thoughtworks.xstream.security.TypePermission... typePermissions)
Set XStream type permissions such as
AnyTypePermission ,
ExplicitTypePermission etc,
as an alternative to overriding the customizeXStream(com.thoughtworks.xstream.XStream) method. |
void |
setUseAttributeFor(Map<?,?> useAttributeFor)
Set the types to use XML attributes for.
|
void |
setUseAttributeForTypes(Class<?>... useAttributeForTypes)
Set types to use XML attributes for.
|
boolean |
supports(Class<?> clazz)
Indicate 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 . |
Object |
unmarshalInputStream(InputStream inputStream)
Abstract template method for unmarshalling from a given
InputStream . |
Object |
unmarshalInputStream(InputStream inputStream,
com.thoughtworks.xstream.converters.DataHolder dataHolder) |
Object |
unmarshalReader(Reader reader)
Abstract template method for unmarshalling from a given
Reader . |
Object |
unmarshalReader(Reader reader,
com.thoughtworks.xstream.converters.DataHolder dataHolder) |
protected Object |
unmarshalSaxReader(XMLReader xmlReader,
InputSource inputSource)
Abstract template method for unmarshalling using a given SAX
XMLReader
and InputSource . |
protected Object |
unmarshalStreamSource(StreamSource streamSource)
Template method for handling
StreamSource s. |
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 . |
buildDocument, createDocumentBuilder, createDocumentBuilderFactory, createXmlReader, isProcessExternalEntities, isSupportDtd, marshal, marshalDomResult, marshalSaxResult, marshalStaxResult, marshalStreamResult, setProcessExternalEntities, setSupportDtd, unmarshal, unmarshalDomSource, unmarshalSaxSource, unmarshalStaxSource
public static final String DEFAULT_ENCODING
public void setReflectionProvider(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider)
ReflectionProvider
to use.public void setStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver)
HierarchicalStreamDriver
to be used for readers and writers.
As of Spring 4.0, this stream driver will also be passed to the XStream
constructor and therefore used by streaming-related native API methods themselves.
public void setMapper(com.thoughtworks.xstream.mapper.Mapper mapper)
Mapper
to use.public void setMapperWrappers(Class<? extends com.thoughtworks.xstream.mapper.MapperWrapper>... mapperWrappers)
MapperWrapper
classes.
Each of those classes needs to have a constructor with a single argument
of type Mapper
or MapperWrapper
.public void setConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup converterLookup)
ConverterLookup
to use.
Also used as ConverterRegistry
if the given reference implements it as well.DefaultConverterLookup
public void setConverterRegistry(com.thoughtworks.xstream.converters.ConverterRegistry converterRegistry)
ConverterRegistry
to use.setConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup)
,
DefaultConverterLookup
public void setConverters(com.thoughtworks.xstream.converters.ConverterMatcher... converters)
Converters
or SingleValueConverters
to be registered
with the XStream
instance.Converter
,
SingleValueConverter
public void setTypePermissions(com.thoughtworks.xstream.security.TypePermission... typePermissions)
AnyTypePermission
,
ExplicitTypePermission
etc,
as an alternative to overriding the customizeXStream(com.thoughtworks.xstream.XStream)
method.
Note: As of XStream 1.4.18, the default type permissions are restricted to well-known core JDK types. For any custom types, explicit type permissions need to be registered.
public void setMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy)
MarshallingStrategy
to use.public void setMode(int mode)
XStream.ID_REFERENCES
,
XStream.NO_REFERENCES
public void setAliases(Map<String,?> aliases)
Keys are aliases; values are either Class
instances, or String class names.
XStream.alias(String, Class)
public void setAliasesByType(Map<String,?> aliasesByType)
Any class that is assignable to this type will be aliased to the same name.
Keys are aliases; values are either Class
instances, or String class names.
XStream.aliasType(String, Class)
public void setFieldAliases(Map<String,String> fieldAliases)
XStream.aliasField(String, Class, String)
public void setUseAttributeForTypes(Class<?>... useAttributeForTypes)
XStream.useAttributeFor(Class)
public void setUseAttributeFor(Map<?,?> useAttributeFor)
<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)
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 setEncoding(String encoding)
DEFAULT_ENCODING
protected String getDefaultEncoding()
AbstractMarshaller
null
if none.
The default implementation returns null
.
getDefaultEncoding
in class AbstractMarshaller
public void setNameCoder(com.thoughtworks.xstream.io.naming.NameCoder nameCoder)
NameCoder
to use.
The default is an XmlFriendlyNameCoder
.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 loaderpublic void afterPropertiesSet()
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, ApplicationContextAware
etc.
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
afterPropertiesSet
in interface InitializingBean
protected com.thoughtworks.xstream.XStream buildXStream()
constructXStream()
, configureXStream(com.thoughtworks.xstream.XStream)
,
and customizeXStream(com.thoughtworks.xstream.XStream)
.protected com.thoughtworks.xstream.XStream constructXStream()
XStream
instanceprotected void configureXStream(com.thoughtworks.xstream.XStream xstream)
xstream
- the XStream
instanceprotected void customizeXStream(com.thoughtworks.xstream.XStream xstream)
XStream
.
The default implementation is empty.
xstream
- the XStream
instancepublic final com.thoughtworks.xstream.XStream getXStream()
NOTE: This method has been marked as final as of Spring 4.0. It can be used to access the fully configured XStream for marshalling but not configuration purposes anymore.
As of Spring Framework 5.1.16, creation of the XStream
instance
returned by this method is thread safe.
public 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 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 marshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable 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 handlerspublic 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 occurspublic void marshalOutputStream(Object graph, OutputStream outputStream, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
XmlMappingException
IOException
public 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 occurspublic void marshalWriter(Object graph, Writer writer, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
XmlMappingException
IOException
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException
AbstractMarshaller
StreamSource
s.
This implementation delegates to unmarshalInputStream
or unmarshalReader
.
unmarshalStreamSource
in class AbstractMarshaller
streamSource
- the StreamSource
XmlMappingException
- if the given source cannot be mapped to an objectIOException
- 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 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 occurspublic 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 occurspublic Object unmarshalInputStream(InputStream inputStream, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
XmlMappingException
IOException
public 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 occurspublic Object unmarshalReader(Reader reader, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
XmlMappingException
IOException
protected 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
- the XStream exception that occurredmarshalling
- indicates whether the exception occurs during marshalling (true
),
or unmarshalling (false
)XmlMappingException