org.openspml.util
Class XmlParser

org.openspml.util.XmlParser

public class XmlParser

A wrapper around the Xerces parser that provides a simpler interface, and maintains a pool of parser objects for better performance.

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


Constructor Summary
XmlParser(boolean validating)
          Construct a private utility object.
 
Method Summary
static void clearPool()
          Manually clear the pool
static void dumpPool()
          Dump information about the pool.
 void error(org.openspml.util.SAXParseException ex)
          Parser error callback handler
 void fatalError(org.openspml.util.SAXParseException ex)
          Parser fatal error callback handler.
static java.lang.String getFilePath(java.lang.String name)
          Build an absolute path name for a file.
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 java.lang.String itoa(int i)
           
static void main(java.lang.String[] args)
          Parses an XML file, and displays any error or warning messages.
static org.openspml.util.Document parse(java.lang.String xml, boolean validation)
          Parses a string with or without validation and returns the Document.
 org.openspml.util.Document parseString(java.lang.String xml)
          Parses a string and returns the Document.
 void pool()
          Return the parser to the pool.
static void poolParser(XmlParser p)
          Return an object to the pool.
static java.lang.String readFile(java.lang.String name)
          Read the contents of a file and return it as a String.
static byte[] readFileBytes(java.io.File file)
           
static byte[] readFileBytes(java.lang.String name)
           
static byte[] readInputStreamBytes(java.io.InputStream is, boolean close)
           
 org.openspml.util.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.openspml.util.ContentHandler handler)
          Parse a stream using SAX.
 void saxParse(java.lang.String uri, org.openspml.util.ContentHandler handler)
          Parse a uri using SAX.
 void saxParseString(java.lang.String xml, org.openspml.util.ContentHandler handler)
          Parse a string using SAX.
 void warning(org.openspml.util.SAXParseException ex)
          Parser warning callback handler.
static void writeFile(java.lang.String name, byte[] contents)
          Store the contents of a String in a file.
static void writeFile(java.lang.String name, java.lang.String contents)
          Store the contents of a String in a file.
 

Constructor Detail

XmlParser

public XmlParser(boolean validating)
          throws org.openspml.util.ParserConfigurationException,
                 org.openspml.util.FactoryConfigurationError
Construct a private utility object.
Method Detail

parse

public static org.openspml.util.Document parse(java.lang.String xml,
                                               boolean validation)
                                        throws SpmlException
Parses a string with or without validation and returns the Document.

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.

itoa

public static java.lang.String itoa(int i)

dumpPool

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

pool

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

parseString

public org.openspml.util.Document parseString(java.lang.String xml)
                                       throws SpmlException
Parses a string and returns the Document. Validation may be on or off depending on how the parser is created.

resolveEntity

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

warning

public void warning(org.openspml.util.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.


error

public void error(org.openspml.util.SAXParseException ex)
           throws org.openspml.util.SAXParseException
Parser error callback handler

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


fatalError

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

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


saxParse

public void saxParse(java.lang.String uri,
                     org.openspml.util.ContentHandler handler)
              throws SpmlException
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.


saxParse

public void saxParse(java.io.InputStream input,
                     org.openspml.util.ContentHandler handler)
              throws SpmlException
Parse a stream using SAX.

saxParseString

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

clearPool

public static void clearPool()
Manually clear the pool

readFile

public static java.lang.String readFile(java.lang.String name)
                                 throws SpmlException
Read the contents of a file and return it as a String.

readFileBytes

public static byte[] readFileBytes(java.lang.String name)
                            throws SpmlException

readFileBytes

public static byte[] readFileBytes(java.io.File file)
                            throws SpmlException

readInputStreamBytes

public static byte[] readInputStreamBytes(java.io.InputStream is,
                                          boolean close)
                                   throws SpmlException
Parameters:
close - - if true, close when finished reading.
Returns:
input stream content.

writeFile

public static void writeFile(java.lang.String name,
                             java.lang.String contents)
                      throws SpmlException
Store the contents of a String in a file.

writeFile

public static void writeFile(java.lang.String name,
                             byte[] contents)
                      throws SpmlException
Store the contents of a String in a file.

getFilePath

public static java.lang.String getFilePath(java.lang.String name)
Build an absolute path name for a file. Typically used when you want to write an output file to the "current working directory". If the name is already an absolute path we use it, otherwise we use the system property "user.dir" as the root directory and add the name.

main

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