org.apache.jackrabbit.commons.xml
Class SerializingContentHandler

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.apache.jackrabbit.commons.xml.DefaultContentHandler
          extended by org.apache.jackrabbit.commons.xml.SerializingContentHandler
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler

public class SerializingContentHandler
extends DefaultContentHandler

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
static String ENCODING
          The character encoding used for serialization (UTF-8).
 
Method Summary
 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().
 
Methods inherited from class org.apache.jackrabbit.commons.xml.DefaultContentHandler
characters, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
error, fatalError, notationDecl, resolveEntity, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCODING

public static final 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:
https://issues.apache.org/jira/browse/JCR-1621, Constant Field Values
Method Detail

getSerializer

public static DefaultHandler getSerializer(OutputStream output)
                                    throws SAXException
Creates a serializing content handler that writes to the given stream.

Parameters:
stream - serialization target
Returns:
serializing content handler
Throws:
SAXException - if the content handler could not be initialized

getSerializer

public static DefaultHandler getSerializer(Writer writer)
                                    throws SAXException
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

public static DefaultHandler getSerializer(Result result)
                                    throws SAXException
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

public void startDocument()
                   throws SAXException
Description copied from class: DefaultContentHandler
Delegated to DefaultContentHandler.handler.

Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class DefaultContentHandler
Throws:
SAXException - if an error occurs

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String uri)
                        throws SAXException
Track mappings to be able to add xmlns: attributes in startElement().

Specified by:
startPrefixMapping in interface ContentHandler
Overrides:
startPrefixMapping in class DefaultContentHandler
Parameters:
prefix - passed through
uri - 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 as 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().

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultContentHandler
Parameters:
eltUri - passed through
eltLocalName - passed through
eltQName - passed through
attrs - passed through
Throws:
SAXException - if an error occurs

endElement

public void endElement(String eltUri,
                       String eltLocalName,
                       String eltQName)
                throws SAXException
Receive notification of the end of an element. Try to restore the element qName.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultContentHandler
Parameters:
eltUri - passed through
eltLocalName - passed through
eltQName - passed through
Throws:
SAXException - if an error occurs

endPrefixMapping

public void endPrefixMapping(String prefix)
                      throws SAXException
End the scope of a prefix-URI mapping: remove entry from mapping tables.

Specified by:
endPrefixMapping in interface ContentHandler
Overrides:
endPrefixMapping in class DefaultContentHandler
Parameters:
prefix - passed through
Throws:
SAXException - if an error occurs

endDocument

public void endDocument()
                 throws SAXException
Description copied from class: DefaultContentHandler
Delegated to DefaultContentHandler.handler.

Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class DefaultContentHandler
Throws:
SAXException - if an error occurs


Copyright © 2004-2010 The Apache Software Foundation. All Rights Reserved.