Class SerializingContentHandler
- java.lang.Object
-
- org.xml.sax.helpers.DefaultHandler
-
- org.apache.jackrabbit.commons.xml.DefaultContentHandler
-
- org.apache.jackrabbit.commons.xml.SerializingContentHandler
-
- All Implemented Interfaces:
org.xml.sax.ContentHandler
,org.xml.sax.DTDHandler
,org.xml.sax.EntityResolver
,org.xml.sax.ErrorHandler
public class SerializingContentHandler extends DefaultContentHandler
AContentHandler
that serializes SAX events to a givenResult
instance. The JAXPSAXTransformerFactory
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.
- Since:
- Jackrabbit JCR Commons 1.5
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ENCODING
The character encoding used for serialization (UTF-8).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
endDocument()
Delegated toDefaultContentHandler.handler
.void
endElement(java.lang.String eltUri, java.lang.String eltLocalName, java.lang.String eltQName)
Receive notification of the end of an element.void
endPrefixMapping(java.lang.String prefix)
End the scope of a prefix-URI mapping: remove entry from mapping tables.static org.xml.sax.helpers.DefaultHandler
getSerializer(java.io.OutputStream output)
Creates a serializing content handler that writes to the given stream.static org.xml.sax.helpers.DefaultHandler
getSerializer(java.io.Writer writer)
Creates a serializing content handler that writes to the given writer.static org.xml.sax.helpers.DefaultHandler
getSerializer(javax.xml.transform.Result result)
Creates a serializing content handler that writes to the given result.void
startDocument()
Delegated toDefaultContentHandler.handler
.void
startElement(java.lang.String eltUri, java.lang.String eltLocalName, java.lang.String eltQName, org.xml.sax.Attributes attrs)
Ensure all namespace declarations are present asxmlns:
attributes and add those needed before calling superclass.void
startPrefixMapping(java.lang.String prefix, java.lang.String uri)
Track mappings to be able to addxmlns:
attributes instartElement()
.-
Methods inherited from class org.apache.jackrabbit.commons.xml.DefaultContentHandler
characters, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity
-
-
-
-
Field Detail
-
ENCODING
public static final java.lang.String ENCODING
The character encoding used for serialization (UTF-8). The encoding is fixed to make the text/xml content type safer to use.- See Also:
- JCR-1621, Constant Field Values
-
-
Method Detail
-
getSerializer
public static org.xml.sax.helpers.DefaultHandler getSerializer(java.io.OutputStream output) throws org.xml.sax.SAXException
Creates a serializing content handler that writes to the given stream.- Parameters:
output
- serialization target- Returns:
- serializing content handler
- Throws:
org.xml.sax.SAXException
- if the content handler could not be initialized
-
getSerializer
public static org.xml.sax.helpers.DefaultHandler getSerializer(java.io.Writer writer) throws org.xml.sax.SAXException
Creates a serializing content handler that writes to the given writer.- Parameters:
writer
- serialization target- Returns:
- serializing content handler
- Throws:
org.xml.sax.SAXException
- if the content handler could not be initialized
-
getSerializer
public static org.xml.sax.helpers.DefaultHandler getSerializer(javax.xml.transform.Result result) throws org.xml.sax.SAXException
Creates a serializing content handler that writes to the given result.- Parameters:
result
- serialization target- Returns:
- serializing content handler
- Throws:
org.xml.sax.SAXException
- if the content handler could not be initialized
-
startDocument
public void startDocument() throws org.xml.sax.SAXException
Description copied from class:DefaultContentHandler
Delegated toDefaultContentHandler.handler
.- Specified by:
startDocument
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startDocument
in classDefaultContentHandler
- Throws:
org.xml.sax.SAXException
- if an error occurs
-
startPrefixMapping
public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) throws org.xml.sax.SAXException
Track mappings to be able to addxmlns:
attributes instartElement()
.- Specified by:
startPrefixMapping
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startPrefixMapping
in classDefaultContentHandler
- Parameters:
prefix
- passed throughuri
- passed through- Throws:
org.xml.sax.SAXException
- if an error occurs
-
startElement
public void startElement(java.lang.String eltUri, java.lang.String eltLocalName, java.lang.String eltQName, org.xml.sax.Attributes attrs) throws org.xml.sax.SAXException
Ensure all namespace declarations are present asxmlns:
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
ignoresstart/endPrefixMapping()
.- Specified by:
startElement
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startElement
in classDefaultContentHandler
- Parameters:
eltUri
- passed througheltLocalName
- passed througheltQName
- passed throughattrs
- passed through- Throws:
org.xml.sax.SAXException
- if an error occurs
-
endElement
public void endElement(java.lang.String eltUri, java.lang.String eltLocalName, java.lang.String eltQName) throws org.xml.sax.SAXException
Receive notification of the end of an element. Try to restore the element qName.- Specified by:
endElement
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endElement
in classDefaultContentHandler
- Parameters:
eltUri
- passed througheltLocalName
- passed througheltQName
- passed through- Throws:
org.xml.sax.SAXException
- if an error occurs
-
endPrefixMapping
public void endPrefixMapping(java.lang.String prefix) throws org.xml.sax.SAXException
End the scope of a prefix-URI mapping: remove entry from mapping tables.- Specified by:
endPrefixMapping
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endPrefixMapping
in classDefaultContentHandler
- Parameters:
prefix
- passed through- Throws:
org.xml.sax.SAXException
- if an error occurs
-
endDocument
public void endDocument() throws org.xml.sax.SAXException
Description copied from class:DefaultContentHandler
Delegated toDefaultContentHandler.handler
.- Specified by:
endDocument
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endDocument
in classDefaultContentHandler
- Throws:
org.xml.sax.SAXException
- if an error occurs
-
-