01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.springframework.binding.expression.support;
17:
18: import org.springframework.beans.BeanWrapperImpl;
19: import org.springframework.beans.BeansException;
20: import org.springframework.binding.expression.EvaluationAttempt;
21: import org.springframework.binding.expression.EvaluationContext;
22: import org.springframework.binding.expression.EvaluationException;
23: import org.springframework.binding.expression.SetValueAttempt;
24: import org.springframework.binding.expression.SettableExpression;
25: import org.springframework.util.Assert;
26:
27: /**
28: * An expression evaluator that uses the Spring bean wrapper.
29: *
30: * @author Keith Donald
31: */
32: class BeanWrapperExpression implements SettableExpression {
33:
34: /**
35: * The expression.
36: */
37: private String expression;
38:
39: public BeanWrapperExpression(String expression) {
40: this .expression = expression;
41: }
42:
43: public int hashCode() {
44: return expression.hashCode();
45: }
46:
47: public boolean equals(Object o) {
48: if (!(o instanceof BeanWrapperExpression)) {
49: return false;
50: }
51: BeanWrapperExpression other = (BeanWrapperExpression) o;
52: return expression.equals(other.expression);
53: }
54:
55: public Object evaluate(Object target, EvaluationContext context)
56: throws EvaluationException {
57: try {
58: return new BeanWrapperImpl(target)
59: .getPropertyValue(expression);
60: } catch (BeansException e) {
61: throw new EvaluationException(new EvaluationAttempt(this ,
62: target, context), e);
63: }
64: }
65:
66: public void evaluateToSet(Object target, Object value,
67: EvaluationContext context) throws EvaluationException {
68: try {
69: Assert.notNull(target,
70: "The target object to evaluate is required");
71: new BeanWrapperImpl(target).setPropertyValue(expression,
72: value);
73: } catch (BeansException e) {
74: throw new EvaluationException(new SetValueAttempt(this ,
75: target, value, context), e);
76: }
77: }
78:
79: public String toString() {
80: return expression;
81: }
82: }
|