KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > swing > BindingExpression


1 package org.objectstyle.cayenne.swing;
2
3 import java.util.Map JavaDoc;
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 /**
12  * A class for expression evaluation using a designated scripting language (now - OGNL).
13  *
14  * @author Andrei Adamchik
15  */

16 public class BindingExpression {
17
18     private Object JavaDoc compiled;
19     protected String JavaDoc expression;
20
21     static Throwable JavaDoc unwind(Throwable JavaDoc th) {
22         if (th instanceof OgnlException) {
23             Throwable JavaDoc 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 JavaDoc 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 JavaDoc getExpression() {
45         return expression;
46     }
47
48     public void setValue(Object JavaDoc context, Map JavaDoc contextVariables, Object JavaDoc 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 JavaDoc getValue(Object JavaDoc context, Map JavaDoc 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