001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.engine.support;
017:
018: import org.springframework.binding.expression.EvaluationContext;
019: import org.springframework.binding.expression.EvaluationException;
020: import org.springframework.binding.expression.Expression;
021: import org.springframework.binding.expression.SettableExpression;
022: import org.springframework.core.style.ToStringCreator;
023: import org.springframework.util.Assert;
024: import org.springframework.webflow.core.collection.AttributeMap;
025: import org.springframework.webflow.core.collection.MutableAttributeMap;
026: import org.springframework.webflow.execution.RequestContext;
027: import org.springframework.webflow.execution.ScopeType;
028:
029: /**
030: * Expression evaluator that can evalute attribute maps and supported
031: * request context scope types.
032: *
033: * @see org.springframework.webflow.execution.RequestContext
034: * @see org.springframework.webflow.core.collection.AttributeMap
035: *
036: * @author Keith Donald
037: * @author Erwin Vervaet
038: */
039: public class AttributeExpression implements SettableExpression {
040:
041: /**
042: * The expression to evaluate.
043: */
044: private Expression expression;
045:
046: /**
047: * The scope type.
048: */
049: private ScopeType scopeType;
050:
051: /**
052: * Create a new expression evaluator that executes given expression in an
053: * attribute map. When using this wrapper to set a property value, make
054: * sure the given expression is a {@link SettableExpression}}.
055: * @param expression the nested evaluator to execute
056: */
057: public AttributeExpression(Expression expression) {
058: this (expression, null);
059: }
060:
061: /**
062: * Create a new expression evaluator that executes given expression in the
063: * specified scope. When using this wrapper to set a property value, make
064: * sure the given expression is a {@link SettableExpression}}.
065: * @param expression the nested evaluator to execute
066: * @param scopeType the scopeType
067: */
068: public AttributeExpression(Expression expression,
069: ScopeType scopeType) {
070: this .expression = expression;
071: this .scopeType = scopeType;
072: }
073:
074: /**
075: * Returns the expression that will be evaluated.
076: */
077: protected Expression getExpression() {
078: return expression;
079: }
080:
081: public Object evaluate(Object target, EvaluationContext context)
082: throws EvaluationException {
083: if (target instanceof RequestContext) {
084: RequestContext requestContext = (RequestContext) target;
085: AttributeMap scope = scopeType.getScope(requestContext);
086: return expression.evaluate(scope, context);
087: } else if (target instanceof AttributeMap) {
088: return expression.evaluate(target, context);
089: } else {
090: throw new IllegalArgumentException(
091: "Only supports evaluation against a [RequestContext] or [AttributeMap] instance, but was a ["
092: + target.getClass() + "]");
093: }
094: }
095:
096: public void evaluateToSet(Object target, Object value,
097: EvaluationContext context) throws EvaluationException {
098: Assert
099: .isInstanceOf(
100: SettableExpression.class,
101: expression,
102: "When an AttributeExpression is used to set a property value, the nested expression needs "
103: + "to be a SettableExpression");
104: if (target instanceof RequestContext) {
105: RequestContext requestContext = (RequestContext) target;
106: MutableAttributeMap scope = scopeType
107: .getScope(requestContext);
108: ((SettableExpression) expression).evaluateToSet(scope,
109: value, context);
110: } else if (target instanceof AttributeMap) {
111: ((SettableExpression) expression).evaluateToSet(target,
112: value, context);
113: } else {
114: throw new IllegalArgumentException(
115: "Only supports evaluation against a [RequestContext] or [AttributeMap] instance, but was a ["
116: + target.getClass() + "]");
117: }
118: }
119:
120: public String toString() {
121: return new ToStringCreator(this ).append("expression",
122: expression).toString();
123: }
124: }
|