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 javax.servlet.jsp.el.ELException;
20 import javax.servlet.jsp.el.FunctionMapper;
21 import javax.servlet.jsp.el.VariableResolver;
22
23 /**
24 *
25 * <p>Represents an expression String consisting of a mixture of
26 * Strings and Expressions.
27 *
28 * @author Nathan Abramson - Art Technology Group
29 * @author Shawn Bayern
30 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
31 **/
32
33 public class ExpressionString extends Expression
34 {
35 //-------------------------------------
36 // Properties
37 //-------------------------------------
38 // property elements
39
40 Object [] mElements;
41 public Object [] getElements ()
42 { return mElements; }
43 public void setElements (Object [] pElements)
44 { mElements = pElements; }
45
46 //-------------------------------------
47 /**
48 *
49 * Constructor
50 **/
51 public ExpressionString (Object [] pElements)
52 {
53 mElements = pElements;
54 }
55
56 //-------------------------------------
57 /**
58 *
59 * Evaluates the expression string by evaluating each element,
60 * converting it to a String (using toString, or "" for null values)
61 * and concatenating the results into a single String.
62 **/
63 public Object evaluate (VariableResolver pResolver,
64 FunctionMapper functions)
65 throws ELException
66 {
67 StringBuffer buf = new StringBuffer ();
68 for (int i = 0; i < mElements.length; i++) {
69 Object elem = mElements [i];
70 if (elem instanceof String) {
71 buf.append ((String) elem);
72 }
73 else if (elem instanceof Expression) {
74 Object val =
75 ((Expression) elem).evaluate (pResolver, functions);
76 if (val != null) {
77 buf.append (val.toString ());
78 }
79 }
80 }
81 return buf.toString ();
82 }
83
84 //-------------------------------------
85 /**
86 *
87 * Returns the expression in the expression language syntax
88 **/
89 public String getExpressionString ()
90 {
91 StringBuffer buf = new StringBuffer ();
92 for (int i = 0; i < mElements.length; i++) {
93 Object elem = mElements [i];
94 if (elem instanceof String) {
95 buf.append ((String) elem);
96 }
97 else if (elem instanceof Expression) {
98 buf.append ("${");
99 buf.append (((Expression) elem).getExpressionString ());
100 buf.append ("}");
101 }
102 }
103 return buf.toString ();
104 }
105
106 //-------------------------------------
107
108 public Expression bindFunctions(FunctionMapper functions) throws ELException {
109 final Object[] boundElements = new Object[mElements.length];
110 for (int i = 0; i < mElements.length; i++) {
111 if (mElements[i] instanceof Expression) {
112 boundElements[i] = ((Expression)mElements[i]).bindFunctions(functions);
113 } else {
114 boundElements[i] = mElements[i];
115 }
116 }
117 return new ExpressionString(boundElements);
118 }
119 }