Class ToXmlContentHandler
java.lang.Object
org.xml.sax.helpers.DefaultHandler
org.apache.jackrabbit.commons.xml.ToXmlContentHandler
- All Implemented Interfaces:
ContentHandler,DTDHandler,EntityResolver,ErrorHandler
Simple XML serializer. This content handler serializes the received
SAX events as XML to a given
Writer or OutputStream.
The serialization assumes that the incoming SAX events are well-formed,
i.e. that all elements are properly nested, that element and attribute
names are valid and that no invalid XML characters are included. Assuming
these preconditions are met, the result will be a well-formed XML stream.
This serializer does not have any special support for namespaces. For
example, namespace prefixes are declared in the resulting XML stream
if and only if the corresponding "xmlns" attributes are explicitly
included in the Attributes instances passed in
startElement(String, String, String, Attributes) calls.
As a convenience this class inherits the DefaultHandler class
instead of just the ContentHandler interface. This makes it
easier to pass instances of this class to methods like
SAXParser.parse(String, DefaultHandler) that
expect a DefaultHandler instance instead of a ContentHandler.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an XML serializer that writes the serialized XML stream to an internal buffer.ToXmlContentHandler(OutputStream stream) Creates an XML serializer that writes the serialized XML stream to the given output stream using the UTF-8 character encoding.ToXmlContentHandler(OutputStream stream, String encoding) Creates an XML serializer that writes the serialized XML stream to the given output stream using the given character encoding.ToXmlContentHandler(Writer writer) Creates an XML serializer that writes the serialized XML stream to the given writer. -
Method Summary
Modifier and TypeMethodDescriptionvoidcharacters(char[] ch, int start, int length) Escapes and outputs the given characters.voidEnds the XML serialization by flushing the output stream.voidendElement(String namespaceURI, String localName, String qName) Outputs the specified end tag.voidignorableWhitespace(char[] ch, int start, int length) Escapes and outputs the given characters.voidprocessingInstruction(String target, String data) Serializes a processing instruction.voidStarts the XML serialization by outputting the <?xml?> header.voidstartElement(String namespaceURI, String localName, String qName, Attributes atts) Outputs the specified start tag with the given attributes.toString()Returns the serialized XML document (assuming the default no-argument constructor was used).Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, error, fatalError, notationDecl, resolveEntity, setDocumentLocator, skippedEntity, startPrefixMapping, unparsedEntityDecl, warningMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandler
declaration
-
Constructor Details
-
ToXmlContentHandler
public ToXmlContentHandler(OutputStream stream, String encoding) throws UnsupportedEncodingException Creates an XML serializer that writes the serialized XML stream to the given output stream using the given character encoding.- Parameters:
stream- XML output streamencoding- character encoding- Throws:
UnsupportedEncodingException- if the encoding is not supported
-
ToXmlContentHandler
Creates an XML serializer that writes the serialized XML stream to the given output stream using the UTF-8 character encoding.- Parameters:
stream- XML output stream
-
ToXmlContentHandler
Creates an XML serializer that writes the serialized XML stream to the given writer.- Parameters:
writer- XML output stream
-
ToXmlContentHandler
public ToXmlContentHandler()Creates an XML serializer that writes the serialized XML stream to an internal buffer. Use thetoString()method to access the serialized XML document.
-
-
Method Details
-
startDocument
Starts the XML serialization by outputting the <?xml?> header.- Specified by:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classDefaultHandler- Throws:
SAXException
-
endDocument
Ends the XML serialization by flushing the output stream.- Specified by:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classDefaultHandler- Throws:
SAXException
-
processingInstruction
Serializes a processing instruction.- Specified by:
processingInstructionin interfaceContentHandler- Overrides:
processingInstructionin classDefaultHandler- Throws:
SAXException
-
startElement
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException Outputs the specified start tag with the given attributes.- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler- Throws:
SAXException
-
characters
Escapes and outputs the given characters.- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler- Throws:
SAXException
-
ignorableWhitespace
Escapes and outputs the given characters.- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classDefaultHandler- Throws:
SAXException
-
endElement
Outputs the specified end tag.- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler- Throws:
SAXException
-
toString
Returns the serialized XML document (assuming the default no-argument constructor was used).
-