Example usage for org.apache.commons.el ValueSuffix getExpressionString

List of usage examples for org.apache.commons.el ValueSuffix getExpressionString

Introduction

In this page you can find the example usage for org.apache.commons.el ValueSuffix getExpressionString.

Prototype

public abstract String getExpressionString();

Source Link

Document

Returns the expression in the expression language syntax

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");
    }/*  w  w w. j  ava  2s  . com*/

    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 };
}

From source file:org.seasar.teeda.core.el.impl.commons.CommonsExpressionProcessorImpl.java

public Object resolveBase(FacesContext context, Object expression)
        throws ReferenceSyntaxException, PropertyNotFoundException {
    while (expression instanceof ConditionalExpression) {
        ConditionalExpression conditionalExpression = ((ConditionalExpression) expression);
        Object value = evaluate(context, conditionalExpression.getCondition());
        boolean condition = CoercionsUtil.coerceToPrimitiveBoolean(value);
        expression = (condition) ? conditionalExpression.getTrueBranch()
                : conditionalExpression.getFalseBranch();
    }//from ww  w  .j  av  a  2s. c om
    if (expression instanceof NamedValue) {
        return ((NamedValue) expression).getName();
    }
    if (!(expression instanceof ComplexValue)) {
        return null;
    }
    ComplexValue complexValue = (ComplexValue) expression;
    Object base = evaluate(context, complexValue.getPrefix());
    VariableResolver resolver = new ELVariableResolver(context);
    if (base == null) {
        throw new PropertyNotFoundException("Base is null: " + complexValue.getPrefix().getExpressionString());
    }

    List suffixes = complexValue.getSuffixes();
    int max = suffixes.size() - 1;
    for (int i = 0; i < max; i++) {
        ValueSuffix suffix = (ValueSuffix) suffixes.get(i);
        try {
            base = suffix.evaluate(base, resolver, mapper_, CommonsElLogger.getLogger());
        } catch (ELException e) {
            throw new EvaluationException(e);
        }
        if (base == null) {
            throw new PropertyNotFoundException("Base is null: " + suffix.getExpressionString());
        }
    }
    ArraySuffix arraySuffix = (ArraySuffix) suffixes.get(max);
    Expression arraySuffixIndex = arraySuffix.getIndex();
    Object index;
    if (arraySuffixIndex != null) {
        index = evaluate(context, arraySuffixIndex);
        if (index == null) {
            throw new PropertyNotFoundException("Index is null: " + arraySuffixIndex.getExpressionString());
        }
    } else {
        index = ((PropertySuffix) arraySuffix).getName();
    }
    return new Object[] { base, index };
}