Method from org.exolab.castor.xml.Marshaller Detail: |
public void addProcessingInstruction(String target,
String data) {
if ((target == null) || (target.length() == 0)) {
String err = "the argument 'target' must not be null or empty.";
throw new IllegalArgumentException(err);
}
if (data == null) {
String err = "the argument 'data' must not be null.";
throw new IllegalArgumentException(err);
}
_processingInstructions.add(new ProcessingInstruction(target, data));
}
Adds the given processing instruction data to the set of
processing instructions to output during marshalling. |
public boolean getMarshalExtendedType() {
return _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. |
public boolean getNSPrefixAtRoot() {
return true;
} Deprecated!
Returns True if the given namespace mappings will be declared at the root node. |
public String getProperty(String name) {
return getInternalContext().getStringProperty(name);
}
Returns the value of the given Castor XML-specific property. |
public XMLClassDescriptorResolver getResolver() {
// if (_cdResolver == null) {
// _cdResolver = (XMLClassDescriptorResolver)
// ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML);
// }
if ((getInternalContext() == null)
|| (getInternalContext().getXMLClassDescriptorResolver() == null)) {
String message = "No internal context or no class descriptor in context.";
LOG.warn(message);
throw new IllegalStateException(message);
}
return getInternalContext().getXMLClassDescriptorResolver();
}
Returns the ClassDescriptorResolver for use during marshalling |
public String getRootElement() {
return _rootElement;
}
Returns the name of the root element to use |
public boolean getValidation() {
return _validate;
}
|
public void marshal(Object object) throws MarshalException, ValidationException {
if (object == null)
throw new MarshalException("object must not be null");
if (LOG.isDebugEnabled()) {
LOG.debug("Marshalling " + object.getClass().getName());
}
if (object instanceof AnyNode) {
try{
AnyNode2SAX2.fireEvents((AnyNode)object, _handler, _namespaces);
} catch(SAXException e) {
throw new MarshalException(e);
}
}
else {
validate(object);
MarshalState mstate = new MarshalState(object, "root");
if (_asDocument) {
try {
_handler.startDocument();
//-- handle processing instructions
for (int i = 0; i < _processingInstructions.size(); i++) {
ProcessingInstruction pi = (ProcessingInstruction)
_processingInstructions.get(i);
_handler.processingInstruction(pi.getTarget(),
pi.getData());
}
marshal(object, null, _handler, mstate);
_handler.endDocument();
} catch (SAXException sx) {
throw new MarshalException(sx);
}
}
else {
marshal(object, null, _handler, mstate);
}
}
}
Marshals the given Object as XML using the DocumentHandler
for this Marshaller. |
public static void marshal(Object object,
Writer out) throws MarshalException, ValidationException {
try {
staticMarshal(object, new Marshaller(out));
} catch (IOException e) {
throw new MarshalException(e);
}
}
Marshals the given Object as XML using the given writer. |
public static void marshal(Object object,
DocumentHandler handler) throws MarshalException, ValidationException {
staticMarshal(object, new Marshaller(handler));
}
Marshals the given Object as XML using the given DocumentHandler
to send events to. |
public static void marshal(Object object,
ContentHandler handler) throws MarshalException, ValidationException, IOException {
staticMarshal(object, new Marshaller(handler));
}
Marshals the given Object as XML using the given ContentHandler
to send events to. |
public static void marshal(Object object,
Node node) throws MarshalException, ValidationException {
staticMarshal(object, new Marshaller(node));
}
Marshals the given Object as XML using the given DOM Node
to send events to. |
public void setContentHandler(ContentHandler contentHandler) {
_handler = contentHandler;
}
To set the SAX ContentHandler which is used as destination at marshalling. |
public void setDoctype(String publicId,
String systemId) {
if (_serializer != null) {
if (_format == null) {
_format = getInternalContext().getOutputFormat();
}
_format.setDoctype(publicId, systemId);
//-- reset output format, this needs to be done
//-- any time a change occurs to the format.
_serializer.setOutputFormat( _format );
try {
//-- Due to a Xerces Serializer bug that doesn't allow declaring
//-- multiple prefixes to the same namespace, we use the old
//-- DocumentHandler format and process namespaces ourselves
_handler = new DocumentHandlerAdapter(_serializer.asDocumentHandler());
}
catch (java.io.IOException iox) {
//-- we can ignore this exception since it shouldn't
//-- happen. If _serializer is not null, it means
//-- we've already called this method sucessfully
//-- in the Marshaller() constructor
if (LOG.isDebugEnabled()) {
LOG.debug("Error setting up document handler", iox);
}
}
}
else {
String error = "doctype cannot be set if you've passed in "+
"your own DocumentHandler";
throw new IllegalStateException(error);
}
}
Sets the document type definition for the serializer. Note that this method
cannot be called if you've passed in your own DocumentHandler. |
public void setDocumentHandler(DocumentHandler handler) {
if (handler == null) {
throw new IllegalArgumentException("The given 'org.sax.DocumentHandler' "
+ "instance is null.");
}
setContentHandler(new DocumentHandlerAdapter(handler));
}
|
public void setEncoding(String encoding) {
if (_serializer != null) {
if (_format == null) {
_format = getInternalContext().getOutputFormat();
}
_format.setEncoding(encoding);
//-- reset output format, this needs to be done
//-- any time a change occurs to the format.
_serializer.setOutputFormat( _format );
try {
//-- Due to a Xerces Serializer bug that doesn't allow declaring
//-- multiple prefixes to the same namespace, we use the old
//-- DocumentHandler format and process namespaces ourselves
_handler = new DocumentHandlerAdapter(_serializer.asDocumentHandler());
}
catch (java.io.IOException iox) {
//-- we can ignore this exception since it shouldn't
//-- happen. If _serializer is not null, it means
//-- we've already called this method sucessfully
//-- in the Marshaller() constructor
if (LOG.isDebugEnabled()) {
LOG.debug("Error setting encoding to " + encoding, iox);
}
}
}
else {
String error = "encoding cannot be set if you've passed in "+
"your own DocumentHandler";
throw new IllegalStateException(error);
}
}
Sets the encoding for the serializer. Note that this method
cannot be called if you've passed in your own DocumentHandler. |
public void setInternalContext(InternalContext internalContext) {
super.setInternalContext(internalContext);
deriveProperties();
}
|
public void setLogWriter(PrintWriter printWriter) {
}
Sets the PrintWriter used for logging |
public void setMapping(Mapping mapping) throws MappingException {
// if (_cdResolver == null) {
// _cdResolver = (XMLClassDescriptorResolver) ClassDescriptorResolverFactory
// .createClassDescriptorResolver(BindingType.XML);
// }
if ((getInternalContext() == null)
|| (getInternalContext().getXMLClassDescriptorResolver() == null)) {
String message = "No internal context or no class descriptor in context.";
LOG.warn(message);
throw new IllegalStateException(message);
}
MappingUnmarshaller mum = new MappingUnmarshaller();
MappingLoader resolver = mum.getMappingLoader(mapping, BindingType.XML);
getInternalContext().getXMLClassDescriptorResolver().setMappingLoader(resolver);
}
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. |
public void setMarshalAsDocument(boolean asDocument) {
_asDocument = asDocument;
if (_serializer != null) {
if (_format == null) {
_format = getInternalContext().getOutputFormat();
}
_format.setOmitXMLDeclaration( ! asDocument );
_format.setOmitDocumentType( ! asDocument );
//-- reset output format, this needs to be done
//-- any time a change occurs to the format.
_serializer.setOutputFormat( _format );
try {
//-- Due to a Xerces Serializer bug that doesn't allow declaring
//-- multiple prefixes to the same namespace, we use the old
//-- DocumentHandler format and process namespaces ourselves
_handler = new DocumentHandlerAdapter(_serializer.asDocumentHandler());
}
catch (java.io.IOException iox) {
//-- we can ignore this exception since it shouldn't
//-- happen. If _serializer is not null, it means
//-- we've already called this method sucessfully
//-- in the Marshaller() constructor
if (LOG.isDebugEnabled()) {
LOG.debug("Error setting up document handler", iox);
}
}
}
}
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); |
public void setMarshalExtendedType(boolean marshalExtendedType) {
_marshalExtendedType = 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. |
public void setMarshalListener(MarshalListener listener) {
_marshalListener = listener;
}
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. |
public void setNSPrefixAtRoot(boolean nsPrefixAtRoot) {
// leaving for now...backward compatability
//_nsPrefixAtRoot = nsPrefixAtRoot;
} Deprecated!
Set to True to declare the given namespace mappings at the root node. Default is False. |
public void setNamespaceMapping(String nsPrefix,
String nsURI) {
if ((nsURI == null) || (nsURI.length() == 0)) {
String err = "namespace URI must not be null.";
throw new IllegalArgumentException(err);
}
_namespaces.addNamespace(nsPrefix, nsURI);
}
Sets the mapping for the given Namespace prefix |
public void setNoNamespaceSchemaLocation(String schemaLocation) {
if (schemaLocation == null) {
//-- remove if necessary
//-- to be added later.
}
else {
_topLevelAtts.setAttribute(XSI_NO_NAMESPACE_SCHEMA_LOCATION,
schemaLocation, XSI_NAMESPACE);
}
}
Sets the value for the xsi:noNamespaceSchemaLocation attribute.
When set, this attribute will appear on the root element
of the marshalled document. |
public void setNode(Node node) {
if (node == null) {
throw new IllegalArgumentException("The given org.w3c.dom.Node instance is null.");
}
setContentHandler(new DocumentHandlerAdapter(new SAX2DOMHandler(node)));
}
Sets the W3C Node instance to marshal to. |
public void setProperty(String name,
String value) {
getInternalContext().setProperty(name, value);
deriveProperties();
}
Sets a custom value of a given Castor XML-specific property. |
public void setResolver(XMLClassDescriptorResolver cdr) {
if (cdr != null) {
getInternalContext().setXMLClassDescriptorResolver(cdr);
// _cdResolver = cdr;
}
}
Sets the ClassDescriptorResolver to use during marshalling.
Note: This method will nullify any Mapping
currently being used by this Marshaller |
public void setRootElement(String rootElement) {
_rootElement = rootElement;
}
Sets the name of the root element to use. |
public void setSchemaLocation(String schemaLocation) {
if (schemaLocation == null) {
//-- remove if necessary
//-- to be added later.
}
else {
_topLevelAtts.setAttribute(XSI_SCHEMA_LOCATION,
schemaLocation, XSI_NAMESPACE);
}
}
Sets the value for the xsi:schemaLocation attribute.
When set, this attribute will appear on the root element
of the marshalled document. |
public void setSuppressNamespaces(boolean suppressNamespaces) {
_suppressNamespaces = 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. |
public void setSuppressXSIType(boolean suppressXSIType) {
_suppressXSIType = suppressXSIType;
}
Sets whether or not the xsi:type attribute should appear
on the marshalled document. |
public void setSupressXMLDeclaration(boolean supressXMLDeclaration) {
setMarshalAsDocument(!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); |
public void setUseXSITypeAtRoot(boolean useXSITypeAtRoot) {
_useXSITypeAtRoot = 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. |
public void setValidation(boolean validate) {
_validate = 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. |
public void setWriter(Writer out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("The given 'java.io.Writer instance' is null.");
}
configureSerializer(out);
}
Sets the java.io.Writer to be used during marshalling. |