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