public class SerializingContentHandler extends DefaultContentHandler
ContentHandler
that serializes SAX events to a given
Result
instance. The JAXP SAXTransformerFactory
facility is used for the serialization.
This class explicitly ensures that all namespace prefixes are also present as xmlns attributes in the serialized XML document. This avoids problems with Xalan's serialization behaviour which was (at least during JDK 1.4) to ignore namespaces if they were not present as xmlns attributes.
NOTE: The code in this class was originally written for Apache Cocoon and is included with some modifications here in Apache Jackrabbit. See the org.apache.cocoon.serialization.AbstractTextSerializer class in the cocoon-pipeline-impl component for the original code.
Modifier and Type | Field and Description |
---|---|
static String |
ENCODING
The character encoding used for serialization (UTF-8).
|
Modifier and Type | Method and Description |
---|---|
void |
endDocument()
Delegated to
DefaultContentHandler.handler . |
void |
endElement(String eltUri,
String eltLocalName,
String eltQName)
Receive notification of the end of an element.
|
void |
endPrefixMapping(String prefix)
End the scope of a prefix-URI mapping:
remove entry from mapping tables.
|
static DefaultHandler |
getSerializer(OutputStream output)
Creates a serializing content handler that writes to the given stream.
|
static DefaultHandler |
getSerializer(Result result)
Creates a serializing content handler that writes to the given result.
|
static DefaultHandler |
getSerializer(Writer writer)
Creates a serializing content handler that writes to the given writer.
|
void |
startDocument()
Delegated to
DefaultContentHandler.handler . |
void |
startElement(String eltUri,
String eltLocalName,
String eltQName,
Attributes attrs)
Ensure all namespace declarations are present as
xmlns: attributes
and add those needed before calling superclass. |
void |
startPrefixMapping(String prefix,
String uri)
Track mappings to be able to add
xmlns: attributes
in startElement() . |
characters, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity
error, fatalError, notationDecl, resolveEntity, unparsedEntityDecl, warning
public static final String ENCODING
public static DefaultHandler getSerializer(OutputStream output) throws SAXException
output
- serialization targetSAXException
- if the content handler could not be initializedpublic static DefaultHandler getSerializer(Writer writer) throws SAXException
writer
- serialization targetSAXException
- if the content handler could not be initializedpublic static DefaultHandler getSerializer(Result result) throws SAXException
result
- serialization targetSAXException
- if the content handler could not be initializedpublic void startDocument() throws SAXException
DefaultContentHandler
DefaultContentHandler.handler
.startDocument
in interface ContentHandler
startDocument
in class DefaultContentHandler
SAXException
- if an error occurspublic void startPrefixMapping(String prefix, String uri) throws SAXException
xmlns:
attributes
in startElement()
.startPrefixMapping
in interface ContentHandler
startPrefixMapping
in class DefaultContentHandler
prefix
- passed throughuri
- passed throughSAXException
- if an error occurspublic void startElement(String eltUri, String eltLocalName, String eltQName, Attributes attrs) throws SAXException
xmlns:
attributes
and add those needed before calling superclass. This is a workaround for a Xalan bug
(at least in version 2.0.1) : org.apache.xalan.serialize.SerializerToXML
ignores start/endPrefixMapping()
.startElement
in interface ContentHandler
startElement
in class DefaultContentHandler
eltUri
- passed througheltLocalName
- passed througheltQName
- passed throughattrs
- passed throughSAXException
- if an error occurspublic void endElement(String eltUri, String eltLocalName, String eltQName) throws SAXException
endElement
in interface ContentHandler
endElement
in class DefaultContentHandler
eltUri
- passed througheltLocalName
- passed througheltQName
- passed throughSAXException
- if an error occurspublic void endPrefixMapping(String prefix) throws SAXException
endPrefixMapping
in interface ContentHandler
endPrefixMapping
in class DefaultContentHandler
prefix
- passed throughSAXException
- if an error occurspublic void endDocument() throws SAXException
DefaultContentHandler
DefaultContentHandler.handler
.endDocument
in interface ContentHandler
endDocument
in class DefaultContentHandler
SAXException
- if an error occursCopyright © 2004–2021 The Apache Software Foundation. All rights reserved.