Class XStreamMarshaller
- All Implemented Interfaces:
- Aware, BeanClassLoaderAware, InitializingBean, Marshaller, Unmarshaller
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 Framework 6.0, the default HierarchicalStreamDriver is
a DomDriver that uses the configured encoding and NameCoder. The driver can be
changed via setStreamDriver(HierarchicalStreamDriver).
- Since:
- 3.0
- Author:
- Peter Meijer, Arjen Poutsma, Juergen Hoeller, Sam Brannen
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe default encoding used for stream access: UTF-8.Fields inherited from class AbstractMarshallerlogger
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected com.thoughtworks.xstream.XStreamBuild the native XStream delegate to be used by this marshaller, delegating toconstructXStream(),configureXStream(XStream), andcustomizeXStream(XStream).protected voidconfigureXStream(com.thoughtworks.xstream.XStream xstream) Configure the XStream instance with this marshaller's bean properties.protected com.thoughtworks.xstream.XStreamConstruct an XStream instance, either using one of the standard constructors or creating a custom subclass.protected XmlMappingExceptionconvertXStreamException(Exception ex, boolean marshalling) Convert the given XStream exception to an appropriate exception from theorg.springframework.oxmhierarchy.protected voidcustomizeXStream(com.thoughtworks.xstream.XStream xstream) Template to allow for customizing the givenXStream.protected StringDetermine the default encoding to use for marshalling or unmarshalling from a byte stream, ornullif none.final com.thoughtworks.xstream.XStreamReturn the native XStream delegate used by this marshaller.protected voidmarshalDomNode(Object graph, Node node) Abstract template method for marshalling the given object graph to a DOMNode.voidmarshalOutputStream(Object graph, OutputStream outputStream) Abstract template method for marshalling the given object graph to aOutputStream.voidmarshalOutputStream(Object graph, OutputStream outputStream, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) protected voidmarshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler) Abstract template method for marshalling the given object graph to a SAXContentHandler.voidmarshalWriter(Object graph, Writer writer) Abstract template method for marshalling the given object graph to aWriter.voidmarshalWriter(Object graph, Writer writer, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) protected voidmarshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) Abstract template method for marshalling the given object to a StAXXMLEventWriter.protected voidmarshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) Abstract template method for marshalling the given object to a StAXXMLStreamWriter.voidsetAliases(Map<String, ?> aliases) Set the alias/type map, consisting of string aliases mapped to classes.voidsetAliasesByType(Map<String, ?> aliasesByType) Set the aliases by type map, consisting of string aliases mapped to classes.voidsetAnnotatedClasses(Class<?>... annotatedClasses) Set annotated classes for which aliases will be read from class-level annotation metadata.voidsetAutodetectAnnotations(boolean autodetectAnnotations) Activate XStream's autodetection mode.voidsetBeanClassLoader(ClassLoader classLoader) Callback that supplies the beanclass loaderto a bean instance.voidsetConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup converterLookup) Set a custom XStreamConverterLookupto use.voidsetConverterRegistry(com.thoughtworks.xstream.converters.ConverterRegistry converterRegistry) Set a custom XStreamConverterRegistryto use.voidsetConverters(com.thoughtworks.xstream.converters.ConverterMatcher... converters) Set theConvertersorSingleValueConvertersto be registered with theXStreaminstance.voidsetEncoding(String encoding) Set the encoding to be used for stream access.voidsetFieldAliases(Map<String, String> fieldAliases) Set the field alias/type map, consisting of field names.voidsetImplicitCollections(Map<Class<?>, String> implicitCollections) Specify implicit collection fields, as a Map consisting ofClassinstances mapped to comma separated collection field names.voidsetMapper(com.thoughtworks.xstream.mapper.Mapper mapper) Set a custom XStreamMapperto use.voidsetMapperWrappers(Class<? extends com.thoughtworks.xstream.mapper.MapperWrapper>... mapperWrappers) Set one or more custom XStreamMapperWrapperclasses.voidsetMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy) Set a custom XStreamMarshallingStrategyto use.voidsetMode(int mode) Set the XStream mode to use.voidsetNameCoder(com.thoughtworks.xstream.io.naming.NameCoder nameCoder) Set a custom XStreamNameCoderto use.voidsetOmittedFields(Map<Class<?>, String> omittedFields) Specify omitted fields, as a Map consisting ofClassinstances mapped to comma separated field names.voidsetReflectionProvider(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider) Set a custom XStreamReflectionProviderto use.voidsetStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver) Set an XStreamHierarchicalStreamDriverto be used for readers and writers.voidsetSupportedClasses(Class<?>... supportedClasses) Set the classes supported by this marshaller.voidsetTypePermissions(com.thoughtworks.xstream.security.TypePermission... typePermissions) Set XStream type permissions such asAnyTypePermission,ExplicitTypePermissionetc, as an alternative to overriding thecustomizeXStream(XStream)method.voidsetUseAttributeFor(Map<?, ?> useAttributeFor) Set the types to use XML attributes for.voidsetUseAttributeForTypes(Class<?>... useAttributeForTypes) Set types to use XML attributes for.booleanIndicate whether this marshaller can marshal instances of the supplied type.protected ObjectunmarshalDomNode(Node node) Abstract template method for unmarshalling from a given DOMNode.unmarshalInputStream(InputStream inputStream) Abstract template method for unmarshalling from a givenInputStream.unmarshalInputStream(InputStream inputStream, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) unmarshalReader(Reader reader) Abstract template method for unmarshalling from a givenReader.unmarshalReader(Reader reader, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) protected ObjectunmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) Abstract template method for unmarshalling using a given SAXXMLReaderandInputSource.protected ObjectunmarshalStreamSource(StreamSource streamSource) Template method for handlingStreamSources.protected ObjectunmarshalXmlEventReader(XMLEventReader eventReader) Abstract template method for unmarshalling from a given StaxXMLEventReader.protected ObjectunmarshalXmlStreamReader(XMLStreamReader streamReader) Abstract template method for unmarshalling from a given StaxXMLStreamReader.Methods inherited from class AbstractMarshallerbuildDocument, createDocumentBuilder, createDocumentBuilderFactory, createXmlReader, isProcessExternalEntities, isSupportDtd, marshal, marshalDomResult, marshalSaxResult, marshalStaxResult, marshalStreamResult, setProcessExternalEntities, setSupportDtd, unmarshal, unmarshalDomSource, unmarshalSaxSource, unmarshalStaxSource
- 
Field Details- 
DEFAULT_ENCODINGThe default encoding used for stream access: UTF-8.- See Also:
 
 
- 
- 
Constructor Details- 
XStreamMarshallerpublic XStreamMarshaller()
 
