类 XStreamMarshaller
- java.lang.Object
- org.springframework.oxm.support.AbstractMarshaller
- org.springframework.oxm.xstream.XStreamMarshaller
- 所有已实现的接口:
Aware
,BeanClassLoaderAware
,InitializingBean
,Marshaller
,Unmarshaller
public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLoaderAware, InitializingBean
Implementation of theMarshaller
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 theXStreamMarshaller
to unmarshal external XML, set thesupportedClasses
andconverters
properties (possibly using aCatchAllConverter
) or override thecustomizeXStream(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.- 从以下版本开始:
- 3.0
- 作者:
- Peter Meijer, Arjen Poutsma, Juergen Hoeller
字段概要
字段 修饰符和类型 字段 说明 static String
DEFAULT_ENCODING
The default encoding used for stream access: UTF-8.从类继承的字段 org.springframework.oxm.support.AbstractMarshaller
logger
构造器概要
构造器 构造器 说明 XStreamMarshaller()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected com.thoughtworks.xstream.XStream
buildXStream()
Build the native XStream delegate to be used by this marshaller, delegating toconstructXStream()
,configureXStream(com.thoughtworks.xstream.XStream)
andcustomizeXStream(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 theorg.springframework.oxm
hierarchy.protected void
customizeXStream(com.thoughtworks.xstream.XStream xstream)
Template to allow for customizing the givenXStream
.protected String
getDefaultEncoding()
Determine the default encoding to use for marshalling or unmarshalling from a byte stream, ornull
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 DOMNode
.void
marshalOutputStream(Object graph, OutputStream outputStream)
Abstract template method for marshalling the given object graph to aOutputStream
.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 SAXContentHandler
.void
marshalWriter(Object graph, Writer writer)
Abstract template method for marshalling the given object graph to aWriter
.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 StAXXMLEventWriter
.protected void
marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter)
Abstract template method for marshalling the given object to a StAXXMLStreamWriter
.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 beanclass loader
to a bean instance.void
setConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup converterLookup)
Set a custom XStreamConverterLookup
to use.void
setConverterRegistry(com.thoughtworks.xstream.converters.ConverterRegistry converterRegistry)
Set a custom XStreamConverterRegistry
to use.void
setConverters(com.thoughtworks.xstream.converters.ConverterMatcher... converters)
Set theConverters
orSingleValueConverters
to be registered with theXStream
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 ofClass
instances mapped to comma separated collection field names.void
setMapper(com.thoughtworks.xstream.mapper.Mapper mapper)
Set a custom XStreamMapper
to use.void
setMapperWrappers(Class<? extends com.thoughtworks.xstream.mapper.MapperWrapper>... mapperWrappers)
Set one or more custom XStreamMapperWrapper
classes.void
setMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy)
Set a custom XStreamMarshallingStrategy
to use.void
setMode(int mode)
Set the XStream mode to use.void
setNameCoder(com.thoughtworks.xstream.io.naming.NameCoder nameCoder)
Set a custom XStreamNameCoder
to use.void
setOmittedFields(Map<Class<?>,String> omittedFields)
Specify omitted fields, as a Map consisting ofClass
instances mapped to comma separated field names.void
setReflectionProvider(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider)
Set a custom XStreamReflectionProvider
to use.void
setStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver)
Set a XStreamHierarchicalStreamDriver
to be used for readers and writers.void
setSupportedClasses(Class<?>... supportedClasses)
Set the classes supported by this marshaller.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 DOMNode
.Object
unmarshalInputStream(InputStream inputStream)
Abstract template method for unmarshalling from a givenInputStream
.Object
unmarshalInputStream(InputStream inputStream, com.thoughtworks.xstream.converters.DataHolder dataHolder)
Object
unmarshalReader(Reader reader)
Abstract template method for unmarshalling from a givenReader
.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 SAXXMLReader
andInputSource
.protected Object
unmarshalStreamSource(StreamSource streamSource)
Template method for handlingStreamSource
s.protected Object
unmarshalXmlEventReader(XMLEventReader eventReader)
Abstract template method for unmarshalling from a given StaxXMLEventReader
.protected Object
unmarshalXmlStreamReader(XMLStreamReader streamReader)
Abstract template method for unmarshalling from a given StaxXMLStreamReader
.从类继承的方法 org.springframework.oxm.support.AbstractMarshaller
buildDocument, createDocumentBuilder, createDocumentBuilderFactory, createXmlReader, isProcessExternalEntities, isSupportDtd, marshal, marshalDomResult, marshalSaxResult, marshalStaxResult, marshalStreamResult, setProcessExternalEntities, setSupportDtd, unmarshal, unmarshalDomSource, unmarshalSaxSource, unmarshalStaxSource
字段详细资料
DEFAULT_ENCODING
public static final String DEFAULT_ENCODING
The default encoding used for stream access: UTF-8.- 另请参阅:
- 常量字段值
构造器详细资料
XStreamMarshaller
public XStreamMarshaller()
方法详细资料
setReflectionProvider
public void setReflectionProvider(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider)
Set a custom XStreamReflectionProvider
to use.- 从以下版本开始:
- 4.0
setStreamDriver
public void setStreamDriver(com.thoughtworks.xstream.io.HierarchicalStreamDriver streamDriver)
Set a XStreamHierarchicalStreamDriver
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.
setMapper
public void setMapper(com.thoughtworks.xstream.mapper.Mapper mapper)
Set a custom XStreamMapper
to use.- 从以下版本开始:
- 4.0
setMapperWrappers
public void setMapperWrappers(Class<? extends com.thoughtworks.xstream.mapper.MapperWrapper>... mapperWrappers)
Set one or more custom XStreamMapperWrapper
classes. Each of those classes needs to have a constructor with a single argument of typeMapper
orMapperWrapper
.- 从以下版本开始:
- 4.0
setConverterLookup
public void setConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup converterLookup)
Set a custom XStreamConverterLookup
to use. Also used asConverterRegistry
if the given reference implements it as well.- 从以下版本开始:
- 4.0
- 另请参阅:
DefaultConverterLookup
setConverterRegistry
public void setConverterRegistry(com.thoughtworks.xstream.converters.ConverterRegistry converterRegistry)
Set a custom XStreamConverterRegistry
to use.- 从以下版本开始:
- 4.0
- 另请参阅:
setConverterLookup(com.thoughtworks.xstream.converters.ConverterLookup)
,DefaultConverterLookup
setConverters
public void setConverters(com.thoughtworks.xstream.converters.ConverterMatcher... converters)
Set theConverters
orSingleValueConverters
to be registered with theXStream
instance.- 另请参阅:
Converter
,SingleValueConverter
setMarshallingStrategy
public void setMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy)
Set a custom XStreamMarshallingStrategy
to use.- 从以下版本开始:
- 4.0
setMode
public void setMode(int mode)
Set the XStream mode to use.- 另请参阅:
XStream.ID_REFERENCES
,XStream.NO_REFERENCES
setAliases
public void setAliases(Map<String,?> aliases)
Set the alias/type map, consisting of string aliases mapped to classes.Keys are aliases; values are either
Class
instances, or String class names.- 另请参阅:
XStream.alias(String, Class)
setAliasesByType
public void setAliasesByType(Map<String,?> aliasesByType)
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.- 另请参阅:
XStream.aliasType(String, Class)
setFieldAliases
public void setFieldAliases(Map<String,String> fieldAliases)
Set the field alias/type map, consisting of field names.- 另请参阅:
XStream.aliasField(String, Class, String)
setUseAttributeForTypes
public void setUseAttributeForTypes(Class<?>... useAttributeForTypes)
Set types to use XML attributes for.- 另请参阅:
XStream.useAttributeFor(Class)
setUseAttributeFor
public void setUseAttributeFor(Map<?,?> useAttributeFor)
Set 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
public void setImplicitCollections(Map<Class<?>,String> implicitCollections)
Specify implicit collection fields, as a Map consisting ofClass
instances mapped to comma separated collection field names.- 另请参阅:
XStream.addImplicitCollection(Class, String)
setOmittedFields
public void setOmittedFields(Map<Class<?>,String> omittedFields)
Specify omitted fields, as a Map consisting ofClass
instances mapped to comma separated field names.- 另请参阅:
XStream.omitField(Class, String)
setAnnotatedClasses
public void setAnnotatedClasses(Class<?>... annotatedClasses)
Set annotated classes for which aliases will be read from class-level annotation metadata.- 另请参阅:
XStream.processAnnotations(Class[])
setAutodetectAnnotations
public 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.
- 另请参阅:
XStream.autodetectAnnotations(boolean)
setEncoding
public void setEncoding(String encoding)
Set the encoding to be used for stream access.- 另请参阅:
DEFAULT_ENCODING
getDefaultEncoding
protected String getDefaultEncoding()
从类复制的说明:AbstractMarshaller
Determine the default encoding to use for marshalling or unmarshalling from a byte stream, ornull
if none.The default implementation returns
null
.
setNameCoder
public void setNameCoder(com.thoughtworks.xstream.io.naming.NameCoder nameCoder)
Set a custom XStreamNameCoder
to use. The default is anXmlFriendlyNameCoder
.- 从以下版本开始:
- 4.0.4
setSupportedClasses
public void setSupportedClasses(Class<?>... supportedClasses)
Set the classes supported by this marshaller.If this property is empty (the default), all classes are supported.
- 另请参阅:
supports(Class)
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
从接口复制的说明:BeanClassLoaderAware
Callback that supplies the beanclass 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
在接口中BeanClassLoaderAware
- 参数:
classLoader
- the owning class loader; may benull
in which case a defaultClassLoader
must be used, for example theClassLoader
obtained viaClassUtils.getDefaultClassLoader()
afterPropertiesSet
public void afterPropertiesSet()
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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
在接口中InitializingBean
buildXStream
protected com.thoughtworks.xstream.XStream buildXStream()
Build the native XStream delegate to be used by this marshaller, delegating toconstructXStream()
,configureXStream(com.thoughtworks.xstream.XStream)
andcustomizeXStream(com.thoughtworks.xstream.XStream)
.
constructXStream
protected com.thoughtworks.xstream.XStream constructXStream()
Construct an XStream instance, either using one of the standard constructors or creating a custom subclass.- 返回:
- the
XStream
instance
configureXStream
protected void configureXStream(com.thoughtworks.xstream.XStream xstream)
Configure the XStream instance with this marshaller's bean properties.- 参数:
xstream
- theXStream
instance
customizeXStream
protected void customizeXStream(com.thoughtworks.xstream.XStream xstream)
Template to allow for customizing the givenXStream
.The default implementation is empty.
- 参数:
xstream
- theXStream
instance
getXStream
public final com.thoughtworks.xstream.XStream getXStream()
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.
supports
public boolean supports(Class<?> clazz)
从接口复制的说明:Marshaller
Indicate whether this marshaller can marshal instances of the supplied type.- 指定者:
supports
在接口中Marshaller
- 指定者:
supports
在接口中Unmarshaller
- 参数:
clazz
- the class that this marshaller is being asked if it can marshal- 返回:
true
if this marshaller can indeed marshal instances of the supplied class;false
otherwise
marshalDomNode
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException
从类复制的说明:AbstractMarshaller
Abstract template method for marshalling the given object graph to a DOMNode
.In practice, node is be a
Document
node, aDocumentFragment
node, or aElement
node. In other words, a node that accepts children.- 指定者:
marshalDomNode
在类中AbstractMarshaller
- 参数:
graph
- the root of the object graph to marshalnode
- the DOM node that will contain the result tree- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the DOM node- 另请参阅:
Document
,DocumentFragment
,Element
marshalXmlEventWriter
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException
从类复制的说明:AbstractMarshaller
Abstract template method for marshalling the given object to a StAXXMLEventWriter
.- 指定者:
marshalXmlEventWriter
在类中AbstractMarshaller
- 参数:
graph
- the root of the object graph to marshaleventWriter
- theXMLEventWriter
to write to- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the DOM node
marshalXmlStreamWriter
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException
从类复制的说明:AbstractMarshaller
Abstract template method for marshalling the given object to a StAXXMLStreamWriter
.- 指定者:
marshalXmlStreamWriter
在类中AbstractMarshaller
- 参数:
graph
- the root of the object graph to marshalstreamWriter
- theXMLStreamWriter
to write to- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the DOM node
marshalSaxHandlers
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException
从类复制的说明:AbstractMarshaller
Abstract template method for marshalling the given object graph to a SAXContentHandler
.- 指定者:
marshalSaxHandlers
在类中AbstractMarshaller
- 参数:
graph
- the root of the object graph to marshalcontentHandler
- the SAXContentHandler
lexicalHandler
- the SAX2LexicalHandler
. Can benull
.- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the handlers
marshalOutputStream
public void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException
从类复制的说明:AbstractMarshaller
Abstract template method for marshalling the given object graph to aOutputStream
.- 指定者:
marshalOutputStream
在类中AbstractMarshaller
- 参数:
graph
- the root of the object graph to marshaloutputStream
- theOutputStream
to write to- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the writerIOException
- if an I/O exception occurs
marshalOutputStream
public void marshalOutputStream(Object graph, OutputStream outputStream, com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
marshalWriter
public void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException
从类复制的说明:AbstractMarshaller
Abstract template method for marshalling the given object graph to aWriter
.- 指定者:
marshalWriter
在类中AbstractMarshaller
- 参数:
graph
- the root of the object graph to marshalwriter
- theWriter
to write to- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the writerIOException
- if an I/O exception occurs
marshalWriter
public void marshalWriter(Object graph, Writer writer, com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
unmarshalStreamSource
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException
从类复制的说明:AbstractMarshaller
Template method for handlingStreamSource
s.This implementation delegates to
unmarshalInputStream
orunmarshalReader
.- 覆盖:
unmarshalStreamSource
在类中AbstractMarshaller
- 参数:
streamSource
- theStreamSource
- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given source cannot be mapped to an objectIOException
- if an I/O exception occurs
unmarshalDomNode
protected Object unmarshalDomNode(Node node) throws XmlMappingException
从类复制的说明:AbstractMarshaller
Abstract template method for unmarshalling from a given DOMNode
.- 指定者:
unmarshalDomNode
在类中AbstractMarshaller
- 参数:
node
- the DOM node that contains the objects to be unmarshalled- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given DOM node cannot be mapped to an object
unmarshalXmlEventReader
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException
从类复制的说明:AbstractMarshaller
Abstract template method for unmarshalling from a given StaxXMLEventReader
.- 指定者:
unmarshalXmlEventReader
在类中AbstractMarshaller
- 参数:
eventReader
- theXMLEventReader
to read from- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given event reader cannot be converted to an object
unmarshalXmlStreamReader
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException
从类复制的说明:AbstractMarshaller
Abstract template method for unmarshalling from a given StaxXMLStreamReader
.- 指定者:
unmarshalXmlStreamReader
在类中AbstractMarshaller
- 参数:
streamReader
- theXMLStreamReader
to read from- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given stream reader cannot be converted to an object
unmarshalSaxReader
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException
从类复制的说明:AbstractMarshaller
Abstract template method for unmarshalling using a given SAXXMLReader
andInputSource
.- 指定者:
unmarshalSaxReader
在类中AbstractMarshaller
- 参数:
xmlReader
- the SAXXMLReader
to parse withinputSource
- the input source to parse from- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given reader and input source cannot be converted to an objectIOException
- if an I/O exception occurs
unmarshalInputStream
public Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException
从类复制的说明:AbstractMarshaller
Abstract template method for unmarshalling from a givenInputStream
.- 指定者:
unmarshalInputStream
在类中AbstractMarshaller
- 参数:
inputStream
- theInputStreamStream
to read from- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given stream cannot be converted to an objectIOException
- if an I/O exception occurs
unmarshalInputStream
public Object unmarshalInputStream(InputStream inputStream, com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
unmarshalReader
public Object unmarshalReader(Reader reader) throws XmlMappingException, IOException
从类复制的说明:AbstractMarshaller
Abstract template method for unmarshalling from a givenReader
.- 指定者:
unmarshalReader
在类中AbstractMarshaller
- 参数:
reader
- theReader
to read from- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given reader cannot be converted to an objectIOException
- if an I/O exception occurs
unmarshalReader
public Object unmarshalReader(Reader reader, com.thoughtworks.xstream.converters.DataHolder dataHolder) throws XmlMappingException, IOException
convertXStreamException
protected XmlMappingException convertXStreamException(Exception ex, boolean marshalling)
Convert the given XStream exception to an appropriate exception from theorg.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 occurredmarshalling
- indicates whether the exception occurs during marshalling (true
), or unmarshalling (false
)- 返回:
- the corresponding
XmlMappingException