public class Marshaller extends Object
Modifier and Type | Class and Description |
---|---|
static class |
MarshalFramework.InheritanceMatch
Used to store the information when we find a possible inheritance.
|
static class |
Marshaller.NilObject
A wrapper for a "Nil" object
|
Modifier and Type | Field and Description |
---|---|
static String |
LANG_ATTR
The xml:lang attribute, without the "xml:" prefix.
|
static String |
NIL_ATTR
The xsi:nil attribute, without the "xsi:" prefix.
|
static String |
SPACE_ATTR
The xml:space attribute name, without the "xml:" prefix.
|
static String |
TRUE_VALUE
The value of 'true'.
|
static String |
TYPE_ATTR
The xsi:type attribute name, without the "xsi:" prefix.
|
static String |
XML_LANG_ATTR
The xml:lang attribute name.
|
static String |
XML_SPACE_ATTR
The xml:space attribute name.
|
static String |
XSI_NAMESPACE
The XSI Namespace URI.
|
static String |
XSI_NIL_ATTR
The xsi:nil attribute.
|
static String |
XSI_NO_NAMESPACE_SCHEMA_LOCATION
The name of the no namespace schema location attribute.
|
static String |
XSI_SCHEMA_LOCATION
The name of the Schema location attribute.
|
Constructor and Description |
---|
Marshaller()
Creates a default instance of Marshaller, where the sink needs to be set
separately.
|
Marshaller(ContentHandler contentHandler)
Creates a new
Marshaller with the given SAX ContentHandler . |
Marshaller(DocumentHandler handler)
Creates a new
Marshaller with the given SAX DocumentHandler . |
Marshaller(InternalContext internalContext)
The one
Marshaller constructor that is used by XMLContext which
sets an InternalContext that comes from outside. |
Marshaller(Node node)
Creates a new
Marshaller for the given DOM Node . |
Marshaller(Writer out)
Creates a new
Marshaller with the given writer. |
Marshaller(XMLEventWriter xmlEventWriter)
Creates a new
Marshaller with the given XMLEventWriter . |
Marshaller(XMLStreamWriter xmlStreamWriter)
Creates a new
Marshaller with the given XMLStreamWriter . |
Modifier and Type | Method and Description |
---|---|
void |
addProcessingInstruction(String target,
String data)
Adds the given processing instruction data to the set of
processing instructions to output during marshalling.
|
CollectionHandler |
getCollectionHandler(Class clazz)
Returns the CollectionHandler associated with the
given collection, or null if no such handler exists.
|
InternalContext |
getInternalContext()
To get the
AbstractInternalContext to use. |
JavaNaming |
getJavaNaming()
To get the
JavaNaming instance to be used. |
boolean |
getMarshalExtendedType()
If True the marshaller will use the 'xsi:type' attribute
to marshall a field value that extended the defined field type.
|
boolean |
getNSPrefixAtRoot()
Deprecated.
|
String |
getProperty(String name)
Returns the value of the given Castor XML-specific property.
|
XMLClassDescriptorResolver |
getResolver()
Returns the ClassDescriptorResolver for use during marshalling
|
String |
getRootElement()
Returns the name of the root element to use
|
boolean |
getValidation() |
static boolean |
isCollection(Class clazz)
Returns true if the given Class is a considered a
collection by the marshalling framework.
|
void |
marshal(Object object)
Marshals the given Object as XML using the DocumentHandler
for this Marshaller.
|
static void |
marshal(Object object,
ContentHandler handler)
Marshals the given Object as XML using the given ContentHandler
to send events to.
|
static void |
marshal(Object object,
DocumentHandler handler)
Marshals the given Object as XML using the given DocumentHandler
to send events to.
|
static void |
marshal(Object object,
Node node)
Marshals the given Object as XML using the given DOM Node
to send events to.
|
static void |
marshal(Object object,
Writer out)
Marshals the given Object as XML using the given writer.
|
static boolean |
namespaceEquals(String ns1,
String ns2)
Compares the given namespaces (as strings) for equality.
|
protected 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.
|
void |
setContentHandler(ContentHandler contentHandler)
To set the SAX
ContentHandler which is used as destination at marshalling. |
void |
setDoctype(String publicId,
String systemId)
Sets the document type definition for the serializer.
|
void |
setDocumentHandler(DocumentHandler handler)
Sets the given SAX
DocumentHandler to 'marshal' into. |
void |
setEncoding(String encoding)
Sets the encoding for the serializer.
|
void |
setInternalContext(InternalContext internalContext)
To set the
InternalContext to use, and to
initialize Marshaller properties linked to it. |
void |
setLogWriter(PrintWriter printWriter)
Sets the PrintWriter used for logging
|
void |
setMapping(Mapping mapping)
Sets the given mapping to be used by the marshalling Framework.
|
void |
setMarshalAsDocument(boolean asDocument)
Sets whether or not to marshal as a document which includes
the XML declaration, and if necessary the DOCTYPE declaration.
|
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.
|
void |
setMarshalListener(MarshalListener listener)
Sets an optional MarshalListener to recieve pre and post
marshal notification for each Object in the tree.
|
void |
setNamespaceMapping(String nsPrefix,
String nsURI)
Sets the mapping for the given Namespace prefix.
|
void |
setNode(Node node)
Sets the W3C
Node instance to marshal to. |
void |
setNoNamespaceSchemaLocation(String schemaLocation)
Sets the value for the xsi:noNamespaceSchemaLocation attribute.
|
void |
setNSPrefixAtRoot(boolean nsPrefixAtRoot)
Deprecated.
|
void |
setProperty(String name,
String value)
Sets a custom value of a given Castor XML-specific property.
|
void |
setResolver(XMLClassDescriptorResolver cdr)
Sets the ClassDescriptorResolver to use during marshalling.
|
void |
setResult(Result result)
Sets the
Result into which the output xml will be written. |
void |
setRootElement(String rootElement)
Sets the name of the root element to use.
|
void |
setSchemaLocation(String schemaLocation)
Sets the value for the xsi:schemaLocation attribute.
|
void |
setSuppressNamespaces(boolean suppressNamespaces)
Sets whether or not namespaces are output.
|
void |
setSuppressXSIType(boolean suppressXSIType)
Sets whether or not the xsi:type attribute should appear
on the marshalled document.
|
void |
setSupressXMLDeclaration(boolean supressXMLDeclaration)
Sets whether or not to marshal as a document which includes
the XML declaration, and if necessary the DOCTYPE declaration.
|
void |
setUseXSITypeAtRoot(boolean useXSITypeAtRoot)
Sets whether or not to output the xsi:type at the root
element.
|
void |
setValidation(boolean validate)
Sets whether or not to validate the object model
before marshalling.
|
void |
setWriter(Writer out)
Sets the java.io.Writer to be used during marshalling.
|
void |
setXmlEventWriter(XMLEventWriter xmlEventWriter)
Sets the
XMLEventWriter to use. |
void |
setXmlStreamWriter(XMLStreamWriter xmlStreamWriter)
Sets the
XMLStreamWriter to use. |
public static final String XSI_NAMESPACE
public static final String XSI_SCHEMA_LOCATION
public static final String XSI_NO_NAMESPACE_SCHEMA_LOCATION
public static final String XML_LANG_ATTR
public static final String LANG_ATTR
public static final String NIL_ATTR
public static final String XSI_NIL_ATTR
public static final String XML_SPACE_ATTR
public static final String SPACE_ATTR
public static final String TYPE_ATTR
public static final String TRUE_VALUE
public Marshaller(DocumentHandler handler)
Marshaller
with the given SAX DocumentHandler
.handler
- the SAX DocumentHandler
to "marshal" to.IllegalArgumentException
- if the given DocumentHandler
is nullXMLContext#createMarshaller()}
,
Marshaller#setDocumentHandler(DocumentHandler)}
,
XMLContext
public Marshaller(ContentHandler contentHandler)
Marshaller
with the given SAX ContentHandler
.contentHandler
- the ContentHandler
to "marshal" to.IllegalArgumentException
- if the gievn ContentHandler
is nullXMLContext#createMarshaller()}
,
Marshaller#setContentHandler(ContentHandler)}
,
XMLContext
public Marshaller(InternalContext internalContext)
Marshaller
constructor that is used by XMLContext
which
sets an InternalContext
that comes from outside. Writer or ContentHandler
have to be set in a second step.internalContext
- the InternalContext
to initialize the Marshaller
instance withpublic Marshaller()
public Marshaller(Writer out) throws IOException
Marshaller
with the given writer.out
- the Writer
to serialise to.IllegalArgumentException
- if the given Writer
is nullIOException
- If the given Writer
instance cannot be opened.XMLContext#createMarshaller()}
,
Marshaller#setWriter(Writer)}
,
XMLContext
public Marshaller(XMLStreamWriter xmlStreamWriter)
Marshaller
with the given XMLStreamWriter
.xmlStreamWriter
- the XMLStreamWriter
IllegalArgumentException
- if the given XMLStreamWriter
is nullXMLContext#createMarshaller()}
,
Marshaller#setXmlStreamWriter(javax.xml.stream.XMLStreamWriter)}
,
XMLContext
public Marshaller(XMLEventWriter xmlEventWriter)
Marshaller
with the given XMLEventWriter
.xmlEventWriter
- the XMLEventWriter
IllegalArgumentException
- if the given XMLEventWriter
is nullXMLContext#createMarshaller()}
,
Marshaller#setXmlEventWriter(javax.xml.stream.XMLEventWriter)}
,
XMLContext
public Marshaller(Node node)
Marshaller
for the given DOM Node
.node
- the DOM Node
to marshal into.IllegalArgumentException
- if node is nullXMLContext#createMarshaller()}
,
Marshaller#setNode(Node)}
,
XMLContext
public void setDocumentHandler(DocumentHandler handler)
DocumentHandler
to 'marshal' into.handler
- the SAX DocumentHandler
to "marshal" to.IllegalArgumentException
- if the given DocumentHandler
is nullpublic void setWriter(Writer out) throws IOException
out
- The writer to use for marshallingIllegalArgumentException
- if out is nullIOException
- If there's a problem accessing the java.io.Writer providedpublic void setResult(Result result) throws IOException
Result
into which the output xml will be written. Currently this method supports
DOMResult
, SAXResult
and StreamResult
.result
- the Result
instance to setIllegalArgumentException
- if the result is null or it is not supportedIOException
public void setNode(Node node)
Node
instance to marshal to.node
- the DOM Node
to marshal into.IllegalArgumentException
- if node is nullpublic void setXmlStreamWriter(XMLStreamWriter xmlStreamWriter)
XMLStreamWriter
to use.xmlStreamWriter
- the XMLStreamWriter
instance to useIllegalArgumentException
- if the xmlStreamWriter is nullpublic void setXmlEventWriter(XMLEventWriter xmlEventWriter)
XMLEventWriter
to use.xmlEventWriter
- the XMLEventWriter
instance to useIllegalArgumentException
- if the xmlEventReader is nullpublic void setInternalContext(InternalContext internalContext)
InternalContext
to use, and to
initialize Marshaller
properties linked to it.internalContext
- the InternalContext
to usepublic void addProcessingInstruction(String target, String data)
target
- the processing instruction targetdata
- the processing instruction dataIllegalArgumentException
- if target is null or empty string or data is nullpublic void setDoctype(String publicId, String systemId)
publicId
- the public identifiersystemId
- the system identifierpublic void setSupressXMLDeclaration(boolean supressXMLDeclaration)
supressXMLDeclaration
- a boolean that when true
includes that generated XML should not contain
the XML declaration.setMarshalAsDocument(boolean)
public void setMarshalAsDocument(boolean asDocument)
asDocument
- a boolean, when true, indicating to marshal
as a complete XML document.setSupressXMLDeclaration(boolean)
public void setMapping(Mapping mapping) throws MappingException
mapping
- Mapping to using during marshalling.MappingException
public void setMarshalListener(MarshalListener listener)
listener
- the MarshalListener to set.public void setNamespaceMapping(String nsPrefix, String nsURI)
nsPrefix
- the namespace prefixnsURI
- the namespace that the prefix resolves toIllegalArgumentException
- if nsURI is null or empty stringpublic void setRootElement(String rootElement)
rootElement
- The name of the root element to use.public String getRootElement()
public void setNSPrefixAtRoot(boolean nsPrefixAtRoot)
nsPrefixAtRoot
- public boolean getNSPrefixAtRoot()
public XMLClassDescriptorResolver getResolver()
setResolver(org.exolab.castor.xml.XMLClassDescriptorResolver)
public void setResolver(XMLClassDescriptorResolver cdr)
cdr
- the ClassDescriptorResolver to usesetMapping(org.exolab.castor.mapping.Mapping)
,
getResolver()
public void setValidation(boolean validate)
validate
- the boolean indicating whether or not to
validate the object model before marshalling.public boolean getValidation()
public void setMarshalExtendedType(boolean marshalExtendedType)
public boolean getMarshalExtendedType()
public static void marshal(Object object, Writer out) throws MarshalException, ValidationException
object
- The Object to marshal.out
- The writer to marshal to.MarshalException
ValidationException
public static void marshal(Object object, DocumentHandler handler) throws MarshalException, ValidationException
object
- The Object to marshal.handler
- The DocumentHandler to marshal to.MarshalException
ValidationException
public static void marshal(Object object, ContentHandler handler) throws MarshalException, ValidationException
object
- The Object to marshal.handler
- The ContentHandler to marshal to.MarshalException
ValidationException
public static void marshal(Object object, Node node) throws MarshalException, ValidationException
object
- The Object to marshal.node
- The DOM Node to marshal to.MarshalException
ValidationException
public void marshal(Object object) throws MarshalException, ValidationException
object
- The Object to marshal.MarshalException
ValidationException
public void setLogWriter(PrintWriter printWriter)
printWriter
- the PrintWriter to use for loggingpublic void setEncoding(String encoding)
encoding
- the encoding to setpublic void setNoNamespaceSchemaLocation(String schemaLocation)
schemaLocation
- the URI location of the schema
to which the marshalled document is an instance of.public void setSchemaLocation(String schemaLocation)
schemaLocation
- the URI location of the schema
to which the marshalled document is an instance of.public void setSuppressNamespaces(boolean suppressNamespaces)
suppressNamespaces
- a boolean that when true
will prevent namespaces from being output.public void setSuppressXSIType(boolean suppressXSIType)
suppressXSIType
- a boolean that when true will prevent
xsi:type attribute from being used in the marshalling process.public void setUseXSITypeAtRoot(boolean useXSITypeAtRoot)
useXSITypeAtRoot
- a boolean that when true indicates
that the xsi:type should be output on the root element.public String getProperty(String name)
name
- Qualified name of the CASTOR XML-specific property.public void setProperty(String name, String value)
name
- Name of the Castor XML propertyvalue
- Custom value to set.public void setContentHandler(ContentHandler contentHandler)
ContentHandler
which is used as destination at marshalling.contentHandler
- the SAX ContentHandler
to use as destination at marshallingpublic JavaNaming getJavaNaming()
JavaNaming
instance to be used.public InternalContext getInternalContext()
AbstractInternalContext
to use.AbstractInternalContext
to usepublic static boolean isCollection(Class clazz)
clazz
- the Class to checkpublic CollectionHandler getCollectionHandler(Class clazz)
clazz
- the Class to checkpublic static boolean namespaceEquals(String ns1, String ns2)
ns1
- the namespace to compare to argument ns2ns2
- the namespace to compare to argument ns1protected MarshalFramework.InheritanceMatch[] searchInheritance(String name, String namespace, XMLClassDescriptor classDesc) throws MarshalException
name
- XML name of the fieldnamespace
- namespace of the fieldclassDesc
- the class descriptor to match againstcdResolver
- the class descriptor resolver to useMarshalException
- if the resolver called fails fatallyCopyright © 2021. All rights reserved.