Building Parsers with Java

sjm.examples.coffee
Class ShowCoffeeXML

java.lang.Object
  |
  +--org.xml.sax.helpers.DefaultHandler
        |
        +--sjm.examples.coffee.ShowCoffeeXML

public class ShowCoffeeXML
extends org.xml.sax.helpers.DefaultHandler

Show the recognition of a list of types of coffee, reading from an XML file.

This class keeps a hashtable whose keys are element names, such as "roast", and whose values are subclasses of Helper, such as RoastHelper.

When an object of this class receives an event indicating that the parser has found a new element, it looks up the element name in the hashtable, to find the right helper for the element. For example, on seeing the "roast" element, this class sets its helper object to be an instance of RoastHelper. Then, when this class receives characters from the parser, it passes the characters to the current helper.

Helpers expect a target object, and this class consistently uses a Vector of Coffee objects as the target for helpers.


Field Summary
protected  java.util.Vector coffees
           
protected  Helper helper
           
protected  java.util.Hashtable helpers
           
 
Constructor Summary
ShowCoffeeXML()
           
 
Method Summary
 void characters(char[] ch, int start, int len)
          Receive notification of character data inside an element.
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String rawName)
          Receive notification of the end of an element, which means that no helper should be active.
protected  java.util.Hashtable helpers()
           
static void main(java.lang.String[] argv)
          Show how to recognize coffees in an XML file.
 void startElement(java.lang.String uri, java.lang.String local, java.lang.String raw, org.xml.sax.Attributes atts)
          Receive notification of the start of an element.
 
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

helpers

protected java.util.Hashtable helpers

helper

protected Helper helper

coffees

protected java.util.Vector coffees
Constructor Detail

ShowCoffeeXML

public ShowCoffeeXML()
Method Detail

characters

public void characters(char[] ch,
                       int start,
                       int len)
Receive notification of character data inside an element. If there is a helper ready to go, ask the helper to process these characters.
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Parameters:
ch - The characters
start - The start position in the character array
len - The number of characters to use from the character array

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String rawName)
Receive notification of the end of an element, which means that no helper should be active.
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
all - all arguments ignored

helpers

protected java.util.Hashtable helpers()

main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Show how to recognize coffees in an XML file.

startElement

public void startElement(java.lang.String uri,
                         java.lang.String local,
                         java.lang.String raw,
                         org.xml.sax.Attributes atts)
Receive notification of the start of an element.

If the helpers hashtable has a key of for the given element name, then inform the helper that this element has appeared.

Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
uri - the uniform resource identifier, ignored
local - the local name, ignored
raw - the raw XML 1.0 name, which is the helper lookup key
attributes - the attributes attached to the element, ignored

by Steve Metsker