Class GrammarFileHandler

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

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

GrammarFileHandler reads the basic grammar files, whose constituents are simple Constituents (no features).

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

    Grammar <- START("Grammar") Description? GrammarRule* END("Grammar")
    Description <- START(DESCRIPTION) characters END(Description)
    GrammarRule <- START("Rule") Constit+ END("Rule")
    Constit <- START(CAT) END(CAT)
 
where CAT is a syntactic category.

See implementation notes for LexiconFileHandler.


Field Summary
(package private)  Symbol cat
           
(package private)  java.util.LinkedList constits
           
(package private)  Grammar grammar
          The Grammar we are building while reading the file.
(package private)  org.xml.sax.Locator locator
          Locator used in producing error messages.
(package private)  Symbol ruleId
           
(package private)  int state
           
(package private) static int STATE_CONSTIT
           
(package private) static int STATE_DESCRIPTION
           
(package private) static int STATE_FILE
           
(package private) static int STATE_GRAMMAR
           
(package private) static int STATE_RULE
           
(package private)  java.lang.StringBuffer textBuffer
           
 
Constructor Summary
GrammarFileHandler(Grammar g)
          Create a new GrammarFileHandler to read into the given Grammar.
 
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 GrammarFileHandler.
 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

grammar

Grammar grammar
The Grammar we are building while reading the file.


state

int state

STATE_FILE

static final int STATE_FILE
See Also:
Constant Field Values

STATE_GRAMMAR

static final int STATE_GRAMMAR
See Also:
Constant Field Values

STATE_DESCRIPTION

static final int STATE_DESCRIPTION
See Also:
Constant Field Values

STATE_RULE

static final int STATE_RULE
See Also:
Constant Field Values

STATE_CONSTIT

static final int STATE_CONSTIT
See Also:
Constant Field Values

textBuffer

java.lang.StringBuffer textBuffer

ruleId

Symbol ruleId

constits

java.util.LinkedList constits

cat

Symbol cat

locator

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

Constructor Detail

GrammarFileHandler

public GrammarFileHandler(Grammar g)
Create a new GrammarFileHandler to read into the given Grammar.

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 GrammarFileHandler.

Throws:
java.io.IOException