Expression Interpreter

The expression interpreter is a central component which adds the capability to dynamical evaluate contents. It is heavily used int the printing and the web module.

Click here to browse the API documentation of the current version.

The basic syntax is very similar to the new JAVA Expression Language, introduced with the JSP 2.0 specification, but this Expression Interpreter can do much more.

You can easily use the ExpressionInterpreter from your own code. Here's a simple example.
try {
  ExpressionInterpreter exi = new ExpressionInterpreter();
  ExpressionContext ctx = new ExpressionContext();
  ctx.setProperty("word", "UJAC");
  ctx.setProperty("number", new Integer(10));
  System.out.println("expression result: " + exi.evalString("${${word} + '-' + ${number + 1}}", properties));
} catch (ExpressionException ex) {
  // Oops, something went wrong ...

The expression interpreter can be used in various ways:
if (exi.evalBoolean("${word isDefined}", ctx)) {
Evaluates a text that is allowed to contain exactly one expression as a boolean.

int len = exi.evalInt("${word length}", ctx);
Evaluates a text that is allowed to contain exactly one expression as a int.

It provides type specific handlers to add support for various classes. By default, the Expression Interpreter handles objects according to the JavaBean standard, which means it provides access to the object's properties using the getter methods. In case the object's class matches a registered type, it adopts the behaviour of this type.

Look at the type handler documentation to see which operations are implemented.

Adding support for your own custom types is very easy, you only have to implement a type class, extending the class org.ujac.util.exi.type.BaseType. Within this type class you have to implement the operations of this type. The operations are also classes, which have to implement the very simple interface org.ujac.util.exi.ExpressionOperation. After registering the type handler at the Expression Interpreter instance using its method registerTypeHandler, it automatically handles the type according to your specified expressions.

Now the syntax! Expressions can be simple like this:
Gets the field 'field' from the table 'table'
${1 + 2 + 3}
Gets the result of the calculation (6 in this case - I guess).

The processing of complex operations has been moved from the ExpressionInterpreter to the TemplateInterpreter component.