1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.betwixt.expression;
  17. /** <p><code>VariableExpression</code> represents a variable expression such as
  18. * <code>$foo</code> which returns the value of the given variable.</p>
  19. *
  20. * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  21. * @version $Revision: 1.6 $
  22. */
  23. public class VariableExpression implements Expression {
  24. /** The variable name */
  25. private String variableName;
  26. /** Base constructor */
  27. public VariableExpression() {
  28. }
  29. /**
  30. * Convenience constructor sets <code>VariableName</code> property
  31. * @param variableName the name of the context variable
  32. * whose value will be returned by an evaluation
  33. */
  34. public VariableExpression(String variableName) {
  35. this.variableName = variableName;
  36. }
  37. /** Return the value of a context variable.
  38. *
  39. * @param context evaluate against this context
  40. * @return the value of the context variable named by the <code>VariableName</code> property
  41. */
  42. public Object evaluate(Context context) {
  43. return context.getVariable( variableName );
  44. }
  45. /**
  46. * Gets the variable name
  47. * @return the name of the context variable whose value will be returned by an evaluation
  48. */
  49. public String getVariableName() {
  50. return variableName;
  51. }
  52. /**
  53. * Sets the variable name
  54. * @param variableName the name of the context variable
  55. * whose value will be returned by an evaluation
  56. */
  57. public void setVariableName(String variableName) {
  58. this.variableName = variableName;
  59. }
  60. /**
  61. * Do nothing
  62. * @see org.apache.commons.betwixt.expression.Expression
  63. */
  64. public void update(Context context, String newValue) {
  65. // do nothing
  66. }
  67. /**
  68. * Returns something useful for logging
  69. * @return something useful for logging
  70. */
  71. public String toString() {
  72. return "VariableExpression [variable name=" + variableName + "]";
  73. }
  74. }