1 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 39 public class AttributeExpression implements SettableExpression { 40 41 44 private Expression expression; 45 46 49 private ScopeType scopeType; 50 51 57 public AttributeExpression(Expression expression) { 58 this(expression, null); 59 } 60 61 68 public AttributeExpression(Expression expression, ScopeType scopeType) { 69 this.expression = expression; 70 this.scopeType = scopeType; 71 } 72 73 76 protected Expression getExpression() { 77 return expression; 78 } 79 80 public Object evaluate(Object 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 ( 91 "Only supports evaluation against a [RequestContext] or [AttributeMap] instance, but was a [" 92 + target.getClass() + "]"); 93 } 94 } 95 96 public void evaluateToSet(Object target, Object 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 ( 110 "Only supports evaluation against a [RequestContext] or [AttributeMap] instance, but was a [" 111 + target.getClass() + "]"); 112 } 113 } 114 115 public String toString() { 116 return new ToStringCreator(this).append("expression", expression).toString(); 117 } 118 } | Popular Tags |