http://www.eteks.com

com.eteks.parser
Class ExpressionParser

java.lang.Object
  |
  +--com.eteks.parser.Parser
        |
        +--com.eteks.parser.FunctionParser
              |
              +--com.eteks.parser.ExpressionParser
Direct Known Subclasses:
JeksExpressionParser

public class ExpressionParser
extends FunctionParser

Parser able to compile expressions with parameters of the form = expression operating on parameters. The expression may contain unary or binary operators, conditions, common functions or other functions operating on literals, constants and paramaters checked on the fly.
A parser is associated to a syntax, instance of the Syntax interface that describes the different lexicals used by the parser and their type. This parser uses also an instance of ExpressionParameter used to check the parameters of parsed expressions and to return their value at runtime.
The CompiledExpression compileExpression (String expressionDefinition) method of this class allows to parse and compile the definition expressionDefinition of an expression, that must be made of the following elements :

White spaces returned by the getWhiteSpaceCharacters () method of the syntax may be used anywhere in the definition of the function.
If the compileExpression () method fails to compile a definition, it throws an CompilationException exception that describe the found error (syntax error or other error).
Once an expression is compiled, the computeExpression () methods available for the returned object allow to compute its value with the value of its parameters returned by the getParameterValue () method of its instance of ExpressionParameter.

Here are a few examples of expressions that can be compiled with this parser. This example shows how to compute new column values from the columns of a JDBC result set :

 // A class implementing ExpressionParameter supporting the name of columns of a ResultSet
 class ResultSetColumnParameter implements ExpressionParameter
 {
   ResultSet resultSet;

   public ResultSetColumnParameter (ResultSet resultSet)
   {
     this.resultSet = resultSet;
   }

   public Object getParameterKey (String parameter)
   {
     try
     {
       // Check if parameter exists among the names of columns
       if (resultSet.findColumn (parameter) > 0)
         return parameter;
     }
     catch (SQLException e)
     { }
     return null;
   }

   public Object getParameterValue (Object parameterKey)
   {
     try
     {
       // Simply return the value of the column in current row
       return resultSet.getObject ((String)parameterKey);
     }
     catch (SQLException e)
     {
       throw new IllegalArgumentException (e.getMessage ());
     }
   }
 }

 // Connect to a database and select values
 Connection connection = DriverManager.getConnection ("jdbc:....");
 Statement  statement = connection.createStatement ();
 ResultSet  resultSet = statement.executeQuery ("SELECT X, Y, Z FROM TABLE1");

 // Create a parser with the syntax DefaultSyntax
 // and an instance of ResultSetColumnParameter
 ExpressionParser parser = new ExpressionParser (new ResultSetColumnParameter (resultSet));

 // Compile expressions with this parser
 CompiledExpression ex1 = parser.compileExpression ("= X * LN (X)");
 CompiledExpression ex2 = parser.compileExpression ("= 5.E-1 * (X + Y) ^ Z");

 // Display the values of resultSet and
 // the new values computed with default interpreter DoubleInterpreter
 System.out.println ("X\tY\tZ\tX * LN (X)\t5.E-1 * (X + Y) ^ Z");
 while (resultSet.next ())
   System.out.println (  String.valueOf (resultSet.getObject ("X")) + "\t"
                       + String.valueOf (resultSet.getObject ("Y")) + "\t"
                       + String.valueOf (resultSet.getObject ("Z")) + "\t"
                       + String.valueOf (ex1.computeExpression ()) + "\t"
                       + String.valueOf (ex2.computeExpression ()));
The methods of this class are thread safe (if the methods of the class implementing ExpressionParameter are also thread safe).

Since:
Jeks 1.0
Version:
1.0
Author:
Emmanuel Puybaret
See Also:
Syntax, CompiledExpression, ExpressionParameter, JeksParameter, JeksCellEditor

Constructor Summary
ExpressionParser(ExpressionParameter expressionParameter)
          Creates a parser for expressions whose syntax is an instance of DefaultSyntax.
ExpressionParser(Syntax syntax, ExpressionParameter expressionParameter)
          Creates a parser for expressions with the syntax syntax.
 
Method Summary
 CompiledExpression compileExpression(java.lang.String expressionDefinition)
          Compiles the definition of an expression expressionDefinition and returns the instance of CompiledExpression matching the definition.
 ExpressionParameter getExpressionParameter()
          Returns the instance of ExpressionParameter of this parser.
 
Methods inherited from class com.eteks.parser.FunctionParser
compileFunction, getSyntax
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionParser

public ExpressionParser(ExpressionParameter expressionParameter)
Creates a parser for expressions whose syntax is an instance of DefaultSyntax.
Parameters:
expressionParameter - the instance of ExpressionParameter used to check parameters of parsed expressions.

ExpressionParser

public ExpressionParser(Syntax syntax,
                        ExpressionParameter expressionParameter)
Creates a parser for expressions with the syntax syntax.
Parameters:
syntax - syntax of the parser.
expressionParameter - the instance of ExpressionParameter used to check parameters of parsed expressions.
Method Detail

getExpressionParameter

public ExpressionParameter getExpressionParameter()
Returns the instance of ExpressionParameter of this parser.
Returns:
the instance of ExpressionParameter of this parser.

compileExpression

public CompiledExpression compileExpression(java.lang.String expressionDefinition)
                                     throws CompilationException
Compiles the definition of an expression expressionDefinition and returns the instance of CompiledExpression matching the definition. This method builds a tree of type ExpressionNode matching the expression. This tree is used to compute the value of an expression at interpretation time.
Parameters:
expressionDefinition - the definition of the expression.
Returns:
an instance of CompiledExpression, with which values can be computed.
Throws:
CompilationException - if the syntax of the expression is incorrect or if some of its elements (literals, constants, operators, functions,...) are not accepted by the syntax of the parser.

&cp; 1998-2003 eTeks - All rights reserved