1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.compiler; |
20 | |
|
21 | |
import java.util.Collection; |
22 | |
|
23 | |
import javax.faces.application.FacesMessage; |
24 | |
import javax.faces.component.NamingContainer; |
25 | |
import javax.faces.component.UIComponent; |
26 | |
import javax.faces.component.UIInput; |
27 | |
import javax.faces.component.UIViewRoot; |
28 | |
import javax.faces.component.UniqueIdVendor; |
29 | |
import javax.faces.context.FacesContext; |
30 | |
import javax.faces.el.EvaluationException; |
31 | |
import javax.faces.el.MethodBinding; |
32 | |
import javax.faces.el.ValueBinding; |
33 | |
import javax.faces.validator.Validator; |
34 | |
import javax.faces.validator.ValidatorException; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
class _ComponentUtils |
43 | |
{ |
44 | |
private _ComponentUtils() |
45 | 0 | { |
46 | 0 | } |
47 | |
|
48 | |
static UIComponent findParentNamingContainer(UIComponent component, boolean returnRootIfNotFound) |
49 | |
{ |
50 | 0 | UIComponent parent = component.getParent(); |
51 | 0 | if (returnRootIfNotFound && parent == null) |
52 | |
{ |
53 | 0 | return component; |
54 | |
} |
55 | 0 | while (parent != null) |
56 | |
{ |
57 | 0 | if (parent instanceof NamingContainer) |
58 | |
{ |
59 | 0 | return parent; |
60 | |
} |
61 | 0 | if (returnRootIfNotFound) |
62 | |
{ |
63 | 0 | UIComponent nextParent = parent.getParent(); |
64 | 0 | if (nextParent == null) |
65 | |
{ |
66 | 0 | return parent; |
67 | |
} |
68 | 0 | parent = nextParent; |
69 | 0 | } |
70 | |
else |
71 | |
{ |
72 | 0 | parent = parent.getParent(); |
73 | |
} |
74 | |
} |
75 | 0 | return null; |
76 | |
} |
77 | |
|
78 | |
static UniqueIdVendor findParentUniqueIdVendor(UIComponent component) |
79 | |
{ |
80 | 0 | UIComponent parent = component.getParent(); |
81 | |
|
82 | 0 | while (parent != null) |
83 | |
{ |
84 | 0 | if (parent instanceof UniqueIdVendor) |
85 | |
{ |
86 | 0 | return (UniqueIdVendor) parent; |
87 | |
} |
88 | 0 | parent = parent.getParent(); |
89 | |
} |
90 | 0 | return null; |
91 | |
} |
92 | |
|
93 | |
static UIComponent getRootComponent(UIComponent component) |
94 | |
{ |
95 | |
UIComponent parent; |
96 | |
for (;;) |
97 | |
{ |
98 | 0 | parent = component.getParent(); |
99 | 0 | if (parent == null) |
100 | |
{ |
101 | 0 | return component; |
102 | |
} |
103 | 0 | component = parent; |
104 | |
} |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
static UIComponent findComponent(UIComponent findBase, String id, final char separatorChar) |
120 | |
{ |
121 | 0 | if (!(findBase instanceof NamingContainer) && idsAreEqual(id, findBase)) |
122 | |
{ |
123 | 0 | return findBase; |
124 | |
} |
125 | |
|
126 | 0 | int facetCount = findBase.getFacetCount(); |
127 | 0 | if (facetCount > 0) |
128 | |
{ |
129 | 0 | for (UIComponent facet : findBase.getFacets().values()) |
130 | |
{ |
131 | 0 | if (!(facet instanceof NamingContainer)) |
132 | |
{ |
133 | 0 | UIComponent find = findComponent(facet, id, separatorChar); |
134 | 0 | if (find != null) |
135 | |
{ |
136 | 0 | return find; |
137 | |
} |
138 | 0 | } |
139 | 0 | else if (idsAreEqual(id, facet)) |
140 | |
{ |
141 | 0 | return facet; |
142 | |
} |
143 | 0 | } |
144 | |
} |
145 | |
|
146 | 0 | for (int i = 0, childCount = findBase.getChildCount(); i < childCount; i++) |
147 | |
{ |
148 | 0 | UIComponent child = findBase.getChildren().get(i); |
149 | 0 | if (!(child instanceof NamingContainer)) |
150 | |
{ |
151 | 0 | UIComponent find = findComponent(child, id, separatorChar); |
152 | 0 | if (find != null) |
153 | |
{ |
154 | 0 | return find; |
155 | |
} |
156 | 0 | } |
157 | 0 | else if (idsAreEqual(id, child)) |
158 | |
{ |
159 | 0 | return child; |
160 | |
} |
161 | |
} |
162 | |
|
163 | 0 | if (findBase instanceof NamingContainer && idsAreEqual(id, findBase)) |
164 | |
{ |
165 | 0 | return findBase; |
166 | |
} |
167 | |
|
168 | 0 | return null; |
169 | |
} |
170 | |
|
171 | |
static UIComponent findComponentChildOrFacetFrom(UIComponent parent, String id, String innerExpr) |
172 | |
{ |
173 | 0 | if (parent.getFacetCount() > 0) |
174 | |
{ |
175 | 0 | for (UIComponent facet : parent.getFacets().values()) |
176 | |
{ |
177 | 0 | if (id.equals(facet.getId())) |
178 | |
{ |
179 | 0 | if (innerExpr == null) |
180 | |
{ |
181 | 0 | return facet; |
182 | |
} |
183 | 0 | else if (facet instanceof NamingContainer) |
184 | |
{ |
185 | 0 | UIComponent find = facet.findComponent(innerExpr); |
186 | 0 | if (find != null) |
187 | |
{ |
188 | 0 | return find; |
189 | |
} |
190 | 0 | } |
191 | |
} |
192 | 0 | else if (!(facet instanceof NamingContainer)) |
193 | |
{ |
194 | 0 | UIComponent find = findComponentChildOrFacetFrom(facet, id, innerExpr); |
195 | 0 | if (find != null) |
196 | |
{ |
197 | 0 | return find; |
198 | |
} |
199 | |
} |
200 | 0 | } |
201 | |
} |
202 | 0 | if (parent.getChildCount() > 0) |
203 | |
{ |
204 | 0 | for (int i = 0, childCount = parent.getChildCount(); i < childCount; i++) |
205 | |
{ |
206 | 0 | UIComponent child = parent.getChildren().get(i); |
207 | 0 | if (id.equals(child.getId())) |
208 | |
{ |
209 | 0 | if (innerExpr == null) |
210 | |
{ |
211 | 0 | return child; |
212 | |
} |
213 | 0 | else if (child instanceof NamingContainer) |
214 | |
{ |
215 | 0 | UIComponent find = child.findComponent(innerExpr); |
216 | 0 | if (find != null) |
217 | |
{ |
218 | 0 | return find; |
219 | |
} |
220 | 0 | } |
221 | |
} |
222 | 0 | else if (!(child instanceof NamingContainer)) |
223 | |
{ |
224 | 0 | UIComponent find = findComponentChildOrFacetFrom(child, id, innerExpr); |
225 | 0 | if (find != null) |
226 | |
{ |
227 | 0 | return find; |
228 | |
} |
229 | |
} |
230 | |
} |
231 | |
} |
232 | 0 | return null; |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
private static boolean idsAreEqual(String id, UIComponent cmp) |
240 | |
{ |
241 | 0 | if (id.equals(cmp.getId())) |
242 | |
{ |
243 | 0 | return true; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | 0 | return false; |
261 | |
} |
262 | |
|
263 | |
static void callValidators(FacesContext context, UIInput input, Object convertedValue) |
264 | |
{ |
265 | |
|
266 | 0 | Validator[] validators = input.getValidators(); |
267 | 0 | for (int i = 0; i < validators.length; i++) |
268 | |
{ |
269 | 0 | Validator validator = validators[i]; |
270 | |
try |
271 | |
{ |
272 | 0 | validator.validate(context, input, convertedValue); |
273 | |
} |
274 | 0 | catch (ValidatorException e) |
275 | |
{ |
276 | 0 | input.setValid(false); |
277 | |
|
278 | 0 | String validatorMessage = input.getValidatorMessage(); |
279 | 0 | if (validatorMessage != null) |
280 | |
{ |
281 | 0 | context.addMessage(input.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR, |
282 | |
validatorMessage, validatorMessage)); |
283 | |
} |
284 | |
else |
285 | |
{ |
286 | 0 | FacesMessage facesMessage = e.getFacesMessage(); |
287 | 0 | if (facesMessage != null) |
288 | |
{ |
289 | 0 | context.addMessage(input.getClientId(context), facesMessage); |
290 | |
} |
291 | 0 | Collection<FacesMessage> facesMessages = e.getFacesMessages(); |
292 | 0 | if (facesMessages != null) |
293 | |
{ |
294 | 0 | for (FacesMessage message : facesMessages) |
295 | |
{ |
296 | 0 | context.addMessage(input.getClientId(context), message); |
297 | 0 | } |
298 | |
} |
299 | |
} |
300 | 0 | } |
301 | |
} |
302 | |
|
303 | |
|
304 | |
|
305 | 0 | MethodBinding validatorBinding = input.getValidator(); |
306 | 0 | if (validatorBinding != null) |
307 | |
{ |
308 | |
try |
309 | |
{ |
310 | 0 | validatorBinding.invoke(context, new Object[] { context, input, convertedValue }); |
311 | |
} |
312 | 0 | catch (EvaluationException e) |
313 | |
{ |
314 | 0 | input.setValid(false); |
315 | 0 | Throwable cause = e.getCause(); |
316 | 0 | if (cause instanceof ValidatorException) |
317 | |
{ |
318 | 0 | String validatorMessage = input.getValidatorMessage(); |
319 | 0 | if (validatorMessage != null) |
320 | |
{ |
321 | 0 | context.addMessage(input.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR, |
322 | |
validatorMessage, validatorMessage)); |
323 | |
} |
324 | |
else |
325 | |
{ |
326 | 0 | FacesMessage facesMessage = ((ValidatorException)cause).getFacesMessage(); |
327 | 0 | if (facesMessage != null) |
328 | |
{ |
329 | 0 | context.addMessage(input.getClientId(context), facesMessage); |
330 | |
} |
331 | 0 | Collection<FacesMessage> facesMessages = ((ValidatorException)cause).getFacesMessages(); |
332 | 0 | if (facesMessages != null) |
333 | |
{ |
334 | 0 | for (FacesMessage message : facesMessages) |
335 | |
{ |
336 | 0 | context.addMessage(input.getClientId(context), message); |
337 | 0 | } |
338 | |
} |
339 | |
} |
340 | 0 | } |
341 | |
else |
342 | |
{ |
343 | 0 | throw e; |
344 | |
} |
345 | 0 | } |
346 | |
} |
347 | 0 | } |
348 | |
|
349 | |
static String getStringValue(FacesContext context, ValueBinding vb) |
350 | |
{ |
351 | 0 | Object value = vb.getValue(context); |
352 | 0 | if (value == null) |
353 | |
{ |
354 | 0 | return null; |
355 | |
} |
356 | 0 | return value.toString(); |
357 | |
} |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
static String getPathToComponent(UIComponent component) |
376 | |
{ |
377 | 0 | StringBuffer buf = new StringBuffer(); |
378 | |
|
379 | 0 | if (component == null) |
380 | |
{ |
381 | 0 | buf.append("{Component-Path : "); |
382 | 0 | buf.append("[null]}"); |
383 | 0 | return buf.toString(); |
384 | |
} |
385 | |
|
386 | 0 | getPathToComponent(component, buf); |
387 | |
|
388 | 0 | buf.insert(0, "{Component-Path : "); |
389 | 0 | buf.append("}"); |
390 | |
|
391 | 0 | return buf.toString(); |
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
static boolean isRendered(FacesContext facesContext, UIComponent uiComponent) |
400 | |
{ |
401 | |
|
402 | |
|
403 | |
try |
404 | |
{ |
405 | 0 | uiComponent.pushComponentToEL(facesContext, uiComponent); |
406 | 0 | return uiComponent.isRendered(); |
407 | |
} |
408 | |
finally |
409 | |
{ |
410 | 0 | uiComponent.popComponentFromEL(facesContext); |
411 | |
} |
412 | |
} |
413 | |
|
414 | |
private static void getPathToComponent(UIComponent component, StringBuffer buf) |
415 | |
{ |
416 | 0 | if (component == null) |
417 | |
{ |
418 | 0 | return; |
419 | |
} |
420 | |
|
421 | 0 | StringBuffer intBuf = new StringBuffer(); |
422 | |
|
423 | 0 | intBuf.append("[Class: "); |
424 | 0 | intBuf.append(component.getClass().getName()); |
425 | 0 | if (component instanceof UIViewRoot) |
426 | |
{ |
427 | 0 | intBuf.append(",ViewId: "); |
428 | 0 | intBuf.append(((UIViewRoot)component).getViewId()); |
429 | |
} |
430 | |
else |
431 | |
{ |
432 | 0 | intBuf.append(",Id: "); |
433 | 0 | intBuf.append(component.getId()); |
434 | |
} |
435 | 0 | intBuf.append("]"); |
436 | |
|
437 | 0 | buf.insert(0, intBuf.toString()); |
438 | |
|
439 | 0 | getPathToComponent(component.getParent(), buf); |
440 | 0 | } |
441 | |
} |