Package de.odysseus.el

Examples of de.odysseus.el.ExpressionFactoryImpl


* @version
*/
public class JuelTest extends TestCase {

    public void testJuel() throws Exception {
        ExpressionFactory factory = new ExpressionFactoryImpl();
        ELContext context  = new SimpleContext();
        ValueExpression valueExpression = factory.createValueExpression(context, "${123 * 2}", Object.class);
        Object value = valueExpression.getValue(context);

        assertEquals("Result is a Long object", 246L, value);
    }
View Full Code Here


* Other expressions are evaluated.
* An empty line terminates the calculator.
*/
public class Calculator {
  public static void main(String[] args) throws NoSuchMethodException, IOException {
    ExpressionFactory factory = new ExpressionFactoryImpl();

    SimpleContext context = new SimpleContext();

    // variables e, pi
    context.setVariable("e", factory.createValueExpression(Math.E, double.class));
    context.setVariable("pi", factory.createValueExpression(Math.PI, double.class));
   
    // functions sin, cos, tan, exp, log, abs, sqrt, min, max, pow
    context.setFunction("", "sin", Math.class.getMethod("sin", double.class));
    context.setFunction("", "cos", Math.class.getMethod("cos", double.class));
    context.setFunction("", "tan", Math.class.getMethod("tan", double.class));
    context.setFunction("", "exp", Math.class.getMethod("exp", double.class));
    context.setFunction("", "log", Math.class.getMethod("log", double.class));
    context.setFunction("", "abs", Math.class.getMethod("abs", double.class));
    context.setFunction("", "sqrt", Math.class.getMethod("sqrt", double.class));
    context.setFunction("", "min", Math.class.getMethod("min", double.class, double.class));
    context.setFunction("", "max", Math.class.getMethod("max", double.class, double.class));
    context.setFunction("", "pow", Math.class.getMethod("pow", double.class, double.class));

    // print out the rules of the game...
    System.out.println("> Enter one expression per line (without \"${\" and \"}\"). An expressions matching");
    System.out.println("> \"m[0-9]*\" saves the previous evaluation result to \"memory\". Other expressions");
    System.out.println("> are simply evaluated. Functions are sin, cos, tan, exp, log, abs, sqrt, min,");
    System.out.println("> max and pow, variables are e and pi. An empty line terminates the calculator.");
   
    // read/evaluate expressions
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    Object display = 0;
    while (true) {
      System.out.print("< ");
      String line = reader.readLine().trim();
      if (line.length() == 0) {
        System.out.println("> Good bye.");
        System.exit(0);
      }
      try {
        ValueExpression expr =
          factory.createValueExpression(context, "${" + line + "}", Object.class);
        if (line.matches("m[0-9]*")) { // "save to memory"
          expr.setValue(context, display);
        } else {
          display = expr.getValue(context);
        }
View Full Code Here

    // create our customized builder
    TreeBuilder builder = new Builder(Builder.Feature.METHOD_INVOCATIONS);

    // create our factory which uses our customized builder
    ExpressionFactory f = new ExpressionFactoryImpl(new TreeStore(builder, new Cache(10)));

    // create our resolver
    ELResolver resolver = new MethodResolver(method);

    // create our context
    ELContext context = new SimpleContext(resolver);

    // let's go...
    ValueExpression e = null;
   
    e = f.createValueExpression(context, "${'foo'.matches('foo|bar')}", boolean.class);
    System.out.println(e.getValue(context)); // --> true
   
    e = f.createValueExpression(context, "${'bar'.matches('foo|bar')}", boolean.class);
    System.out.println(e.getValue(context)); // --> true
   
    e = f.createValueExpression(context, "${'baz'.matches('foo|bar')}", boolean.class);
    System.out.println(e.getValue(context)); // --> false
  }
View Full Code Here

  public static void main(String... args) throws NoSuchMethodException {
    // create our customized builder
    TreeBuilder builder = new Builder(Builder.Feature.NULL_PROPERTIES);

    // create our factory which uses our customized builder
    ExpressionFactory f = new ExpressionFactoryImpl(new TreeStore(builder, new Cache(10)));

    // create our context
    ELContext context = new SimpleContext();

    // create our expression we want to evaluate
    ValueExpression e = f.createValueExpression(context, "${map[null]}", String.class);

    // create a map containing a value for key <code>null</code> and make it available
    Map<String, String> map = new HashMap<String, String>();
    map.put(null, "foo");
    context.getELResolver().setValue(context, null, "map", map);
View Full Code Here

*/
public class VarArgs {
  public static void main(String... args) throws NoSuchMethodException {
    // create our factory which uses our customized builder
    System.setProperty("javax.el.varArgs", "true");   
    ExpressionFactory f = new ExpressionFactoryImpl(System.getProperties());

    // create our context with function "vararg:format"
    Method method = String.class.getMethod("format", new Class[]{String.class, Object[].class});
    SimpleContext context = new SimpleContext();
    context.setFunction("varargs", "format", method);

    // our expression we want to evaluate
    String expression = "${varargs:format('Hey %s','Joe')}";

    // let's go...
    ValueExpression e = f.createValueExpression(context, expression, String.class);
    System.out.println(e.getValue(context)); // --> Hey Joe
  }
View Full Code Here

* Other expressions are evaluated.
* An empty line terminates the calculator.
*/
public class Calculator {
  public static void main(String[] args) throws NoSuchMethodException, IOException {
    ExpressionFactory factory = new ExpressionFactoryImpl();

    SimpleContext context = new SimpleContext();

    // variables e, pi
    context.setVariable("e", factory.createValueExpression(Math.E, double.class));
    context.setVariable("pi", factory.createValueExpression(Math.PI, double.class));
   
    // functions sin, cos, tan, exp, log, abs, sqrt, min, max, pow
    context.setFunction("", "sin", Math.class.getMethod("sin", double.class));
    context.setFunction("", "cos", Math.class.getMethod("cos", double.class));
    context.setFunction("", "tan", Math.class.getMethod("tan", double.class));
    context.setFunction("", "exp", Math.class.getMethod("exp", double.class));
    context.setFunction("", "log", Math.class.getMethod("log", double.class));
    context.setFunction("", "abs", Math.class.getMethod("abs", double.class));
    context.setFunction("", "sqrt", Math.class.getMethod("sqrt", double.class));
    context.setFunction("", "min", Math.class.getMethod("min", double.class, double.class));
    context.setFunction("", "max", Math.class.getMethod("max", double.class, double.class));
    context.setFunction("", "pow", Math.class.getMethod("pow", double.class, double.class));

    // print out the rules of the game...
    System.out.println("> Enter one expression per line (without \"${\" and \"}\"). An expressions matching");
    System.out.println("> \"m[0-9]*\" saves the previous evaluation result to \"memory\". Other expressions");
    System.out.println("> are simply evaluated. Functions are sin, cos, tan, exp, log, abs, sqrt, min,");
    System.out.println("> max and pow, variables are e and pi. An empty line terminates the calculator.");
   
    // read/evaluate expressions
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    Object display = 0;
    while (true) {
      System.out.print("< ");
      String line = reader.readLine();
      if (line != null) {
        line = line.trim();
      }
      if (line == null || line.length() == 0) {
        System.out.println("> Good bye.");
        System.exit(0);
      }
      try {
        ValueExpression expr =
          factory.createValueExpression(context, "${" + line + "}", Object.class);
        if (line.matches("m[0-9]*")) { // "save to memory"
          expr.setValue(context, display);
        } else {
          display = expr.getValue(context);
        }
View Full Code Here

    return new ExtendedParser(this, expression);
  }
 
  public static void main(String[] args) {
    System.setProperty(TreeBuilder.class.getName(), SyntaxExtension.class.getName());   
    ExpressionFactory factory = new ExpressionFactoryImpl(System.getProperties());
   
    String[] expressions = {
        "${'abab' matches '(ab)*'}",
        "${'abab' matches '(ba)*'}",
        "${'abab' ~ '(ab)*'}",
        "${'abab' ~ '(ba)*'}"
    };

    SimpleContext context = new SimpleContext();
    for (String expression : expressions) {
      ValueExpression e = factory.createValueExpression(context, expression, boolean.class);
      System.out.println(expression + " --> " + e.getValue(context));
    }
  }
View Full Code Here

  }

  public static void main(String... args) throws NoSuchMethodException {
    // create our factory which uses our customized builder
    System.setProperty("javax.el.methodInvocations", "true");   
    ExpressionFactory f = new ExpressionFactoryImpl(System.getProperties());

    // create our resolver
    CompositeELResolver resolver = new CompositeELResolver();
    resolver.add(new PublicMethodResolver());
    resolver.add(new BeanELResolver());

    // create our context
    ELContext context = new SimpleContext(resolver);

    // let's go...
    ValueExpression e = null;
   
    e = f.createValueExpression(context, "${'foo'.matches('foo|bar')}", boolean.class);
    System.out.println(e.getValue(context)); // --> true
   
    e = f.createValueExpression(context, "${'bar'.toUpperCase()}", String.class);
    System.out.println(e.getValue(context)); // --> BAR
   
    e = f.createValueExpression(context, "${'foobar '.trim().length()}", int.class);
    System.out.println(e.getValue(context)); // --> 6
  }
View Full Code Here

*/
public class NullProperties {
  public static void main(String... args) throws NoSuchMethodException {
    // create our factory which uses our customized builder
    System.setProperty("javax.el.nullProperties", "true");   
    ExpressionFactory f = new ExpressionFactoryImpl(System.getProperties());

    // create our context
    ELContext context = new SimpleContext();

    // create our expression we want to evaluate
    ValueExpression e = f.createValueExpression(context, "${map[null]}", String.class);

    // create a map containing a value for key <code>null</code> and make it available
    Map<String, String> map = new HashMap<String, String>();
    map.put(null, "foo");
    context.getELResolver().setValue(context, null, "map", map);
View Full Code Here

  }
 
 
  private ExpressionFactory newExpressionFactory(CacheProvider cacheProvider) {
    TreeStore store = new TreeStore(new Builder(), createTreeCache(cacheProvider));
    return new ExpressionFactoryImpl(store, typeConverter);
  }
View Full Code Here

TOP

Related Classes of de.odysseus.el.ExpressionFactoryImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.