|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.xml.sax.helpers.DefaultHandler
cc.xml.PrettyPrinter
public class PrettyPrinter
This class "pretty prints" an XML stream to something more human-readable.
It duplicates the character content with some modifications to whitespace,
restoring line breaks and a simple pattern of indenting child elements.
This version of the class acts as a SAX 2.0 DefaultHandler,
so to provide the unformatted XML just pass a new instance to a SAX parser.
Its output is via the toString method.
One major limitation: we gather character data for elements in a single
buffer, so mixed-content documents will lose a lot of data! This works
best with data-centric documents where elements either have single values
or child elements, but not both.
| Nested Class Summary | |
|---|---|
static class |
PrettyPrinter.StreamAdapter
|
| Field Summary | |
|---|---|
private java.lang.StringBuffer |
currentValue
A buffer for character data. |
private static java.lang.String |
endLine
|
private java.lang.String |
indent
This whitespace string is expanded and collapsed to manage the output indenting. |
private boolean |
justHitStartTag
|
private java.lang.StringBuffer |
output
The primary buffer for accumulating the formatted XML. |
private static java.lang.String |
standardIndent
|
| Constructor Summary | |
|---|---|
PrettyPrinter()
|
|
| Method Summary | |
|---|---|
void |
characters(char[] chars,
int start,
int length)
When the currentValue buffer is enabled, appends character
data into it, to be gathered when the element end tag is encountered. |
void |
endDocument()
Prints a blank line at the end of the reformatted document. |
void |
endElement(java.lang.String URI,
java.lang.String name,
java.lang.String qName)
Checks the currentValue buffer to gather element content. |
private static java.lang.String |
escape(char[] chars,
int start,
int length)
Filter to pass strings to output, escaping < and & characters to < and & respectively. |
static java.lang.String |
prettyPrint(byte[] content)
Convenience method to wrap pretty-printing SAX pass over existing content. |
static java.lang.String |
prettyPrint(org.w3c.dom.Document doc)
Convenience method to wrap pretty-printing SAX pass over existing content. |
static java.lang.String |
prettyPrint(java.io.InputStream content)
Convenience method to wrap pretty-printing SAX pass over existing content. |
static java.lang.String |
prettyPrint(java.lang.String content)
Convenience method to wrap pretty-printing SAX pass over existing content. |
void |
startDocument()
Prints the XML declaration. |
void |
startElement(java.lang.String URI,
java.lang.String name,
java.lang.String qName,
org.xml.sax.Attributes attributes)
Writes the start tag for the element. |
java.lang.String |
toString()
Call this to get the formatted XML post-parsing. |
| Methods inherited from class org.xml.sax.helpers.DefaultHandler |
|---|
endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
private java.lang.String indent
private java.lang.StringBuffer currentValue
startElement by being initialized to a
new StringBuffer, and then read and reset to
null in endElement.
private java.lang.StringBuffer output
private boolean justHitStartTag
private static final java.lang.String standardIndent
private static final java.lang.String endLine
| Constructor Detail |
|---|
public PrettyPrinter()
| Method Detail |
|---|
public static java.lang.String prettyPrint(byte[] content)
public static java.lang.String prettyPrint(java.lang.String content)
public static java.lang.String prettyPrint(java.io.InputStream content)
public static java.lang.String prettyPrint(org.w3c.dom.Document doc)
throws javax.xml.transform.TransformerException
javax.xml.transform.TransformerExceptionpublic java.lang.String toString()
toString in class java.lang.Object
public void startDocument()
throws org.xml.sax.SAXException
startDocument in interface org.xml.sax.ContentHandlerstartDocument in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXException
public void endDocument()
throws org.xml.sax.SAXException
endDocument in interface org.xml.sax.ContentHandlerendDocument in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXException
public void startElement(java.lang.String URI,
java.lang.String name,
java.lang.String qName,
org.xml.sax.Attributes attributes)
throws org.xml.sax.SAXException
startElement in interface org.xml.sax.ContentHandlerstartElement in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXException
public void endElement(java.lang.String URI,
java.lang.String name,
java.lang.String qName)
throws org.xml.sax.SAXException
currentValue buffer to gather element content.
Writes this out if it is available. Writes the element end tag.
endElement in interface org.xml.sax.ContentHandlerendElement in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXException
public void characters(char[] chars,
int start,
int length)
throws org.xml.sax.SAXException
currentValue buffer is enabled, appends character
data into it, to be gathered when the element end tag is encountered.
characters in interface org.xml.sax.ContentHandlercharacters in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXException
private static java.lang.String escape(char[] chars,
int start,
int length)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||