jsky.util
Class SaxParserUtil

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by jsky.util.SaxParserUtil
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler
Direct Known Subclasses:
AstroCatXML

public abstract class SaxParserUtil
extends org.xml.sax.helpers.DefaultHandler

Utility bass class for parsing an XML stream using a SAX parser.

This class uses reflection to call subclass methods for each start and end element. The method names are _${element}Start and _${element}End. For example, for the following XML code:

<para>some text</para>

would cause the methods _paraStart(Attributes) and _paraEnd() to be called. The text between the two tags can be retrieved with the getCData() method.

Version:
$Revision: 1.1 $
Author:
Allan Brighton

Constructor Summary
SaxParserUtil()
          Default constructor.
 
Method Summary
 void characters(char[] buf, int offset, int len)
          Receive notification of character data (CDATA) inside an element.
 void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
          Called for each element end tag.
 void error(org.xml.sax.SAXParseException e)
          Called for recoverable errors
 void fatalError(org.xml.sax.SAXParseException e)
          Called for recoverable errors
protected  java.lang.String getCData()
          Return the value of character data (CDATA section) for current element.
 java.net.URL getURL()
          Return the URL of the last XML file parsed by this instance.
 void parse(java.io.Reader reader)
          Parse the XML from the given Reader.
 void parse(java.lang.String urlStr)
          Parse the given XML file.
 void parse(java.net.URL url)
          Parse the given XML file.
 void parse(java.net.URL url, org.xml.sax.InputSource inputSource)
          Parse an XML file from an already open input source.
 void parse(java.net.URL url, java.io.InputStream in)
          Parse an XML file from an already open input stream.
 org.xml.sax.InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
          This method tries to locate a local copy of the DTD as a JSky resource.
protected  void setCData(java.lang.String s)
          Set the value of character data (CDATA section) for current element.
 void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attrs)
          Called for each element start tag.
 void warning(org.xml.sax.SAXParseException e)
          Called for XML parser warnings
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SaxParserUtil

public SaxParserUtil()
Default constructor. Call parse(urlStr) to do the actual parsing.

Method Detail

parse

public void parse(java.net.URL url)
Parse the given XML file.

Parameters:
url - the URL for the XML file

parse

public void parse(java.net.URL url,
                  org.xml.sax.InputSource inputSource)
Parse an XML file from an already open input source.

Parameters:
url - the URL for the XML file, for reference
inputSource - the input source for the XML file

parse

public void parse(java.lang.String urlStr)
           throws java.net.MalformedURLException
Parse the given XML file.

Parameters:
urlStr - the URL string for the XML file
Throws:
java.net.MalformedURLException

parse

public void parse(java.io.Reader reader)
Parse the XML from the given Reader.

Parameters:
reader - used to read the XML file

parse

public void parse(java.net.URL url,
                  java.io.InputStream in)
Parse an XML file from an already open input stream.

Parameters:
url - the URL for the XML file, for reference
in - the input stream for the given URL

getURL

public java.net.URL getURL()
Return the URL of the last XML file parsed by this instance.


resolveEntity

public org.xml.sax.InputSource resolveEntity(java.lang.String publicId,
                                             java.lang.String systemId)
This method tries to locate a local copy of the DTD as a JSky resource. Derived classes will need to redefine this method if a different resource class is used.

Specified by:
resolveEntity in interface org.xml.sax.EntityResolver
Overrides:
resolveEntity in class org.xml.sax.helpers.DefaultHandler
See Also:
Resources

fatalError

public void fatalError(org.xml.sax.SAXParseException e)
                throws org.xml.sax.SAXParseException
Called for recoverable errors

Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Overrides:
fatalError in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXParseException

error

public void error(org.xml.sax.SAXParseException e)
           throws org.xml.sax.SAXParseException
Called for recoverable errors

Specified by:
error in interface org.xml.sax.ErrorHandler
Overrides:
error in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXParseException

warning

public void warning(org.xml.sax.SAXParseException e)
             throws org.xml.sax.SAXParseException
Called for XML parser warnings

Specified by:
warning in interface org.xml.sax.ErrorHandler
Overrides:
warning in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXParseException

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
Called for each element start tag. Calls a method named: ("_" + qName + "Start") and passes the Attributes as a parameter.

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

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Called for each element end tag. Calls a method named ("_" + qName + "End") with no arguments.

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

characters

public void characters(char[] buf,
                       int offset,
                       int len)
                throws org.xml.sax.SAXException
Receive notification of character data (CDATA) inside an element.

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

getCData

protected java.lang.String getCData()
Return the value of character data (CDATA section) for current element.


setCData

protected void setCData(java.lang.String s)
Set the value of character data (CDATA section) for current element.