Example usage for org.apache.commons.el Coercions coerceToBoolean

List of usage examples for org.apache.commons.el Coercions coerceToBoolean

Introduction

In this page you can find the example usage for org.apache.commons.el Coercions coerceToBoolean.

Prototype

public static Boolean coerceToBoolean(Object pValue, Logger pLogger) throws ELException 

Source Link

Document

Coerces a value to a Boolean

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  w  ww  .  j  a va 2s  . 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.CoercionsUtil.java

public static boolean coerceToPrimitiveBoolean(Object value, Logger logger) throws EvaluationException {
    try {//  w  ww.j  ava 2  s  . c  o m
        return Coercions.coerceToBoolean(value, logger).booleanValue();
    } catch (ELException e) {
        throw new EvaluationException(e);
    }
}