{@link 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 {@link OutputFormat} specifying the output properties. Serializer implementations may provide additional methods to control the way in which documents are serialized, or extend {@link OutputFormat} and offer additional output properties.
The {@link Serializer} and {@link 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 {@link 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 {@link 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 {@link 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 {@link OutputFormat} object, or directly when serializing SAX events through the {@link SerializerHandler} interface. Serializers are not required to implement the {@link SerializerHandler} interface.
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(); }
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 {@link Serializer} implementation. In addition a property org.xml.serialize.format.[method] names the class of a suitable {@link OutputFormat} implementation.