Java Code Examples for javax.el.ExpressionFactory#createValueExpression()

The following examples show how to use javax.el.ExpressionFactory#createValueExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testJavaKeyWordIdentifier() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterBeanA beanA = new TesterBeanA();
    beanA.setInt("five");
    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("this", var);

    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${this}", String.class);
    } catch (ELException ele) {
        e = ele;
    }
    Assert.assertNotNull(e);
}
 
Example 2
Source File: TestELParser.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaKeyWordIdentifier() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setInt("five");
    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("this", var);

    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${this}", String.class);
    } catch (ELException ele) {
        e = ele;
    }
    assertNotNull(e);
}
 
Example 3
Source File: TestValueExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug50105() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterEnum testEnum = TesterEnum.APPLE;

    ValueExpression var =
        factory.createValueExpression(testEnum, TesterEnum.class);
    context.getVariableMapper().setVariable("testEnum", var);

    // When coercing an Enum to a String, name() should always be used.
    ValueExpression ve1 = factory.createValueExpression(
            context, "${testEnum}", String.class);
    String result1 = (String) ve1.getValue(context);
    Assert.assertEquals("APPLE", result1);

    ValueExpression ve2 = factory.createValueExpression(
            context, "foo${testEnum}bar", String.class);
    String result2 = (String) ve2.getValue(context);
    Assert.assertEquals("fooAPPLEbar", result2);
}
 
Example 4
Source File: TesterVariableMapperImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testSetVariable01() {
    ExpressionFactory factory = ELManager.getExpressionFactory();
    ELContext context = new TesterELContext();
    ValueExpression ve1 =
            factory.createValueExpression(context, "${1}", int.class);
    ValueExpression ve2 =
            factory.createValueExpression(context, "${2}", int.class);
    ValueExpression ve3 =
            factory.createValueExpression(context, "${3}", int.class);

    VariableMapper mapper = new VariableMapperImpl();

    mapper.setVariable("var1", ve1);
    mapper.setVariable("var2", ve2);
    mapper.setVariable("var3", ve3);


    mapper.setVariable("var2", null);

    Assert.assertEquals(ve1, mapper.resolveVariable("var1"));
    Assert.assertNull(mapper.resolveVariable("var2"));
    Assert.assertEquals(ve3, mapper.resolveVariable("var3"));
}
 
Example 5
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Test returning an empty list as a bean property.
 */
@Test
public void testBug51544Bean() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setValList(Collections.emptyList());

    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanA.valList.size()}", Integer.class);

    Integer result = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(0), result);
}
 
Example 6
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void bug56185() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterBeanC beanC = new TesterBeanC();
    ValueExpression var =
        factory.createValueExpression(beanC, TesterBeanC.class);
    context.getVariableMapper().setVariable("myBean", var);

    ValueExpression ve = factory.createValueExpression(context,
        "${(myBean.int1 > 1 and myBean.myBool) or "+
        "((myBean.myBool or myBean.myBool1) and myBean.int1 > 1)}",
        Boolean.class);
    Assert.assertEquals(Boolean.FALSE, ve.getValue(context));
    beanC.setInt1(2);
    beanC.setMyBool1(true);
    Assert.assertEquals(Boolean.TRUE, ve.getValue(context));
}
 
Example 7
Source File: TestValueExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug51544Bean() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterBeanA beanA = new TesterBeanA();
    beanA.setValList(Collections.emptyList());

    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanA.valList.size()}", Integer.class);

    Integer result = (Integer) ve.getValue(context);
    Assert.assertEquals(Integer.valueOf(0), result);
}
 
Example 8
Source File: TestELParser.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void doTestParser(String input, String expectedResult, String expectedBuilderOutput) throws JasperException {

        ELException elException = null;
        String elResult = null;

        // Don't try and evaluate expressions that depend on variables or functions
        if (expectedResult != null) {
            try {
                ExpressionFactory factory = ExpressionFactory.newInstance();
                ELContext context = new ELContextImpl();
                ValueExpression ve = factory.createValueExpression(context, input, String.class);
                elResult = ve.getValue(context).toString();
                Assert.assertEquals(expectedResult, elResult);
            } catch (ELException ele) {
                elException = ele;
            }
        }

        Nodes nodes = null;
        try {
            nodes = ELParser.parse(input, false);
            Assert.assertNull(elException);
        } catch (IllegalArgumentException iae) {
            Assert.assertNotNull(elResult, elException);
            // Not strictly true but enables us to report both
            iae.initCause(elException);
            throw iae;
        }

        TextBuilder textBuilder = new TextBuilder(false);

        nodes.visit(textBuilder);

        Assert.assertEquals(expectedBuilderOutput, textBuilder.getText());
    }
 
