How to implement a custom XML Serializer for Castor XML Intended Audience Prerequisites Steps Locate the required interfaces Implement the required interfaces Integrate the custom implementations with Castor XML References
Intended Audience
Anyone who is using Castor XML for marshalling, and wants to implement
a custom XML Serializer specific to the parser used.
This document addresses the basics to get people familiar with the main
concepts and discusses some implementation details.
The example given implements the XML Serializer interface specific to WebLogic's
XML parser (which happens to be an extension to Apache Xerces).
Prerequisites
You should have downloaded the Castor sources and expanded them into a custom folder of yours.
Steps
Here is how to proceed.
Locate the required interfaces
To implement your custom XML serialization mechanism, you'll have to provide
implementations of the following interfaces:
Implement the required interfaces
A possible implementation for Weblogic's (enhanced) Xerces implementation could look
as follows.
package org.somewhere.custom;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.xml.sax.DocumentHandler;
public class XercesSerializer implements org.exolab.castor.xml.Serializer {
private org.apache.xml.serialize.Serializer _serializer;
public XercesSerializer() {
_serializer = new XMLSerializer();
}
public void setOutputCharStream(Writer out) {
_serializer.setOutputCharStream(out);
}
public DocumentHandler asDocumentHandler() throws IOException {
return _serializer.asDocumentHandler();
}
public void setOutputFormat(org.exolab.castor.xml.OutputFormat format) {
_serializer.setOutputFormat((OutputFormat) format.getFormat());
}
public void setOutputByteStream(OutputStream output) {
_serializer.setOutputByteStream(output);
}
}
|
|
package org.somewhere.custom;
import org.apache.xml.serialize.OutputFormat;
import org.exolab.castor.util.Messages;
public class XercesOutputFormat implements org.exolab.castor.xml.OutputFormat {
private org.apache.xml.serialize.OutputFormat _outputFormat;
public XercesOutputFormat() {
_outputFormat = new org.apache.xml.serialize.OutputFormat();
}
public void setMethod(String method) {
_outputFormat.setMethod(method);
}
public void setIndenting(boolean indent) {
_outputFormat.setIndenting(indent);
}
public void setPreserveSpace(boolean preserveSpace) {
_outputFormat.setPreserveSpace(preserveSpace);
}
public Object getFormat() {
return _outputFormat;
}
public void setDoctype (String type1, String type2) {
_outputFormat.setDoctype(type1, type2);
}
public void setOmitXMLDeclaration(boolean omitXMLDeclaration) {
_outputFormat.setOmitXMLDeclaration(omitXMLDeclaration);
}
public void setOmitDocumentType(boolean omitDocumentType) {
_outputFormat.setOmitDocumentType(omitDocumentType);
}
public void setEncoding(String encoding) {
_outputFormat.setEncoding(encoding);
}
}
|
|
Integrate the custom implementations with Castor XML
In order to be able to use these custom implementations, you will have to provide a new
custom org.castor.exolab.xml.XMLSerializerFactory instance and instruct Castor XML to start
using it (instead of the default Xerces XML serializer factory).
The former can be achieved by providing a new implementation of the
org.castor.exolab.xml.XMLSerializerFactory interface as follows:
package org.somewhere.custom;
/**
* Weblogic Xerces-specific implementation of the {@link XMLSerializerFactory} interface.
* Returns Weblogic Xerces-specific instances of the {@link Serializer} and
* {@link OutputFormat} interfaces.
*/
public class WeblogicXMLSerializerFactory implements XMLSerializerFactory {
/**
* @inheritDoc
*/
public Serializer getSerializer() {
return new WeblogicSerializer();
}
/**
* @inheritDoc
*/
public OutputFormat getOutputFormat() {
return new WeblogicOutputFormat();
}
} |
|
The latter can be achieved by pointing the
org.exolab.castor.xml.serializer.factory property of the
castor.properties to your new custom XMLSerializerFactory
implementation as shown below:
# Defines the (default) XML serializer factory to use by Castor,
# which must implement org.exolab.castor.xml.SerializerFactory;
# default is org.exolab.castor.xml.XercesXMLSerializerFactory
org.exolab.castor.xml.serializer.factory=org.somewhere.custom.WeblogicXMLSerializerFactory |
|
References
|