View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.el;
18  
19  import java.util.List;
20  import java.util.Iterator;
21  import java.util.ArrayList;
22  
23  import javax.servlet.jsp.el.ELException;
24  import javax.servlet.jsp.el.FunctionMapper;
25  import javax.servlet.jsp.el.VariableResolver;
26  
27  /**
28   *
29   * <p>An expression representing a binary operator on a value
30   * 
31   * @author Nathan Abramson - Art Technology Group
32   * @author Shawn Bayern
33   * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
34   **/
35  
36  public class BinaryOperatorExpression
37    extends Expression
38  {
39    //-------------------------------------
40    // Properties
41    //-------------------------------------
42    // property expression
43  
44    Expression mExpression;
45    public Expression getExpression ()
46    { return mExpression; }
47    public void setExpression (Expression pExpression)
48    { mExpression = pExpression; }
49  
50    //-------------------------------------
51    // property operators
52  
53    List mOperators;
54    public List getOperators ()
55    { return mOperators; }
56    public void setOperators (List pOperators)
57    { mOperators = pOperators; }
58  
59    //-------------------------------------
60    // property expressions
61  
62    List mExpressions;
63    public List getExpressions ()
64    { return mExpressions; }
65    public void setExpressions (List pExpressions)
66    { mExpressions = pExpressions; }
67  
68    //-------------------------------------
69    /**
70     *
71     * Constructor
72     **/
73    public BinaryOperatorExpression (Expression pExpression,
74  				   List pOperators,
75  				   List pExpressions)
76    {
77      mExpression = pExpression;
78      mOperators = pOperators;
79      mExpressions = pExpressions;
80    }
81  
82    //-------------------------------------
83    // Expression methods
84    //-------------------------------------
85    /**
86     *
87     * Returns the expression in the expression language syntax
88     **/
89    public String getExpressionString ()
90    {
91      StringBuffer buf = new StringBuffer ();
92      buf.append ("(");
93      buf.append (mExpression.getExpressionString ());
94      for (int i = 0, size = mOperators.size(); i < size; i++) {
95        BinaryOperator operator = (BinaryOperator) mOperators.get (i);
96        Expression expression = (Expression) mExpressions.get (i);
97        buf.append (" ");
98        buf.append (operator.getOperatorSymbol ());
99        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 }