Package com.icl.saxon.trax.serialize

Version: Alpha CVS $Revision$ $Date:$ Author: Assaf Arkin Goals: Define an interface for serializers that will allow an application to use any implementation. Support SAX 1, SAX 2, DOM Level 1, DOM Level 2 serialization Allow implementations to extend the interfaces and provide additional functionality, and support implementations for any output method. Provide a factory for the default output methods (XML, HTML, Text).

See:
          Description

Interface Summary
DOMSerializer Interface for a DOM serializer implementation.
Serializer Interface to a serializer implementation.
SerializerHandler Interface that supplements DocumentHandler and ContentHandler with additional methods suitable for serialization.
 

Class Summary
Method Names of the four default output methods.
OutputFormat The output format affects the manner in which a document is serialized.
QName A qualified name.
SerializerFactory Factory for creating new serializers.
 

Package com.icl.saxon.trax.serialize Description

Version:
Alpha CVS $Revision$ $Date:$
Author:
Assaf Arkin
Goals:

The Serializer Interfaces:

Serializer defines the interface supported by a serializer. A serializer implements a mechanism for producing output from a series of SAX events or a DOM document, in a given output format (aka the output method). A serializer can be constructed directly, or obtained from some factory, it may implement the base functionality or provide additional functionality suitable for the given output format (e.g. indentation in XML, page control in PDF, etc).

A serializer is not thread safe and may not be used concurrently, however, a serializer may be recyclable and used to serialize any number of documents with the same output method.

Before serializing a document, the serializer must be set with the output stream or writer, and optionally with an OutputFormat specifying the output properties. Serializer implementations may provide additional methods to control the way in which documents are serialized, or extend OutputFormat and offer additional output properties.

The Serializer and OutputFormat provides the minimum functionality that all serializer implementations must provide and that an application may depend on, and are based on the XSLT 1.0 specification.

For the purpose of serializing, a handle to the serializer is obtained that can be either a SAX 1 DocumentHandler, a SAX 2 ContentHandler or a DOM Level 1/2 DOMSerializer. The application should obtain and use only one handle at any given time and may not reuse the handle to serialize multiple documents. It is illegal for the application to call two different handle returning methods without resetting the serializer, or two use the same handle after resetting the serializer.

The SerializerFactory provides a means of obtaining the default serializers available from a given implementation. At the minimum an implementation should support XML, HTML and Text serializers. When additional serializers are available, the application may obtain them through the SerializerFactory or construct them directly.

Non-escaping and whitespace preserving output control is offered for XML, HTML and similar output method, but it is not mandatory that a serializer support these output control methods. Non-escaping and whitespace preserving can be set globally through the OutputFormat object, or directly when serializing SAX events through the SerializerHandler interface. Serializers are not required to implement the SerializerHandler interface.

Usage Examples:

Serialize a DOM document as XML:

  void printXML( Document doc, OutputStream stream, String encoding )
  {
    OutputFormat format;
    Serializer   ser;

    // Obtain a suitable output format for the XML method and
    // set the encoding.
    format = SerializerFactory.getOutputFormat( Method.XML );
    format.setEncoding( encoding );

    // Obtain a suitable serializer for the XML method and
    // set the output stream.
    ser = SerializerFactory.getSerializer( format );
    ser.setOutputStream( stream );

    // Use DOMSerializer to serialize the document
    ser.asDOMSerializer().serialize( doc );
  }
    

Serialize an empty HTML document using SAX events, reuse the serializer:

  Serializer ser;

  // Obtain an HTML serializer once, use it multiple times.
  ser = SerializerFactory.getSerializer( Method.HTML );
  printEmptyHTML( ser, System.out );
  printEmptyHTML( ser, System.err );
  . . . 

  void printEmptyHTML( Serializer ser, OutputStream os )
  {
    ser.setOutputStream( os );
    ser.asDocumentHandler().startDocument();
    ser.asDocumentHandler().startElement( "html", new AttributeListImpl() );
    ser.asDocumentHandler().endElement( "html" );
    ser.asDocumentHandler().endDocument();
    ser.reset();
  }
    

The Properties File:

An implementation will include a serializer properties file called serializer.properties located in the org.xml.serialize package. The properties file lists all the default serializers supported by that implementation. Serializers that are not listed in the properties file may be constructed directly by the application.

The properties file contains a property org.xml.serialize.methods listing all the output methods supported by the implementation (comma separated list). For each method a property org.xml.serialize.[method] names the class of the Serializer implementation. In addition a property org.xml.serialize.format.[method] names the class of a suitable OutputFormat implementation.