Example usage for org.apache.commons.el Expression evaluate

List of usage examples for org.apache.commons.el Expression evaluate

Introduction

In this page you can find the example usage for org.apache.commons.el Expression evaluate.

Prototype

public abstract Object evaluate(VariableResolver pResolver, FunctionMapper functions, Logger pLogger)
        throws ELException;

Source Link

Document

Evaluates the expression in the given context

Usage

From source file:org.apache.myfaces.el.ValueBindingImpl.java

protected Object resolveToBaseAndProperty(FacesContext facesContext)
        throws ELException, NotVariableReferenceException {
    if (facesContext == null) {
        throw new NullPointerException("facesContext");
    }/*from  www .  jav  a2  s  . c om*/

    VariableResolver variableResolver = new ELVariableResolver(facesContext);
    Object expression = _expression;

    while (expression instanceof ConditionalExpression) {
        ConditionalExpression conditionalExpression = ((ConditionalExpression) expression);
        // first, evaluate the condition (and coerce the result to a
        // boolean value)
        boolean condition = Coercions.coerceToBoolean(conditionalExpression.getCondition()
                .evaluate(variableResolver, s_functionMapper, ELParserHelper.LOGGER), ELParserHelper.LOGGER)
                .booleanValue();

        // then, use this boolean to branch appropriately
        expression = condition ? conditionalExpression.getTrueBranch() : conditionalExpression.getFalseBranch();
    }

    if (expression instanceof NamedValue) {
        return ((NamedValue) expression).getName();
    }

    if (!(expression instanceof ComplexValue)) {
        // all other cases are not variable references
        throw new NotVariableReferenceException(
                "Parsed Expression of unsupported type for this operation. Expression class: "
                        + _expression.getClass().getName() + ". Expression: '" + _expressionString + "'");
    }

    ComplexValue complexValue = (ComplexValue) expression;

    // resolve the prefix
    Object base = complexValue.getPrefix().evaluate(variableResolver, s_functionMapper, ELParserHelper.LOGGER);
    if (base == null) {
        throw new PropertyNotFoundException("Base is null: " + complexValue.getPrefix().getExpressionString());
    }

    // Resolve and apply the suffixes
    List suffixes = complexValue.getSuffixes();
    int max = suffixes.size() - 1;
    for (int i = 0; i < max; i++) {
        ValueSuffix suffix = (ValueSuffix) suffixes.get(i);
        base = suffix.evaluate(base, variableResolver, s_functionMapper, ELParserHelper.LOGGER);
        if (base == null) {
            throw new PropertyNotFoundException("Base is null: " + suffix.getExpressionString());
        }
    }

    // Resolve the last suffix
    ArraySuffix arraySuffix = (ArraySuffix) suffixes.get(max);
    Expression arraySuffixIndex = arraySuffix.getIndex();

    Object index;
    if (arraySuffixIndex != null) {
        index = arraySuffixIndex.evaluate(variableResolver, s_functionMapper, ELParserHelper.LOGGER);
        if (index == null) {
            throw new PropertyNotFoundException("Index is null: " + arraySuffixIndex.getExpressionString());
        }
    } else {
        index = ((PropertySuffix) arraySuffix).getName();
    }

    return new Object[] { base, index };
}