Class LexiconFileHandler

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended byLexiconFileHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler
Direct Known Subclasses:
AugmentedLexiconFileHandler

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

LexiconFileHandler reads lexicon files whose constitents are simple Constituents (no features).

The BNF grammar we are trying to parse is something like the following:

    Lexicon <- START("Lexicon") Description? Word* END("Lexicon")
    Description <- START("Description") characters END("Description")
    Word <- START("Word" id=) Sense+ END("Word")
    Sense <- START("Sense") Category END("Sense")
    Category <- START("Category") CATEGORY END("Category")
 
where CATEGORY is a syntactic category ("N", "V", etc.).

Implementation note: The SAX ContentHandler interface calls us when start- and end-element tags are read, and for the text between them. Using these callbacks, we need to maintain the state of our parse of the file, and use that state to decide what to do with a particular tag's start and end. Usually, handling a start-element tag involves extracting attributes needed for the element, then change to state of the parse to reflect that we are now processing that element. Then, the handling of the matching end-element tag performs the operation appropriate to the end of the element (usually creating some object and saving it in a parent data structure), then setting the state back to continue the parse.

It would be much more elegant to have a tool like yacc/bison which compiled CFG specifications into Java code that used the SAX parser as its tokenizer (input), and looked after all the state management. Maybe some day...


Field Summary
(package private)  Symbol cat
           
(package private)  java.lang.String description
           
(package private)  Lexicon lexicon
          The Lexicon we are building while reading the file.
(package private)  org.xml.sax.Locator locator
          Locator used in producing error messages.
(package private)  int state
           
(package private) static int STATE_CATEGORY
           
(package private) static int STATE_DESCRIPTION
           
(package private) static int STATE_FILE
           
(package private) static int STATE_LEXICON
           
(package private) static int STATE_SENSE
           
(package private) static int STATE_WORD
           
(package private)  java.lang.StringBuffer textBuffer
           
(package private)  Symbol word
           
 
Constructor Summary
LexiconFileHandler(Lexicon l)
          Create a new LexiconFileHandler to read into the given Lexicon.
 
Method Summary
 void characters(char[] buf, int offset, int len)
          Called by the SAX parsing engine when characters are read outside of any element tag.
(package private)  java.lang.String consumeText()
          Returns the contents of the textBuffer and clears the buffer.
 void endElement(java.lang.String namespaceURI, java.lang.String sName, java.lang.String qName)
          SAX ContentHandler method called when an end-element tag is encountered.
 void error(org.xml.sax.SAXParseException ex)
          Receive notification of a recoverable error.
(package private)  void ERROR(java.lang.String msg)
          Generates a SAXParseException with the given message and the current locator.
 void fatalError(org.xml.sax.SAXParseException ex)
          Receive notification of a non-recoverable error.
static void main(java.lang.String[] argv)
          Test the LexiconFileHandler.
 void readFile(java.lang.String filename)
          Read the given filename using this handler.
 void setDocumentLocator(org.xml.sax.Locator locator)
          SAX ContentHandler method called at the start of processing a document.
 void startElement(java.lang.String namespaceURI, java.lang.String sName, java.lang.String qName, org.xml.sax.Attributes attrs)
          SAX ContentHandler method called when a start-element tag is encountered.
 void warning(org.xml.sax.SAXParseException ex)
          Receive notification of a warning.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lexicon

Lexicon lexicon
The Lexicon we are building while reading the file.


state

int state

STATE_FILE

static final int STATE_FILE
See Also:
Constant Field Values

STATE_LEXICON

static final int STATE_LEXICON
See Also:
Constant Field Values

STATE_DESCRIPTION

static final int STATE_DESCRIPTION
See Also:
Constant Field Values

STATE_WORD

static final int STATE_WORD
See Also:
Constant Field Values

STATE_SENSE

static final int STATE_SENSE
See Also:
Constant Field Values

STATE_CATEGORY

static final int STATE_CATEGORY
See Also:
Constant Field Values

textBuffer

java.lang.StringBuffer textBuffer

description

java.lang.String description

word

Symbol word

cat

Symbol cat

locator

org.xml.sax.Locator locator
Locator used in producing error messages.

Constructor Detail

LexiconFileHandler

public LexiconFileHandler(Lexicon l)
Create a new LexiconFileHandler to read into the given Lexicon.

See Also:
Lexicon.readFile(java.lang.String)
Method Detail

readFile

public void readFile(java.lang.String filename)
              throws java.io.IOException
Read the given filename using this handler.

Throws:
java.io.IOException

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String sName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
SAX ContentHandler method called when a start-element tag is encountered.

Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String sName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
SAX ContentHandler method called when an end-element tag is encountered.

Throws:
org.xml.sax.SAXException

characters

public void characters(char[] buf,
                       int offset,
                       int len)
                throws org.xml.sax.SAXException
Called by the SAX parsing engine when characters are read outside of any element tag. This can be called multiple times, such as when the text spans multiple lines (depends on whitespace treatment, whether the parser is validating, etc.). So the safe thing to do is just hold onto the text for later use when an end-element tag is reached (assuming we don't care about the actual form of the original text, but just its content, which is the case for us). We could be more careful, saving the text in states where we're expecting text and otherwise indicating a state-specific error, but frankly I don't care that much.

Throws:
org.xml.sax.SAXException

consumeText

java.lang.String consumeText()
Returns the contents of the textBuffer and clears the buffer.


setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
SAX ContentHandler method called at the start of processing a document.


ERROR

void ERROR(java.lang.String msg)
     throws org.xml.sax.SAXParseException
Generates a SAXParseException with the given message and the current locator.

Throws:
org.xml.sax.SAXParseException

error

public void error(org.xml.sax.SAXParseException ex)
Receive notification of a recoverable error.


fatalError

public void fatalError(org.xml.sax.SAXParseException ex)
Receive notification of a non-recoverable error.


warning

public void warning(org.xml.sax.SAXParseException ex)
Receive notification of a warning.


main

public static void main(java.lang.String[] argv)
                 throws java.io.IOException
Test the LexiconFileHandler.

Throws:
java.io.IOException