Class Marshaller
java.lang.Object
org.exolab.castor.xml.Marshaller
A Marshaller that serializes Java Object's to XML
Note: This class is not thread safe, and not intended to be,
so please create a new Marshaller for each thread if it
is to be used in a multithreaded environment.
- Version:
- $Revision: 9147 $ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
- Author:
- Keith Visco
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe xml:lang attribute, without the "xml:" prefix.static final StringThe xsi:nil attribute, without the "xsi:" prefix.static final StringThe xml:space attribute name, without the "xml:" prefix.static final StringThe value of 'true'.static final StringThe xsi:type attribute name, without the "xsi:" prefix.static final StringThe xml:lang attribute name.static final StringThe xml:space attribute name.static final StringThe XSI Namespace URI.static final StringThe xsi:nil attribute.static final StringThe name of the no namespace schema location attribute.static final StringThe name of the Schema location attribute. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a default instance of Marshaller, where the sink needs to be set separately.Marshaller(Writer out) Creates a newMarshallerwith the given writer.Marshaller(XMLEventWriter xmlEventWriter) Creates a newMarshallerwith the givenXMLEventWriter.Marshaller(XMLStreamWriter xmlStreamWriter) Creates a newMarshallerwith the givenXMLStreamWriter.Marshaller(InternalContext internalContext) The oneMarshallerconstructor that is used byXMLContextwhich sets anInternalContextthat comes from outside.Marshaller(Node node) Creates a newMarshallerfor the given DOMNode.Marshaller(ContentHandler contentHandler) Creates a newMarshallerwith the given SAXContentHandler.Marshaller(DocumentHandler handler) Creates a newMarshallerwith the given SAXDocumentHandler. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddProcessingInstruction(String target, String data) Adds the given processing instruction data to the set of processing instructions to output during marshalling.getCollectionHandler(Class clazz) Returns the CollectionHandler associated with the given collection, or null if no such handler exists.To get theAbstractInternalContextto use.To get theJavaNaminginstance to be used.booleanIf True the marshaller will use the 'xsi:type' attribute to marshall a field value that extended the defined field type.booleanDeprecated.getProperty(String name) Returns the value of the given Castor XML-specific property.Returns the ClassDescriptorResolver for use during marshallingReturns the name of the root element to usebooleanstatic booleanisCollection(Class clazz) Returns true if the given Class is a considered a collection by the marshalling framework.voidMarshals the given Object as XML using the DocumentHandler for this Marshaller.static voidMarshals the given Object as XML using the given writer.static voidMarshals the given Object as XML using the given DOM Node to send events to.static voidmarshal(Object object, ContentHandler handler) Marshals the given Object as XML using the given ContentHandler to send events to.static voidmarshal(Object object, DocumentHandler handler) Marshals the given Object as XML using the given DocumentHandler to send events to.static booleannamespaceEquals(String ns1, String ns2) Compares the given namespaces (as strings) for equality.protected org.exolab.castor.xml.MarshalFramework.InheritanceMatch[]searchInheritance(String name, String namespace, XMLClassDescriptor classDesc) Search there is a field descriptor which can accept one of the class descriptor which match the given name and namespace.voidsetContentHandler(ContentHandler contentHandler) To set the SAXContentHandlerwhich is used as destination at marshalling.voidsetDoctype(String publicId, String systemId) Sets the document type definition for the serializer.voidsetDocumentHandler(DocumentHandler handler) Sets the given SAXDocumentHandlerto 'marshal' into.voidsetEncoding(String encoding) Sets the encoding for the serializer.voidsetInternalContext(InternalContext internalContext) To set theInternalContextto use, and to initializeMarshallerproperties linked to it.voidsetLogWriter(PrintWriter printWriter) Sets the PrintWriter used for loggingvoidsetMapping(Mapping mapping) Sets the given mapping to be used by the marshalling Framework.voidsetMarshalAsDocument(boolean asDocument) Sets whether or not to marshal as a document which includes the XML declaration, and if necessary the DOCTYPE declaration.voidsetMarshalExtendedType(boolean marshalExtendedType) If True the marshaller will use the 'xsi:type' attribute to marshall a field value that extended the defined field type.voidsetMarshalListener(MarshalListener listener) Sets an optional MarshalListener to recieve pre and post marshal notification for each Object in the tree.voidsetNamespaceMapping(String nsPrefix, String nsURI) Sets the mapping for the given Namespace prefix.voidSets the W3CNodeinstance to marshal to.voidsetNoNamespaceSchemaLocation(String schemaLocation) Sets the value for the xsi:noNamespaceSchemaLocation attribute.voidsetNSPrefixAtRoot(boolean nsPrefixAtRoot) Deprecated.voidsetProperty(String name, String value) Sets a custom value of a given Castor XML-specific property.voidSets the ClassDescriptorResolver to use during marshalling.voidSets theResultinto which the output xml will be written.voidsetRootElement(String rootElement) Sets the name of the root element to use.voidsetSchemaLocation(String schemaLocation) Sets the value for the xsi:schemaLocation attribute.voidsetSuppressNamespaces(boolean suppressNamespaces) Sets whether or not namespaces are output.voidsetSuppressXSIType(boolean suppressXSIType) Sets whether or not the xsi:type attribute should appear on the marshalled document.voidsetSupressXMLDeclaration(boolean supressXMLDeclaration) Sets whether or not to marshal as a document which includes the XML declaration, and if necessary the DOCTYPE declaration.voidsetUseXSITypeAtRoot(boolean useXSITypeAtRoot) Sets whether or not to output the xsi:type at the root element.voidsetValidation(boolean validate) Sets whether or not to validate the object model before marshalling.voidSets the java.io.Writer to be used during marshalling.voidsetXmlEventWriter(XMLEventWriter xmlEventWriter) Sets theXMLEventWriterto use.voidsetXmlStreamWriter(XMLStreamWriter xmlStreamWriter) Sets theXMLStreamWriterto use.
-
Field Details
-
XSI_NAMESPACE
-
XSI_SCHEMA_LOCATION
-
XSI_NO_NAMESPACE_SCHEMA_LOCATION
The name of the no namespace schema location attribute.- See Also:
-
XML_LANG_ATTR
-
LANG_ATTR
-
NIL_ATTR
-
XSI_NIL_ATTR
-
XML_SPACE_ATTR
-
SPACE_ATTR
The xml:space attribute name, without the "xml:" prefix.- See Also:
-
TYPE_ATTR
The xsi:type attribute name, without the "xsi:" prefix.- See Also:
-
TRUE_VALUE
-
-
Constructor Details
-
Marshaller
Creates a newMarshallerwith the given SAXDocumentHandler.- Parameters:
handler- the SAXDocumentHandlerto "marshal" to.- Throws:
IllegalArgumentException- if the givenDocumentHandleris null- See Also:
-
Marshaller
Creates a newMarshallerwith the given SAXContentHandler.- Parameters:
contentHandler- theContentHandlerto "marshal" to.- Throws:
IllegalArgumentException- if the gievnContentHandleris null- See Also:
-
Marshaller
The oneMarshallerconstructor that is used byXMLContextwhich sets anInternalContextthat comes from outside. Writer orContentHandlerhave to be set in a second step.- Parameters:
internalContext- theInternalContextto initialize theMarshallerinstance with
-
Marshaller
public Marshaller()Creates a default instance of Marshaller, where the sink needs to be set separately. -
Marshaller
Creates a newMarshallerwith the given writer.- Parameters:
out- theWriterto serialise to.- Throws:
IllegalArgumentException- if the givenWriteris nullIOException- If the givenWriterinstance cannot be opened.- See Also:
-
Marshaller
Creates a newMarshallerwith the givenXMLStreamWriter.- Parameters:
xmlStreamWriter- theXMLStreamWriter- Throws:
IllegalArgumentException- if the givenXMLStreamWriteris null- Since:
- 1.3.3
- See Also:
-
Marshaller
Creates a newMarshallerwith the givenXMLEventWriter.- Parameters:
xmlEventWriter- theXMLEventWriter- Throws:
IllegalArgumentException- if the givenXMLEventWriteris null- Since:
- 1.3.3
- See Also:
-
Marshaller
Creates a newMarshallerfor the given DOMNode.- Parameters:
node- the DOMNodeto marshal into.- Throws:
IllegalArgumentException- if node is null- See Also:
-
-
Method Details
-
setDocumentHandler
Sets the given SAXDocumentHandlerto 'marshal' into.- Parameters:
handler- the SAXDocumentHandlerto "marshal" to.- Throws:
IllegalArgumentException- if the givenDocumentHandleris null
-
setWriter
Sets the java.io.Writer to be used during marshalling.- Parameters:
out- The writer to use for marshalling- Throws:
IllegalArgumentException- if out is nullIOException- If there's a problem accessing the java.io.Writer provided
-
setResult
Sets theResultinto which the output xml will be written. Currently this method supportsDOMResult,SAXResultandStreamResult.- Parameters:
result- theResultinstance to set- Throws:
IllegalArgumentException- if the result is null or it is not supportedIOException- Since:
- 1.3.3
-
setNode
Sets the W3CNodeinstance to marshal to.- Parameters:
node- the DOMNodeto marshal into.- Throws:
IllegalArgumentException- if node is null
-
setXmlStreamWriter
Sets theXMLStreamWriterto use.- Parameters:
xmlStreamWriter- theXMLStreamWriterinstance to use- Throws:
IllegalArgumentException- if the xmlStreamWriter is null- Since:
- 1.3.3
-
setXmlEventWriter
Sets theXMLEventWriterto use.- Parameters:
xmlEventWriter- theXMLEventWriterinstance to use- Throws:
IllegalArgumentException- if the xmlEventReader is null- Since:
- 1.3.3
-
setInternalContext
To set theInternalContextto use, and to initializeMarshallerproperties linked to it.- Parameters:
internalContext- theInternalContextto use
-
addProcessingInstruction
Adds the given processing instruction data to the set of processing instructions to output during marshalling.- Parameters:
target- the processing instruction targetdata- the processing instruction data- Throws:
IllegalArgumentException- if target is null or empty string or data is null
-
setDoctype
-
setSupressXMLDeclaration
public void setSupressXMLDeclaration(boolean supressXMLDeclaration) Sets whether or not to marshal as a document which includes the XML declaration, and if necessary the DOCTYPE declaration. By default the Marshaller will marshal as a well formed XML document including the XML Declaration. If the given boolean is true, the Marshaller will marshal as a well formed XML fragment (no XML declaration or DOCTYPE). This method is basically the same as calling #setMarshalAsDocument(false);- Parameters:
supressXMLDeclaration- a boolean that when true includes that generated XML should not contain the XML declaration.- See Also:
-
setMarshalAsDocument
public void setMarshalAsDocument(boolean asDocument) Sets whether or not to marshal as a document which includes the XML declaration, and if necessary the DOCTYPE declaration. By default the Marshaller will marshal as a well formed XML document including the XML Declaration. If the given boolean is false, the Marshaller will marshal as a well formed XML fragment (no XML declaration or DOCTYPE). This method is basically the same as calling #setSupressXMLDeclaration(true);- Parameters:
asDocument- a boolean, when true, indicating to marshal as a complete XML document.- See Also:
-
setMapping
Sets the given mapping to be used by the marshalling Framework. If a resolver exists this mapping will be added to the existing ClassDescriptorResolver. Otherwise a new ClassDescriptorResolver will be created.- Parameters:
mapping- Mapping to using during marshalling.- Throws:
MappingException
-
setMarshalListener
Sets an optional MarshalListener to recieve pre and post marshal notification for each Object in the tree. MarshalListener is only for complex objects that map into elements, simpleTypes and types that map into attributes do not cause any pre and post event notifications. Current only one (1) listener is allowed. If you need register multiple listeners, you will have to create your own master listener that will forward the event notifications and manage the multiple listeners.- Parameters:
listener- the MarshalListener to set.
-
setNamespaceMapping
Sets the mapping for the given Namespace prefix.- Parameters:
nsPrefix- the namespace prefixnsURI- the namespace that the prefix resolves to- Throws:
IllegalArgumentException- if nsURI is null or empty string
-
setRootElement
Sets the name of the root element to use.- Parameters:
rootElement- The name of the root element to use.
-
getRootElement
Returns the name of the root element to use- Returns:
- Returns the name of the root element to use
-
setNSPrefixAtRoot
public void setNSPrefixAtRoot(boolean nsPrefixAtRoot) Deprecated.Set to True to declare the given namespace mappings at the root node. Default is False.- Parameters:
nsPrefixAtRoot-
-
getNSPrefixAtRoot
public boolean getNSPrefixAtRoot()Deprecated.Returns True if the given namespace mappings will be declared at the root node.- Returns:
- Returns True if the given namespace mappings will be declared at the root node.
-
getResolver
Returns the ClassDescriptorResolver for use during marshalling- Returns:
- the ClassDescriptorResolver
- See Also:
-
setResolver
Sets the ClassDescriptorResolver to use during marshalling.
Note: This method will nullify any Mapping currently being used by this Marshaller- Parameters:
cdr- the ClassDescriptorResolver to use- See Also:
-
setValidation
public void setValidation(boolean validate) Sets whether or not to validate the object model before marshalling. By default validation is enabled. This method is really for debugging. I do not recommend turning off validation, since you could marshal a document, which you can then not unmarshal. If you know the object model is guaranteed to be valid, disabling validation will improve performace.- Parameters:
validate- the boolean indicating whether or not to validate the object model before marshalling.
-
getValidation
public boolean getValidation() -
setMarshalExtendedType
public void setMarshalExtendedType(boolean marshalExtendedType) If True the marshaller will use the 'xsi:type' attribute to marshall a field value that extended the defined field type. Default is True. -
getMarshalExtendedType
public boolean getMarshalExtendedType()If True the marshaller will use the 'xsi:type' attribute to marshall a field value that extended the defined field type. Default is True.- Returns:
- If True the marshaller will use the 'xsi:type' attribute to marshall a field value that extended the defined field type. Default is True.
-
marshal
Marshals the given Object as XML using the given writer.- Parameters:
object- The Object to marshal.out- The writer to marshal to.- Throws:
MarshalExceptionValidationException
-
marshal
public static void marshal(Object object, DocumentHandler handler) throws MarshalException, ValidationException Marshals the given Object as XML using the given DocumentHandler to send events to.- Parameters:
object- The Object to marshal.handler- The DocumentHandler to marshal to.- Throws:
MarshalExceptionValidationException
-
marshal
public static void marshal(Object object, ContentHandler handler) throws MarshalException, ValidationException Marshals the given Object as XML using the given ContentHandler to send events to.- Parameters:
object- The Object to marshal.handler- The ContentHandler to marshal to.- Throws:
MarshalExceptionValidationException
-
marshal
Marshals the given Object as XML using the given DOM Node to send events to.- Parameters:
object- The Object to marshal.node- The DOM Node to marshal to.- Throws:
MarshalExceptionValidationException
-
marshal
Marshals the given Object as XML using the DocumentHandler for this Marshaller.- Parameters:
object- The Object to marshal.- Throws:
MarshalExceptionValidationException
-
setLogWriter
Sets the PrintWriter used for logging- Parameters:
printWriter- the PrintWriter to use for logging
-
setEncoding
Sets the encoding for the serializer. Note that this method cannot be called if you've passed in your own DocumentHandler.- Parameters:
encoding- the encoding to set
-
setNoNamespaceSchemaLocation
Sets the value for the xsi:noNamespaceSchemaLocation attribute. When set, this attribute will appear on the root element of the marshalled document.- Parameters:
schemaLocation- the URI location of the schema to which the marshalled document is an instance of.
-
setSchemaLocation
Sets the value for the xsi:schemaLocation attribute. When set, this attribute will appear on the root element of the marshalled document.- Parameters:
schemaLocation- the URI location of the schema to which the marshalled document is an instance of.
-
setSuppressNamespaces
public void setSuppressNamespaces(boolean suppressNamespaces) Sets whether or not namespaces are output. By default the Marshaller will output namespace declarations and prefix elements and attributes with their respective namespace prefix. This method can be used to prevent the usage of namespaces.- Parameters:
suppressNamespaces- a boolean that when true will prevent namespaces from being output.
-
setSuppressXSIType
public void setSuppressXSIType(boolean suppressXSIType) Sets whether or not the xsi:type attribute should appear on the marshalled document.- Parameters:
suppressXSIType- a boolean that when true will prevent xsi:type attribute from being used in the marshalling process.
-
setUseXSITypeAtRoot
public void setUseXSITypeAtRoot(boolean useXSITypeAtRoot) Sets whether or not to output the xsi:type at the root element. This is usually needed when the root element type cannot be determined by the element name alone. By default xsi:type will not be output on the root element.- Parameters:
useXSITypeAtRoot- a boolean that when true indicates that the xsi:type should be output on the root element.
-
getProperty
-
setProperty
-
setContentHandler
To set the SAXContentHandlerwhich is used as destination at marshalling.- Parameters:
contentHandler- the SAXContentHandlerto use as destination at marshalling
-
getJavaNaming
To get theJavaNaminginstance to be used.- Returns:
- the JavaNaming to be used
-
getInternalContext
To get theAbstractInternalContextto use.- Returns:
- the
AbstractInternalContextto use
-
isCollection
Returns true if the given Class is a considered a collection by the marshalling framework.- Parameters:
clazz- the Class to check- Returns:
- true if the given Class is considered a collection. TODO: joachim: this code exists somewhere else too!!
-
getCollectionHandler
Returns the CollectionHandler associated with the given collection, or null if no such handler exists.- Parameters:
clazz- the Class to check- Returns:
- the CollectionHandler for the associated type.
-
namespaceEquals
Compares the given namespaces (as strings) for equality. null and empty values are considered equal.- Parameters:
ns1- the namespace to compare to argument ns2ns2- the namespace to compare to argument ns1- Returns:
- true if the namespaces are considert equal TODO: joachim put it into XMLNaming!
-
searchInheritance
protected org.exolab.castor.xml.MarshalFramework.InheritanceMatch[] searchInheritance(String name, String namespace, XMLClassDescriptor classDesc) throws MarshalException Search there is a field descriptor which can accept one of the class descriptor which match the given name and namespace.- Parameters:
name- XML name of the fieldnamespace- namespace of the fieldclassDesc- the class descriptor to match againstcdResolver- the class descriptor resolver to use- Returns:
- An array of InheritanceMatch.
- Throws:
MarshalException- if the resolver called fails fatally
-