org.apache.xml.serializer

Class SerializerBase

Implemented Interfaces:
ContentHandler, DeclHandler, DOM2DTM.CharacterNodeHandler, DOMSerializer, ErrorHandler, ExtendedContentHandler, ExtendedLexicalHandler, LexicalHandler, SerializationHandler, Serializer, SerializerConstants, XSLOutputAttributes
Known Direct Subclasses:
ToSAXHandler, ToStream, ToUnknownStream

public abstract class SerializerBase
extends java.lang.Object
implements SerializationHandler, SerializerConstants, DOM2DTM.CharacterNodeHandler

This class acts as a base class for the XML "serializers" and the stream serializers. It contains a number of common fields and methods.
Authors:
Santiago Pericas-Geertsen
G. Todd Miller

Fields inherited from interface org.apache.xml.serializer.ExtendedContentHandler

HTML_ATTREMPTY, HTML_ATTRURL, NO_BAD_CHARS

Fields inherited from interface org.apache.xml.serializer.SerializerConstants

BEGCDATA, CDATA_CONTINUE, CDATA_DELIMITER_CLOSE, CDATA_DELIMITER_OPEN, CNTCDATA, DEFAULT_SAX_SERIALIZER, EMPTYSTRING, ENDCDATA, ENTITY_AMP, ENTITY_CRLF, ENTITY_GT, ENTITY_LT, ENTITY_QUOT, XMLNS_PREFIX, XMLNS_URI, XML_PREFIX

Method Summary

void
addAttribute(String name, String value)
Adds the given attribute to the set of collected attributes, but only if there is a currently open element.
void
addAttribute(String uri, String localName, String rawName, String type, String value)
Adds the given attribute to the set of collected attributes , but only if there is a currently open element.
void
addAttributeAlways(String uri, String localName, String rawName, String type, String value)
Adds the given attribute to the set of attributes, even if there is no currently open element.
void
addAttributes(Attributes atts)
Add the given attributes to the currently collected ones.
ContentHandler
asContentHandler()
Return a ContentHandler interface into this serializer.
DOMSerializer
asDOMSerializer()
Return a DOMSerializer interface into this serializer.
void
characters(Node node)
This method gets the nodes value as a String and uses that String as if it were an input character notification.
void
close()
Flush and close the underlying java.io.Writer.
void
comment(String data)
Receive notification of a comment.
void
endEntity(String name)
Report the end of an entity.
void
entityReference(String name)
Entity reference event.
void
error(SAXParseException exc)
void
fatalError(SAXParseException exc)
void
fireEndEntity(String name)
To fire off end entity trace event
String
getDoctypePublic()
Returns the previously set value of the value to be used as the public identifier in the document type declaration (DTD).
String
getDoctypeSystem()
Returns the previously set value of the value to be used as the system identifier in the document type declaration (DTD).
String
getEncoding()
Returns the character encoding to be used in the output document.
boolean
getIndent()
int
getIndentAmount()
String
getMediaType()
Gets the mediatype the media-type or MIME type associated with the output document.
NamespaceMappings
getNamespaceMappings()
Some users of the serializer may need the current namespace mappings
String
getNamespaceURI(String qname, boolean isElement)
Returns the URI of an element or attribute.
String
getNamespaceURIFromPrefix(String prefix)
Returns the URI of prefix (if any)
boolean
getOmitXMLDeclaration()
String
getPrefix(String namespaceURI)
Returns the prefix currently pointing to the given URI (if any).
String
getStandalone()
Gets the XSL standalone attribute
Transformer
getTransformer()
Gets the transformer associated with this serializer
String
getVersion()
Gets the version of the output format.
void
namespaceAfterStartElement(String uri, String prefix)
This method is used when a prefix/uri namespace mapping is indicated after the element was started with a startElement() and before and endElement().
boolean
reset()
Resets the serializer.
void
setDoctype(String doctypeSystem, String doctypePublic)
Set the value coming from the xsl:output doctype-public and doctype-system stylesheet properties
void
setDoctypePublic(String doctypePublic)
Set the value coming from the xsl:output doctype-public stylesheet attribute.
void
setDoctypeSystem(String doctypeSystem)
Set the value coming from the xsl:output doctype-system stylesheet attribute.
void
setDocumentLocator(Locator locator)
Receive an object for locating the origin of SAX document events.
void
setEncoding(String m_encoding)
Sets the character encoding coming from the xsl:output encoding stylesheet attribute.
void
setIndent(boolean doIndent)
Sets the value coming from the xsl:output indent stylesheet attribute.
void
setIndentAmount(int m_indentAmount)
Sets the indentation amount.
void
setMediaType(String mediaType)
Sets the value coming from the xsl:output media-type stylesheet attribute.
void
setNamespaceMappings(NamespaceMappings mappings)
Used only by TransformerSnapshotImpl to restore the serialization to a previous state.
void
setOmitXMLDeclaration(boolean b)
Sets the value coming from the xsl:output omit-xml-declaration stylesheet attribute
void
setSourceLocator(SourceLocator locator)
This method is used to set the source locator, which might be used to generated an error message.
void
setStandalone(String standalone)
Sets the value coming from the xsl:output standalone stylesheet attribute.
void
setTransformer(Transformer t)
Sets the transformer associated with this serializer
void
setVersion(String version)
Sets the value coming from the xsl:output version attribute.
void
startDocument()
Receive notification of the beginning of a document.
void
warning(SAXParseException exc)

