|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--uk.org.ogsadai.activity.files.SubstitutionStringParser
Implementation of a recursive-descent parser intended to parse strings which contain variables represented by $
n or ${
n}
, where n is a non-negative integer.
The grammar from which valid strings are drawn is:
where n is an integer, 0 <= n <= 9; c is a character.
STRING ::=
CHAR | CHAR STRING CHAR ::=
\
c | c | VARVAR ::=
$
INT |$
{
INT}
INT ::=
n | n INT
This class is intended to be used in the following way:
String[] groups = matcher.groups();
SubstitutionStringParser parser = new SubstitutionStringParser(groups, string);
String result = parser.readString();
Initially, we have a matcher
object, of type AbstractMatcher
, which has already found a match of its regular expression. The string
object, of type String
, is a string containing group variables. The resulting result
string contains the contents of string
with group variables replaced by the matching text in the appropriate element of the groups
array.
Field Summary | |
private static java.lang.String |
COPYRIGHT_NOTICE Copyright statement |
private java.lang.String[] |
mGroups An array of the text matching each group in the matcher's regular expression. |
private int |
mIndex The offset of the character in the string that we are currently considering. |
private java.lang.String |
mString The string containing the group variables. |
private char |
mToken The character in the string that we are currently considering. |
Constructor Summary | |
SubstitutionStringParser(java.lang.String[] groups, java.lang.String string) Construct a new parser, with supplied text matched by groups in a regular expression, and a string containing group variables. |
Method Summary | |
private static boolean |
isNumber(char c) Return whether a character is an integer in the range [0, 9]. |
void |
lex() Move forward to consider the next character in the string. |
private java.lang.String |
outputToken(char token) Convert a character to a string containing just that character. |
private java.lang.String |
readChar() From the current position in the string, read an instance of the CHAR non-terminal symbol in the grammar. |
private int |
readInt() From the current position in the string, read an instance of the INT non-terminal symbol in the grammar. |
java.lang.String |
readString() Return the original string, but with the group variables replaced by the corresponding values from the matched groups of text. |
private java.lang.String |
readVar() From the current position in the string, read an instance of the VAR non-terminal symbol in the grammar. |
private java.lang.String |
variable(int var) Return the value of the text match corresponding to a particular group in the regular expression. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final java.lang.String COPYRIGHT_NOTICE
private java.lang.String mString
private java.lang.String[] mGroups
private int mIndex
private char mToken
Constructor Detail |
public SubstitutionStringParser(java.lang.String[] groups, java.lang.String string)
groups
- The text matched by the parenthesised groups in the regular expression. The element at index zero is the string matched by the entire regular expression.string
- The string containing group variablesMethod Detail |
public void lex()
public java.lang.String readString() throws MalformedReplacementStringException
MalformedReplacementStringException
- If the string could not be parsedprivate java.lang.String readChar() throws MalformedReplacementStringException
MalformedReplacementStringException
- If the CHAR could not be readprivate java.lang.String readVar() throws MalformedReplacementStringException
MalformedReplacementStringException
- If the VAR could not be readprivate int readInt() throws MalformedReplacementStringException
MalformedReplacementStringException
- If the INT could not be readprivate static boolean isNumber(char c)
c
- A character to testtrue
if the character is a digit, false
otherwiseprivate java.lang.String variable(int var)
var
- The number of the group to obtain the value ofprivate java.lang.String outputToken(char token)
token
- The character
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |