|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.oxm.support.AbstractMarshaller org.springframework.oxm.xstream.XStreamMarshaller
public class XStreamMarshaller
Implementation of the Marshaller
interface for XStream.
By default, XStream does not require any further configuration, though class aliases can be used to have more control over the behavior of XStream.
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.
setAliases(java.util.Map)
,
setConverters(ConverterMatcher[])
,
setEncoding(java.lang.String)
Field Summary | |
---|---|
private java.lang.ClassLoader |
classLoader
|
static java.lang.String |
DEFAULT_ENCODING
The default encoding used for stream access: UTF-8. |
private java.lang.String |
encoding
|
private HierarchicalStreamDriver |
streamDriver
|
private java.lang.Class[] |
supportedClasses
|
private XStream |
xstream
|
Fields inherited from class org.springframework.oxm.support.AbstractMarshaller |
---|
logger |
Constructor Summary | |
---|---|
XStreamMarshaller()
|
Method Summary | |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware). |
protected XmlMappingException |
convertXStreamException(java.lang.Exception ex,
boolean marshalling)
Convert the given XStream exception to an appropriate exception from the org.springframework.oxm hierarchy. |
protected void |
customizeXStream(XStream xstream)
Template to allow for customizing of the given XStream . |
XStream |
getXStream()
Returns the XStream instance used by this marshaller. |
private void |
marshal(java.lang.Object graph,
HierarchicalStreamWriter streamWriter)
Marshals the given graph to the given XStream HierarchicalStreamWriter. |
protected void |
marshalDomNode(java.lang.Object graph,
org.w3c.dom.Node node)
Abstract template method for marshalling the given object graph to a DOM Node . |
protected void |
marshalOutputStream(java.lang.Object graph,
java.io.OutputStream outputStream)
Abstract template method for marshalling the given object graph to a OutputStream . |
protected void |
marshalSaxHandlers(java.lang.Object graph,
org.xml.sax.ContentHandler contentHandler,
org.xml.sax.ext.LexicalHandler lexicalHandler)
Abstract template method for marshalling the given object graph to a SAX ContentHandler . |
protected void |
marshalWriter(java.lang.Object graph,
java.io.Writer writer)
Abstract template method for marshalling the given object graph to a Writer . |
protected void |
marshalXmlEventWriter(java.lang.Object graph,
javax.xml.stream.XMLEventWriter eventWriter)
Abstract template method for marshalling the given object to a StAX XMLEventWriter . |
protected void |
marshalXmlStreamWriter(java.lang.Object graph,
javax.xml.stream.XMLStreamWriter streamWriter)
Abstract template method for marshalling the given object to a StAX XMLStreamWriter . |
void |
setAliases(java.util.Map<java.lang.String,?> aliases)
Sets an alias/type map, consisting of string aliases mapped to classes. |
void |
setAliasesByType(java.util.Map<java.lang.String,?> aliases)
Sets the aliases by type map, consisting of string aliases mapped to classes. |
void |
setAnnotatedClass(java.lang.Class<?> annotatedClass)
Set the classes for which mappings will be read from class-level JDK 1.5+ annotation metadata. |
void |
setAnnotatedClasses(java.lang.Class<?>[] annotatedClasses)
Set annotated classes for which aliases will be read from class-level JDK 1.5+ annotation metadata. |
void |
setAutodetectAnnotations(boolean autodetectAnnotations)
Set the auto-detection mode of XStream. |
void |
setBeanClassLoader(java.lang.ClassLoader classLoader)
Callback that supplies the bean class loader to
a bean instance. |
void |
setConverters(ConverterMatcher[] converters)
Set the Converters or SingleValueConverters to be registered
with the XStream instance. |
void |
setEncoding(java.lang.String encoding)
Set the encoding to be used for stream access. |
void |
setFieldAliases(java.util.Map<java.lang.String,java.lang.String> aliases)
Sets a field alias/type map, consiting of field names |
void |
setImplicitCollections(java.util.Map<java.lang.Class<?>,java.lang.String> implicitCollections)
Specify implicit collection fields, as a Map consisting of Class instances
mapped to comma separated collection field names. |
void |
setMode(int mode)
Set the XStream mode. |
void |
setOmittedFields(java.util.Map<java.lang.Class<?>,java.lang.String> omittedFields)
Specify omitted fields, as a Map consisting of Class instances
mapped to comma separated field names. |
void |
setStreamDriver(HierarchicalStreamDriver streamDriver)
Set the XStream hierarchical stream driver to be used with stream readers and writers. |
void |
setSupportedClasses(java.lang.Class[] supportedClasses)
Set the classes supported by this marshaller. |
void |
setUseAttributeFor(java.util.Map<?,?> attributes)
Set the types to use XML attributes for. |
void |
setUseAttributeForTypes(java.lang.Class[] types)
Set types to use XML attributes for. |
boolean |
supports(java.lang.Class clazz)
Indicates whether this marshaller can marshal instances of the supplied type. |
private java.util.Map<java.lang.String,java.lang.Class<?>> |
toClassMap(java.util.Map<java.lang.String,?> map)
|
private java.lang.Object |
unmarshal(HierarchicalStreamReader streamReader)
Unmarshals the given provided javax.xml.transform.Source into an object graph. |
protected java.lang.Object |
unmarshalDomNode(org.w3c.dom.Node node)
Abstract template method for unmarshalling from a given DOM Node . |
protected java.lang.Object |
unmarshalInputStream(java.io.InputStream inputStream)
Abstract template method for unmarshalling from a given InputStream . |
protected java.lang.Object |
unmarshalReader(java.io.Reader reader)
Abstract template method for unmarshalling from a given Reader . |
protected java.lang.Object |
unmarshalSaxReader(org.xml.sax.XMLReader xmlReader,
org.xml.sax.InputSource inputSource)
Abstract template method for unmarshalling using a given SAX XMLReader
and InputSource . |
protected java.lang.Object |
unmarshalXmlEventReader(javax.xml.stream.XMLEventReader eventReader)
Abstract template method for unmarshalling from a given Stax XMLEventReader . |
protected java.lang.Object |
unmarshalXmlStreamReader(javax.xml.stream.XMLStreamReader streamReader)
Abstract template method for unmarshalling from a given Stax XMLStreamReader . |
Methods inherited from class org.springframework.oxm.support.AbstractMarshaller |
---|
createDocumentBuilder, createDocumentBuilderFactory, createXmlReader, marshalDomResult, marshalSaxResult, marshalStaxResult, marshalStreamResult, unmarshalDomSource, unmarshalSaxSource, unmarshalStaxSource, unmarshalStreamSource |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String DEFAULT_ENCODING
private final XStream xstream
private HierarchicalStreamDriver streamDriver
private java.lang.String encoding
private java.lang.Class[] supportedClasses
private java.lang.ClassLoader classLoader
Constructor Detail |
---|
public XStreamMarshaller()
Method Detail |
---|
public XStream getXStream()
public void setMode(int mode)
XStream#XPATH_REFERENCES
,
XStream#ID_REFERENCES
,
XStream#NO_REFERENCES
public void setConverters(ConverterMatcher[] converters)
Converters
or SingleValueConverters
to be registered
with the XStream
instance.
Converter
,
SingleValueConverter
public void setAliases(java.util.Map<java.lang.String,?> aliases) throws java.lang.ClassNotFoundException
Class
instances, or String class names.
java.lang.ClassNotFoundException
XStream#alias(String, Class)
public void setAliasesByType(java.util.Map<java.lang.String,?> aliases) throws java.lang.ClassNotFoundException
Class
instances, or String class names.
java.lang.ClassNotFoundException
XStream#aliasType(String, Class)
private java.util.Map<java.lang.String,java.lang.Class<?>> toClassMap(java.util.Map<java.lang.String,?> map) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public void setFieldAliases(java.util.Map<java.lang.String,java.lang.String> aliases) throws java.lang.ClassNotFoundException, java.lang.NoSuchFieldException
aliases
-
java.lang.ClassNotFoundException
java.lang.NoSuchFieldException
XStream#aliasField(String, Class, String)
public void setUseAttributeForTypes(java.lang.Class[] types)
XStream#useAttributeFor(Class)
public void setUseAttributeFor(java.util.Map<?,?> attributes)
<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(java.util.Map<java.lang.Class<?>,java.lang.String> implicitCollections)
Class
instances
mapped to comma separated collection field names.
XStream#addImplicitCollection(Class, String)
public void setOmittedFields(java.util.Map<java.lang.Class<?>,java.lang.String> omittedFields)
Class
instances
mapped to comma separated field names.
XStream#omitField(Class, String)
public void setAnnotatedClass(java.lang.Class<?> annotatedClass)
XStream#processAnnotations(Class)
public void setAnnotatedClasses(java.lang.Class<?>[] annotatedClasses)
XStream#processAnnotations(Class[])
public void setAutodetectAnnotations(boolean autodetectAnnotations)
Note that auto-detection implies that the XStream is configured while it is processing the XML steams, and thus introduces a potential concurrency problem.
XStream#autodetectAnnotations(boolean)
public void setStreamDriver(HierarchicalStreamDriver streamDriver)
public void setEncoding(java.lang.String encoding)
DEFAULT_ENCODING
public void setSupportedClasses(java.lang.Class[] supportedClasses)
If this property is empty (the default), all classes are supported.
supports(Class)
public final void afterPropertiesSet() throws java.lang.Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
java.lang.Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.public void setBeanClassLoader(java.lang.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 loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
protected void customizeXStream(XStream xstream)
XStream
.
Default implementation is empty.
xstream
- the XStream
instancepublic boolean supports(java.lang.Class clazz)
Marshaller
supports
in interface Marshaller
supports
in interface 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
otherwiseprotected void marshalDomNode(java.lang.Object graph, org.w3c.dom.Node node) throws XmlMappingException
AbstractMarshaller
Node
.
In practice, node is be 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 tree
XmlMappingException
- if the given object cannot be marshalled to the DOM nodeDocument
,
DocumentFragment
,
Element
protected void marshalXmlEventWriter(java.lang.Object graph, javax.xml.stream.XMLEventWriter eventWriter) throws XmlMappingException
AbstractMarshaller
XMLEventWriter
.
marshalXmlEventWriter
in class AbstractMarshaller
graph
- the root of the object graph to marshaleventWriter
- the XMLEventWriter
to write to
XmlMappingException
- if the given object cannot be marshalled to the DOM nodeprotected void marshalXmlStreamWriter(java.lang.Object graph, javax.xml.stream.XMLStreamWriter streamWriter) throws XmlMappingException
AbstractMarshaller
XMLStreamWriter
.
marshalXmlStreamWriter
in class AbstractMarshaller
graph
- the root of the object graph to marshalstreamWriter
- the XMLStreamWriter
to write to
XmlMappingException
- if the given object cannot be marshalled to the DOM nodeprotected void marshalOutputStream(java.lang.Object graph, java.io.OutputStream outputStream) throws XmlMappingException, java.io.IOException
AbstractMarshaller
OutputStream
.
marshalOutputStream
in class AbstractMarshaller
graph
- the root of the object graph to marshaloutputStream
- the OutputStream
to write to
XmlMappingException
- if the given object cannot be marshalled to the writer
java.io.IOException
- if an I/O exception occursprotected void marshalSaxHandlers(java.lang.Object graph, org.xml.sax.ContentHandler contentHandler, org.xml.sax.ext.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 handlersprotected void marshalWriter(java.lang.Object graph, java.io.Writer writer) throws XmlMappingException, java.io.IOException
AbstractMarshaller
Writer
.
marshalWriter
in class AbstractMarshaller
graph
- the root of the object graph to marshalwriter
- the Writer
to write to
XmlMappingException
- if the given object cannot be marshalled to the writer
java.io.IOException
- if an I/O exception occursprivate void marshal(java.lang.Object graph, HierarchicalStreamWriter streamWriter)
convertXStreamException(java.lang.Exception, boolean)
.
marshal
in interface Marshaller
marshal
in class AbstractMarshaller
graph
- the root of the object graph to marshalstreamWriter
- the result to marshal toAbstractMarshaller.marshalDomResult(Object, javax.xml.transform.dom.DOMResult)
,
AbstractMarshaller.marshalSaxResult(Object, javax.xml.transform.sax.SAXResult)
,
AbstractMarshaller.marshalStreamResult(Object, javax.xml.transform.stream.StreamResult)
protected java.lang.Object unmarshalDomNode(org.w3c.dom.Node node) throws XmlMappingException
AbstractMarshaller
Node
.
unmarshalDomNode
in class AbstractMarshaller
node
- the DOM node that contains the objects to be unmarshalled
XmlMappingException
- if the given DOM node cannot be mapped to an objectprotected java.lang.Object unmarshalXmlEventReader(javax.xml.stream.XMLEventReader eventReader) throws XmlMappingException
AbstractMarshaller
XMLEventReader
.
unmarshalXmlEventReader
in class AbstractMarshaller
eventReader
- the XMLEventReader
to read from
XmlMappingException
- if the given event reader cannot be converted to an objectprotected java.lang.Object unmarshalXmlStreamReader(javax.xml.stream.XMLStreamReader streamReader) throws XmlMappingException
AbstractMarshaller
XMLStreamReader
.
unmarshalXmlStreamReader
in class AbstractMarshaller
streamReader
- the XMLStreamReader
to read from
XmlMappingException
- if the given stream reader cannot be converted to an objectprotected java.lang.Object unmarshalInputStream(java.io.InputStream inputStream) throws XmlMappingException, java.io.IOException
AbstractMarshaller
InputStream
.
unmarshalInputStream
in class AbstractMarshaller
inputStream
- the InputStreamStream
to read from
XmlMappingException
- if the given stream cannot be converted to an object
java.io.IOException
- if an I/O exception occursprotected java.lang.Object unmarshalReader(java.io.Reader reader) throws XmlMappingException, java.io.IOException
AbstractMarshaller
Reader
.
unmarshalReader
in class AbstractMarshaller
reader
- the Reader
to read from
XmlMappingException
- if the given reader cannot be converted to an object
java.io.IOException
- if an I/O exception occursprotected java.lang.Object unmarshalSaxReader(org.xml.sax.XMLReader xmlReader, org.xml.sax.InputSource inputSource) throws XmlMappingException, java.io.IOException
AbstractMarshaller
XMLReader
and InputSource
.
unmarshalSaxReader
in class AbstractMarshaller
xmlReader
- the SAX XMLReader
to parse withinputSource
- the input source to parse from
XmlMappingException
- if the given reader and input source cannot be converted to an object
java.io.IOException
- if an I/O exception occursprivate java.lang.Object unmarshal(HierarchicalStreamReader streamReader)
AbstractMarshaller
javax.xml.transform.Source
into an object graph.
This implementation inspects the given result, and calls unmarshalDomSource
,
unmarshalSaxSource
, or unmarshalStreamSource
.
unmarshal
in interface Unmarshaller
unmarshal
in class AbstractMarshaller
streamReader
- the source to marshal from
AbstractMarshaller.unmarshalDomSource(javax.xml.transform.dom.DOMSource)
,
AbstractMarshaller.unmarshalSaxSource(javax.xml.transform.sax.SAXSource)
,
AbstractMarshaller.unmarshalStreamSource(javax.xml.transform.stream.StreamSource)
protected XmlMappingException convertXStreamException(java.lang.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
- XStream exception that occuredmarshalling
- indicates whether the exception occurs during marshalling (true
),
or unmarshalling (false
)
XmlMappingException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |