uk.org.ogsadai.activity.files
Class SubstitutionStringParser

java.lang.Object
  |
  +--uk.org.ogsadai.activity.files.SubstitutionStringParser

public class SubstitutionStringParser
extends java.lang.Object

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:

STRING ::= CHAR | CHAR STRING
CHAR ::= \ c | c | VAR
VAR ::= $ INT | $ { INT }
INT ::= n | n INT
where n is an integer, 0 <= n <= 9; c is a character.

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.

Author:
The OGSA-DAI Project Team

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

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
Copyright statement

See Also:
Constant Field Values

mString

private java.lang.String mString
The string containing the group variables.


mGroups

private java.lang.String[] mGroups
An array of the text matching each group in the matcher's regular expression.


mIndex

private int mIndex
The offset of the character in the string that we are currently considering.


mToken

private char mToken
The character in the string that we are currently considering.

Constructor Detail

SubstitutionStringParser

public 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.

Parameters:
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 variables
Method Detail

lex

public void lex()
Move forward to consider the next character in the string.


readString

public java.lang.String readString()
                            throws MalformedReplacementStringException
Return the original string, but with the group variables replaced by the corresponding values from the matched groups of text. This corresponds to the STRING non-terminal in the grammar.

Returns:
the string, with appropriate replacements made
Throws:
MalformedReplacementStringException - If the string could not be parsed

readChar

private java.lang.String readChar()
                           throws MalformedReplacementStringException
From the current position in the string, read an instance of the CHAR non-terminal symbol in the grammar.

Returns:
the value of the CHAR non-terminal
Throws:
MalformedReplacementStringException - If the CHAR could not be read

readVar

private java.lang.String readVar()
                          throws MalformedReplacementStringException
From the current position in the string, read an instance of the VAR non-terminal symbol in the grammar.

Returns:
the value of the VAR non-terminal
Throws:
MalformedReplacementStringException - If the VAR could not be read

readInt

private int readInt()
             throws MalformedReplacementStringException
From the current position in the string, read an instance of the INT non-terminal symbol in the grammar.

Returns:
the value of the INT non-terminal
Throws:
MalformedReplacementStringException - If the INT could not be read

isNumber

private static boolean isNumber(char c)
Return whether a character is an integer in the range [0, 9].

Parameters:
c - A character to test
Returns:
true if the character is a digit, false otherwise

variable

private java.lang.String variable(int var)
Return the value of the text match corresponding to a particular group in the regular expression.

Parameters:
var - The number of the group to obtain the value of
Returns:
the text matched by the parenthesised group of the regular expression

outputToken

private java.lang.String outputToken(char token)
Convert a character to a string containing just that character.

Parameters:
token - The character
Returns:
the corresponding string