org.inria.ns.reflex.util
Interface StringConstraint

All Superinterfaces:
Constraint

public interface StringConstraint
extends Constraint

A string constraint is used by scanners to read strings under conditions, such as limiting the number of characters to read, or stop reading when a whitespace is encountered.

An implementation is also responsible of the way the characters have to be appended to the target string, for example when some characters can be escaped in the source.

Author:
Philippe Poulard

Field Summary
static StringConstraint WS_CONSTRAINT
          Read only Java whitespaces.
 
Method Summary
 int append(int sourceIndex, int targetLength, Scanner scanner, StringBuffer buf)
          Append the current character to the buffer.
 boolean stopCondition(int sourceIndex, int targetLength, Scanner scanner)
          Evaluate the stop condition from the given parameters.
 

Field Detail

WS_CONSTRAINT

static final StringConstraint WS_CONSTRAINT
Read only Java whitespaces.

See Also:
Character.isWhitespace(char)
Method Detail

stopCondition

boolean stopCondition(int sourceIndex,
                      int targetLength,
                      Scanner scanner)
                      throws IOException
Evaluate the stop condition from the given parameters.

This condition is evaluated by the scanner to check if it has to append characters to the current buffer.

Parameters:
sourceIndex - The number of characters read so far ; might be useful in certain stop conditions.
targetLength - The number of characters put in the target buffer so far ; might be useful in certain stop conditions, for example to limit the length of the string to return.
scanner - The scanner that reads the input. According to the parsing strategy, if the sequence of characters involved in the stop condition (if any) don't have to be consumed, the relevant methods of the scanner should be involved.
Returns:
true to indicate that the current scan must stop, false if more characters have to be read.
Throws:
IOException - When the scanner cause an error.
See Also:
append(int, int, Scanner, StringBuffer)

append

int append(int sourceIndex,
           int targetLength,
           Scanner scanner,
           StringBuffer buf)
           throws IOException
Append the current character to the buffer.

This is a kind of post-process for characters that have to be accepted in the target string. An escape mechanism can be applied here (note that this method could be called by stopCondition(int, int, Scanner) as well). Several characters can be produced as well.

Parameters:
sourceIndex - The position of the index read so far. Start at 0.
targetLength - The number of characters put in the target buffer so far.
scanner - The scanner that reads the input. The characters to read can be scanned as well (if necessary, marks can be used safely).
buf - The buffer where the accepted characters will be appended.
Returns:
The number of characters actually appended to the buffer.
Throws:
IOException - When the scanner cause an error.
See Also:
Scanner#nextString(StringBuffer, StringConstraint)