|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.inria.ns.reflex.util.Scanner
public abstract class Scanner
A scanner can read characters from an input stream under conditions.
Convenient methods are available for testing for example whether the next content is a number or not, for reading characters, strings and numbers.
Additional classes can help to read an object under constraints, for example "get the next number that has less than 5 digits", or the next number that fit in a specific type.
It is a progressive scanner in the sense that the next content is inherently independent of what was read before. To hold a context and go back when successive items doesn't satisfy a specific grammar, the user will have to set markers.
The mark(int), consume(), and
cancel() methods can help to read some
characters and go back to the marked position.
Several successive positions can be marked without
canceling or consuming the previous ones ;
as marks are stacked, it falls to the user
to apply the appropriate number of cancel + consume
calls.
A specific device is available for single characters :
just use hasNextChar(char, boolean),
hasNextChar(String, boolean), or get it simply
with lookAhead() ; you don't need to set a mark
for testing the next character to read.
It is markable-reentrant that is to say that once marked, it can used itself for further scanning with or without setting new marks.
| Constructor Summary | |
|---|---|
Scanner()
|
|
| Method Summary | |
|---|---|
abstract void |
cancel()
Cancel the characters read since the last marked position (the next read will start from the last marked position). |
abstract void |
consume()
Consume the characters read so far. |
int |
getPosition()
Return the current position from the unique physical mark. |
abstract Reader |
getRemainder()
Return the remainder to read from the current position. |
abstract String |
getRemainderString()
Return the remainder to read from the current position. |
int |
getSourceIndex()
Return the index in the source after parsing successfully an item under constraint. |
boolean |
hasNext()
Indicates whether there remains characters to read in the input. |
boolean |
hasNextChar(char c,
boolean consume)
Indicates whether the next character in the input is the given character. |
boolean |
hasNextChar(String chars,
boolean consume)
Indicates whether the next character in the input is one of the given characters. |
boolean |
hasNextString(String string,
boolean consume)
Test whether or not the next string in the input is those given. |
char |
lookAhead()
Return the next character to read without advancing the cursor. |
abstract void |
mark(int limit)
Mark the present position in the stream. |
char |
nextChar()
Read the next character. |
char |
nextChar(String chars,
boolean consume)
Return the next character in the input that is one of the given characters. |
Number |
nextNumber()
Read the next number. |
Number |
nextNumber(NumberConstraint constraint)
Read the next number. |
UserData |
nextObject(ObjectConstraint constraint)
Read the next object. |
int |
nextString(StringConstraint constraint)
Skip the next string (under constraint). |
int |
nextString(StringConstraint constraint,
StringBuffer buf)
Append the next string in the given buffer. |
abstract void |
read()
Read the next character. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Scanner()
| Method Detail |
|---|
public int nextString(StringConstraint constraint,
StringBuffer buf)
throws IOException
The string stops at the first separator specified by the stop condition of the constraint or at the end of the stream. Each character to append is filtered by the constraint (for example to allow escaping characters).
The characters involved in the stop condition can be preserved (they will be the next characters to read) or not according to the stop condition.
*** It is the responsibility of the {@link StringConstraint#stopCondition(int, int, Scanner)} method to mark and reset the scanner if necessary ***
.In any case the characters involved in the stop condition are not appended to the buffer.
constraint - The non-null constraint that the
string to read has to satisfy.buf - The buffer to which the next string will be appended.
IOException - When an I/O error occur.
public int nextString(StringConstraint constraint)
throws IOException
The string stops at the first separator specified by the stop condition of the constraint or at the end of the stream.
The characters involved in the stop condition can be preserved (they will be the next characters to read) or not according to the stop condition.
*** It is the responsibility of the {@link StringConstraint#stopCondition(int, int, Scanner)} method to mark and reset the scanner if necessary ***
.
constraint - The constraints to apply on the string.
IOException - When an I/O error occur.nextString(StringConstraint, StringBuffer)public char lookAhead()
It is a convenient method that avoids to use a marker for reading a single char.
nextChar()
public char nextChar()
throws IOException
IOException - When an I/O error occur.lookAhead()
public Number nextNumber()
throws IOException
null if none
found. The type of the number will be the more
suitable.
IOException - When an I/O error occur.nextNumber(NumberConstraint)
public Number nextNumber(NumberConstraint constraint)
throws IOException
constraint - The non-null constraint that the
number to read has to satisfy.
null if none
found under the constraints.
IOException - When an I/O error occur.NumberOperator.parseNumber(String, boolean, Class)
public UserData nextObject(ObjectConstraint constraint)
throws IOException
constraint - The non-null constraint that the
object to read has to satisfy.
null.
IOException - When an I/O error occur.public boolean hasNext()
true if at least 1 character
can be read, false if the end of
the input stream was reached.getPosition()public int getPosition()
hasNext()public int getSourceIndex()
nextNumber(),
nextNumber(NumberConstraint),
nextObject(ObjectConstraint),
nextString(StringConstraint),
nextString(StringConstraint, StringBuffer)
public boolean hasNextChar(char c,
boolean consume)
throws IOException
c - The character to test.consume - true if the character found have to be
consumed, false otherwise.
true if the next character in the input
matches the one given, false otherwise.
IOException - When an I/O error occur.
public boolean hasNextChar(String chars,
boolean consume)
throws IOException
chars - The string that contains the characters to test.consume - true if the character found have to be
consumed, false otherwise.
true if the next character in the input
matches one of those given, false otherwise.
IOException - When an I/O error occur.
public char nextChar(String chars,
boolean consume)
throws IOException
chars - The string that contains the characters to test.consume - true if the character found have to be
consumed, false otherwise.
(char) -1 otherwise.
IOException - When an I/O error occur.
public boolean hasNextString(String string,
boolean consume)
throws IOException
string - The string to test.consume - true if the string found have to be
consumed, false otherwise.
true if the string matches the input,
false otherwise.
IOException - When an I/O error occur.
public abstract void read()
throws IOException
IOException - When an I/O error occur.#hasNextChar(char),
nextChar(),
lookAhead()
public abstract void mark(int limit)
throws IOException
This method can be called safely several times.
For each mark set, there should be sooner or later a consume or cancel.
Some convenient methods are available for getting a single
character without using a mark :
lookAhead(), nextChar(), hasNextChar(char, boolean)
and hasNextChar(String, boolean).
limit - The maximum number of characters
that can be read before loosing the mark.
IOException - When an I/O error occur.Reader.mark(int),
cancel(),
consume()
public abstract void cancel()
throws IOException
IOException - When an I/O error occur.
IllegalStateException - When this method is called
whereas no position was marked so far.mark(int)
public abstract void consume()
throws IOException
This implies that the last marked position is removed and the next read goes on from the current position. If there wasn't other marker, it will be impossible to go back. If there was at least another one marker, it can be itself cancelled or consumed independently.
IOException - When an I/O error occur.
IllegalStateException - When this method is called
whereas no position was marked so far.mark(int)
public abstract Reader getRemainder()
throws IOException
null
if the end was reached.
IOException - When an I/O error occur.getRemainderString()
public abstract String getRemainderString()
throws IOException
null
if the end was reached.
IOException - When an I/O error occur.getRemainder()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||