2010年6月7日

Apache Commons JEXL

Apache Commons JEXL (Java Expression Language)
JEXL Syntax
//-- Main.java --
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;

public class Main {

public static void main(String[] args) throws Exception {
// Create or retrieve a JexlEngine
JexlEngine jexl = new JexlEngine();
// Create an expression object
String jexlExp = "if ((x * 2) == 5) { y = 1; } else {y = 2;}";
Expression e = jexl.createExpression(jexlExp);

// Create a context and add data
JexlContext jc = new MapContext();
jc.set("x", 2.5);

// Now evaluate the expression, getting the result
Object o = e.evaluate(jc);
System.out.println(o);
}

}

沒有留言:

網誌存檔