1 package org.objectstyle.cayenne.swing; 2 3 import java.util.Map ; 4 5 import ognl.Ognl; 6 import ognl.OgnlException; 7 8 import org.objectstyle.cayenne.CayenneRuntimeException; 9 import org.objectstyle.cayenne.util.Util; 10 11 16 public class BindingExpression { 17 18 private Object compiled; 19 protected String expression; 20 21 static Throwable unwind(Throwable th) { 22 if (th instanceof OgnlException) { 23 Throwable reason = ((OgnlException) th).getReason(); 24 return (reason != null) ? unwind(reason) : th; 25 } 26 else { 27 return Util.unwindException(th); 28 } 29 } 30 31 public BindingExpression(String expression) { 32 try { 33 this.compiled = Ognl.parseExpression(expression); 34 } 35 catch (OgnlException ex) { 36 throw new CayenneRuntimeException( 37 "Invalid expression - " + expression, 38 BindingBase.unwind(ex)); 39 } 40 41 this.expression = expression; 42 } 43 44 public String getExpression() { 45 return expression; 46 } 47 48 public void setValue(Object context, Map contextVariables, Object value) { 49 if (context == null) { 50 throw new BindingException("No context"); 51 } 52 53 try { 54 Ognl.setValue(compiled, contextVariables, context, value); 55 } 56 catch (OgnlException ex) { 57 throw new BindingException( 58 "Evaluation failed in context: " + context, 59 unwind(ex)); 60 } 61 } 62 63 public Object getValue(Object context, Map contextVariables) { 64 if (context == null) { 65 throw new BindingException("No context"); 66 } 67 68 try { 69 return Ognl.getValue(compiled, contextVariables, context); 70 } 71 catch (OgnlException ex) { 72 throw new BindingException( 73 "Evaluation failed in context: " + context, 74 unwind(ex)); 75 } 76 } 77 } | Popular Tags |