Method Details

addAttribute

public void addAttribute(String name,
                         String value)
Adds the given attribute to the set of collected attributes, but only if there is a currently open element. This method is only called by XSLTC.
Specified by:
addAttribute in interface ExtendedContentHandler
Parameters:
name - the attribute's qualified name
value - the value of the attribute

addAttribute

public void addAttribute(String uri,
                         String localName,
                         String rawName,
                         String type,
                         String value)
            throws SAXException
Adds the given attribute to the set of collected attributes , but only if there is a currently open element. An element is currently open if a startElement() notification has occured but the start of the element has not yet been written to the output. In the stream case this means that we have not yet been forced to close the elements opening tag by another notification, such as a character notification.
Specified by:
addAttribute in interface ExtendedContentHandler
Parameters:
uri - the URI of the attribute
localName - the local name of the attribute
rawName - the qualified name of the attribute
type - the type of the attribute (probably CDATA)
value - the value of the attribute

addAttributeAlways

public void addAttributeAlways(String uri,
                               String localName,
                               String rawName,
                               String type,
                               String value)
Adds the given attribute to the set of attributes, even if there is no currently open element. This is useful if a SAX startPrefixMapping() should need to add an attribute before the element name is seen.
Parameters:
uri - the URI of the attribute
localName - the local name of the attribute
rawName - the qualified name of the attribute
type - the type of the attribute (probably CDATA)
value - the value of the attribute

addAttributes

public void addAttributes(Attributes atts)
            throws SAXException
Add the given attributes to the currently collected ones. These attributes are always added, regardless of whether on not an element is currently open.
Specified by:
addAttributes in interface ExtendedContentHandler
Parameters:
atts - List of attributes to add to this list

asContentHandler

public ContentHandler asContentHandler()
            throws IOException
Return a ContentHandler interface into this serializer. If the serializer does not support the ContentHandler interface, it should return null.
Specified by:
asContentHandler in interface Serializer
Returns:
A ContentHandler interface into this serializer, or null if the serializer is not SAX 2 capable

asDOMSerializer

public DOMSerializer asDOMSerializer()
            throws IOException
Return a DOMSerializer interface into this serializer. If the serializer does not support the DOMSerializer interface, it should return null.
Specified by:
asDOMSerializer in interface Serializer
Returns:
A DOMSerializer interface into this serializer, or null if the serializer is not DOM capable

characters

public void characters(Node node)
            throws SAXException
This method gets the nodes value as a String and uses that String as if it were an input character notification.
Specified by:
characters in interface DOM2DTM.CharacterNodeHandler
Parameters:
node - the Node to serialize
Throws:
SAXException -

close

public void close()
Flush and close the underlying java.io.Writer. This method applies to ToStream serializers, not ToSAXHandler serializers.
Specified by:
close in interface SerializationHandler
See Also:
ToStream

comment

public void comment(String data)
            throws SAXException
Receive notification of a comment.
Specified by:
comment in interface ExtendedLexicalHandler

endEntity

public void endEntity(String name)
            throws SAXException
Report the end of an entity.
Specified by:
endEntity in interface LexicalHandler
Parameters:
name - The name of the entity that is ending.
Throws:
SAXException - The application may raise an exception.
See Also:
SerializerBase

entityReference

public void entityReference(String name)
            throws SAXException
Entity reference event.
Specified by:
entityReference in interface ExtendedContentHandler
Parameters:
name - Name of entity
Throws:
SAXException -

error

public void error(SAXParseException exc)
            throws SAXException
Specified by:
error in interface ErrorHandler

fatalError

public void fatalError(SAXParseException exc)
            throws SAXException
Specified by:
fatalError in interface ErrorHandler

