KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > expression > support > OgnlExpression


1 /*
2  * Copyright 2002-2006 the original author or authors.
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 package org.springframework.binding.expression.support;
17
18 import java.util.Collections JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import ognl.Ognl;
22 import ognl.OgnlException;
23
24 import org.springframework.binding.expression.EvaluationAttempt;
25 import org.springframework.binding.expression.EvaluationContext;
26 import org.springframework.binding.expression.EvaluationException;
27 import org.springframework.binding.expression.SetValueAttempt;
28 import org.springframework.binding.expression.SettableExpression;
29 import org.springframework.util.Assert;
30
31 /**
32  * Evaluates a parsed ognl expression.
33  * <p>
34  * IMPLEMENTATION NOTE: ognl 2.6.7 expression objects do not respect equality
35  * properly, so the equality operations defined within this class do not
36  * function properly.
37  *
38  * @author Keith Donald
39  */

40 class OgnlExpression implements SettableExpression {
41
42     /**
43      * The expression.
44      */

45     private Object JavaDoc expression;
46
47     /**
48      * Creates a new OGNL expression.
49      * @param expression the parsed expression
50      */

51     public OgnlExpression(Object JavaDoc expression) {
52         this.expression = expression;
53     }
54
55     public int hashCode() {
56         return expression.hashCode();
57     }
58
59     public boolean equals(Object JavaDoc o) {
60         if (!(o instanceof OgnlExpression)) {
61             return false;
62         }
63         // as late as Ognl 2.6.7, their expression objects don't implement
64
// equals
65
// so this always returns false
66
OgnlExpression other = (OgnlExpression) o;
67         return expression.equals(other.expression);
68     }
69
70     public Object JavaDoc evaluate(Object JavaDoc target, EvaluationContext context) throws EvaluationException {
71         Assert.notNull(target, "The target object to evaluate is required");
72         Map JavaDoc contextAttributes = (context != null ? context.getAttributes() : Collections.EMPTY_MAP);
73         try {
74             return Ognl.getValue(expression, contextAttributes, target);
75         } catch (OgnlException e) {
76             throw new EvaluationException(new EvaluationAttempt(this, target, contextAttributes), e);
77         }
78     }
79
80     public void evaluateToSet(Object JavaDoc target, Object JavaDoc value, EvaluationContext context) {
81         Assert.notNull(target, "The target object to evaluate is required");
82         Map JavaDoc contextAttributes = (context != null ? context.getAttributes() : Collections.EMPTY_MAP);
83         try {
84             Ognl.setValue(expression, contextAttributes, target, value);
85         } catch (OgnlException e) {
86             throw new EvaluationException(new SetValueAttempt(this, target, value, contextAttributes), e);
87         }
88     }
89
90     public String JavaDoc toString() {
91         return expression.toString();
92     }
93 }
Popular Tags