- 
- 
Method Details- 
setReflectionProviderpublic void setReflectionProvider(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider) Set a custom XStreamReflectionProviderto use.- Since:
- 4.0
 
- 
setStreamDriverpublic void setStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver) Set an XStreamHierarchicalStreamDriverto be used for readers and writers.As of Spring 4.0, this stream driver will also be passed to the XStreamconstructor and therefore used by streaming-related native API methods themselves.
- 
setMapperpublic void setMapper(com.thoughtworks.xstream.mapper.Mapper mapper) Set a custom XStreamMapperto use.- Since:
- 4.0
 
- 
setMapperWrapperspublic void setMapperWrappers(Class<? extends com.thoughtworks.xstream.mapper.MapperWrapper>... mapperWrappers) Set one or more custom XStreamMapperWrapperclasses. Each of those classes needs to have a constructor with a single argument of typeMapperorMapperWrapper.- Since:
- 4.0
 
- 
setConverterLookuppublic void setConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup converterLookup) Set a custom XStreamConverterLookupto use. Also used asConverterRegistryif the given reference implements it as well.- Since:
- 4.0
- See Also:
 
- 
setConverterRegistrypublic void setConverterRegistry(com.thoughtworks.xstream.converters.ConverterRegistry converterRegistry) Set a custom XStreamConverterRegistryto use.- Since:
- 4.0
- See Also:
 
- 
setConverterspublic void setConverters(com.thoughtworks.xstream.converters.ConverterMatcher... converters) Set theConvertersorSingleValueConvertersto be registered with theXStreaminstance.- See Also:
 
- 
setTypePermissionspublic void setTypePermissions(com.thoughtworks.xstream.security.TypePermission... typePermissions) Set XStream type permissions such asAnyTypePermission,ExplicitTypePermissionetc, as an alternative to overriding thecustomizeXStream(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. - Since:
- 5.2.17
 
- 
setMarshallingStrategypublic void setMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy) Set a custom XStreamMarshallingStrategyto use.- Since:
- 4.0
 
