uk.org.redfelineninja.util
Class CommandTokenizer

java.lang.Object
  |
  +--uk.org.redfelineninja.util.CommandTokenizer

public class CommandTokenizer
extends java.lang.Object

This class provides a simple means to parse strings in command driven situations. This class is almost equivalent to java.io.StreamTokenizer but can be used in command driven situations. java.io.StreamTokenizer can not detect the end of a line until the next line has been presented to it. This is not approptiate in command driven situations. This class can be used as a drop in replacement in a command driven system or as an advanded version of java.util.StringTokenizer.

WARNING: This has not yet acheived its goal as a drop in replacement for StreamTokenizer. It has no decent comment handling.

Version:
0.8
Author:
Daniel Thompson, Feb 1999

Field Summary
 double nval
          The numeric value of the current token if applicable.
 java.lang.String sval
          The string value of the current token if applicable.
static int TT_EOF
          ttype takes this value if the end of the file has been reached.
static int TT_EOL
          ttype takes this value at the end of a line.
static int TT_NUMBER
          ttype takes this value if the current token is numeric.
static int TT_UNDEF
          ttype takes this value when pushed back and at any other time when its value is not defined.
static int TT_WORD
          ttype takes this value if the current token is a single word.
 int ttype
          The type of the current token.
 
Constructor Summary
CommandTokenizer(java.io.InputStream is)
          Deprecated. The prefered way to create a CommandTokenizer is by supplying it with a Reader.
CommandTokenizer(java.io.Reader r)
          Create a new CommandTokenizer that uses the supplied Reader.
CommandTokenizer(java.lang.String c)
          Create a new CommandTokenizer that takes its data from the supplied string.
 
Method Summary
 void commentChar(int ch)
           
 void eolIsSignificant(boolean b)
          Sets whether or not EOLs will be treated as tokens.
 java.lang.Character getCharacter()
          Try to get a character from the tokenizer.
 boolean getEOF()
          Test whether the tokenizer has reached the end of a file.
 boolean getEOL()
          Test whether the tokenizer has reached the end of a line.
 java.lang.String getLine()
          Get the whole of the current line for processing in a different way.
 java.lang.Double getNumber()
          Try to get a number from the tokenizer.
 java.lang.String getString()
          Try to get a string from the supplied tokenizer.
 char[] getStringDelim()
           
 java.lang.String getText()
          Try to get a text token from the tokenizer.
 java.lang.String getWord()
          Try to get a word from the tokenizer.
static boolean in(int ch, char[] chars)
          Checks a charecter for membership of the supplied array.
 boolean isNumeric()
          Test if the Token is numeric.
 boolean isString()
          Test if the token is a quoted string.
 boolean isText()
          Test if sval is defined for this token.
 boolean isWord()
          Test if the token is a single word.
 int nextToken()
          Move the tokenizer to the next token.
 void ordinaryChar(int ch)
           
 void pushBack()
          Pushes the current token back so that nextToken will return it again.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sval

public java.lang.String sval
The string value of the current token if applicable.


nval

public double nval
The numeric value of the current token if applicable.


ttype

public int ttype
The type of the current token.


TT_EOF

public static final int TT_EOF
ttype takes this value if the end of the file has been reached.

See Also:
Constant Field Values

TT_NUMBER

public static final int TT_NUMBER
ttype takes this value if the current token is numeric.

See Also:
Constant Field Values

TT_WORD

public static final int TT_WORD
ttype takes this value if the current token is a single word.

See Also:
Constant Field Values

TT_EOL

public static final int TT_EOL
ttype takes this value at the end of a line. This value will not be returned unless EOLs are made significant.

See Also:
eolIsSignificant(boolean), Constant Field Values

TT_UNDEF

public static final int TT_UNDEF
ttype takes this value when pushed back and at any other time when its value is not defined. This can generally be ignored as a possibility in well writen code.

See Also:
Constant Field Values
Constructor Detail

CommandTokenizer

public CommandTokenizer(java.io.InputStream is)
Deprecated. The prefered way to create a CommandTokenizer is by supplying it with a Reader.