fireEndEntity

public void fireEndEntity(String name)
            throws SAXException
To fire off end entity trace event
Parameters:
name - Name of entity

getDoctypePublic

public String getDoctypePublic()
Returns the previously set value of the value to be used as the public identifier in the document type declaration (DTD).
Specified by:
getDoctypePublic in interface XSLOutputAttributes
Returns:
the public identifier to be used in the DOCTYPE declaration in the output document.

getDoctypeSystem

public String getDoctypeSystem()
Returns the previously set value of the value to be used as the system identifier in the document type declaration (DTD).
Specified by:
getDoctypeSystem in interface XSLOutputAttributes
Returns:
the system identifier to be used in the DOCTYPE declaration in the output document.

getEncoding

public String getEncoding()
Returns the character encoding to be used in the output document.
Specified by:
getEncoding in interface XSLOutputAttributes
Returns:
the character encoding to be used in the output document.

getIndent

public boolean getIndent()
Specified by:
getIndent in interface XSLOutputAttributes
Returns:
true if the output document should be indented to visually indicate its structure.

getIndentAmount

public int getIndentAmount()
Specified by:
getIndentAmount in interface XSLOutputAttributes
Returns:
the number of spaces to indent for each indentation level.

getMediaType

public String getMediaType()
Gets the mediatype the media-type or MIME type associated with the output document.
Specified by:
getMediaType in interface XSLOutputAttributes
Returns:
the mediatype the media-type or MIME type associated with the output document.

getNamespaceMappings

public NamespaceMappings getNamespaceMappings()
Some users of the serializer may need the current namespace mappings
Specified by:
getNamespaceMappings in interface ExtendedContentHandler
Returns:
the current namespace mappings (prefix/uri)

getNamespaceURI

public String getNamespaceURI(String qname,
                              boolean isElement)
Returns the URI of an element or attribute. Note that default namespaces do not apply directly to attributes.
Specified by:
getNamespaceURI in interface ExtendedContentHandler
Parameters:
qname - a qualified name
isElement - true if the qualified name is the name of an element.
Returns:
returns the namespace URI associated with the qualified name.

getNamespaceURIFromPrefix

public String getNamespaceURIFromPrefix(String prefix)
Returns the URI of prefix (if any)
Specified by:
getNamespaceURIFromPrefix in interface ExtendedContentHandler
Parameters:
prefix - the prefix whose URI is searched for
Returns:
the namespace URI currently associated with the prefix, null if the prefix is undefined.

getOmitXMLDeclaration

public boolean getOmitXMLDeclaration()
Specified by:
getOmitXMLDeclaration in interface XSLOutputAttributes
Returns:
true if the XML declaration is to be omitted from the output document.

getPrefix

public String getPrefix(String namespaceURI)
Returns the prefix currently pointing to the given URI (if any).
Specified by:
getPrefix in interface ExtendedContentHandler
Parameters:
namespaceURI - the uri of the namespace in question
Returns:
a prefix pointing to the given URI (if any).

getStandalone

public String getStandalone()
Gets the XSL standalone attribute
Specified by:
getStandalone in interface XSLOutputAttributes
Returns:
a value of "yes" if the standalone delaration is to be included in the output document.

getTransformer

public Transformer getTransformer()
Gets the transformer associated with this serializer
Specified by:
getTransformer in interface SerializationHandler
Returns:
returns the transformer associated with this serializer.

getVersion

public String getVersion()
Gets the version of the output format.
Specified by:
getVersion in interface XSLOutputAttributes
Returns:
the version of the output format.

namespaceAfterStartElement

public void namespaceAfterStartElement(String uri,
                                       String prefix)
            throws SAXException
This method is used when a prefix/uri namespace mapping is indicated after the element was started with a startElement() and before and endElement(). startPrefixMapping(prefix,uri) would be used before the startElement() call.
Specified by:
namespaceAfterStartElement in interface ExtendedContentHandler
Parameters:
uri - the URI of the namespace
prefix - the prefix associated with the given URI.

reset

public boolean reset()
Resets the serializer. If this method returns true, the serializer may be used for subsequent serialization of new documents. It is possible to change the output format and output stream prior to serializing, or to use the existing output format and output stream.
Specified by:
reset in interface Serializer
Returns:
True if serializer has been reset and can be reused

setDoctype

public void setDoctype(String doctypeSystem,
                       String doctypePublic)
Set the value coming from the xsl:output doctype-public and doctype-system stylesheet properties
Specified by:
setDoctype in interface XSLOutputAttributes
Parameters:
doctypeSystem - the system identifier to be used in the DOCTYPE declaration in the output document.
doctypePublic - the public identifier to be used in the DOCTYPE declaration in the output document.

