1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.binding; |
16 |
| |
17 |
| import org.apache.hivemind.Location; |
18 |
| import org.apache.tapestry.BindingException; |
19 |
| import org.apache.tapestry.IComponent; |
20 |
| import org.apache.tapestry.coerce.ValueConverter; |
21 |
| import org.apache.tapestry.services.ExpressionCache; |
22 |
| import org.apache.tapestry.services.ExpressionEvaluator; |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class ExpressionBinding extends AbstractBinding |
35 |
| { |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| private final IComponent _root; |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| private String _expression; |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| private boolean _invariant = false; |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| private Object _parsedExpression; |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| private boolean _initialized; |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| private ExpressionEvaluator _evaluator; |
71 |
| |
72 |
| |
73 |
| |
74 |
| private ExpressionCache _cache; |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
1021
| public ExpressionBinding(String description, Location location, ValueConverter valueConverter,
|
81 |
| IComponent root, String expression, ExpressionEvaluator evaluator, |
82 |
| ExpressionCache cache) |
83 |
| { |
84 |
1021
| super(description, valueConverter, location);
|
85 |
| |
86 |
1021
| _root = root;
|
87 |
1021
| _expression = expression;
|
88 |
1021
| _evaluator = evaluator;
|
89 |
1021
| _cache = cache;
|
90 |
| } |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
2039
| public Object getObject()
|
100 |
| { |
101 |
2039
| initialize();
|
102 |
| |
103 |
2039
| return resolveExpression();
|
104 |
| } |
105 |
| |
106 |
2039
| private Object resolveExpression()
|
107 |
| { |
108 |
2039
| try
|
109 |
| { |
110 |
2039
| return _evaluator.readCompiled(_root, _parsedExpression);
|
111 |
| } |
112 |
| catch (Throwable t) |
113 |
| { |
114 |
2
| throw new BindingException(t.getMessage(), this, t);
|
115 |
| } |
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
2175
| public boolean isInvariant()
|
123 |
| { |
124 |
2175
| initialize();
|
125 |
| |
126 |
2174
| return _invariant;
|
127 |
| } |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
5404
| private void initialize()
|
135 |
| { |
136 |
5404
| if (_initialized)
|
137 |
4559
| return;
|
138 |
| |
139 |
845
| _initialized = true;
|
140 |
| |
141 |
845
| try
|
142 |
| { |
143 |
845
| _parsedExpression = _cache.getCompiledExpression(_expression);
|
144 |
| |
145 |
844
| _invariant = _evaluator.isConstant(_expression);
|
146 |
| } |
147 |
| catch (Exception ex) |
148 |
| { |
149 |
1
| throw new BindingException(ex.getMessage(), this, ex);
|
150 |
| } |
151 |
| } |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
1190
| public void setObject(Object value)
|
164 |
| { |
165 |
1190
| initialize();
|
166 |
| |
167 |
1190
| if (_invariant)
|
168 |
1
| throw createReadOnlyBindingException(this);
|
169 |
| |
170 |
1189
| try
|
171 |
| { |
172 |
1189
| _evaluator.writeCompiled(_root, _parsedExpression, value);
|
173 |
| } |
174 |
| catch (Throwable ex) |
175 |
| { |
176 |
1
| throw new BindingException(ex.getMessage(), this, ex);
|
177 |
| } |
178 |
| } |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
4
| public String toString()
|
187 |
| { |
188 |
4
| StringBuffer buffer = new StringBuffer();
|
189 |
| |
190 |
4
| buffer.append("ExpressionBinding[");
|
191 |
4
| buffer.append(_root.getExtendedId());
|
192 |
| |
193 |
4
| if (_expression != null)
|
194 |
| { |
195 |
4
| buffer.append(' ');
|
196 |
4
| buffer.append(_expression);
|
197 |
| } |
198 |
| |
199 |
4
| buffer.append(']');
|
200 |
| |
201 |
4
| return buffer.toString();
|
202 |
| } |
203 |
| |
204 |
| |
205 |
1
| public Object getComponent()
|
206 |
| { |
207 |
1
| return _root;
|
208 |
| } |
209 |
| } |