org.alicebot.server.core.parser
Class GenericReader

java.lang.Object
  extended by org.alicebot.server.core.parser.GenericReader
Direct Known Subclasses:
AIMLReader, TargetsReader

public abstract class GenericReader
extends java.lang.Object

Provides generic reading functionality for such classes as AIMLReader and org.alicebot.server.core.util.TargetsReader.

Author:
Noel Bush

Nested Class Summary
 class GenericReader.TransitionMade
          Thrown by the various transition methods when a transition is successfully made.
 
Field Summary
protected static java.lang.String ASTERISK
          An asterisk.
protected  java.lang.StringBuffer buffer
          The parse buffer.
protected static int bufferStartCapacity
          An estimate of the maximum buffer length needed (helps tune performance).
protected  java.lang.String bufferString
          The parse buffer as a String.
protected  java.io.BufferedReader buffReader
          Access to the file.
protected  long byteCount
          Used to count bytes read.
protected static java.lang.String COLON
          A colon.
protected static java.lang.String COMMENT_MARK
          '!--'
protected  boolean done
          The flag that indicates whether parsing is done.
protected static java.lang.String EMPTY_STRING
          An empty string.
protected  java.lang.String encoding
          Used to calculate bytes read.
protected  java.lang.String fileName
          The name of the file.
protected static java.lang.String LINE_SEPARATOR
          The system line separator.
protected  int lineNumber
          The current line number.
protected  GenericReaderListener listener
          The Listener that will handle new items.
protected static java.lang.String MARKER_END
          The end of a tag marker.
protected static java.lang.String MARKER_START
          The start of a tag marker.
protected static java.lang.String QUOTE_MARK
          A quote mark.
protected  GenericReader readerInstance
          An instance of GenericReader should set this field to itself.
protected  boolean searching
          The flag that indicates whether the read method is searching for a tag marker start.
protected  int searchStart
          The location in bufferString to......
protected static java.lang.String SLASH
          A slash.
protected static java.lang.String SPACE
          A space.
protected  int state
          Parser state.
protected  int tagLength
          The most recent length of a tag being checked.
protected  int tagStart
          The most recent start of a tag.
protected  GenericReader.TransitionMade TRANSITION_MADE
          A custom Throwable thrown by the various transition methods if they succeed.
 
Constructor Summary
GenericReader(java.lang.String fileName, java.io.BufferedReader buffReader, GenericReaderListener listener)
          Constructs a new GenericReader, given a BufferedReader handle to some input stream (buffReader), a filename to use in printing error messages (fileName), and a TargetsReaderListener that will handle creation of new categories as they are discovered.
GenericReader(java.lang.String fileName, java.io.BufferedReader buffReader, java.lang.String encoding, boolean countBytes, GenericReaderListener listener)
          Constructs a new GenericReader, given a BufferedReader handle to some input stream (buffReader), a filename to use in printing error messages (fileName), and a TargetsReaderListener that will handle creation of new categories as they are discovered.
 
Method Summary
protected abstract  void initialize()
           
 void read()
          Reads a targets data file and looks for categories.
protected  boolean succeed(java.lang.String tag, int toState)
           Checks whether bufferString contains tag at tagStart,
protected  void transition(java.lang.String tag, int toState)
          If bufferString contains tag at tagStart and sets state to toState.
protected  void transition(java.lang.String tag, int toState, java.lang.reflect.Field component)
          If bufferString contains tag at tagStart, sets state to toState, captures the substring of bufferString from 0 to tagStart, filtering whitespace and setting component to the result.
protected  void transition(java.lang.String tag, int toState, java.lang.reflect.Field component, java.lang.String attributeName)
           If bufferString contains tag at tagStart, sets state to toState, captures the substring of bufferString from 0 to tagStart, filtering whitespace and setting component to the result, then deleting the tag from the buffer.
protected abstract  void tryStates()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MARKER_START

protected static final java.lang.String MARKER_START
The start of a tag marker.

See Also:
Constant Field Values

MARKER_END

protected static final java.lang.String MARKER_END
The end of a tag marker.

See Also:
Constant Field Values

COMMENT_MARK

protected static final java.lang.String COMMENT_MARK
'!--'

See Also:
Constant Field Values

EMPTY_STRING

protected static final java.lang.String EMPTY_STRING
An empty string.

See Also:
Constant Field Values

SLASH

protected static final java.lang.String SLASH
A slash.

See Also:
Constant Field Values

QUOTE_MARK

protected static final java.lang.String QUOTE_MARK
A quote mark.

See Also:
Constant Field Values

ASTERISK

protected static final java.lang.String ASTERISK
An asterisk.

See Also:
Constant Field Values

COLON

protected static final java.lang.String COLON
A colon.

See Also:
Constant Field Values

