Package com.topologi.diffx.xml
Class XMLIndenter
java.lang.Object
org.xml.sax.helpers.DefaultHandler
com.topologi.diffx.xml.XMLIndenter
- All Implemented Interfaces:
ContentHandler,DTDHandler,EntityResolver,ErrorHandler
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
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final IntegerElement has neither text, nor children.private static final IntegerElement has children.private static final IntegerElement has text.private intThe indentation level.The stack of statesprivate final PrintWriterThe writer where the XML goes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcharacters(char[] ch, int position, int offset) Prints the characters.voidendElement(String uri, String localName, String qName) voidignorableWhitespace(char[] ch, int position, int offset) Does nothing.static voidIndents the given XML String.static StringIndents the given XML String.static booleanindentSilent(Reader r, Writer w) Indents the given XML String.static StringindentSilent(String xml) Indents the given XML String.voidstartElement(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, warningMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandler
declaration, endDocument, endPrefixMapping, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping
-
Field Details
-
writer
The writer where the XML goes. -
indentLevel
private transient int indentLevelThe indentation level. -
states
The stack of states -
EMPTY
Element has neither text, nor children. -
HAS_TEXT
Element has text. -
HAS_CHILDREN
Element has children.
-
-
Constructor Details
-
XMLIndenter
Creates a new XML Indenter.- Parameters:
w- The writer to use.
-
-
Method Details
-
startElement
- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler
-
endElement
- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler
-
characters
public void characters(char[] ch, int position, int offset) Prints the characters.- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler
-
ignorableWhitespace
public void ignorableWhitespace(char[] ch, int position, int offset) Does nothing.- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classDefaultHandler
-
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 dataw- 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
Indents the given XML String.- Parameters:
xml- The XML string to indent- Returns:
- The indented XML String or
nullif an error occurred.
-
indentSilent
Indents the given XML String.This method does not throw any exception out of convenience, instead it returns a
booleanvalue to indicate whether the XML indenting was performed succesfully.- Parameters:
r- A reader on XML dataw- A writer for the indented XML- Returns:
trueif the operation was successful,falseif an error occurred.
-