1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.el.convert; |
20 | |
|
21 | |
import javax.el.ELContext; |
22 | |
import javax.el.ELException; |
23 | |
import javax.el.ELResolver; |
24 | |
import javax.el.ExpressionFactory; |
25 | |
import javax.el.PropertyNotFoundException; |
26 | |
import javax.el.PropertyNotWritableException; |
27 | |
import javax.faces.FactoryFinder; |
28 | |
import javax.faces.application.ApplicationFactory; |
29 | |
import javax.faces.context.FacesContext; |
30 | |
import javax.faces.el.EvaluationException; |
31 | |
import javax.faces.el.PropertyResolver; |
32 | |
import java.beans.FeatureDescriptor; |
33 | |
import java.util.Iterator; |
34 | |
import java.util.List; |
35 | |
import org.apache.myfaces.el.DefaultPropertyResolver; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
@SuppressWarnings("deprecation") |
45 | |
public final class PropertyResolverToELResolver extends ELResolver |
46 | |
{ |
47 | |
private PropertyResolver propertyResolver; |
48 | |
|
49 | |
private ExpressionFactory expressionFactory; |
50 | |
|
51 | |
private boolean isDefaultLegacyPropertyResolver; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public PropertyResolverToELResolver(final PropertyResolver propertyResolver) |
57 | 0 | { |
58 | 0 | this.propertyResolver = propertyResolver; |
59 | 0 | this.isDefaultLegacyPropertyResolver = (propertyResolver instanceof DefaultPropertyResolver); |
60 | 0 | } |
61 | |
|
62 | |
@Override |
63 | |
public void setValue(final ELContext context, final Object base, final Object property, final Object value) |
64 | |
throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException |
65 | |
{ |
66 | 0 | if (isDefaultLegacyPropertyResolver) |
67 | |
{ |
68 | 0 | return; |
69 | |
} |
70 | 0 | if (base == null || property == null) |
71 | |
{ |
72 | 0 | return; |
73 | |
} |
74 | |
|
75 | |
try |
76 | |
{ |
77 | 0 | context.setPropertyResolved(true); |
78 | 0 | if (needsCoersion(base)) |
79 | |
{ |
80 | 0 | propertyResolver.setValue(base, coerceToInt(property), value); |
81 | |
} |
82 | |
else |
83 | |
{ |
84 | 0 | propertyResolver.setValue(base, property, value); |
85 | |
} |
86 | |
|
87 | 0 | context.setPropertyResolved( |
88 | |
FacesContext.getCurrentInstance().getELContext().isPropertyResolved()); |
89 | |
|
90 | |
} |
91 | 0 | catch (javax.faces.el.PropertyNotFoundException e) |
92 | |
{ |
93 | 0 | context.setPropertyResolved(false); |
94 | 0 | throw new PropertyNotFoundException(e.getMessage(), e); |
95 | |
} |
96 | 0 | catch (EvaluationException e) |
97 | |
{ |
98 | 0 | context.setPropertyResolved(false); |
99 | 0 | throw new ELException(e.getMessage(), e); |
100 | |
} |
101 | 0 | catch (RuntimeException e) |
102 | |
{ |
103 | 0 | context.setPropertyResolved(false); |
104 | 0 | throw e; |
105 | 0 | } |
106 | 0 | } |
107 | |
|
108 | |
@Override |
109 | |
public boolean isReadOnly(final ELContext context, final Object base, final Object property) |
110 | |
throws NullPointerException, PropertyNotFoundException, ELException |
111 | |
{ |
112 | 0 | if (isDefaultLegacyPropertyResolver) |
113 | |
{ |
114 | 0 | return false; |
115 | |
} |
116 | 0 | if (base == null || property == null) |
117 | |
{ |
118 | 0 | return true; |
119 | |
} |
120 | |
|
121 | |
try |
122 | |
{ |
123 | |
boolean result; |
124 | 0 | context.setPropertyResolved(true); |
125 | 0 | if (needsCoersion(base)) |
126 | |
{ |
127 | 0 | result = propertyResolver.isReadOnly(base, coerceToInt(property)); |
128 | |
} |
129 | |
else |
130 | |
{ |
131 | 0 | result = propertyResolver.isReadOnly(base, property); |
132 | |
} |
133 | |
|
134 | |
|
135 | 0 | context.setPropertyResolved( |
136 | |
FacesContext.getCurrentInstance().getELContext().isPropertyResolved()); |
137 | 0 | return result; |
138 | |
} |
139 | 0 | catch (javax.faces.el.PropertyNotFoundException e) |
140 | |
{ |
141 | 0 | context.setPropertyResolved(false); |
142 | 0 | throw new PropertyNotFoundException(e.getMessage(), e); |
143 | |
} |
144 | 0 | catch (EvaluationException e) |
145 | |
{ |
146 | 0 | context.setPropertyResolved(false); |
147 | 0 | throw new ELException(e.getMessage(), e); |
148 | |
} |
149 | 0 | catch (RuntimeException e) |
150 | |
{ |
151 | 0 | context.setPropertyResolved(false); |
152 | 0 | throw e; |
153 | |
} |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public Object getValue(final ELContext context, final Object base, final Object property) |
158 | |
throws NullPointerException, PropertyNotFoundException, ELException |
159 | |
{ |
160 | 0 | if (isDefaultLegacyPropertyResolver) |
161 | |
{ |
162 | 0 | return null; |
163 | |
} |
164 | 0 | if (base == null || property == null) |
165 | |
{ |
166 | 0 | return null; |
167 | |
} |
168 | |
|
169 | |
try |
170 | |
{ |
171 | 0 | context.setPropertyResolved(true); |
172 | |
Object value; |
173 | 0 | if (needsCoersion(base)) |
174 | |
{ |
175 | 0 | value = propertyResolver.getValue(base, coerceToInt(property)); |
176 | |
} |
177 | |
else |
178 | |
{ |
179 | 0 | value = propertyResolver.getValue(base, property); |
180 | |
} |
181 | |
|
182 | |
|
183 | 0 | context.setPropertyResolved( |
184 | |
FacesContext.getCurrentInstance().getELContext().isPropertyResolved()); |
185 | |
|
186 | 0 | return value; |
187 | |
} |
188 | 0 | catch (javax.faces.el.PropertyNotFoundException e) |
189 | |
{ |
190 | 0 | context.setPropertyResolved(false); |
191 | 0 | throw new PropertyNotFoundException(e.getMessage(), e); |
192 | |
} |
193 | 0 | catch (EvaluationException e) |
194 | |
{ |
195 | 0 | context.setPropertyResolved(false); |
196 | 0 | throw new ELException(e.getMessage(), e); |
197 | |
} |
198 | 0 | catch (RuntimeException e) |
199 | |
{ |
200 | 0 | context.setPropertyResolved(false); |
201 | 0 | throw e; |
202 | |
} |
203 | |
} |
204 | |
|
205 | |
@Override |
206 | |
public Class<?> getType(final ELContext context, final Object base, final Object property) |
207 | |
throws NullPointerException, PropertyNotFoundException, ELException |
208 | |
{ |
209 | 0 | if (isDefaultLegacyPropertyResolver) |
210 | |
{ |
211 | 0 | return null; |
212 | |
} |
213 | 0 | if (base == null || property == null) |
214 | |
{ |
215 | 0 | return null; |
216 | |
} |
217 | |
|
218 | |
try |
219 | |
{ |
220 | 0 | context.setPropertyResolved(true); |
221 | |
Class<?> value; |
222 | 0 | if (needsCoersion(base)) |
223 | |
{ |
224 | 0 | value = propertyResolver.getType(base, coerceToInt(property)); |
225 | |
} |
226 | |
else |
227 | |
{ |
228 | 0 | value = propertyResolver.getType(base, property); |
229 | |
} |
230 | |
|
231 | |
|
232 | 0 | context.setPropertyResolved( |
233 | |
FacesContext.getCurrentInstance().getELContext().isPropertyResolved()); |
234 | |
|
235 | 0 | return value; |
236 | |
} |
237 | 0 | catch (javax.faces.el.PropertyNotFoundException e) |
238 | |
{ |
239 | 0 | context.setPropertyResolved(false); |
240 | 0 | throw new PropertyNotFoundException(e.getMessage(), e); |
241 | |
} |
242 | 0 | catch (EvaluationException e) |
243 | |
{ |
244 | 0 | context.setPropertyResolved(false); |
245 | 0 | throw new ELException(e.getMessage(), e); |
246 | |
} |
247 | 0 | catch (RuntimeException e) |
248 | |
{ |
249 | 0 | context.setPropertyResolved(false); |
250 | 0 | throw e; |
251 | |
} |
252 | |
} |
253 | |
|
254 | |
@Override |
255 | |
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) |
256 | |
{ |
257 | 0 | return null; |
258 | |
} |
259 | |
|
260 | |
@Override |
261 | |
public Class<?> getCommonPropertyType(ELContext context, Object base) |
262 | |
{ |
263 | 0 | if (isDefaultLegacyPropertyResolver) |
264 | |
{ |
265 | 0 | return null; |
266 | |
} |
267 | 0 | if (base == null) |
268 | |
{ |
269 | 0 | return null; |
270 | |
} |
271 | |
|
272 | 0 | return Object.class; |
273 | |
} |
274 | |
|
275 | |
private static boolean needsCoersion(Object base) |
276 | |
{ |
277 | 0 | return (base instanceof List) || base.getClass().isArray(); |
278 | |
} |
279 | |
|
280 | |
protected ExpressionFactory getExpressionFactory() |
281 | |
{ |
282 | 0 | if (expressionFactory == null) |
283 | |
{ |
284 | 0 | ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder |
285 | |
.getFactory(FactoryFinder.APPLICATION_FACTORY); |
286 | 0 | expressionFactory = appFactory.getApplication().getExpressionFactory(); |
287 | |
} |
288 | 0 | return expressionFactory; |
289 | |
} |
290 | |
|
291 | |
public void setExpressionFactory(ExpressionFactory expressionFactory) |
292 | |
{ |
293 | 0 | this.expressionFactory = expressionFactory; |
294 | 0 | } |
295 | |
|
296 | |
private int coerceToInt(Object property) |
297 | |
{ |
298 | 0 | return (Integer) getExpressionFactory().coerceToType(property, Integer.class); |
299 | |
} |
300 | |
|
301 | |
|
302 | |
public PropertyResolver getPropertyResolver() |
303 | |
{ |
304 | 0 | return propertyResolver; |
305 | |
} |
306 | |
} |