Example 9
Source File: TestAstListData.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testGetType() {
    ELProcessor processor = new ELProcessor();
    ELContext context = processor.getELManager().getELContext();
    ExpressionFactory factory = ELManager.getExpressionFactory();

    ValueExpression ve = factory.createValueExpression(
            context, "${['a','b','c','b','c']}", List.class);

    Assert.assertEquals(List.class, ve.getType(context));
    Assert.assertEquals(simpleList, ve.getValue(context));
}
 
Example 10
Source File: TestValueExpressionImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug49345() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    beanA.setBean(beanB);

    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanA.bean.name}", String.class);

    // First check the basics work
    String result = (String) ve.getValue(context);
    assertEquals("Tomcat", result);

    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    assertNotNull(vr);

    assertEquals(beanB, vr.getBase());
    assertEquals("name", vr.getProperty());
}
 
Example 11
Source File: Validator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void prepareExpression(ELNode.Nodes el, Node n, String expr)
        throws JasperException {
    validateFunctions(el, n);

    // test it out
    ELContextImpl ctx = new ELContextImpl(expressionFactory);
    ctx.setFunctionMapper(this.getFunctionMapper(el));
    ExpressionFactory ef = this.pageInfo.getExpressionFactory();
    try {
        ef.createValueExpression(ctx, expr, Object.class);
    } catch (ELException e) {
        throw new JasperException(e);
    }
}
 
Example 12
Source File: ELTools.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates an EL expression into an object.
 *
 * @param p_expression
 *            the expression
 * @throws PropertyNotFoundException
 *             if the attribute doesn't exist at all (as opposed to being null)
 * @return the object
 */
public static Object evalAsObject(String p_expression) throws PropertyNotFoundException {
	FacesContext context = FacesContext.getCurrentInstance();
	ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
	ELContext elContext = context.getELContext();
	ValueExpression vex = expressionFactory.createValueExpression(elContext, p_expression, Object.class);

	Object result = vex.getValue(elContext);
	return result;
}
 
Example 13
Source File: ExpressionEvaluatorImpl.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
public Object evaluate(String expression,
                        Class expectedType,
                        VariableResolver vResolver,
                        FunctionMapper fMapper )
            throws ELException {

    ELContextImpl elContext;
    if (vResolver instanceof VariableResolverImpl) {
        elContext = (ELContextImpl) pageContext.getELContext();
    }
    else {
        // The provided variable Resolver is a custom resolver,
        // wrap it with a ELResolver 
        elContext = new ELContextImpl(new ELResolverWrapper(vResolver));
    }

    javax.el.FunctionMapper fm = new FunctionMapperWrapper(fMapper);
    elContext.setFunctionMapper(fm);
    ExpressionFactory fac = ExpressionFactory.newInstance();
    Object value;
    try {
        ValueExpression expr = fac.createValueExpression(
                             elContext,
                             expression,
                             expectedType);
        value = expr.getValue(elContext);
    } catch (javax.el.ELException ex) {
        throw new ELException(ex);
    }
    return value;
}
 
Example 14
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void testExpression(String expression, String expected) {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    ValueExpression ve = factory.createValueExpression(
            context, expression, String.class);

    String result = (String) ve.getValue(context);
    Assert.assertEquals(expected, result);
}
 
Example 15
Source File: Validator.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void prepareExpression(ELNode.Nodes el, Node n, String expr)
        throws JasperException {
    validateFunctions(el, n);

    // test it out
    ELContextImpl ctx = new ELContextImpl();
    ctx.setFunctionMapper(this.getFunctionMapper(el));
    ExpressionFactory ef = this.pageInfo.getExpressionFactory();
    try {
        ef.createValueExpression(ctx, expr, Object.class);
    } catch (ELException e) {

    }
}
 
