xtom.parser
Class Parser

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended byxtom.parser.Parser
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public final class Parser
extends org.xml.sax.helpers.DefaultHandler

Description: This is a main parser that will be responsible for parsing XML string into an XMLTree.
This class will use Java 1.4 SAX Parser to construct a tree.

Version:
$Revision: 1.2 $
Author:
taras

Field Summary
private static Element current
           
private  java.lang.Exception ex
           
private  java.io.InputStream in
           
private  java.util.logging.Logger logger
           
private static java.io.Writer out
           
private static Element previous
           
private static Element root
           
private  XMLTree tree
           
private  java.lang.String xml
           
 
Constructor Summary
private Parser()
           
  Parser(java.io.InputStream stream)
          Default parser.
  Parser(java.lang.String xml)
          Default parser.
 
Method Summary
 void addHandler(java.util.logging.Handler h)
          Adds a handler to the current logger.
 void characters(char[] buf, int offset, int len)
           
 void endElement(java.lang.String namespaceURI, java.lang.String sName, java.lang.String qName)
           
 java.lang.Exception getException()
           
 XMLTree getXMLTree()
          The tree is saved in memory for as long as Parser lives.
private  void logException(java.lang.Exception e)
          Logs the exception to the specified Handler
 XMLTree parse()
          Main Parser method
This parser only checks if the xml is well formed, meaning that each tag has a closing tag.
private  void parse(java.io.InputStream in)
          This is the method that will do actual parsing.
 void setLoggingLevel(java.util.logging.Level l)
          Sets the level for Logging Default Logging Level is WARNING
private  void setUpLogging()
          Sets up the Logging for the Parser.
 void startElement(java.lang.String namespaceURI, java.lang.String sName, java.lang.String qName, org.xml.sax.Attributes attrs)
           
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private java.util.logging.Logger logger

xml

private java.lang.String xml

tree

private XMLTree tree

in

private java.io.InputStream in

current

private static Element current

previous

private static Element previous

root

private static Element root

out

private static java.io.Writer out

ex

private java.lang.Exception ex
Constructor Detail

Parser

private Parser()

Parser

public Parser(java.lang.String xml)
Default parser. checks to see if the XML passed is not null and is at least 4 characters long, minimal xml . Sets the Default Logging Level and a Default Handler.

Parameters:
xml - xml string that must be parsed.
Throws:
java.lang.IllegalArgumentException - if the String is NULL or is less than 4 characters.

Parser

public Parser(java.io.InputStream stream)
Default parser. Reads the input stream. It only performs one check to see if the InputStream is not NULL. This constructor is provided as a convenience constructor.

Parameters:
stream - The Stream of XML Data.
Throws:
java.lang.IllegalArgumentException - if the InputStream was NULL.
Method Detail

setUpLogging

private void setUpLogging()
Sets up the Logging for the Parser. The J2SE 1.4 Logger is used.
Default Logger Level is OFF. This could be changed by using setLoggingLevel method.
The Default Handler is a ConsoleHandler. This could be changed by using addHandler method.


parse

public XMLTree parse()
Main Parser method
This parser only checks if the xml is well formed, meaning that each tag has a closing tag. It does not verify if the xml is valid.

Throws:
ParserIOException - if there was a problem parsing xml.
java.lang.UnsupportedOperationException - if any other excetion other then IO is thrown. This should not happen.
MissingRootElementException - if a root element was NULL after the parsing occured.

parse

private void parse(java.io.InputStream in)
            throws java.lang.Exception
This is the method that will do actual parsing.

Throws:
java.lang.Exception

getXMLTree

public XMLTree getXMLTree()
The tree is saved in memory for as long as Parser lives. The XML is guarenteed to be the same since the XML argument in the constructor is final.

Returns:
the xml tree, null, if the XML has not been parsed yet.

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String sName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
Parameters:
namespaceURI -
sName -
qName -
attrs -
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String sName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)

characters

public void characters(char[] buf,
                       int offset,
                       int len)
                throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.characters(char[], int, int)

getException

public java.lang.Exception getException()
Returns:
Returns the exception generated by the parser.

addHandler

public void addHandler(java.util.logging.Handler h)
Adds a handler to the current logger. Default Hanlder is ConsoleHanlder.

Parameters:
h - The handler.

setLoggingLevel

public void setLoggingLevel(java.util.logging.Level l)
Sets the level for Logging Default Logging Level is WARNING

Parameters:
l -

logException

private void logException(java.lang.Exception e)
Logs the exception to the specified Handler

Parameters:
e - Exception to be logged. If exception is null, it exits