1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
32
33
34
35
36 public class BinaryOperatorExpression
37 extends Expression
38 {
39
40
41
42
43
44 Expression mExpression;
45 public Expression getExpression ()
46 { return mExpression; }
47 public void setExpression (Expression pExpression)
48 { mExpression = pExpression; }
49
50
51
52
53 List mOperators;
54 public List getOperators ()
55 { return mOperators; }
56 public void setOperators (List pOperators)
57 { mOperators = pOperators; }
58
59
60
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
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
84
85
86
87
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
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
121
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
143
144 return new BinaryOperatorExpression(
145 mExpression.bindFunctions(functions),
146 mOperators,
147 args);
148 }
149
150
151 }