com.adventnet.utilities.xml.indenter
Class XMLIndenter

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

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

2 constructors Takes a file name as input and fills the StringBuffer sb . Returns the indented string as output through getData() method The policy of indentation is by default 4 spaces This class uses com.sun.xml.parser.Parser and requires the package org.xml.sax package This will not retain any comments or anything else. It only parses and indents pure XML rest every thing is just removed.


Field Summary
(package private)  int level
           
(package private)  javax.xml.parsers.SAXParser parser
           
(package private)  java.lang.StringBuffer sb
           
(package private)  java.lang.String spacePerLevel
           
(package private)  org.xml.sax.XMLReader xmlReader
           
 
Constructor Summary
XMLIndenter()
          Takes a file name as input and fills the StringBuffer sb .
 
Method Summary
 void characters(char[] ch, int start, int length)
           
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String name)
           
 void error(org.xml.sax.SAXParseException sax)
           
static java.lang.String escape(java.lang.String text_pm)
          This function add the escaoe characters for the XML file KEY < < > > " " ' '
 void fatalError(org.xml.sax.SAXParseException sax)
           
 java.lang.String getData()
           
 void ignorableWhitespace(char[] ch, int start, int length)
           
static void indentFile(java.io.File file)
           
static java.lang.String indentString(java.lang.Object ob)
           
static void main(java.lang.String[] s)
           
 void processingInstruction(java.lang.String target, java.lang.String data)
          Not sure if this function is implemented correctly
 void setNewLineForCloseTag(java.lang.String s)
          Set this before start is called .
 void setSpacePerLevel(java.lang.String s)
          Set the space before start is called .
 void start(java.io.File file)
          User defined method to start the Indenter.
 java.lang.String start(java.lang.Object fileContents)
          User defined method to start the Indenter.
 void startElement(java.lang.String uriName, java.lang.String name, java.lang.String rawName, org.xml.sax.Attributes atts)
           
 java.lang.String toURL(java.io.File f)
           
 void warning(org.xml.sax.SAXParseException sax)
           
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, notationDecl, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sb

java.lang.StringBuffer sb

level

int level

spacePerLevel

java.lang.String spacePerLevel

parser

javax.xml.parsers.SAXParser parser

xmlReader

org.xml.sax.XMLReader xmlReader
Constructor Detail

XMLIndenter

public XMLIndenter()
            throws javax.xml.parsers.ParserConfigurationException,
                   org.xml.sax.SAXException
Takes a file name as input and fills the StringBuffer sb . Returns the indented string as output through getData() method This class can be used to handle huge dat also by adding support to flush the data in between by implementing an interface and another applicvation also implementing the interface and setting into this class before the start function is called.

Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
Method Detail

start

public java.lang.String start(java.lang.Object fileContents)
                       throws java.io.IOException,
                              org.xml.sax.SAXException,
                              javax.xml.parsers.ParserConfigurationException
User defined method to start the Indenter.

Returns:
The whole indented String
Throws:
java.io.IOException
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException

start

public void start(java.io.File file)
           throws java.io.IOException,
                  java.io.FileNotFoundException,
                  org.xml.sax.SAXException,
                  java.net.MalformedURLException,
                  javax.xml.parsers.ParserConfigurationException
User defined method to start the Indenter. It will indent and write to the file If any IO error occurs when the Indenter is over writing your file, the file will be lost.

Throws:
java.io.IOException
java.io.FileNotFoundException
org.xml.sax.SAXException
java.net.MalformedURLException
javax.xml.parsers.ParserConfigurationException

toURL

public java.lang.String toURL(java.io.File f)
                       throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

indentFile

public static void indentFile(java.io.File file)
                       throws java.lang.Exception
Throws:
java.lang.Exception

indentString

public static java.lang.String indentString(java.lang.Object ob)
                                     throws java.lang.Exception
Throws:
java.lang.Exception

startElement

public void startElement(java.lang.String uriName,
                         java.lang.String name,
                         java.lang.String rawName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Overrides:
ignorableWhitespace in class org.xml.sax.helpers.DefaultHandler

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String name)
                throws org.xml.sax.SAXException
Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Not sure if this function is implemented correctly

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

error

public void error(org.xml.sax.SAXParseException sax)
           throws org.xml.sax.SAXException
Specified by:
error in interface org.xml.sax.ErrorHandler
Overrides:
error in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

fatalError

public void fatalError(org.xml.sax.SAXParseException sax)
                throws org.xml.sax.SAXException
Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Overrides:
fatalError in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

warning

public void warning(org.xml.sax.SAXParseException sax)
             throws org.xml.sax.SAXException
Specified by:
warning in interface org.xml.sax.ErrorHandler
Overrides:
warning in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

setSpacePerLevel

public void setSpacePerLevel(java.lang.String s)
Set the space before start is called . It defaults to 4 spaces


setNewLineForCloseTag

public void setNewLineForCloseTag(java.lang.String s)
Set this before start is called . It defaults to 4 spaces This puts the space and new line before the closing tag in case of tags without data ie which have child tags eg :


getData

public java.lang.String getData()

escape

public static java.lang.String escape(java.lang.String text_pm)
This function add the escaoe characters for the XML file KEY < < > > " " ' '


main

public static void main(java.lang.String[] s)
                 throws java.lang.Exception
Throws:
java.lang.Exception