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.ArrayList;
21
22 import javax.servlet.jsp.el.ELException;
23 import javax.servlet.jsp.el.FunctionMapper;
24 import javax.servlet.jsp.el.VariableResolver;
25
26 /**
27 *
28 * <p>Represents a dynamic value, which consists of a prefix and an
29 * optional set of ValueSuffix elements. A prefix is something like
30 * an identifier, and a suffix is something like a "property of" or
31 * "indexed element of" operator.
32 *
33 * @author Nathan Abramson - Art Technology Group
34 * @author Shawn Bayern
35 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
36 **/
37
38 public class ComplexValue
39 extends Expression
40 {
41 //-------------------------------------
42 // Properties
43 //-------------------------------------
44 // property prefix
45
46 Expression mPrefix;
47 public Expression getPrefix ()
48 { return mPrefix; }
49 public void setPrefix (Expression pPrefix)
50 { mPrefix = pPrefix; }
51
52 //-------------------------------------
53 // property suffixes
54
55 List mSuffixes;
56 public List getSuffixes ()
57 { return mSuffixes; }
58 public void setSuffixes (List pSuffixes)
59 { mSuffixes = pSuffixes; }
60
61 //-------------------------------------
62 /**
63 *
64 * Constructor
65 **/
66 public ComplexValue (Expression pPrefix,
67 List pSuffixes)
68 {
69 mPrefix = pPrefix;
70 mSuffixes = pSuffixes;
71 }
72
73 //-------------------------------------
74 // Expression methods
75 //-------------------------------------
76 /**
77 *
78 * Returns the expression in the expression language syntax
79 **/
80 public String getExpressionString ()
81 {
82 StringBuffer buf = new StringBuffer ();
83 buf.append (mPrefix.getExpressionString ());
84
85 for (int i = 0; mSuffixes != null && i < mSuffixes.size (); i++) {
86 ValueSuffix suffix = (ValueSuffix) mSuffixes.get (i);
87 buf.append (suffix.getExpressionString ());
88 }
89
90 return buf.toString ();
91 }
92
93 //-------------------------------------
94 /**
95 *
96 * Evaluates by evaluating the prefix, then applying the suffixes
97 **/
98 public Object evaluate (VariableResolver pResolver,
99 FunctionMapper functions)
100 throws ELException
101 {
102 Object ret = mPrefix.evaluate (pResolver, functions);
103
104 // Apply the suffixes
105 for (int i = 0; mSuffixes != null && i < mSuffixes.size (); i++) {
106 ValueSuffix suffix = (ValueSuffix) mSuffixes.get (i);
107 ret = suffix.evaluate (ret, pResolver, functions);
108 }
109
110 return ret;
111 }
112
113 public Expression bindFunctions(final FunctionMapper functions) throws ELException {
114 final List suffixes = new ArrayList(mSuffixes.size());
115 for (int i = 0; mSuffixes != null && i < mSuffixes.size (); i++) {
116 ValueSuffix suffix = (ValueSuffix) mSuffixes.get (i);
117 suffixes.add(suffix.bindFunctions(functions));
118 }
119 return new ComplexValue(mPrefix.bindFunctions(functions), suffixes);
120 }
121
122 //-------------------------------------
123 }