Example 16
Source File: TestAstSetData.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testGetType() {
    ELProcessor processor = new ELProcessor();
    ELContext context = processor.getELManager().getELContext();
    ExpressionFactory factory = ELManager.getExpressionFactory();

    ValueExpression ve = factory.createValueExpression(
            context, "${{'a','b','c'}}", Set.class);

    Assert.assertEquals(Set.class, ve.getType(context));
    Assert.assertEquals(simpleSet, ve.getValue(context));
}
 
Example 17
Source File: ElExpressionSample.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void safeEL() {
    FacesContext context = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
    ELContext elContext = context.getELContext();
    ValueExpression vex = expressionFactory.createValueExpression(elContext, "1+1", String.class);
    String result = (String) vex.getValue(elContext);
    System.out.println(result);
}
 
Example 18
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValueReferenceVariable() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var =
        factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    ValueExpression var2 = factory.createValueExpression(
            context, "${beanB.name}", String.class);

    context.getVariableMapper().setVariable("foo", var2);

    ValueExpression ve = factory.createValueExpression(
            context, "${foo}", ValueExpression.class);


    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    assertNotNull(vr);

    assertEquals(beanB, vr.getBase());
    assertEquals("name", vr.getProperty());
}
 
Example 19
Source File: BenchmarkEl.java    From proctor with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) {
    final FunctionMapper functionMapper = new LibraryFunctionMapperBuilder().add("proctor", ProctorRuleFunctions.class).build();

    final ExpressionFactory expressionFactory = new ExpressionFactoryImpl();

    final CompositeELResolver elResolver = new CompositeELResolver();
    elResolver.add(new ArrayELResolver());
    elResolver.add(new ListELResolver());
    elResolver.add(new BeanELResolver());
    elResolver.add(new MapELResolver());

    final Map<String, Object> values = Maps.newLinkedHashMap();
    values.put("countries", Sets.newHashSet("AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM"));
    values.put("AA", "AA");
    values.put("CC", "CC");
    values.put("NN", "NN");
    values.put("ZZ", "ZZ");
    values.put("I1", 239235);
    values.put("I2", 569071142);
    values.put("I3", -189245);
    values.put("D1", 129835.12512);
    values.put("D2", -9582.9385);
    values.put("D3", 98982223.598731412);
    values.put("BT", Boolean.TRUE);
    values.put("BF", Boolean.FALSE);
    values.put("GLOOP", "");

    final String[] expressions = {
            "${proctor:contains(countries, AA) || proctor:contains(countries, CC) || D2 < I3 && BF}",
            "${! proctor:contains(countries, ZZ) && I1 < I2 && empty GLOOP}",
            "${I2 - I3 + D3 - D1}",
            "${NN == '0' && ZZ == 'ZZ'}",
            "${BT != BF}",
    };

    final int iterations = 100*1000;
    long elapsed = -System.currentTimeMillis();
    for (int i = 0; i < iterations; i++) {
        final Map<String, ValueExpression> localContext = ProctorUtils.convertToValueExpressionMap(expressionFactory, values);
        final VariableMapper variableMapper = new MulticontextReadOnlyVariableMapper(localContext);

        final ELContext elContext = new ELContext() {
            @Override
            public ELResolver getELResolver() {
                return elResolver;
            }

            @Override
            public FunctionMapper getFunctionMapper() {
                return functionMapper;
            }

            @Override
            public VariableMapper getVariableMapper() {
                return variableMapper;
            }
        };

        for (int j = 0; j < expressions.length; j++) {
            final ValueExpression ve = expressionFactory.createValueExpression(elContext, expressions[j], Object.class);
            final Object result = ve.getValue(elContext);
            if (i % 10000 == 0) {
                System.out.println(result);
            }
        }
    }
    elapsed += System.currentTimeMillis();

    final int total = iterations * expressions.length;
    System.out.println(total + " expressions in " + elapsed + " ms (average " + (elapsed/(((double) total))) + " ms/expression)");
}
 
Example 20
Source File: Node.java    From Tomcat8-Source-Read with MIT License 3 votes vote down vote up
/**
 * Allow node to validate itself.
 *
 * @param ef The expression factory to use to evaluate any EL
 * @param ctx The context to use to evaluate any EL
 *
 * @throws ELException If validation fails
 */
public void validateEL(ExpressionFactory ef, ELContext ctx)
        throws ELException {
    if (this.el != null) {
        // determine exact type
        ef.createValueExpression(ctx, this.value, String.class);
    }
}