Top Description Fields Constructors Methods
org.springframework.oxm.xstream

public Class XStreamMarshaller

extends AbstractMarshaller
implements BeanClassLoaderAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.Aware
Imports
java.io.IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .Reader, .Writer, java.lang.reflect.Constructor, java.util.List, .Map, javax.xml.stream.XMLEventReader, .XMLEventWriter, .XMLStreamException, .XMLStreamReader, .XMLStreamWriter, javax.xml.transform.stream.StreamSource, com.thoughtworks.xstream.MarshallingStrategy, .XStream, com.thoughtworks.xstream.converters.ConversionException, .Converter, .ConverterLookup, .ConverterMatcher, .ConverterRegistry, .DataHolder, .SingleValueConverter, com.thoughtworks.xstream.converters.reflection.ReflectionProvider, com.thoughtworks.xstream.core.ClassLoaderReference, .DefaultConverterLookup, com.thoughtworks.xstream.io.HierarchicalStreamDriver, .HierarchicalStreamReader, .HierarchicalStreamWriter, .StreamException, com.thoughtworks.xstream.io.naming.NameCoder, com.thoughtworks.xstream.io.xml.CompactWriter, .DomDriver, .DomReader, .DomWriter, .QNameMap, .SaxWriter, .StaxDriver, .StaxReader, .StaxWriter, .XmlFriendlyNameCoder, com.thoughtworks.xstream.mapper.CannotResolveClassException, .Mapper, .MapperWrapper, com.thoughtworks.xstream.security.ForbiddenClassException, .TypePermission, org.w3c.dom.Document, .Element, .Node, org.xml.sax.ContentHandler, .InputSource, .XMLReader, org.xml.sax.ext.LexicalHandler, org.springframework.beans.factory.BeanClassLoaderAware, .InitializingBean, org.springframework.lang.Nullable, org.springframework.oxm.MarshallingFailureException, .UncategorizedMappingException, .UnmarshallingFailureException, .XmlMappingException, org.springframework.oxm.support.AbstractMarshaller, org.springframework.util.ClassUtils, .CollectionUtils, .ObjectUtils, .StringUtils, org.springframework.util.function.SingletonSupplier, org.springframework.util.xml.StaxUtils

Implementation of the 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.

As of Spring Framework 6.0, the default HierarchicalStreamDriver is a DomDriver that uses the configured encoding and NameCoder. The driver can be changed via setStreamDriver(HierarchicalStreamDriver).

Authors
Peter Meijer, Arjen Poutsma, Juergen Hoeller, Sam Brannen
Since
3.0

Field Summary

Modifier and TypeField and Description
private Map<String, ?>
private Map<String, ?>
private Class<?>[]
private boolean
private ClassLoader
private ConverterLookup
private ConverterRegistry
private ConverterMatcher[]
public static final String
DEFAULT_ENCODING

The default encoding used for stream access: UTF-8.

private HierarchicalStreamDriver
private String
private Map<String, String>
private Map<Class<?>, String>
private Mapper
private Class<? extends MapperWrapper>[]
private MarshallingStrategy
private Integer
private NameCoder
private Map<Class<?>, String>
private ReflectionProvider
private HierarchicalStreamDriver
private Class<?>[]
private TypePermission[]
private Map<?, ?>
private Class<?>[]
private final SingletonSupplier<XStream>
Inherited from org.springframework.oxm.support.AbstractMarshaller:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing 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.

protected XStream
buildXStream()

Build the native XStream delegate to be used by this marshaller, delegating to constructXStream, configureXStream, and customizeXStream.

protected void
configureXStream(XStream
the XStream instance
xstream
)

Configure the XStream instance with this marshaller's bean properties.

protected XStream

Returns:

the XStream instance
constructXStream
()

Construct an XStream instance, either using one of the standard constructors or creating a custom subclass.

protected XmlMappingException

Returns:

the corresponding XmlMappingException
convertXStreamException
(Exception
the XStream exception that occurred
ex
,
boolean
indicates whether the exception occurs during marshalling (true), or unmarshalling (false)
marshalling
)

