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.betwixt.expression;
018    
019    /** <p><code>VariableExpression</code> represents a variable expression such as 
020      * <code>$foo</code> which returns the value of the given variable.</p>
021      *
022      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
023      * @version $Revision: 438373 $
024      */
025    public class VariableExpression implements Expression {
026    
027        /** The variable name */
028        private String variableName;
029        
030        /** Base constructor */
031        public VariableExpression() {
032        }
033        
034        /** 
035         * Convenience constructor sets <code>VariableName</code> property 
036         * @param variableName the name of the context variable 
037         * whose value will be returned by an evaluation 
038         */
039        public VariableExpression(String variableName) {
040            this.variableName = variableName;
041        }
042        
043        /** Return the value of a context variable.
044          *
045          * @param context evaluate against this context
046          * @return the value of the context variable named by the <code>VariableName</code> property
047          */
048        public Object evaluate(Context context) {
049            return context.getVariable( variableName );
050        }
051        
052        /** 
053         * Gets the variable name 
054         * @return the name of the context variable whose value will be returned by an evaluation
055         */
056        public String getVariableName() {
057            return variableName;
058        }
059        
060        /** 
061         * Sets the variable name 
062         * @param variableName the name of the context variable 
063         * whose value will be returned by an evaluation
064         */
065        public void setVariableName(String variableName) {
066            this.variableName = variableName;
067        }
068        
069        /**
070         * Do nothing
071         * @see org.apache.commons.betwixt.expression.Expression
072         */
073        public void update(Context context, String newValue) {
074            // do nothing
075        }
076    
077        /**
078         * Returns something useful for logging
079         * @return something useful for logging
080         */
081        public String toString() {
082            return "VariableExpression [variable name=" + variableName + "]";
083        }
084    }