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