Convert the given XStream exception to an appropriate exception from the org.springframework.oxm hierarchy.

protected void
customizeXStream(XStream
the XStream instance
xstream
)

Template to allow for customizing the given XStream.

private void
doMarshal(Object graph, HierarchicalStreamWriter streamWriter, DataHolder dataHolder)

Marshals the given graph to the given XStream HierarchicalStreamWriter.

private Object
doUnmarshal(HierarchicalStreamReader streamReader, DataHolder dataHolder)

Unmarshals the given graph to the given XStream HierarchicalStreamWriter.

private HierarchicalStreamDriver
protected String
getDefaultEncoding()

Overrides org.springframework.oxm.support.AbstractMarshaller.getDefaultEncoding.

Determine the default encoding to use for marshalling or unmarshalling from a byte stream, or null if none.

public final XStream
getXStream()

Return the native XStream delegate used by this marshaller.

protected void
marshalDomNode(Object
the root of the object graph to marshal
graph
,
Node
the DOM node that will contain the result tree
node
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.marshalDomNode.

Abstract template method for marshalling the given object graph to a DOM Node.

public void
marshalOutputStream(Object
the root of the object graph to marshal
graph
,
OutputStream
the OutputStream to write to
outputStream
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.marshalOutputStream.

Abstract template method for marshalling the given object graph to a OutputStream.

public void
marshalOutputStream(Object graph, OutputStream outputStream, DataHolder dataHolder)

protected void
marshalSaxHandlers(Object
the root of the object graph to marshal
graph
,
ContentHandler
the SAX ContentHandler
contentHandler
,
LexicalHandler
the SAX2 LexicalHandler. Can be null.
lexicalHandler
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.marshalSaxHandlers.

Abstract template method for marshalling the given object graph to a SAX ContentHandler.

public void
marshalWriter(Object
the root of the object graph to marshal
graph
,
Writer
the Writer to write to
writer
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.marshalWriter.

Abstract template method for marshalling the given object graph to a Writer.

public void
marshalWriter(Object graph, Writer writer, DataHolder dataHolder)

protected void
marshalXmlEventWriter(Object
the root of the object graph to marshal
graph
,
XMLEventWriter
the XMLEventWriter to write to
eventWriter
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.marshalXmlEventWriter.

Abstract template method for marshalling the given object to a StAX XMLEventWriter.

protected void
marshalXmlStreamWriter(Object
the root of the object graph to marshal
graph
,
XMLStreamWriter
the XMLStreamWriter to write to
streamWriter
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.marshalXmlStreamWriter.

Abstract template method for marshalling the given object to a StAX XMLStreamWriter.

public void
setAliases(Map<String, ?> aliases)

Set the alias/type map, consisting of string aliases mapped to classes.

public void
setAliasesByType(Map<String, ?> aliasesByType)

Set the aliases by type map, consisting of string aliases mapped to classes.

public void
setAnnotatedClasses(Class<?>... annotatedClasses)

Set annotated classes for which aliases will be read from class-level annotation metadata.

public void
setAutodetectAnnotations(boolean autodetectAnnotations)

Activate XStream's autodetection mode.

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

public void
setConverterLookup(ConverterLookup converterLookup)

Set a custom XStream ConverterLookup to use.

public void
setConverterRegistry(ConverterRegistry converterRegistry)

Set a custom XStream ConverterRegistry to use.

public void
setConverters(ConverterMatcher... converters)

Set the Converters or SingleValueConverters to be registered with the XStream instance.

public void
setEncoding(String encoding)

Set the encoding to be used for stream access.

public void
setFieldAliases(Map<String, String> fieldAliases)

Set the field alias/type map, consisting of field names.

public void
setImplicitCollections(Map<Class<?>, String> implicitCollections)

Specify implicit collection fields, as a Map consisting of Class instances mapped to comma separated collection field names.

public void
setMapper(Mapper mapper)

Set a custom XStream Mapper to use.

public void
setMapperWrappers(Class<? extends MapperWrapper>... mapperWrappers)

Set one or more custom XStream MapperWrapper classes.

public void
setMarshallingStrategy(MarshallingStrategy marshallingStrategy)

Set a custom XStream MarshallingStrategy to use.

public void
setMode(int mode)

Set the XStream mode to use.

public void
setNameCoder(NameCoder nameCoder)

Set a custom XStream NameCoder to use.

public void
setOmittedFields(Map<Class<?>, String> omittedFields)

Specify omitted fields, as a Map consisting of Class instances mapped to comma separated field names.

public void
setReflectionProvider(ReflectionProvider reflectionProvider)

Set a custom XStream ReflectionProvider to use.

public void
setStreamDriver(HierarchicalStreamDriver streamDriver)

Set an XStream HierarchicalStreamDriver to be used for readers and writers.

public void
setSupportedClasses(Class<?>... supportedClasses)

Set the classes supported by this marshaller.

public void
setTypePermissions(TypePermission... typePermissions)

Set XStream type permissions such as com.thoughtworks.xstream.security.AnyTypePermission, com.thoughtworks.xstream.security.ExplicitTypePermission etc, as an alternative to overriding the customizeXStream method.

public void
setUseAttributeFor(Map<?, ?> useAttributeFor)

Set the types to use XML attributes for.

public void
setUseAttributeForTypes(Class<?>... useAttributeForTypes)

Set types to use XML attributes for.

public boolean
supports(Class<?>
the class that this marshaller is being asked if it can marshal
clazz
)

Implements org.springframework.oxm.Marshaller.supports, org.springframework.oxm.Unmarshaller.supports.

Indicate whether this marshaller can marshal instances of the supplied type.

private Map<String, Class<?>>
protected Object
unmarshalDomNode(Node
the DOM node that contains the objects to be unmarshalled
node
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.unmarshalDomNode.

Abstract template method for unmarshalling from a given DOM Node.

public Object
unmarshalInputStream(InputStream
the InputStreamStream to read from
inputStream
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.unmarshalInputStream.

Abstract template method for unmarshalling from a given InputStream.

public Object
unmarshalInputStream(InputStream inputStream, DataHolder dataHolder)

public Object
unmarshalReader(Reader
the Reader to read from
reader
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.unmarshalReader.

Abstract template method for unmarshalling from a given Reader.

public Object
unmarshalReader(Reader reader, DataHolder dataHolder)

protected Object
unmarshalSaxReader(XMLReader
the SAX XMLReader to parse with
xmlReader
,
InputSource
the input source to parse from
inputSource
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.unmarshalSaxReader.

Abstract template method for unmarshalling using a given SAX XMLReader and InputSource.

protected Object
unmarshalStreamSource(StreamSource
the StreamSource
streamSource
)

Overrides org.springframework.oxm.support.AbstractMarshaller.unmarshalStreamSource.

Template method for handling StreamSources.

protected Object
unmarshalXmlEventReader(XMLEventReader
the XMLEventReader to read from
eventReader
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.unmarshalXmlEventReader.

Abstract template method for unmarshalling from a given Stax XMLEventReader.

protected Object
unmarshalXmlStreamReader(XMLStreamReader
the XMLStreamReader to read from
streamReader
)

Implements abstract org.springframework.oxm.support.AbstractMarshaller.unmarshalXmlStreamReader.

Abstract template method for unmarshalling from a given Stax XMLStreamReader.

Inherited from org.springframework.oxm.support.AbstractMarshaller:
buildDocumentcreateDocumentBuildercreateDocumentBuilderFactorycreateXmlReaderisProcessExternalEntitiesisSupportDtdmarshalmarshalDomResultmarshalSaxResultmarshalStaxResultmarshalStreamResultsetProcessExternalEntitiessetSupportDtdunmarshalunmarshalDomSourceunmarshalSaxSourceunmarshalStaxSource