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 java.util.List;
020    import java.util.ArrayList;
021    
022    import javax.servlet.jsp.el.ELException;
023    import javax.servlet.jsp.el.FunctionMapper;
024    import javax.servlet.jsp.el.VariableResolver;
025    
026    /**
027     *
028     * <p>Represents a dynamic value, which consists of a prefix and an
029     * optional set of ValueSuffix elements.  A prefix is something like
030     * an identifier, and a suffix is something like a "property of" or
031     * "indexed element of" operator.
032     * 
033     * @author Nathan Abramson - Art Technology Group
034     * @author Shawn Bayern
035     * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
036     **/
037    
038    public class ComplexValue
039      extends Expression
040    {
041      //-------------------------------------
042      // Properties
043      //-------------------------------------
044      // property prefix
045    
046      Expression mPrefix;
047      public Expression getPrefix ()
048      { return mPrefix; }
049      public void setPrefix (Expression pPrefix)
050      { mPrefix = pPrefix; }
051    
052      //-------------------------------------
053      // property suffixes
054    
055      List mSuffixes;
056      public List getSuffixes ()
057      { return mSuffixes; }
058      public void setSuffixes (List pSuffixes)
059      { mSuffixes = pSuffixes; }
060    
061      //-------------------------------------
062      /**
063       *
064       * Constructor
065       **/
066      public ComplexValue (Expression pPrefix,
067                           List pSuffixes)
068      {
069        mPrefix = pPrefix;
070        mSuffixes = pSuffixes;
071      }
072    
073      //-------------------------------------
074      // Expression methods
075      //-------------------------------------
076      /**
077       *
078       * Returns the expression in the expression language syntax
079       **/
080      public String getExpressionString ()
081      {
082        StringBuffer buf = new StringBuffer ();
083        buf.append (mPrefix.getExpressionString ());
084    
085        for (int i = 0; mSuffixes != null && i < mSuffixes.size (); i++) {
086          ValueSuffix suffix = (ValueSuffix) mSuffixes.get (i);
087          buf.append (suffix.getExpressionString ());
088        }
089    
090        return buf.toString ();
091      }
092    
093      //-------------------------------------
094      /**
095       *
096       * Evaluates by evaluating the prefix, then applying the suffixes
097       **/
098      public Object evaluate (VariableResolver pResolver,
099                              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    }