Example usage for org.apache.commons.el ComplexValue getPrefix

List of usage examples for org.apache.commons.el ComplexValue getPrefix

Introduction

In this page you can find the example usage for org.apache.commons.el ComplexValue getPrefix.

Prototype

public Expression getPrefix() 

Source Link

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.j a  v  a 2 s . c  o m*/

    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  w  w  w  .ja  va  2  s.  com
    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 };
}