cc.xml
Class PrettyPrinter

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by cc.xml.PrettyPrinter
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class PrettyPrinter
extends org.xml.sax.helpers.DefaultHandler

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 &lt; and &amp; 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

indent

private java.lang.String indent
This whitespace string is expanded and collapsed to manage the output indenting.


currentValue

private java.lang.StringBuffer currentValue
A buffer for character data. It is "enabled" in startElement by being initialized to a new StringBuffer, and then read and reset to null in endElement.


output

private java.lang.StringBuffer output
The primary buffer for accumulating the formatted XML.


justHitStartTag

private boolean justHitStartTag

standardIndent

private static final java.lang.String standardIndent
See Also:
Constant Field Values

endLine

private static final java.lang.String endLine
Constructor Detail

PrettyPrinter

public PrettyPrinter()
Method Detail

prettyPrint

public static java.lang.String prettyPrint(byte[] content)
Convenience method to wrap pretty-printing SAX pass over existing content.


prettyPrint

public static java.lang.String prettyPrint(java.lang.String content)
Convenience method to wrap pretty-printing SAX pass over existing content.


prettyPrint

public static java.lang.String prettyPrint(java.io.InputStream content)
Convenience method to wrap pretty-printing SAX pass over existing content.


prettyPrint

public static java.lang.String prettyPrint(org.w3c.dom.Document doc)
                                    throws javax.xml.transform.TransformerException
Convenience method to wrap pretty-printing SAX pass over existing content.

Throws:
javax.xml.transform.TransformerException

toString

public java.lang.String toString()
Call this to get the formatted XML post-parsing.

Overrides:
toString in class java.lang.Object

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Prints the XML declaration.

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Prints a blank line at the end of the reformatted document.

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

startElement

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
Writes the start tag for the element. Attributes are written out, one to a text line. Starts gathering character data for the element.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String URI,
                       java.lang.String name,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Checks the currentValue buffer to gather element content. Writes this out if it is available. Writes the element end tag.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] chars,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
When the currentValue buffer is enabled, appends character data into it, to be gathered when the element end tag is encountered.

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

escape

private static java.lang.String escape(char[] chars,
                                       int start,
                                       int length)
Filter to pass strings to output, escaping < and & characters to &lt; and &amp; respectively.