I have a little functionality where I need to determine if a user-created rule is syntactically valid.
We believe that the structure of what I create is as follows:
- 1 == 1
- 1 + 1 == 1
- 1 + 1 == 1 OR 1 == 1
- Additional combinations of the above examples
These expressions are stored in a string variable, for example:
String expression = "";
while(items.hasNext())
{
String currentItem = items.next();
expression += currentItem.value();
}
REAL EXPRESSIONS
Valid expressions are those that have a logical operator (<, <=, ==, =>,>), and the output will be true or false (it does not matter which one)
- 1 == 1
- 1 <2
- 1 == 1 OR 1 <4
- 4 == 9 OR 9 == 3
WRONG EXPRESSIONS
Invalid expressions are those that do not have the proper structure to determine if this expression is true or false.
Boolean.valueOf()
Boolean.parse()