com.topologi.diffx.xml
Class XMLIndenter

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.topologi.diffx.xml.XMLIndenter
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler

public final class XMLIndenter
extends DefaultHandler
implements ContentHandler

A class to indent automatically some XML data.

Note: This implementation is not namespace aware, and will not handle entities other than &, <, > or ".

Version:
26 February 2005
Author:
Christophe Lauret - Allette Systems (Australia)

Method Summary
 void characters(char[] ch, int position, int offset)
          Prints the characters.
 void endElement(String uri, String localName, String qName)
          
 void ignorableWhitespace(char[] ch, int position, int offset)
          Does nothing.
static void indent(Reader r, Writer w)
          Indents the given XML String.
static String indent(String xml)
          Indents the given XML String.
static boolean indentSilent(Reader r, Writer w)
          Indents the given XML String.
static String indentSilent(String xml)
          Indents the given XML String.
 void startElement(String uri, String localName, String qName, Attributes atts)
          
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.xml.sax.ContentHandler
endDocument, endPrefixMapping, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping
 

Method Detail

startElement

public void startElement(String uri,
                         String localName,
                         String qName,
                         Attributes atts)

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultHandler

endElement

public void endElement(String uri,
                       String localName,
                       String qName)

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler

characters

public void characters(char[] ch,
                       int position,
                       int offset)
Prints the characters.

Specified by:
characters in interface ContentHandler
Overrides:
characters in class DefaultHandler

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int position,
                                int offset)
Does nothing.

Specified by:
ignorableWhitespace in interface ContentHandler
Overrides:
ignorableWhitespace in class DefaultHandler

indent

public static String indent(String xml)
                     throws SAXException,
                            IOException,
                            ParserConfigurationException
Indents the given XML String.

Parameters:
xml - The XML string to indent
Returns:
The indented XML String.
Throws:
IOException - If an IOException occurs.
SAXException - If the XML is not well-formed.
ParserConfigurationException - If the parser could not be configured

indent

public static void indent(Reader r,
                          Writer w)
                   throws SAXException,
                          IOException,
                          ParserConfigurationException
Indents the given XML String.

Parameters:
r - A reader on XML data
w - A writer for the indented XML
Throws:
IOException - If an IOException occurs.
SAXException - If the XML is not well-formed.
ParserConfigurationException - If the parser could not be configured

indentSilent

public static String indentSilent(String xml)
Indents the given XML String.

Parameters:
xml - The XML string to indent
Returns:
The indented XML String or null if an error occurred.

indentSilent

public static boolean indentSilent(Reader r,
                                   Writer w)
Indents the given XML String.

This method does not throw any exception out of convenience, instead it returns a boolean value to indicate whether the XML indenting was performed succesfully.

Parameters:
r - A reader on XML data
w - A writer for the indented XML
Returns:
true if the operation was successful, false if an error occurred.