|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.xml.sax.helpers.DefaultHandler
LexiconFileHandler
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=where CATEGORY is a syntactic category ("N", "V", etc.).) Sense+ END("Word") Sense <- START("Sense") Category END("Sense") Category <- START("Category") CATEGORY END("Category")
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
int state
static final int STATE_FILE
static final int STATE_LEXICON
static final int STATE_DESCRIPTION
static final int STATE_WORD
static final int STATE_SENSE
static final int STATE_CATEGORY
java.lang.StringBuffer textBuffer
java.lang.String description
Symbol word
Symbol cat
org.xml.sax.Locator locator
Constructor Detail |
public LexiconFileHandler(Lexicon l)
Lexicon.readFile(java.lang.String)
Method Detail |
public void readFile(java.lang.String filename) throws java.io.IOException
java.io.IOException
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
org.xml.sax.SAXException
public void endElement(java.lang.String namespaceURI, java.lang.String sName, java.lang.String qName) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void characters(char[] buf, int offset, int len) throws org.xml.sax.SAXException
org.xml.sax.SAXException
java.lang.String consumeText()
public void setDocumentLocator(org.xml.sax.Locator locator)
void ERROR(java.lang.String msg) throws org.xml.sax.SAXParseException
org.xml.sax.SAXParseException
public void error(org.xml.sax.SAXParseException ex)
public void fatalError(org.xml.sax.SAXParseException ex)
public void warning(org.xml.sax.SAXParseException ex)
public static void main(java.lang.String[] argv) throws java.io.IOException
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |