javax.el.ValueExpression Java Examples

The following examples show how to use javax.el.ValueExpression. 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: AstIdentifier.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public boolean isReadOnly(EvaluationContext ctx) throws ELException {
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            return expr.isReadOnly(ctx.getELContext());
        }
    }
    ctx.setPropertyResolved(false);
    boolean result = ctx.getELResolver().isReadOnly(ctx, null, this.image);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled.null", this.image));
    }
    return result;
}
 
Example #2
Source File: AstIdentifier.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void setValue(EvaluationContext ctx, Object value)
        throws ELException {
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            expr.setValue(ctx.getELContext(), value);
            return;
        }
    }
    ctx.setPropertyResolved(false);
    ctx.getELResolver().setValue(ctx, null, this.image, value);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled.null", this.image));
    }
}
 
Example #3
Source File: AstIdentifier.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public ValueReference getValueReference(EvaluationContext ctx) {
    VariableMapper varMapper = ctx.getVariableMapper();

    if (varMapper == null) {
        return null;
    }

    ValueExpression expr = varMapper.resolveVariable(this.image);

    if (expr == null) {
        return null;
    }

    return expr.getValueReference(ctx);
}
 
Example #4
Source File: ExpressionEvaluatorImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public Expression parseExpression(String expression,
        @SuppressWarnings("rawtypes") // API does not use generics
        Class expectedType,
        FunctionMapper fMapper) throws ELException {
    try {
        ELContextImpl ctx =
            new ELContextImpl(ELContextImpl.getDefaultResolver());
        if (fMapper != null) {
            ctx.setFunctionMapper(new FunctionMapperImpl(fMapper));
        }
        ValueExpression ve = this.factory.createValueExpression(ctx, expression, expectedType);
        return new ExpressionImpl(ve);
    } catch (javax.el.ELException e) {
        throw new ELParseException(e.getMessage());
    }
}
 
Example #5
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 #6
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 #7
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug53792b() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name.length()}", java.lang.Integer.class);
    Integer actual = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(BUG53792.length()), actual);
}
 
Example #8
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsParametersProvided() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var =
        factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    MethodExpression me1 = factory.createMethodExpression(
            context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 = factory.createMethodExpression(
            context, "${beanB.sayHello('JUnit')}", String.class,
            new Class<?>[] { String.class });

    assertFalse(me1.isParmetersProvided());
    assertTrue(me2.isParmetersProvided());
}
 
Example #9
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug50105() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    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);
    assertEquals("APPLE", result1);

    ValueExpression ve2 = factory.createValueExpression(
            context, "foo${testEnum}bar", String.class);
    String result2 = (String) ve2.getValue(context);
    assertEquals("fooAPPLEbar", result2);
}
 
Example #10
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 #11
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 #12
Source File: TestMethodExpressionImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug53792a() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name}", java.lang.String.class);
    String actual = (String) ve.getValue(context);
    assertEquals(BUG53792, actual);
}
 
Example #13
Source File: TestValueExpressionImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug51177ObjectMap() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    Object o1 = "String value";
    Object o2 = Integer.valueOf(32);

    Map<Object,Object> map = new HashMap<Object,Object>();
    map.put("key1", o1);
    map.put("key2", o2);

    ValueExpression var =
        factory.createValueExpression(map, Map.class);
    context.getVariableMapper().setVariable("map", var);

    ValueExpression ve1 = factory.createValueExpression(
            context, "${map.key1}", Object.class);
    ve1.setValue(context, o2);
    assertEquals(o2, ve1.getValue(context));

    ValueExpression ve2 = factory.createValueExpression(
            context, "${map.key2}", Object.class);
    ve2.setValue(context, o1);
    assertEquals(o1, ve2.getValue(context));
}
 
Example #14
Source File: MulticontextReadOnlyVariableMapper.java    From proctor with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ValueExpression resolve(final String variableName, @Nonnull final Map<String, ValueExpression>...contexts) {
    for (final Map<String, ValueExpression> context : contexts) {
        final ValueExpression ve = context.get(variableName);
        if (ve != null) {
            return ve;
        }
    }
    return null;
}
 
Example #15
Source File: ELTools.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Yields the type of the variable displayed by a component.
 *
 * @param p_component
 *            the UIComponent
 * @return the type (as class)
 */
public static Class<?> getType(UIComponent p_component) {
	ValueExpression valueExpression = p_component.getValueExpression("value");
	if (valueExpression != null) {
		FacesContext context = FacesContext.getCurrentInstance();
		ELContext elContext = context.getELContext();
		return valueExpression.getType(elContext);
	}
	return null;
}
 
Example #16
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug52970() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanEnum.submit('APPLE')}", null ,
            new Class<?>[] { TesterBeanEnum.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanEnum.lastSubmitted}", TesterEnum.class);
    TesterEnum actual = (TesterEnum) ve.getValue(context);
    assertEquals(TesterEnum.APPLE, actual);

}
 
Example #17
Source File: TestELParser.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void testExpression(String expression, String expected) {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

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

    String result = (String) ve.getValue(context);
    assertEquals(expected, result);
}
 
Example #18
Source File: ElExpressionSample.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void unsafeEL(String expression) {
    FacesContext context = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
    ELContext elContext = context.getELContext();
    ValueExpression vex = expressionFactory.createValueExpression(elContext, expression, String.class);
    String result = (String) vex.getValue(elContext);
    System.out.println(result);
}
 
Example #19
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuper() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanBB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    ValueExpression ve = factory.createValueExpression(context,
            "${beanA.bean.name}", String.class);
    Object r = ve.getValue(context);
    assertEquals("BB", r);
}
 
Example #20
Source File: UelUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Evaluates a Unified Expression Language expression and sets the resulting object
 * to the specified value.
 * @param context Evaluation context (variables)
 * @param expression UEL expression
 * @param expectedType The expected object Class to set
 */
public static void setValue(Map<String, Object> context, String expression, Class<?> expectedType, Object value) {
    if (Debug.verboseOn()) {
        Debug.logVerbose("UelUtil.setValue invoked, expression = " + expression + ", value = " + value, module);
    }
    ELContext elContext = new BasicContext(context);
    ValueExpression ve = exprFactory.createValueExpression(elContext, expression, expectedType);
    ve.setValue(elContext, value);
}
 
Example #21
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 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 {
                ELManager manager = new ELManager();
                ELContext context = manager.getELContext();
                ExpressionFactory factory = ELManager.getExpressionFactory();
                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 #22
Source File: RuleEvaluator.java    From proctor with Apache License 2.0 5 votes vote down vote up
public boolean evaluateBooleanRule(final String rule, @Nonnull final Map<String, Object> values) throws IllegalArgumentException {
    if (StringUtils.isBlank(rule)) {
        return true;
    }
    if (!rule.startsWith("${") || !rule.endsWith("}")) {
        LOGGER.error("Invalid rule '" +  rule + "'");   //  TODO: should this be an exception?
        return false;
    }
    final String bareRule = ProctorUtils.removeElExpressionBraces(rule);
    if (StringUtils.isBlank(bareRule) || "true".equalsIgnoreCase(bareRule)) {
        return true;    //  always passes
    }
    if ("false".equalsIgnoreCase(bareRule)) {
        return false;
    }

    final ELContext elContext = createElContext(values);
    final ValueExpression ve = expressionFactory.createValueExpression(elContext, rule, boolean.class);
    checkRuleIsBooleanType(rule, elContext, ve);

    final Object result = ve.getValue(elContext);

    if (result instanceof Boolean) {
        return ((Boolean) result);
    }
    // this should never happen, evaluateRule throws ELException when it cannot coerce to Boolean
    throw new IllegalArgumentException("Received non-boolean return value: "
            + (result == null ? "null" : result.getClass().getCanonicalName())
            + " from rule " + rule);
}
 
Example #23
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 #24
Source File: MapBasedVariableMapper.java    From validator-web with Apache License 2.0 5 votes vote down vote up
@Override
public ValueExpression setVariable(String variable, ValueExpression expression) {
    if (map.isEmpty()) {
        map = new HashMap<String, ValueExpression>();
    }
    return map.put(variable, expression);
}
 
Example #25
Source File: DateTimePicker.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Manage EL-expression for snake-case attributes
 */
public void setValueExpression(String name, ValueExpression binding) {
	name = BsfUtils.snakeCaseToCamelCase(name);
	super.setValueExpression(name, binding);
	
	if ("iconAwesome".equals(name))
		AddResourcesListener.setNeedsFontsAwesome(this);
	else if ("iconBrand".equals(name) || "iconLight".equals(name) || "iconRegular".equals(name) || "iconSolid".equals(name))
		AddResourcesListener.setFontAwesomeVersion(5, this);
}
 
Example #26
Source File: FaceletsAuthorizeTag.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
private String getAttributeValue(FaceletContext faceletContext, TagAttribute tagAttribute, boolean evaluate) {
	String value = null;
	if (tagAttribute != null) {
		if (evaluate) {
			ValueExpression expression = tagAttribute.getValueExpression(faceletContext, String.class);
			value = (String) expression.getValue(faceletContext.getFacesContext().getELContext());
		}
		else {
			value = tagAttribute.getValue();
		}
	}
	return value;
}
 
Example #27
Source File: TestValueExpressionImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testGetValueReferenceVariable() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    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);
    Assert.assertNotNull(vr);

    Assert.assertEquals(beanB, vr.getBase());
    Assert.assertEquals("name", vr.getProperty());
}
 
Example #28
Source File: ELContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public ValueExpression resolveVariable(String variable) {
    if (vars == null) {
        return null;
    }
    return vars.get(variable);
}
 
Example #29
Source File: PageContextImpl.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
public static void setMethodVariable(PageContext pageContext,
                                     String variable,
                                     MethodExpression expression) {
    ExpressionFactory expFactory = getExpressionFactory(pageContext);
    ValueExpression exp =
        expFactory.createValueExpression(expression, Object.class);
    setValueVariable(pageContext, variable, exp);
}
 
Example #30
Source File: TestMethodExpressionImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug50790a() throws Exception {
    ValueExpression ve = factory.createValueExpression(context,
            "#{beanAA.name.contains(beanA.name)}", java.lang.Boolean.class);
    Boolean actual = (Boolean) ve.getValue(context);
    Assert.assertEquals(Boolean.TRUE, actual);
}