public class CastorMarshaller extends AbstractMarshaller implements InitializingBean, BeanClassLoaderAware
Marshaller
interface for Castor. By default, Castor does
not require any further configuration, though setting target classes, target packages or
providing a mapping file can be used to have more control over the behavior of Castor.
If a target class is specified using setTargetClass
, the CastorMarshaller
can only be used to unmarshal XML that represents that specific class. If you want to unmarshal
multiple classes, you have to provide a mapping file using setMappingLocations
.
Due to limitations of Castor's API, it is required to set the encoding used for
writing to output streams. It defaults to UTF-8
.
setEncoding(String)
,
setTargetClass(Class)
,
setTargetPackages(String[])
,
setMappingLocation(Resource)
,
setMappingLocations(Resource[])
Modifier and Type | Field and Description |
---|---|
private java.lang.ClassLoader |
beanClassLoader |
private java.util.Map<java.lang.String,java.lang.String> |
castorProperties |
private XMLClassDescriptorResolver |
classDescriptorResolver |
private boolean |
clearCollections |
static java.lang.String |
DEFAULT_ENCODING
The default encoding used for stream access: UTF-8.
|
private java.util.Map<java.lang.String,java.lang.String> |
doctypes |
private java.lang.String |
encoding |
private org.xml.sax.EntityResolver |
entityResolver |
private IDResolver |
idResolver |
private boolean |
ignoreExtraAttributes |
private boolean |
ignoreExtraElements |
private Resource[] |
mappingLocations |
private boolean |
marshalAsDocument |
private boolean |
marshalExtendedType |
private java.util.Map<java.lang.String,java.lang.String> |
namespaceMappings |
private java.util.Map<java.lang.String,java.lang.String> |
namespaceToPackageMapping |
private java.lang.String |
noNamespaceSchemaLocation |
private ObjectFactory |
objectFactory |
private java.util.Map<java.lang.String,java.lang.String> |
processingInstructions |
private boolean |
reuseObjects |
private java.lang.String |
rootElement |
private java.lang.Object |
rootObject |
private java.lang.String |
schemaLocation |
private boolean |
suppressNamespaces |
private boolean |
suppressXsiType |
private java.lang.Class<?>[] |
targetClasses |
private java.lang.String[] |
targetPackages |
private boolean |
useXSITypeAtRoot |
private boolean |
validating |
private boolean |
whitespacePreserve |
private XMLContext |
xmlContext |
logger
Constructor and Description |
---|
CastorMarshaller() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected XmlMappingException |
convertCastorException(XMLException ex,
boolean marshalling)
Convert the given
XMLException to an appropriate exception from the
org.springframework.oxm hierarchy. |
private Unmarshaller |
createUnmarshaller() |
protected XMLContext |
createXMLContext(Resource[] mappingLocations,
java.lang.Class<?>[] targetClasses,
java.lang.String[] targetPackages)
Create the Castor
XMLContext . |
protected void |
customizeMarshaller(Marshaller marshaller)
Template method that allows for customizing of the given Castor
Marshaller . |
protected void |
customizeUnmarshaller(Unmarshaller unmarshaller)
Template method that allows for customizing of the given Castor
Unmarshaller . |
private void |
doMarshal(java.lang.Object graph,
Marshaller marshaller) |
protected java.lang.String |
getDefaultEncoding()
Determine the default encoding to use for marshalling or unmarshalling from
a byte stream, or
null if none. |
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 |
setBeanClassLoader(java.lang.ClassLoader classLoader)
Callback that supplies the bean
class loader to
a bean instance. |
void |
setCastorProperties(java.util.Map<java.lang.String,java.lang.String> castorProperties)
Set Castor-specific properties for marshalling and unmarshalling.
|
void |
setClassDescriptorResolver(XMLClassDescriptorResolver classDescriptorResolver)
Set the
XMLClassDescriptorResolver to be used during unmarshalling. |
void |
setClearCollections(boolean clearCollections)
Sets whether this unmarshaller should clear collections upon the first use.
|
void |
setDoctypes(java.util.Map<java.lang.String,java.lang.String> doctypes)
Set the map containing document type definition for the marshaller.
|
void |
setEncoding(java.lang.String encoding)
Set the encoding to be used for stream access.
|
void |
setEntityResolver(org.xml.sax.EntityResolver entityResolver)
Set the
EntityResolver to be used during unmarshalling. |
void |
setIdResolver(IDResolver idResolver)
Set the Castor
IDResolver to be used during unmarshalling. |
void |
setIgnoreExtraAttributes(boolean ignoreExtraAttributes)
Set whether the Castor
Unmarshaller should ignore attributes that do not match a specific field. |
void |
setIgnoreExtraElements(boolean ignoreExtraElements)
Set whether the Castor
Unmarshaller should ignore elements that do not match a specific field. |
void |
setMappingLocation(Resource mappingLocation)
Set the locations of the Castor XML mapping files.
|
void |
setMappingLocations(Resource... mappingLocations)
Set the locations of the Castor XML mapping files.
|
void |
setMarshalAsDocument(boolean marshalAsDocument)
Set whether this marshaller should output the xml declaration.
|
void |
setMarshalExtendedType(boolean marshalExtendedType)
Set whether this marshaller should output for given type the
xsi:type attribute. |
void |
setNamespaceMappings(java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
Set the namespace mappings.
|
void |
setNamespaceToPackageMapping(java.util.Map<java.lang.String,java.lang.String> namespaceToPackageMapping)
Set the namespace to package mappings.
|
void |
setNoNamespaceSchemaLocation(java.lang.String noNamespaceSchemaLocation)
Set the value of
xsi:noNamespaceSchemaLocation attribute. |
void |
setObjectFactory(ObjectFactory objectFactory)
Set the Castor
ObjectFactory to be used during unmarshalling. |
void |
setProcessingInstructions(java.util.Map<java.lang.String,java.lang.String> processingInstructions)
Sets the processing instructions that will be used by during marshalling.
|
void |
setReuseObjects(boolean reuseObjects)
Set whether this unmarshaller should re-use objects.
|
void |
setRootElement(java.lang.String rootElement)
Set the name of the root element.
|
void |
setRootObject(java.lang.Object root)
Set the expected root object for the unmarshaller, into which the source will be unmarshalled.
|
void |
setSchemaLocation(java.lang.String schemaLocation)
Set the value of
xsi:schemaLocation attribute. |
void |
setSuppressNamespaces(boolean suppressNamespaces)
Sets whether this marshaller should output namespaces.
|
void |
setSuppressXsiType(boolean suppressXsiType)
Set whether this marshaller should output the
xsi:type attribute. |
void |
setTargetClass(java.lang.Class<?> targetClass)
Set the Castor target class.
|
void |
setTargetClasses(java.lang.Class<?>... targetClasses)
Set the Castor target classes.
|
void |
setTargetPackage(java.lang.String targetPackage)
Set the name of a package with the Castor descriptor classes.
|
void |
setTargetPackages(java.lang.String... targetPackages)
Set the names of packages with the Castor descriptor classes.
|
void |
setUseXSITypeAtRoot(boolean useXSITypeAtRoot)
Sets whether this marshaller should output the
xsi:type attribute for the root element. |
void |
setValidating(boolean validating)
Set whether this marshaller should validate in- and outgoing documents.
|
void |
setWhitespacePreserve(boolean whitespacePreserve)
Set whether the Castor
Unmarshaller should preserve "ignorable" whitespace. |
boolean |
supports(java.lang.Class<?> clazz)
Returns
true for all classes, i.e. |
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 . |
buildDocument, createDocumentBuilder, createDocumentBuilderFactory, createXmlReader, isProcessExternalEntities, isSupportDtd, marshal, marshalDomResult, marshalSaxResult, marshalStaxResult, marshalStreamResult, setProcessExternalEntities, setSupportDtd, unmarshal, unmarshalDomSource, unmarshalSaxSource, unmarshalStaxSource, unmarshalStreamSource
public static final java.lang.String DEFAULT_ENCODING
private java.lang.String encoding
@Nullable private java.lang.Class<?>[] targetClasses
@Nullable private java.lang.String[] targetPackages
private boolean validating
private boolean suppressNamespaces
private boolean suppressXsiType
private boolean marshalAsDocument
private boolean marshalExtendedType
@Nullable private java.lang.String rootElement
@Nullable private java.lang.String noNamespaceSchemaLocation
@Nullable private java.lang.String schemaLocation
private boolean useXSITypeAtRoot
private boolean whitespacePreserve
private boolean ignoreExtraAttributes
private boolean ignoreExtraElements
@Nullable private java.lang.Object rootObject
private boolean reuseObjects
private boolean clearCollections
@Nullable private java.util.Map<java.lang.String,java.lang.String> castorProperties
@Nullable private java.util.Map<java.lang.String,java.lang.String> doctypes
@Nullable private java.util.Map<java.lang.String,java.lang.String> processingInstructions
@Nullable private java.util.Map<java.lang.String,java.lang.String> namespaceMappings
@Nullable private java.util.Map<java.lang.String,java.lang.String> namespaceToPackageMapping
@Nullable private org.xml.sax.EntityResolver entityResolver
@Nullable private XMLClassDescriptorResolver classDescriptorResolver
@Nullable private IDResolver idResolver
@Nullable private ObjectFactory objectFactory
@Nullable private java.lang.ClassLoader beanClassLoader
@Nullable private XMLContext xmlContext
public void setEncoding(java.lang.String encoding)
DEFAULT_ENCODING
protected java.lang.String getDefaultEncoding()
AbstractMarshaller
null
if none.
The default implementation returns null
.
getDefaultEncoding
in class AbstractMarshaller
public void setMappingLocation(Resource mappingLocation)
public void setMappingLocations(Resource... mappingLocations)
public void setTargetClass(java.lang.Class<?> targetClass)
public void setTargetClasses(java.lang.Class<?>... targetClasses)
public void setTargetPackage(java.lang.String targetPackage)
public void setTargetPackages(java.lang.String... targetPackages)
public void setValidating(boolean validating)
Default is false
.
Marshaller#setValidation(boolean)
public void setSuppressNamespaces(boolean suppressNamespaces)
The default is false
, i.e. namespaces are written.
org.exolab.castor.xml.Marshaller#setSuppressNamespaces(boolean)
public void setSuppressXsiType(boolean suppressXsiType)
xsi:type
attribute.
The default is false
, i.e. the xsi:type
is written.
org.exolab.castor.xml.Marshaller#setSuppressXSIType(boolean)
public void setMarshalAsDocument(boolean marshalAsDocument)
The default is true
, the XML declaration will be written.
org.exolab.castor.xml.Marshaller#setMarshalAsDocument(boolean)
public void setMarshalExtendedType(boolean marshalExtendedType)
xsi:type
attribute.
The default is true
, the xsi:type
attribute will be written.
org.exolab.castor.xml.Marshaller#setMarshalExtendedType(boolean)
public void setRootElement(java.lang.String rootElement)
org.exolab.castor.xml.Marshaller#setRootElement(String)
public void setNoNamespaceSchemaLocation(java.lang.String noNamespaceSchemaLocation)
xsi:noNamespaceSchemaLocation
attribute. When set, the
xsi:noNamespaceSchemaLocation
attribute will be written for the root element.org.exolab.castor.xml.Marshaller#setNoNamespaceSchemaLocation(String)
public void setSchemaLocation(java.lang.String schemaLocation)
xsi:schemaLocation
attribute. When set, the
xsi:schemaLocation
attribute will be written for the root element.org.exolab.castor.xml.Marshaller#setSchemaLocation(String)
public void setUseXSITypeAtRoot(boolean useXSITypeAtRoot)
xsi:type
attribute for the root element.
This can be useful when the type of the element can not be simply determined from the element name.
The default is false
: The xsi:type
attribute for the root element won't be written.
org.exolab.castor.xml.Marshaller#setUseXSITypeAtRoot(boolean)
public void setWhitespacePreserve(boolean whitespacePreserve)
Unmarshaller
should preserve "ignorable" whitespace.
Default is false
.
org.exolab.castor.xml.Unmarshaller#setWhitespacePreserve(boolean)
public void setIgnoreExtraAttributes(boolean ignoreExtraAttributes)
Unmarshaller
should ignore attributes that do not match a specific field.
Default is true
: Extra attributes are ignored.
org.exolab.castor.xml.Unmarshaller#setIgnoreExtraAttributes(boolean)
public void setIgnoreExtraElements(boolean ignoreExtraElements)
Unmarshaller
should ignore elements that do not match a specific field.
Default is false
: Extra elements are flagged as an error.
org.exolab.castor.xml.Unmarshaller#setIgnoreExtraElements(boolean)
public void setRootObject(java.lang.Object root)
org.exolab.castor.xml.Unmarshaller#setObject(Object)
public void setReuseObjects(boolean reuseObjects)
The default is false
, which means that the objects won't be re-used.
org.exolab.castor.xml.Unmarshaller#setReuseObjects(boolean)
public void setClearCollections(boolean clearCollections)
The default is false
which means that marshaller won't clear collections.
org.exolab.castor.xml.Unmarshaller#setClearCollections(boolean)
public void setCastorProperties(java.util.Map<java.lang.String,java.lang.String> castorProperties)
org.exolab.castor.xml.Marshaller#setProperty(String, String)
,
org.exolab.castor.xml.Unmarshaller#setProperty(String, String)
public void setDoctypes(java.util.Map<java.lang.String,java.lang.String> doctypes)
org.exolab.castor.xml.Marshaller#setDoctype(String, String)
public void setProcessingInstructions(java.util.Map<java.lang.String,java.lang.String> processingInstructions)
org.exolab.castor.xml.Marshaller#addProcessingInstruction(String, String)
public void setNamespaceMappings(java.util.Map<java.lang.String,java.lang.String> namespaceMappings)
org.exolab.castor.xml.Marshaller#setNamespaceMapping(String, String)
public void setNamespaceToPackageMapping(java.util.Map<java.lang.String,java.lang.String> namespaceToPackageMapping)
org.exolab.castor.xml.Marshaller#setNamespaceMapping(String, String)
public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)
EntityResolver
to be used during unmarshalling.
This resolver will used to resolve system and public ids.org.exolab.castor.xml.Unmarshaller#setEntityResolver(EntityResolver)
public void setClassDescriptorResolver(XMLClassDescriptorResolver classDescriptorResolver)
XMLClassDescriptorResolver
to be used during unmarshalling.
This resolver will used to resolve class descriptors.org.exolab.castor.xml.Unmarshaller#setResolver(XMLClassDescriptorResolver)
public void setIdResolver(IDResolver idResolver)
IDResolver
to be used during unmarshalling.org.exolab.castor.xml.Unmarshaller#setIDResolver(IDResolver)
public void setObjectFactory(ObjectFactory objectFactory)
ObjectFactory
to be used during unmarshalling.org.exolab.castor.xml.Unmarshaller#setObjectFactory(ObjectFactory)
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 loaderpublic void afterPropertiesSet() throws CastorMappingException, java.io.IOException
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
CastorMappingException
java.io.IOException
protected XMLContext createXMLContext(@Nullable Resource[] mappingLocations, @Nullable java.lang.Class<?>[] targetClasses, @Nullable java.lang.String[] targetPackages) throws MappingException, ResolverException, java.io.IOException
XMLContext
. Subclasses can override this to create a custom context.
The default implementation loads mapping files if defined, or the target class or packages if defined.
MappingException
- when the mapping file cannot be loadedjava.io.IOException
- in case of I/O errorsResolverException
XMLContext#addMapping(org.exolab.castor.mapping.Mapping)
,
XMLContext#addClass(Class)
public boolean supports(java.lang.Class<?> clazz)
true
for all classes, i.e. Castor supports arbitrary classes.supports
in interface Marshaller
supports
in interface Unmarshaller
clazz
- the class that this marshaller is being asked if it can marshaltrue
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 treeXmlMappingException
- 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 toXmlMappingException
- 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 toXmlMappingException
- if the given object cannot be marshalled to the DOM nodeprotected void marshalSaxHandlers(java.lang.Object graph, org.xml.sax.ContentHandler contentHandler, @Nullable 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 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 toXmlMappingException
- if the given object cannot be marshalled to the writerjava.io.IOException
- if an I/O exception occursprotected 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 toXmlMappingException
- if the given object cannot be marshalled to the writerjava.io.IOException
- if an I/O exception occursprivate void doMarshal(java.lang.Object graph, Marshaller marshaller)
protected void customizeMarshaller(Marshaller marshaller)
Marshaller
.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 unmarshalledXmlMappingException
- if the given DOM node cannot be mapped to an objectprotected java.lang.Object unmarshalXmlEventReader(javax.xml.stream.XMLEventReader eventReader)
AbstractMarshaller
XMLEventReader
.unmarshalXmlEventReader
in class AbstractMarshaller
eventReader
- the XMLEventReader
to read fromprotected java.lang.Object unmarshalXmlStreamReader(javax.xml.stream.XMLStreamReader streamReader)
AbstractMarshaller
XMLStreamReader
.unmarshalXmlStreamReader
in class AbstractMarshaller
streamReader
- the XMLStreamReader
to read fromprotected 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 fromXmlMappingException
- if the given reader and input source cannot be converted to an objectjava.io.IOException
- if an I/O exception occursprotected java.lang.Object unmarshalInputStream(java.io.InputStream inputStream) throws XmlMappingException, java.io.IOException
AbstractMarshaller
InputStream
.unmarshalInputStream
in class AbstractMarshaller
inputStream
- the InputStreamStream
to read fromXmlMappingException
- if the given stream cannot be converted to an objectjava.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 fromXmlMappingException
- if the given reader cannot be converted to an objectjava.io.IOException
- if an I/O exception occursprivate Unmarshaller createUnmarshaller()
protected void customizeUnmarshaller(Unmarshaller unmarshaller)
Unmarshaller
.protected XmlMappingException convertCastorException(XMLException ex, boolean marshalling)
XMLException
to an appropriate exception from the
org.springframework.oxm
hierarchy.
A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since Castor itself does not make this distinction in its exception hierarchy.
ex
- Castor XMLException
that occurredmarshalling
- indicates whether the exception occurs during marshalling (true
),
or unmarshalling (false
)XmlMappingException