View Javadoc

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  
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          // do nothing
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  }