001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.el;
018
019import java.util.List;
020import java.util.Iterator;
021import java.util.ArrayList;
022
023import javax.servlet.jsp.el.ELException;
024import javax.servlet.jsp.el.FunctionMapper;
025import javax.servlet.jsp.el.VariableResolver;
026
027/**
028 *
029 * <p>An expression representing a binary operator on a value
030 * 
031 * @author Nathan Abramson - Art Technology Group
032 * @author Shawn Bayern
033 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
034 **/
035
036public class BinaryOperatorExpression
037  extends Expression
038{
039  //-------------------------------------
040  // Properties
041  //-------------------------------------
042  // property expression
043
044  Expression mExpression;
045  public Expression getExpression ()
046  { return mExpression; }
047  public void setExpression (Expression pExpression)
048  { mExpression = pExpression; }
049
050  //-------------------------------------
051  // property operators
052
053  List mOperators;
054  public List getOperators ()
055  { return mOperators; }
056  public void setOperators (List pOperators)
057  { mOperators = pOperators; }
058
059  //-------------------------------------
060  // property expressions
061
062  List mExpressions;
063  public List getExpressions ()
064  { return mExpressions; }
065  public void setExpressions (List pExpressions)
066  { mExpressions = pExpressions; }
067
068  //-------------------------------------
069  /**
070   *
071   * Constructor
072   **/
073  public BinaryOperatorExpression (Expression pExpression,
074                                   List pOperators,
075                                   List pExpressions)
076  {
077    mExpression = pExpression;
078    mOperators = pOperators;
079    mExpressions = pExpressions;
080  }
081
082  //-------------------------------------
083  // Expression methods
084  //-------------------------------------
085  /**
086   *
087   * Returns the expression in the expression language syntax
088   **/
089  public String getExpressionString ()
090  {
091    StringBuffer buf = new StringBuffer ();
092    buf.append ("(");
093    buf.append (mExpression.getExpressionString ());
094    for (int i = 0, size = mOperators.size(); i < size; i++) {
095      BinaryOperator operator = (BinaryOperator) mOperators.get (i);
096      Expression expression = (Expression) mExpressions.get (i);
097      buf.append (" ");
098      buf.append (operator.getOperatorSymbol ());
099      buf.append (" ");
100      buf.append (expression.getExpressionString ());
101    }
102    buf.append (")");
103
104    return buf.toString ();
105  }
106
107  //-------------------------------------
108  /**
109   *
110   * Evaluates to the literal value
111   **/
112  public Object evaluate (VariableResolver pResolver,
113                          FunctionMapper functions)
114    throws ELException
115  {
116    Object value = mExpression.evaluate (pResolver, functions);
117    for (int i = 0, size = mOperators.size(); i < size; i++) {
118      BinaryOperator operator = (BinaryOperator) mOperators.get (i);
119
120      // For the And/Or operators, we need to coerce to a boolean
121      // before testing if we shouldEvaluate
122      if (operator.shouldCoerceToBoolean ()) {
123        value = Coercions.coerceToBoolean (value);
124      }
125
126      if (operator.shouldEvaluate (value)) {
127        Expression expression = (Expression) mExpressions.get (i);
128        Object nextValue = expression.evaluate (pResolver,
129                                                functions);
130
131        value = operator.apply (value, nextValue);
132      }
133    }
134    return value;
135  }
136
137    public Expression bindFunctions(final FunctionMapper functions) throws ELException {
138        final List args = new ArrayList(mExpressions.size());
139        for (Iterator argIter = mExpressions.iterator(); argIter.hasNext();) {
140            args.add(((Expression)argIter.next()).bindFunctions(functions));
141        }
142        // it would be nice if we knew for sure that the operators list
143        // was immutable, but we'll just assume so for now.
144        return new BinaryOperatorExpression(
145                mExpression.bindFunctions(functions),
146                mOperators,
147                args);
148    }
149
150  //-------------------------------------
151}