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