com.waveset.util
Class XmlParser

java.lang.Object
  extended bycom.waveset.util.XmlParser
All Implemented Interfaces:
org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class XmlParser
extends java.lang.Object
implements org.xml.sax.ErrorHandler, org.xml.sax.EntityResolver

A simplified wrapper around the Xerces DOM parser. We also provide static methods for a parser pool. The pool is necessary since the parser object is not reentrant, and we'd prefer not to synchronize everything on XML parsing.

This implements the Xerces ErrorHandler interface so we can intercept parser errors and mutate them.


Field Summary
static java.lang.String code_id
           
 
Method Summary
static void clearPool()
          Manually clear the pool
static void dumpPool()
          Dump information about the pool.
 void error(org.xml.sax.SAXParseException ex)
          Parser error callback handler
 void fatalError(org.xml.sax.SAXParseException ex)
          Parser fatal error callback handler.
static XmlParser getParser()
          Obtain a parser from the pool.
static XmlParser getParser(boolean validating)
           
static void getStatus(java.lang.StringBuffer b)
          Retrieve information about the pool.
static void main(java.lang.String[] args)
          Parses an XML file, and displays any error or warning messages.
 org.w3c.dom.Document parse(java.lang.String uri)
          Parses the specified URI and returns the document.
 org.w3c.dom.Document parseString(java.lang.String xml)
          Parses a string without validation and returns the Document.
 org.w3c.dom.Document parseString(java.lang.String xml, boolean validation)
          Parses a string with or without validation and returns the Document.
 void pool()
          Return the parser to the pool.
static void poolParser(XmlParser p)
          Return an object to the pool.
 org.xml.sax.InputSource resolveEntity(java.lang.String pubid, java.lang.String sysid)
          Attempt to resolve an entity reference to an absolute file path.
 void saxParse(java.io.InputStream input, org.xml.sax.ContentHandler handler)
          Parse a stream using SAX.
 void saxParse(java.io.Reader input, org.xml.sax.ContentHandler handler)
          Parse a reader using SAX.
 void saxParse(java.lang.String uri, org.xml.sax.ContentHandler handler)
          Parse a uri using SAX.
 void saxParseString(java.lang.String xml, org.xml.sax.ContentHandler handler)
          Parse a string using SAX.
 void saxParseString(java.lang.String xml, org.xml.sax.ContentHandler handler, org.xml.sax.ErrorHandler errorHandler)
          Parse a string using SAX.
 boolean setValidation(boolean validation)
          Turn validation in a parser on or off.
 void warning(org.xml.sax.SAXParseException ex)
          Parser warning callback handler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

code_id

public static final java.lang.String code_id
See Also:
Constant Field Values
Method Detail

getParser

public static XmlParser getParser()
Obtain a parser from the pool.


getParser

public static XmlParser getParser(boolean validating)

poolParser

public static void poolParser(XmlParser p)
Return an object to the pool.


getStatus

public static void getStatus(java.lang.StringBuffer b)
Retrieve information about the pool. This assumes that the caller is assembling an XML Report document, we get to contribute one or more
elements.


dumpPool

public static void dumpPool()
Dump information about the pool.


pool

public void pool()
Return the parser to the pool.


setValidation

public boolean setValidation(boolean validation)
Turn validation in a parser on or off. Ignore exceptions. We've seen this die on occasion if the parser is being used a lot, and it gets into a state where it thinks its still parsing something else.


parse

public org.w3c.dom.Document parse(java.lang.String uri)
                           throws WavesetException
Parses the specified URI and returns the document.

Throws:
WavesetException

parseString

public org.w3c.dom.Document parseString(java.lang.String xml)
                                 throws WavesetException
Parses a string without validation and returns the Document.

Throws:
WavesetException

parseString

public org.w3c.dom.Document parseString(java.lang.String xml,
                                        boolean validation)
                                 throws WavesetException
Parses a string with or without validation and returns the Document.

Throws:
WavesetException

resolveEntity

public org.xml.sax.InputSource resolveEntity(java.lang.String pubid,
                                             java.lang.String sysid)
                                      throws org.xml.sax.SAXException,
                                             java.io.IOException
Attempt to resolve an entity reference to an absolute file path.

Specified by:
resolveEntity in interface org.xml.sax.EntityResolver
Throws:
org.xml.sax.SAXException
java.io.IOException

warning

public void warning(org.xml.sax.SAXParseException ex)
Parser warning callback handler.

What should we do with warnings, collection them, barf to the console? Might want a flag that treats warnings as errors.

Specified by:
warning in interface org.xml.sax.ErrorHandler

error

public void error(org.xml.sax.SAXParseException ex)
           throws org.xml.sax.SAXParseException
Parser error callback handler

We get here for things like DTD syntax errors, and DTD validation errors.

Specified by:
error in interface org.xml.sax.ErrorHandler
Throws:
org.xml.sax.SAXParseException

fatalError

public void fatalError(org.xml.sax.SAXParseException ex)
                throws org.xml.sax.SAXException
Parser fatal error callback handler.

Convert this one to a WavesetException so we can capture the full message text in one place. We get here for things like DTD location failure.

Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Throws:
org.xml.sax.SAXException

saxParse

public void saxParse(java.lang.String uri,
                     org.xml.sax.ContentHandler handler)
              throws WavesetException
Parse a uri using SAX. This is a bit of a kludge, it was glued onto what was formerly a DOM centric parser. The XmlParser class could be refactored into common/sax/dom classes, but I'm not sure that's worth it.

Since we assume we don't do this very often, we'll build a new parser every time.

Throws:
WavesetException

saxParse

public void saxParse(java.io.InputStream input,
                     org.xml.sax.ContentHandler handler)
              throws WavesetException
Parse a stream using SAX.

Throws:
WavesetException

saxParse

public void saxParse(java.io.Reader input,
                     org.xml.sax.ContentHandler handler)
              throws WavesetException
Parse a reader using SAX.

Throws:
WavesetException

saxParseString

public void saxParseString(java.lang.String xml,
                           org.xml.sax.ContentHandler handler)
                    throws WavesetException
Parse a string using SAX.

Throws:
WavesetException

saxParseString

public void saxParseString(java.lang.String xml,
                           org.xml.sax.ContentHandler handler,
                           org.xml.sax.ErrorHandler errorHandler)
                    throws WavesetException
Parse a string using SAX.

Throws:
WavesetException

clearPool

public static void clearPool()
Manually clear the pool


main

public static void main(java.lang.String[] args)
Parses an XML file, and displays any error or warning messages. on the console.