spring-framework / org.springframework.oxm.xstream / XStreamMarshaller

XStreamMarshaller

open class XStreamMarshaller : AbstractMarshaller, BeanClassLoaderAware, InitializingBean

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.5 or higher, as of Spring 4.3. Note that XStream construction has been reworked in 4.0, with the stream driver and the class loader getting passed into XStream itself now.

Author
Peter Meijer

Author
Arjen Poutsma

Author
Juergen Hoeller

Since
3.0

Constructors

<init>

XStreamMarshaller()

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.5 or higher, as of Spring 4.3. Note that XStream construction has been reworked in 4.0, with the stream driver and the class loader getting passed into XStream itself now.

Properties

DEFAULT_ENCODING

static val DEFAULT_ENCODING: String

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

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

getXStream

fun getXStream(): XStream

Return the native XStream delegate used by this marshaller.

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.

marshalOutputStream

open fun marshalOutputStream(graph: Any, outputStream: OutputStream): Unit
open fun marshalOutputStream(graph: Any, outputStream: OutputStream, dataHolder: DataHolder): Unit

marshalWriter

open fun marshalWriter(graph: Any, writer: Writer): Unit
open fun marshalWriter(graph: Any, writer: Writer, dataHolder: DataHolder): Unit

setAliases

open fun setAliases(aliases: MutableMap<String, *>): Unit

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

Keys are aliases; values are either Class instances, or String class names.

setAliasesByType

open fun setAliasesByType(aliasesByType: MutableMap<String, *>): Unit

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

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.

setAnnotatedClasses

open fun setAnnotatedClasses(vararg annotatedClasses: Class<*>): Unit

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

setAutodetectAnnotations

open fun setAutodetectAnnotations(autodetectAnnotations: Boolean): Unit

Activate XStream's autodetection mode.

Note: Autodetection implies that the XStream instance is being configured while it is processing the XML streams, and thus introduces a potential concurrency problem.

setBeanClassLoader

open fun setBeanClassLoader(classLoader: ClassLoader): Unit

setConverterLookup

open fun setConverterLookup(converterLookup: ConverterLookup): Unit

Set a custom XStream ConverterLookup to use. Also used as ConverterRegistry if the given reference implements it as well.

setConverterRegistry

open fun setConverterRegistry(converterRegistry: ConverterRegistry): Unit

Set a custom XStream ConverterRegistry to use.

setConverters

open fun setConverters(vararg converters: ConverterMatcher): Unit

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

setEncoding

open fun setEncoding(encoding: String): Unit

Set the encoding to be used for stream access.

setFieldAliases

open fun setFieldAliases(fieldAliases: MutableMap<String, String>): Unit

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

setImplicitCollections

open fun setImplicitCollections(implicitCollections: MutableMap<Class<*>, String>): Unit

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

setMapper

open fun setMapper(mapper: Mapper): Unit

Set a custom XStream Mapper to use.

setMapperWrappers

open fun setMapperWrappers(vararg mapperWrappers: Class<out MapperWrapper>): Unit

Set one or more custom XStream MapperWrapper classes. Each of those classes needs to have a constructor with a single argument of type Mapper or MapperWrapper.

setMarshallingStrategy

open fun setMarshallingStrategy(marshallingStrategy: MarshallingStrategy): Unit

Set a custom XStream MarshallingStrategy to use.

setMode

open fun setMode(mode: Int): Unit

Set the XStream mode to use.

setNameCoder

open fun setNameCoder(nameCoder: NameCoder): Unit

Set a custom XStream NameCoder to use. The default is an XmlFriendlyNameCoder.

setOmittedFields

open fun setOmittedFields(omittedFields: MutableMap<Class<*>, String>): Unit

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

setReflectionProvider

open fun setReflectionProvider(reflectionProvider: ReflectionProvider): Unit

Set a custom XStream ReflectionProvider to use.

setStreamDriver

open fun setStreamDriver(streamDriver: HierarchicalStreamDriver): Unit

Set a XStream 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.

setSupportedClasses

open fun setSupportedClasses(vararg supportedClasses: Class<*>): Unit

Set the classes supported by this marshaller.

If this property is empty (the default), all classes are supported.

setUseAttributeFor

open fun setUseAttributeFor(useAttributeFor: MutableMap<*, *>): Unit

Set the types to use XML attributes for. The given map can contain either <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.

setUseAttributeForTypes

open fun setUseAttributeForTypes(vararg useAttributeForTypes: Class<*>): Unit

Set types to use XML attributes for.

supports

open fun supports(clazz: Class<*>): Boolean

unmarshalInputStream

open fun unmarshalInputStream(inputStream: InputStream): Any
open fun unmarshalInputStream(inputStream: InputStream, dataHolder: DataHolder): Any

unmarshalReader

open fun unmarshalReader(reader: Reader): Any
open fun unmarshalReader(reader: Reader, dataHolder: DataHolder): Any

Inherited Functions

isProcessExternalEntities

open fun isProcessExternalEntities(): Boolean

Returns the configured value for whether XML external entities are allowed.

isSupportDtd

open fun isSupportDtd(): Boolean

Whether DTD parsing is supported.

marshal

fun marshal(graph: Any, result: Result): Unit

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.

setProcessExternalEntities

open fun setProcessExternalEntities(processExternalEntities: Boolean): Unit

Indicates 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 to true.

setSupportDtd

open fun setSupportDtd(supportDtd: Boolean): Unit

Indicates whether DTD parsing should be supported.

Default is false meaning that DTD is disabled.

unmarshal

fun unmarshal(source: Source): Any

Unmarshals the given provided javax.xml.transform.Source into an object graph.

This implementation inspects the given result, and calls unmarshalDomSource, unmarshalSaxSource, or unmarshalStreamSource.