setDoctypePublic

public void setDoctypePublic(String doctypePublic)
Set the value coming from the xsl:output doctype-public stylesheet attribute.
Specified by:
setDoctypePublic in interface XSLOutputAttributes
Parameters:
doctypePublic - the public identifier to be used in the DOCTYPE declaration in the output document.

setDoctypeSystem

public void setDoctypeSystem(String doctypeSystem)
Set the value coming from the xsl:output doctype-system stylesheet attribute.
Specified by:
setDoctypeSystem in interface XSLOutputAttributes
Parameters:
doctypeSystem - the system identifier to be used in the DOCTYPE declaration in the output document.

setDocumentLocator

public void setDocumentLocator(Locator locator)
Receive an object for locating the origin of SAX document events.
Specified by:
setDocumentLocator in interface ContentHandler
Parameters:
locator - An object that can return the location of any SAX document event. Receive an object for locating the origin of SAX document events.

SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the DocumentHandler interface.

The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application's business rules). The information returned by the locator is probably not sufficient for use with a search engine.

Note that the locator will return correct information only during the invocation of the events in this interface. The application should not attempt to use it at any other time.


setEncoding

public void setEncoding(String m_encoding)
Sets the character encoding coming from the xsl:output encoding stylesheet attribute.
Specified by:
setEncoding in interface XSLOutputAttributes
Parameters:

setIndent

public void setIndent(boolean doIndent)
Sets the value coming from the xsl:output indent stylesheet attribute.
Specified by:
setIndent in interface XSLOutputAttributes
Parameters:
doIndent - true if the output document should be indented to visually indicate its structure.

setIndentAmount

public void setIndentAmount(int m_indentAmount)
Sets the indentation amount.
Specified by:
setIndentAmount in interface SerializationHandler
Parameters:
m_indentAmount - The m_indentAmount to set

setMediaType

public void setMediaType(String mediaType)
Sets the value coming from the xsl:output media-type stylesheet attribute.
Specified by:
setMediaType in interface XSLOutputAttributes
Parameters:
mediaType - the non-null media-type or MIME type associated with the output document.
See Also:
OutputKeys.MEDIA_TYPE, org.apache.xml.serializer.SerializationHandler.setMediaType(String)

setNamespaceMappings

public void setNamespaceMappings(NamespaceMappings mappings)
Used only by TransformerSnapshotImpl to restore the serialization to a previous state.
Specified by:
setNamespaceMappings in interface SerializationHandler
Parameters:

setOmitXMLDeclaration

public void setOmitXMLDeclaration(boolean b)
Sets the value coming from the xsl:output omit-xml-declaration stylesheet attribute
Specified by:
setOmitXMLDeclaration in interface XSLOutputAttributes
Parameters:
b - true if the XML declaration is to be omitted from the output document.

setSourceLocator

public void setSourceLocator(SourceLocator locator)
This method is used to set the source locator, which might be used to generated an error message.
Specified by:
setSourceLocator in interface ExtendedContentHandler
Parameters:
locator - the source locator

setStandalone

public void setStandalone(String standalone)
Sets the value coming from the xsl:output standalone stylesheet attribute.
Specified by:
setStandalone in interface XSLOutputAttributes
Parameters:
standalone - a value of "yes" indicates that the standalone delaration is to be included in the output document. This method remembers if the value was explicitly set using this method, verses if the value is the default value.

setTransformer

public void setTransformer(Transformer t)
Sets the transformer associated with this serializer
Specified by:
setTransformer in interface SerializationHandler
Parameters:
t - the transformer associated with this serializer.

setVersion

public void setVersion(String version)
Sets the value coming from the xsl:output version attribute.
Specified by:
setVersion in interface XSLOutputAttributes
Parameters:
version - the version of the output format.
See Also:
org.apache.xml.serializer.SerializationHandler.setVersion(String)

startDocument

public void startDocument()
            throws SAXException
Receive notification of the beginning of a document. This method is never a self generated call, but only called externally.

The SAX parser will invoke this method only once, before any other methods in this interface or in DTDHandler (except for setDocumentLocator).

Specified by:
startDocument in interface ContentHandler
Throws:
SAXException - Any SAX exception, possibly wrapping another exception.
SAXException -

warning

public void warning(SAXParseException exc)
            throws SAXException
Specified by:
warning in interface ErrorHandler

Copyright B) 2004 Apache XML Project. All Rights Reserved.