Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
DynaBeanExpression |
|
| 1.6666666666666667;1.667 |
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 | 0 | super(); |
39 | 0 | } |
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 | 104 | super(); |
48 | 104 | setPropertyName(propertyName); |
49 | 104 | } |
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 | 39 | if (context.getBean() instanceof DynaBean && propertyName != null) { |
63 | 39 | return ((DynaBean)context.getBean()).get(propertyName); |
64 | } else { |
|
65 | 0 | 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 | 0 | } |
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 | 0 | 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 | 104 | if (propertyName == null) { |
91 | 0 | throw new IllegalArgumentException("propertyName is null"); |
92 | } |
|
93 | 104 | this.propertyName = propertyName; |
94 | 104 | } |
95 | ||
96 | } |