SPACE

protected static final java.lang.String SPACE
A space.

See Also:
Constant Field Values

LINE_SEPARATOR

protected static final java.lang.String LINE_SEPARATOR
The system line separator.


bufferStartCapacity

protected static int bufferStartCapacity
An estimate of the maximum buffer length needed (helps tune performance).


readerInstance

protected GenericReader readerInstance
An instance of GenericReader should set this field to itself.


listener

protected GenericReaderListener listener
The Listener that will handle new items.


buffReader

protected java.io.BufferedReader buffReader
Access to the file.


fileName

protected java.lang.String fileName
The name of the file.


done

protected boolean done
The flag that indicates whether parsing is done.


searching

protected boolean searching
The flag that indicates whether the read method is searching for a tag marker start.


state

protected int state
Parser state.


tagStart

protected int tagStart
The most recent start of a tag.


tagLength

protected int tagLength
The most recent length of a tag being checked.


searchStart

protected int searchStart
The location in bufferString to......


lineNumber

protected int lineNumber
The current line number.


buffer

protected java.lang.StringBuffer buffer
The parse buffer.


bufferString

protected java.lang.String bufferString
The parse buffer as a String.


TRANSITION_MADE

protected GenericReader.TransitionMade TRANSITION_MADE
A custom Throwable thrown by the various transition methods if they succeed.


byteCount

protected long byteCount
Used to count bytes read.


encoding

protected java.lang.String encoding
Used to calculate bytes read.

Constructor Detail

GenericReader

public GenericReader(java.lang.String fileName,
                     java.io.BufferedReader buffReader,
                     java.lang.String encoding,
                     boolean countBytes,
                     GenericReaderListener listener)
Constructs a new GenericReader, given a BufferedReader handle to some input stream (buffReader), a filename to use in printing error messages (fileName), and a TargetsReaderListener that will handle creation of new categories as they are discovered.

Parameters:
fileName - name of the targets data file to be read
buffReader - a BufferedReader already open to the file (could be remote)
encoding - the encoding with which the file is being read
countBytes - whether or not to count bytes read (slows down the process)
listener - will handle new items
See Also:
Targets

GenericReader

public GenericReader(java.lang.String fileName,
                     java.io.BufferedReader buffReader,
                     GenericReaderListener listener)
Constructs a new GenericReader, given a BufferedReader handle to some input stream (buffReader), a filename to use in printing error messages (fileName), and a TargetsReaderListener that will handle creation of new categories as they are discovered. In this version, byte counting is disabled.

Parameters:
fileName - name of the targets data file to be read
buffReader - a BufferedReader already open to the file (could be remote)
listener - will handle new items
See Also:
Targets
Method Detail

initialize

protected abstract void initialize()

read

public void read()
Reads a targets data file and looks for categories. The expected format is:


tryStates

protected abstract void tryStates()
                           throws GenericReader.TransitionMade
Throws:
GenericReader.TransitionMade

succeed

protected boolean succeed(java.lang.String tag,
                          int toState)

Checks whether bufferString contains tag at tagStart,

If so, sets tagLength to the length of tag, sets state to toState and returns true.

If not, returns false.

Parameters:
tag - the tag to look for in buffer
toState - the parser state to assign if successful
Returns:
whether the tag was found

transition

protected void transition(java.lang.String tag,
                          int toState)
                   throws GenericReader.TransitionMade
If bufferString contains tag at tagStart and sets state to toState.

Parameters:
tag - the tag to look for in buffer
toState - the parser state to assign if successful
Throws:
GenericReader.TransitionMade - if the transition is successfully made

transition

protected void transition(java.lang.String tag,
                          int toState,
                          java.lang.reflect.Field component)
                   throws GenericReader.TransitionMade
If bufferString contains tag at tagStart, sets state to toState, captures the substring of bufferString from 0 to tagStart, filtering whitespace and setting component to the result.

Parameters:
tag - the tag to look for in buffer
toState - the parser state to assign if successful
component - the component to set to the whitespace-filtered substring of buffer from 0 to tagStart
Throws:
GenericReader.TransitionMade - if the transition is successfully made

transition

protected void transition(java.lang.String tag,
                          int toState,
                          java.lang.reflect.Field component,
                          java.lang.String attributeName)
                   throws GenericReader.TransitionMade

If bufferString contains tag at tagStart, sets state to toState, captures the substring of bufferString from 0 to tagStart, filtering whitespace and setting component to the result, then deleting the tag from the buffer.

Parameters:
tag - the tag to look for in buffer
toState - the parser state to assign if successful
component - the component to set to the whitespace-filtered substring of buffer from 0 to tagStart
attributeName - the name of the attribute holding the desired content
Throws:
GenericReader.TransitionMade - if the transition is successfully made