- 
setModepublic void setMode(int mode) Set the XStream mode to use.- See Also:
 
- 
setAliases
- 
setAliasesByType
- 
setFieldAliases
- 
setUseAttributeForTypesSet types to use XML attributes for.- See Also:
 
- 
setUseAttributeForSet the types to use XML attributes for. The given map can contain either<String, Class>pairs, in which caseXStream.useAttributeFor(String, Class)is called. Alternatively, the map can contain<Class, String>or<Class, List<String>>pairs, which results inXStream.useAttributeFor(Class, String)calls.
- 
setImplicitCollections
- 
setOmittedFields
- 
setAnnotatedClassesSet annotated classes for which aliases will be read from class-level annotation metadata.- See Also:
 
- 
setAutodetectAnnotationspublic void setAutodetectAnnotations(boolean autodetectAnnotations) 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. - See Also:
 
- 
setEncoding
- 
getDefaultEncodingDescription copied from class:AbstractMarshallerDetermine the default encoding to use for marshalling or unmarshalling from a byte stream, ornullif none.The default implementation returns null.- Overrides:
- getDefaultEncodingin class- AbstractMarshaller
 
- 
setNameCoderpublic void setNameCoder(com.thoughtworks.xstream.io.naming.NameCoder nameCoder) Set a custom XStreamNameCoderto use. The default is anXmlFriendlyNameCoder.- Since:
- 4.0.4
 
- 
setSupportedClassesSet the classes supported by this marshaller.If this property is empty (the default), all classes are supported. - See Also:
 
- 
setBeanClassLoaderDescription copied from interface:BeanClassLoaderAwareCallback that supplies the beanclass loaderto a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean'sInitializingBean.afterPropertiesSet()method or a custom init-method.- Specified by:
- setBeanClassLoaderin interface- BeanClassLoaderAware
- Parameters:
- classLoader- the owning class loader
 
- 
afterPropertiesSetpublic void afterPropertiesSet()Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. - Specified by:
- afterPropertiesSetin interface- InitializingBean
 
- 
buildXStreamprotected com.thoughtworks.xstream.XStream buildXStream()Build the native XStream delegate to be used by this marshaller, delegating toconstructXStream(),configureXStream(XStream), andcustomizeXStream(XStream).
- 
constructXStreamprotected com.thoughtworks.xstream.XStream constructXStream()Construct an XStream instance, either using one of the standard constructors or creating a custom subclass.- Returns:
- the XStreaminstance
 
- 
configureXStreamprotected void configureXStream(com.thoughtworks.xstream.XStream xstream) Configure the XStream instance with this marshaller's bean properties.- Parameters:
- xstream- the- XStreaminstance
 
- 
customizeXStreamprotected void customizeXStream(com.thoughtworks.xstream.XStream xstream) Template to allow for customizing the givenXStream.The default implementation is empty. - Parameters:
- xstream- the- XStreaminstance
 
- 
getXStreampublic final com.thoughtworks.xstream.XStream getXStream()Return the native XStream delegate used by this marshaller.The creation of the XStreaminstance returned by this method is thread safe.NOTE: This method is marked as final. It can be used to access the fully configured XStream for marshalling but not configuration purposes. 
- 
supportsDescription copied from interface:MarshallerIndicate whether this marshaller can marshal instances of the supplied type.- Specified by:
- supportsin interface- Marshaller
- Specified by:
- supportsin interface- Unmarshaller
- Parameters:
- clazz- the class that this marshaller is being asked if it can marshal
- Returns:
- trueif this marshaller can indeed marshal instances of the supplied class;- falseotherwise
 
- 
marshalDomNodeDescription copied from class:AbstractMarshallerAbstract template method for marshalling the given object graph to a DOMNode.In practice, nodeis aDocumentnode, aDocumentFragmentnode, or aElementnode. In other words, a node that accepts children.- Specified by:
- marshalDomNodein class- AbstractMarshaller
- 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:
 
- 
marshalXmlEventWriterprotected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException Description copied from class:AbstractMarshallerAbstract template method for marshalling the given object to a StAXXMLEventWriter.- Specified by:
- marshalXmlEventWriterin class- AbstractMarshaller
- Parameters:
- graph- the root of the object graph to marshal
- eventWriter- the- XMLEventWriterto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the DOM node
 
- 
marshalXmlStreamWriterprotected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException Description copied from class:AbstractMarshallerAbstract template method for marshalling the given object to a StAXXMLStreamWriter.- Specified by:
- marshalXmlStreamWriterin class- AbstractMarshaller
- Parameters:
- graph- the root of the object graph to marshal
- streamWriter- the- XMLStreamWriterto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the DOM node
 
- 
marshalSaxHandlersprotected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler) throws XmlMappingException Description copied from class:AbstractMarshallerAbstract template method for marshalling the given object graph to a SAXContentHandler.- Specified by:
- marshalSaxHandlersin class- AbstractMarshaller
- 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
 
- 
marshalOutputStreampublic void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException Description copied from class:AbstractMarshallerAbstract template method for marshalling the given object graph to aOutputStream.- Specified by:
- marshalOutputStreamin class- AbstractMarshaller
- Parameters:
- graph- the root of the object graph to marshal
- outputStream- the- OutputStreamto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the writer
- IOException- if an I/O exception occurs
 
- 
marshalOutputStreampublic void marshalOutputStream(Object graph, OutputStream outputStream, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException - Throws:
- XmlMappingException
- IOException
 
- 
marshalWriterDescription copied from class:AbstractMarshallerAbstract template method for marshalling the given object graph to aWriter.- Specified by:
- marshalWriterin class- AbstractMarshaller
- Parameters:
- graph- the root of the object graph to marshal
- writer- the- Writerto write to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the writer
- IOException- if an I/O exception occurs
 
- 
marshalWriterpublic void marshalWriter(Object graph, Writer writer, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException - Throws:
- XmlMappingException
- IOException
 
- 
unmarshalStreamSourceprotected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException Description copied from class:AbstractMarshallerTemplate method for handlingStreamSources.This implementation delegates to unmarshalInputStreamorunmarshalReader.- Overrides:
- unmarshalStreamSourcein class- AbstractMarshaller
- Parameters:
- streamSource- the- StreamSource
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given source cannot be mapped to an object
- IOException- if an I/O exception occurs
 
- 
unmarshalDomNodeDescription copied from class:AbstractMarshallerAbstract template method for unmarshalling from a given DOMNode.- Specified by:
- unmarshalDomNodein class- AbstractMarshaller
- 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
 
- 
unmarshalXmlEventReaderDescription copied from class:AbstractMarshallerAbstract template method for unmarshalling from a given StaxXMLEventReader.- Specified by:
- unmarshalXmlEventReaderin class- AbstractMarshaller
- Parameters:
- eventReader- the- XMLEventReaderto read from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given event reader cannot be converted to an object
 
- 
unmarshalXmlStreamReaderDescription copied from class:AbstractMarshallerAbstract template method for unmarshalling from a given StaxXMLStreamReader.- Specified by:
- unmarshalXmlStreamReaderin class- AbstractMarshaller
- Parameters:
- streamReader- the- XMLStreamReaderto read from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given stream reader cannot be converted to an object
 
- 
unmarshalSaxReaderprotected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException Description copied from class:AbstractMarshallerAbstract template method for unmarshalling using a given SAXXMLReaderandInputSource.- Specified by:
- unmarshalSaxReaderin class- AbstractMarshaller
- Parameters:
- xmlReader- the SAX- XMLReaderto 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
 
- 
unmarshalInputStreamDescription copied from class:AbstractMarshallerAbstract template method for unmarshalling from a givenInputStream.- Specified by:
- unmarshalInputStreamin class- AbstractMarshaller
- Parameters:
- inputStream- the- InputStreamStreamto 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
 
- 
unmarshalInputStreampublic Object unmarshalInputStream(InputStream inputStream, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException - Throws:
- XmlMappingException
- IOException
 
- 
unmarshalReaderDescription copied from class:AbstractMarshallerAbstract template method for unmarshalling from a givenReader.- Specified by:
- unmarshalReaderin class- AbstractMarshaller
- Parameters:
- reader- the- Readerto 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
 
- 
unmarshalReaderpublic Object unmarshalReader(Reader reader, @Nullable com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException - Throws:
- XmlMappingException
- IOException
 
- 
convertXStreamExceptionConvert the given XStream exception to an appropriate exception from theorg.springframework.oxmhierarchy.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. - Parameters:
- ex- the XStream exception that occurred
- marshalling- indicates whether the exception occurs during marshalling (- true), or unmarshalling (- false)
- Returns:
- the corresponding XmlMappingException
 
 
-