KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > support > AttributeExpression


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.webflow.engine.support;
17
18 import org.springframework.binding.expression.EvaluationContext;
19 import org.springframework.binding.expression.EvaluationException;
20 import org.springframework.binding.expression.Expression;
21 import org.springframework.binding.expression.SettableExpression;
22 import org.springframework.core.style.ToStringCreator;
23 import org.springframework.util.Assert;
24 import org.springframework.webflow.core.collection.AttributeMap;
25 import org.springframework.webflow.core.collection.MutableAttributeMap;
26 import org.springframework.webflow.execution.RequestContext;
27 import org.springframework.webflow.execution.ScopeType;
28
29 /**
30  * Expression evaluator that can evalute attribute maps and supported
31  * request context scope types.
32  *
33  * @see org.springframework.webflow.execution.RequestContext
34  * @see org.springframework.webflow.core.collection.AttributeMap
35  *
36  * @author Keith Donald
37  * @author Erwin Vervaet
38  */

39 public class AttributeExpression implements SettableExpression {
40
41     /**
42      * The expression to evaluate.
43      */

44     private Expression expression;
45
46     /**
47      * The scope type.
48      */

49     private ScopeType scopeType;
50     
51     /**
52      * Create a new expression evaluator that executes given expression in an
53      * attribute map. When using this wrapper to set a property value, make
54      * sure the given expression is a {@link SettableExpression}}.
55      * @param expression the nested evaluator to execute
56      */

57     public AttributeExpression(Expression expression) {
58         this(expression, null);
59     }
60
61     /**
62      * Create a new expression evaluator that executes given expression in the
63      * specified scope. When using this wrapper to set a property value, make
64      * sure the given expression is a {@link SettableExpression}}.
65      * @param expression the nested evaluator to execute
66      * @param scopeType the scopeType
67      */

68     public AttributeExpression(Expression expression, ScopeType scopeType) {
69         this.expression = expression;
70         this.scopeType = scopeType;
71     }
72
73     /**
74      * Returns the expression that will be evaluated.
75      */

76     protected Expression getExpression() {
77         return expression;
78     }
79
80     public Object JavaDoc evaluate(Object JavaDoc target, EvaluationContext context) throws EvaluationException {
81         if (target instanceof RequestContext) {
82             RequestContext requestContext = (RequestContext)target;
83             AttributeMap scope = scopeType.getScope(requestContext);
84             return expression.evaluate(scope, context);
85         }
86         else if (target instanceof AttributeMap) {
87             return expression.evaluate(target, context);
88         }
89         else {
90             throw new IllegalArgumentException JavaDoc(
91                     "Only supports evaluation against a [RequestContext] or [AttributeMap] instance, but was a ["
92                     + target.getClass() + "]");
93         }
94     }
95
96     public void evaluateToSet(Object JavaDoc target, Object JavaDoc value, EvaluationContext context) throws EvaluationException {
97         Assert.isInstanceOf(SettableExpression.class, expression,
98                 "When an AttributeExpression is used to set a property value, the nested expression needs "
99                 + "to be a SettableExpression");
100         if (target instanceof RequestContext) {
101             RequestContext requestContext = (RequestContext)target;
102             MutableAttributeMap scope = scopeType.getScope(requestContext);
103             ((SettableExpression)expression).evaluateToSet(scope, value, context);
104         }
105         else if (target instanceof AttributeMap) {
106             ((SettableExpression)expression).evaluateToSet(target, value, context);
107         }
108         else {
109             throw new IllegalArgumentException JavaDoc(
110                     "Only supports evaluation against a [RequestContext] or [AttributeMap] instance, but was a ["
111                     + target.getClass() + "]");
112         }
113     }
114
115     public String JavaDoc toString() {
116         return new ToStringCreator(this).append("expression", expression).toString();
117     }
118 }
Popular Tags