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. import org.apache.commons.beanutils.DynaBean;
  18. /**
  19. * An Expression that gets a property value from a DynaBean.
  20. *
  21. * @see org.apache.commons.beanutils.DynaBean
  22. *
  23. * @author Michael Becke
  24. * @since 0.5
  25. */
  26. public class DynaBeanExpression implements Expression {
  27. /** The name of the DynaBean property to get */
  28. private String propertyName;
  29. /**
  30. * Crates a new DynaBeanExpression.
  31. */
  32. public DynaBeanExpression() {
  33. super();
  34. }
  35. /**
  36. * Crates a new DynaBeanExpression.
  37. *
  38. * @param propertyName the name of the DynaBean property to use
  39. */
  40. public DynaBeanExpression(String propertyName) {
  41. super();
  42. setPropertyName(propertyName);
  43. }
  44. /**
  45. * Returns the value of a DynaBean property from the bean stored in
  46. * the Context. Returns <code>null</code> if no DynaBean is stored
  47. * in the Context or if the propertyName has not been set.
  48. *
  49. * @param context the content containing the DynaBean
  50. *
  51. * @return the DynaBean property value or <code>null</code>
  52. */
  53. public Object evaluate(Context context) {
  54. if (context.getBean() instanceof DynaBean && propertyName != null) {
  55. return ((DynaBean)context.getBean()).get(propertyName);
  56. } else {
  57. return null;
  58. }
  59. }
  60. /**
  61. * Do nothing.
  62. * @see Expression#update
  63. */
  64. public void update(Context context, String newValue) {
  65. // do nothing
  66. }
  67. /**
  68. * Gets the name of the property to get from the DynaBean.
  69. * @return the name of the property that this expression reads
  70. */
  71. public String getPropertyName() {
  72. return propertyName;
  73. }
  74. /**
  75. * Sets the name of the property to get from the DynaBean.
  76. * @param propertyName the property that this expression reads, not null
  77. */
  78. public void setPropertyName(String propertyName) {
  79. if (propertyName == null) {
  80. throw new IllegalArgumentException("propertyName is null");
  81. }
  82. this.propertyName = propertyName;
  83. }
  84. }