jsky.util
Class SaxParserUtil

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

public abstract class SaxParserUtil
extends 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.4 $
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(String namespaceURI, String localName, String qName)
          Called for each element end tag.
 void error(SAXParseException e)
          Called for recoverable errors
 void fatalError(SAXParseException e)
          Called for recoverable errors
protected  String getCData()
          Return the value of character data (CDATA section) for current element.
 URL getURL()
          Return the URL of the last XML file parsed by this instance.
 void parse(String urlStr)
          Parse the given XML file.
 void parse(URL url)
          Parse the given XML file.
 void parse(URL url, InputStream in)
          Parse an XML file from an already open input stream.
 InputSource resolveEntity(String publicId, String systemId)
          This method tries to locate a local copy of the DTD as a JSky resource.
 void startElement(String namespaceURI, String localName, String qName, Attributes attrs)
          Called for each element start tag.
 void warning(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(URL url)
Parse the given XML file.

Parameters:
url - the URL for the XML file

parse

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

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

parse

public void parse(URL url,
                  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 URL getURL()
Return the URL of the last XML file parsed by this instance.


resolveEntity

public InputSource resolveEntity(String publicId,
                                 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 EntityResolver
Overrides:
resolveEntity in class DefaultHandler
See Also:
Resources

fatalError

public void fatalError(SAXParseException e)
                throws SAXParseException
Called for recoverable errors

Specified by:
fatalError in interface ErrorHandler
Overrides:
fatalError in class DefaultHandler
Throws:
SAXParseException

error

public void error(SAXParseException e)
           throws SAXParseException
Called for recoverable errors

Specified by:
error in interface ErrorHandler
Overrides:
error in class DefaultHandler
Throws:
SAXParseException

warning

public void warning(SAXParseException e)
             throws SAXParseException
Called for XML parser warnings

Specified by:
warning in interface ErrorHandler
Overrides:
warning in class DefaultHandler
Throws:
SAXParseException

startElement

public void startElement(String namespaceURI,
                         String localName,
                         String qName,
                         Attributes attrs)
                  throws 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 ContentHandler
Overrides:
startElement in class DefaultHandler
Throws:
SAXException

endElement

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

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler
Throws:
SAXException

characters

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

Specified by:
characters in interface ContentHandler
Overrides:
characters in class DefaultHandler
Throws:
SAXException

getCData

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



Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.