1 16 package org.springframework.binding.expression.support; 17 18 import java.util.Collections ; 19 import java.util.Map ; 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 40 class OgnlExpression implements SettableExpression { 41 42 45 private Object expression; 46 47 51 public OgnlExpression(Object expression) { 52 this.expression = expression; 53 } 54 55 public int hashCode() { 56 return expression.hashCode(); 57 } 58 59 public boolean equals(Object o) { 60 if (!(o instanceof OgnlExpression)) { 61 return false; 62 } 63 OgnlExpression other = (OgnlExpression) o; 67 return expression.equals(other.expression); 68 } 69 70 public Object evaluate(Object target, EvaluationContext context) throws EvaluationException { 71 Assert.notNull(target, "The target object to evaluate is required"); 72 Map 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 target, Object value, EvaluationContext context) { 81 Assert.notNull(target, "The target object to evaluate is required"); 82 Map 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 toString() { 91 return expression.toString(); 92 } 93 } | Popular Tags |