org.apache.xml.serialize

Class XMLSerializer

Implemented Interfaces:
ContentHandler, DeclHandler, DocumentHandler, DTDHandler, LexicalHandler, DOMSerializer, Serializer
Known Direct Subclasses:
XML11Serializer

public class XMLSerializer
extends BaseMarkupSerializer

Implements an XML serializer supporting both DOM and SAX pretty serializing. For usage instructions see Serializer.

If an output stream is used, the encoding is taken from the output format (defaults to UTF-8). If a writer is used, make sure the writer uses the same encoding (if applies) as specified in the output format.

The serializer supports both DOM and SAX. SAX serializing is done by firing SAX events and using the serializer as a document handler. DOM serializing is done by calling serialize(Document) or by using DOM Level 3 org.w3c.dom.ls.LSSerializer and serializing with org.w3c.dom.ls.LSSerializer.write, org.w3c.dom.ls.LSSerializer.writeToString.

If an I/O exception occurs while serializing, the serializer will not throw an exception directly, but only throw it at the end of serializing (either DOM or SAX's org.xml.sax.DocumentHandler.endDocument.

For elements that are not specified as whitespace preserving, the serializer will potentially break long text lines at space boundaries, indent lines, and serialize elements on separate lines. Line terminators will be regarded as spaces, and spaces at beginning of line will be stripped.

Version:
$Revision: 476047 $ $Date: 2006-11-16 23:27:45 -0500 (Thu, 16 Nov 2006) $
Authors:
Assaf Arkin
Rahul Srivastava
Elena Litani IBM
See Also:
Serializer

Field Summary

protected static boolean
DEBUG
protected static String
PREFIX
protected org.apache.xerces.util.NamespaceSupport
fLocalNSBinder
stores all namespace bindings on the current element
protected org.apache.xerces.util.NamespaceSupport
fNSBinder
stores namespaces in scope
protected boolean
fNamespacePrefixes
Controls whether namespace prefixes will be printed out during serialization
protected boolean
fNamespaces
Controls whether namespace fixup should be performed during the serialization.
protected org.apache.xerces.util.SymbolTable
fSymbolTable
symbol table for serialization

Fields inherited from class org.apache.xml.serialize.BaseMarkupSerializer

_docTypePublicId, _docTypeSystemId, _encodingInfo, _format, _indenting, _prefixes, _printer, _started, fCurrentNode, fDOMError, fDOMErrorHandler, fDOMFilter, fStrBuffer, features

Constructor Summary

XMLSerializer()
Constructs a new serializer.
XMLSerializer(OutputStream output, OutputFormat format)
Constructs a new serializer that writes to the specified output stream using the specified output format.
XMLSerializer(Writer writer, OutputFormat format)
Constructs a new serializer that writes to the specified writer using the specified output format.
XMLSerializer(OutputFormat format)
Constructs a new serializer.

Method Summary

protected void
checkUnboundNamespacePrefixedNode(Node node)
DOM Level 3: Check a node to determine if it contains unbound namespace prefixes.
void
endElement(String tagName)
void
endElement(String namespaceURI, String localName, String rawName)
void
endElementIO(String namespaceURI, String localName, String rawName)
protected String
getEntityRef(int ch)
protected void
printEscaped(String source)
protected void
printText(String text, boolean preserveSpace, boolean unescaped)
protected void
printText(char[] chars, int start, int length, boolean preserveSpace, boolean unescaped)
protected void
printXMLChar(int ch)
print text data
boolean
reset()
protected void
serializeElement(Element elem)
Called to serialize a DOM element.
void
setNamespaces(boolean namespaces)
This methods turns on namespace fixup algorithm during DOM serialization.
void
setOutputFormat(OutputFormat format)
protected void
startDocument(String rootTagName)
Called to serialize the document's DOCTYPE by the root element.
void
startElement(String tagName, AttributeList attrs)
void
startElement(String namespaceURI, String localName, String rawName, Attributes attrs)

Methods inherited from class org.apache.xml.serialize.BaseMarkupSerializer

asContentHandler, asDOMSerializer, asDocumentHandler, attributeDecl, characters, characters, checkUnboundNamespacePrefixedNode, comment, comment, content, elementDecl, endCDATA, endDTD, endDocument, endEntity, endNonEscaping, endPrefixMapping, endPreserving, enterElementState, externalEntityDecl, fatalError, getElementState, getEntityRef, getPrefix, ignorableWhitespace, internalEntityDecl, isDocumentState, leaveElementState, modifyDOMError, notationDecl, prepare, printCDATAText, printDoctypeURL, printEscaped, printEscaped, printText, printText, processingInstruction, processingInstructionIO, reset, serialize, serialize, serialize, serializeElement, serializeNode, serializePreRoot, setDocumentLocator, setOutputByteStream, setOutputCharStream, setOutputFormat, skippedEntity, startCDATA, startDTD, startDocument, startEntity, startNonEscaping, startPrefixMapping, startPreserving, surrogates, unparsedEntityDecl

Field Details

DEBUG

protected static final boolean DEBUG
Field Value:
false

PREFIX

protected static final String PREFIX

fLocalNSBinder

protected org.apache.xerces.util.NamespaceSupport fLocalNSBinder
stores all namespace bindings on the current element

fNSBinder

protected org.apache.xerces.util.NamespaceSupport fNSBinder
stores namespaces in scope

fNamespacePrefixes

protected boolean fNamespacePrefixes
Controls whether namespace prefixes will be printed out during serialization

fNamespaces

protected boolean fNamespaces
Controls whether namespace fixup should be performed during the serialization. NOTE: if this field is set to true the following fields need to be initialized: fNSBinder, fLocalNSBinder, fSymbolTable, XMLSymbols.EMPTY_STRING, fXmlSymbol, fXmlnsSymbol

fSymbolTable

protected org.apache.xerces.util.SymbolTable fSymbolTable
symbol table for serialization

Constructor Details

XMLSerializer

public XMLSerializer()
Constructs a new serializer. The serializer cannot be used without calling XMLSerializer or XMLSerializer first.

XMLSerializer

public XMLSerializer(OutputStream output,
                     OutputFormat format)
Constructs a new serializer that writes to the specified output stream using the specified output format. If format is null, will use a default output format.
Parameters:
output - The output stream to use
format - The output format to use, null for the default

XMLSerializer

public XMLSerializer(Writer writer,
                     OutputFormat format)
Constructs a new serializer that writes to the specified writer using the specified output format. If format is null, will use a default output format.
Parameters:
writer - The writer to use
format - The output format to use, null for the default

XMLSerializer

public XMLSerializer(OutputFormat format)
Constructs a new serializer. The serializer cannot be used without calling XMLSerializer or XMLSerializer first.

Method Details

checkUnboundNamespacePrefixedNode

protected void checkUnboundNamespacePrefixedNode(Node node)
            throws IOException
DOM Level 3: Check a node to determine if it contains unbound namespace prefixes.
Overrides:
checkUnboundNamespacePrefixedNode in interface BaseMarkupSerializer
Parameters:
node - The node to check for unbound namespace prefices

endElement

public void endElement(String tagName)
            throws SAXException

endElement

public void endElement(String namespaceURI,
                       String localName,
                       String rawName)
            throws SAXException

endElementIO

public void endElementIO(String namespaceURI,
                         String localName,
                         String rawName)
            throws IOException

getEntityRef

protected String getEntityRef(int ch)
Overrides:
getEntityRef in interface BaseMarkupSerializer

printEscaped

protected void printEscaped(String source)
            throws IOException
Overrides:
printEscaped in interface BaseMarkupSerializer

printText

protected void printText(String text,
                         boolean preserveSpace,
                         boolean unescaped)
            throws IOException
Overrides:
printText in interface BaseMarkupSerializer

printText

protected void printText(char[] chars,
                         int start,
                         int length,
                         boolean preserveSpace,
                         boolean unescaped)
            throws IOException
Overrides:
printText in interface BaseMarkupSerializer

printXMLChar

protected void printXMLChar(int ch)
            throws IOException
print text data

reset

public boolean reset()
Overrides:
reset in interface BaseMarkupSerializer

serializeElement

protected void serializeElement(Element elem)
            throws IOException
Called to serialize a DOM element. Equivalent to calling startElement, endElement and serializing everything inbetween, but better optimized.
Overrides:
serializeElement in interface BaseMarkupSerializer

setNamespaces

public void setNamespaces(boolean namespaces)
This methods turns on namespace fixup algorithm during DOM serialization.
Parameters:
namespaces -
See Also:
org.w3c.dom.ls.LSSerializer

setOutputFormat

public void setOutputFormat(OutputFormat format)
Specified by:
setOutputFormat in interface Serializer
Overrides:
setOutputFormat in interface BaseMarkupSerializer

startDocument

protected void startDocument(String rootTagName)
            throws IOException
Called to serialize the document's DOCTYPE by the root element. The document type declaration must name the root element, but the root element is only known when that element is serialized, and not at the start of the document.

This method will check if it has not been called before (BaseMarkupSerializer._started), will serialize the document type declaration, and will serialize all pre-root comments and PIs that were accumulated in the document (see XMLSerializer). Pre-root will be serialized even if this is not the first root element of the document.


startElement

public void startElement(String tagName,
                         AttributeList attrs)
            throws SAXException

startElement

public void startElement(String namespaceURI,
                         String localName,
                         String rawName,
                         Attributes attrs)
            throws SAXException

Copyright © 1999-2007 The Apache Software Foundation. All Rights Reserved.