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 */
017 package org.apache.commons.el;
018
019 import javax.servlet.jsp.el.ELException;
020 import javax.servlet.jsp.el.FunctionMapper;
021 import javax.servlet.jsp.el.VariableResolver;
022
023 /**
024 *
025 * <p>Represents an expression String consisting of a mixture of
026 * Strings and Expressions.
027 *
028 * @author Nathan Abramson - Art Technology Group
029 * @author Shawn Bayern
030 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
031 **/
032
033 public class ExpressionString extends Expression
034 {
035 //-------------------------------------
036 // Properties
037 //-------------------------------------
038 // property elements
039
040 Object [] mElements;
041 public Object [] getElements ()
042 { return mElements; }
043 public void setElements (Object [] pElements)
044 { mElements = pElements; }
045
046 //-------------------------------------
047 /**
048 *
049 * Constructor
050 **/
051 public ExpressionString (Object [] pElements)
052 {
053 mElements = pElements;
054 }
055
056 //-------------------------------------
057 /**
058 *
059 * Evaluates the expression string by evaluating each element,
060 * converting it to a String (using toString, or "" for null values)
061 * and concatenating the results into a single String.
062 **/
063 public Object evaluate (VariableResolver pResolver,
064 FunctionMapper functions)
065 throws ELException
066 {
067 StringBuffer buf = new StringBuffer ();
068 for (int i = 0; i < mElements.length; i++) {
069 Object elem = mElements [i];
070 if (elem instanceof String) {
071 buf.append ((String) elem);
072 }
073 else if (elem instanceof Expression) {
074 Object val =
075 ((Expression) elem).evaluate (pResolver, functions);
076 if (val != null) {
077 buf.append (val.toString ());
078 }
079 }
080 }
081 return buf.toString ();
082 }
083
084 //-------------------------------------
085 /**
086 *
087 * Returns the expression in the expression language syntax
088 **/
089 public String getExpressionString ()
090 {
091 StringBuffer buf = new StringBuffer ();
092 for (int i = 0; i < mElements.length; i++) {
093 Object elem = mElements [i];
094 if (elem instanceof String) {
095 buf.append ((String) elem);
096 }
097 else if (elem instanceof Expression) {
098 buf.append ("${");
099 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 }