|  |  |  | License Codehaus OpenEJB OpenJMS OpenORB Tyrex
Old releases
General
Release 1.3
Release 1.3rc1
Release 1.2
Main
Home
About
Features
Download
Dependencies
Reference guide
Publications
JavaDoc
Maven 2 support
Maven 2 archetypes
DTD & Schemas
Recent HTML changes
News Archive
RSS news feed
Project Wiki
Development/Support
Mailing Lists
SVN/JIRA
Contributing
Support
Continuous builds
Prof. services
Related projects
Spring ORM support
Spring XML factories
WS frameworks
XML
XML
XML Code Generator
XML Code Generator
JDO
Introduction
First steps
Using JDO
JDO Config
Types
JDO Mapping
JDO FAQ
JDO Examples
JDO HOW-TOs
Tips & Tricks
Other Features
JDO sample JAR
Tools
Schema generator
Advanced JDO
Caching
OQL
Trans. & Locks
Design
KeyGen
Long Trans.
Nested Attrs.
Pooling Examples
LOBs
Best practice
DDL Generator
Using DDL Generator
Properties
Ant task
Type Mapping
More
The Examples
3rd Party Tools
JDO Tests
XML Tests
Configuration
About
License
User stories
Contributors
Marketplace
Status, Todo
Changelog
Library
Contact
Project Name
## 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:
- | org.castor.exolab.xml.Serializer
- | org.castor.exolab.xml.OutputFormat
### 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
- | Apache Xerces
| Copyright © 1999-2005 ExoLab Group, Intalio Inc., and Contributors. All rights reserved. Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners. |