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:
ContentHandler,DTDHandler,EntityResolver,ErrorHandler
A
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.
- Since:
- Jackrabbit JCR Commons 1.5
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidDelegated toDefaultContentHandler.handler.voidendElement(String eltUri, String eltLocalName, String eltQName) Receive notification of the end of an element.voidendPrefixMapping(String prefix) End the scope of a prefix-URI mapping: remove entry from mapping tables.static DefaultHandlergetSerializer(OutputStream output) Creates a serializing content handler that writes to the given stream.static DefaultHandlergetSerializer(Writer writer) Creates a serializing content handler that writes to the given writer.static DefaultHandlergetSerializer(Result result) Creates a serializing content handler that writes to the given result.voidDelegated toDefaultContentHandler.handler.voidstartElement(String eltUri, String eltLocalName, String eltQName, Attributes attrs) Ensure all namespace declarations are present asxmlns:attributes and add those needed before calling superclass.voidstartPrefixMapping(String prefix, 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, skippedEntityMethods inherited from class org.xml.sax.helpers.DefaultHandler
error, fatalError, notationDecl, resolveEntity, unparsedEntityDecl, warningMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandler
declaration
-
Field Details
-
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:
-
-
Method Details
-
getSerializer
Creates a serializing content handler that writes to the given stream.- Parameters:
output- serialization target- Returns:
- serializing content handler
- Throws:
SAXException- if the content handler could not be initialized
-
getSerializer
Creates a serializing content handler that writes to the given writer.- Parameters:
writer- serialization target- Returns:
- serializing content handler
- Throws:
SAXException- if the content handler could not be initialized
-
getSerializer
Creates a serializing content handler that writes to the given result.- Parameters:
result- serialization target- Returns:
- serializing content handler
- Throws:
SAXException- if the content handler could not be initialized
-
startDocument
Description copied from class:DefaultContentHandlerDelegated toDefaultContentHandler.handler.- Specified by:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classDefaultContentHandler- Throws:
SAXException- if an error occurs
-
startPrefixMapping
Track mappings to be able to addxmlns:attributes instartElement().- Specified by:
startPrefixMappingin interfaceContentHandler- Overrides:
startPrefixMappingin classDefaultContentHandler- Parameters:
prefix- passed throughuri- passed through- Throws:
SAXException- if an error occurs
-
startElement
public void startElement(String eltUri, String eltLocalName, String eltQName, Attributes attrs) throws 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.SerializerToXMLignoresstart/endPrefixMapping().- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultContentHandler- Parameters:
eltUri- passed througheltLocalName- passed througheltQName- passed throughattrs- passed through- Throws:
SAXException- if an error occurs
-
endElement
Receive notification of the end of an element. Try to restore the element qName.- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultContentHandler- Parameters:
eltUri- passed througheltLocalName- passed througheltQName- passed through- Throws:
SAXException- if an error occurs
-
endPrefixMapping
End the scope of a prefix-URI mapping: remove entry from mapping tables.- Specified by:
endPrefixMappingin interfaceContentHandler- Overrides:
endPrefixMappingin classDefaultContentHandler- Parameters:
prefix- passed through- Throws:
SAXException- if an error occurs
-
endDocument
Description copied from class:DefaultContentHandlerDelegated toDefaultContentHandler.handler.- Specified by:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classDefaultContentHandler- Throws:
SAXException- if an error occurs
-