Create a new CommandTokenizer that uses the supplied InputStream. Refer to CommandTokenizer( Reader ) for default starting states.

See Also:
CommandTokenizer#( Reader ), Reader

CommandTokenizer

public CommandTokenizer(java.io.Reader r)
Create a new CommandTokenizer that uses the supplied Reader. This default state of the CommandTokenizer is as follows.


CommandTokenizer

public CommandTokenizer(java.lang.String c)
Create a new CommandTokenizer that takes its data from the supplied string.

The CommandTokenizer will behave exactly as though the string was received from a Reader. When all tokens in the string have been exhusted the tokenizer will constantly return TT_EOF.

Method Detail

eolIsSignificant

public void eolIsSignificant(boolean b)
Sets whether or not EOLs will be treated as tokens. Setting this to true will cause the tokenizer to return TT_EOL every time the end of a line is reached.


getStringDelim

public char[] getStringDelim()

commentChar

public void commentChar(int ch)

ordinaryChar

public void ordinaryChar(int ch)

pushBack

public void pushBack()
Pushes the current token back so that nextToken will return it again.


getLine

public java.lang.String getLine()
Get the whole of the current line for processing in a different way. This will move the CommandTokenizer onto the next line. Even it EOL is significant the tokenizer will not supplied and EOL signal. To make things easier for EOL detectors the token type (ttype) is changed to TT_EOL


nextToken

public int nextToken()
              throws java.io.IOException
Move the tokenizer to the next token.

The tokens are categorized as follows.

Returns:
a integer to indentfy the current token
java.io.IOException

in

public static final boolean in(int ch,
                               char[] chars)
Checks a charecter for membership of the supplied array.


isNumeric

public final boolean isNumeric()
Test if the Token is numeric.

Returns:
true if the token is numeric

isWord

public final boolean isWord()
Test if the token is a single word.

Returns:
true if the token is a single word

isString

public final boolean isString()
Test if the token is a quoted string.

Returns:
true if the token is a quoted string

isText

public final boolean isText()
Test if sval is defined for this token.

Returns:
true if sval is defined

getNumber

public final java.lang.Double getNumber()
                                 throws java.io.IOException
Try to get a number from the tokenizer. If we can't get a number then we call pushBack so the token is not consumed.

Returns:
a Double if the input was interpreted as a number otherwise null
java.io.IOException

getText

public final java.lang.String getText()
                               throws java.io.IOException
Try to get a text token from the tokenizer. If we can't get text then we call pushBack so the token is not consumed.

Returns:
a String if the input was interpreted as text otherwise null
java.io.IOException

getString

public final java.lang.String getString()
                                 throws java.io.IOException
Try to get a string from the supplied tokenizer. If we can't get a string then we call pushBack so the token is not consumed.

Returns:
a String if the input was interpreted as a string otherwize null
java.io.IOException

getWord

public final java.lang.String getWord()
                               throws java.io.IOException
Try to get a word from the tokenizer. If we can't get a word then we call pushBack so the token is not consumed.

Returns:
a String if the input was interpreted as a word otherwize null
java.io.IOException

getCharacter

public final java.lang.Character getCharacter()
                                       throws java.io.IOException
Try to get a character from the tokenizer. If we can't get a character then we call pushBack so the token is not consumed.

Returns:
a Character if the input was interpreted as a character otherwize null
java.io.IOException

getEOL

public final boolean getEOL()
                     throws java.io.IOException
Test whether the tokenizer has reached the end of a line. It is assumed that if we reach the EOF marker then we have also reached the end of the line.

Returns:
true if the end of line or file has been reached, false otherwise and the input is pushed back so the token is not consumed
java.io.IOException
See Also:
StreamTokenizer.eolIsSignificant(boolean)

getEOF

public final boolean getEOF()
                     throws java.io.IOException
Test whether the tokenizer has reached the end of a file. If the test fails we do not consume the token.

Returns:
true if the end of file has been reached.
java.io.IOException