01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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
64: && propertyName != null) {
65: return ((DynaBean) context.getBean()).get(propertyName);
66: } else {
67: return null;
68: }
69: }
70:
71: /**
72: * Do nothing.
73: * @see Expression#update
74: */
75: public void update(Context context, String newValue) {
76: // do nothing
77: }
78:
79: /**
80: * Gets the name of the property to get from the DynaBean.
81: * @return the name of the property that this expression reads
82: */
83: public String getPropertyName() {
84: return propertyName;
85: }
86:
87: /**
88: * Sets the name of the property to get from the DynaBean.
89: * @param propertyName the property that this expression reads, not null
90: */
91: public void setPropertyName(String propertyName) {
92: if (propertyName == null) {
93: throw new IllegalArgumentException("propertyName is null");
94: }
95: this.propertyName = propertyName;
96: }
97:
98: }
|