1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts.ui.common;
20
21 import java.io.IOException;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import javax.faces.component.NamingContainer;
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UIForm;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30 import javax.faces.el.EvaluationException;
31 import javax.faces.el.MethodBinding;
32 import javax.faces.event.AbortProcessingException;
33 import javax.faces.event.ActionEvent;
34
35 import org.apache.myfaces.shared_impl.renderkit.html.HtmlFormRendererBase;
36
37
38
39
40
41
42
43
44 public final class JSFUtils
45 {
46
47
48
49
50 private JSFUtils()
51 {
52 }
53
54
55
56
57
58
59
60
61
62
63 public static void outputAttribute(ResponseWriter out, Object attr, String mapping)
64 throws IOException
65 {
66 if (attr != null)
67 {
68 out.write(' ');
69 out.write(mapping);
70 out.write("=\"");
71 out.write(attr.toString());
72 out.write('"');
73 }
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87 public static String getActionHiddenFieldName(FacesContext context, UIComponent component)
88 {
89 return JSFUtils.getParentForm(context, component).getClientId(context) + NamingContainer.SEPARATOR_CHAR + "act";
90 }
91
92
93
94
95
96
97
98
99
100 public static void encodeRecursive(FacesContext context, UIComponent component)
101 throws IOException
102 {
103 if (component.isRendered() == true)
104 {
105 component.encodeBegin(context);
106
107
108 if (component.getRendersChildren() == true)
109 {
110 component.encodeChildren(context);
111 }
112 else
113 {
114 if (component.getChildCount() != 0)
115 {
116 for (Iterator i=component.getChildren().iterator(); i.hasNext();
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public static String generateFormSubmit(FacesContext context, UIComponent component, String fieldId, String fieldValue)
141 {
142 return generateFormSubmit(context, component, fieldId, fieldValue, false, null);
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public static String generateFormSubmit(FacesContext context, UIComponent component, String fieldId,
160 String fieldValue, Map<String, String> params)
161 {
162 return generateFormSubmit(context, component, fieldId, fieldValue, false, params);
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public static String generateFormSubmit(FacesContext context, UIComponent component, String fieldId,
183 String fieldValue, boolean valueIsParam, Map<String, String> params)
184 {
185 UIForm form = JSFUtils.getParentForm(context, component);
186 if (form == null)
187 {
188 throw new IllegalStateException("Must nest components inside UIForm to generate form submit!");
189 }
190
191 String formClientId = form.getClientId(context);
192
193 StringBuilder buf = new StringBuilder(200);
194 buf.append("document.forms['");
195 buf.append(formClientId);
196 buf.append("']['");
197 buf.append(fieldId);
198 buf.append("'].value=");
199 if (valueIsParam == false)
200 {
201 buf.append("'");
202 }
203 buf.append(fieldValue);
204 if (valueIsParam == false)
205 {
206 buf.append("'");
207 }
208 buf.append(";");
209
210 if (params != null)
211 {
212 for (String name : params.keySet())
213 {
214 buf.append("document.forms['");
215 buf.append(formClientId);
216 buf.append("']['");
217 buf.append(name);
218 buf.append("'].value='");
219 buf.append(StringUtils.replace(params.get(name), "'", "\\'"));
220 buf.append("';");
221
222
223
224 HtmlFormRendererBase.addHiddenCommandParameter(context, form, name);
225 }
226 }
227
228 buf.append("document.forms['");
229 buf.append(formClientId);
230 buf.append("'].submit();");
231
232 if (valueIsParam == false)
233 {
234 buf.append("return false;");
235 }
236
237
238
239 HtmlFormRendererBase.addHiddenCommandParameter(context, form, fieldId);
240
241 return buf.toString();
242 }
243
244
245
246
247
248
249
250
251
252 public static String generateFormSubmit(FacesContext context, UIComponent component)
253 {
254 UIForm form = JSFUtils.getParentForm(context, component);
255 if (form == null)
256 {
257 throw new IllegalStateException("Must nest components inside UIForm to generate form submit!");
258 }
259
260 String formClientId = form.getClientId(context);
261
262 StringBuilder buf = new StringBuilder(48);
263
264 buf.append("document.forms['");
265 buf.append(formClientId);
266 buf.append("'].submit()");
267
268 buf.append(";return false;");
269
270 return buf.toString();
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 public static String buildImageTag(FacesContext context, String image, int width, int height,
287 String alt, String onclick)
288 {
289 return buildImageTag(context, image, width, height, alt, onclick, null);
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 public static String buildImageTag(FacesContext context, String image, int width, int height,
307 String alt, String onclick, String verticalAlign)
308 {
309 StringBuilder buf = new StringBuilder(200);
310
311 String style = "border-width:0px;";
312 buf.append("<img src='")
313 .append(context.getExternalContext().getRequestContextPath())
314 .append(image)
315 .append("' width='")
316 .append(width)
317 .append("' height='")
318 .append(height)
319 .append("'");
320
321 if (alt != null)
322 {
323 alt = StringUtils.encode(alt);
324 buf.append(" alt='")
325 .append(alt)
326 .append("' title='")
327 .append(alt)
328 .append("'");
329 }
330 else
331 {
332 buf.append(" alt=''");
333 }
334
335 if (verticalAlign != null)
336 {
337 StringBuilder styleBuf = new StringBuilder(40);
338 styleBuf.append(style).append("vertical-align:").append(verticalAlign).append(";");
339 style = styleBuf.toString();
340 }
341
342 if (onclick != null)
343 {
344 buf.append(" onclick=\"").append(onclick).append('"');
345 StringBuilder styleBuf = new StringBuilder(style.length() + 16);
346 styleBuf.append(style).append("cursor:pointer;");
347 style = styleBuf.toString();
348 }
349 buf.append(" style='").append(style).append("'/>");
350
351 return buf.toString();
352 }
353
354
355
356
357
358
359
360
361
362
363
364
365
366 public static String buildImageTag(FacesContext context, String image, int width, int height, String alt)
367 {
368 return buildImageTag(context, image, width, height, alt, null);
369 }
370
371
372
373
374
375
376
377
378
379
380
381 public static String buildImageTag(FacesContext context, String image, String alt)
382 {
383 return buildImageTag(context, image, alt, null);
384 }
385
386
387
388
389
390
391
392
393
394
395
396
397 public static String buildImageTag(FacesContext context, String image, String alt, String verticalAlign)
398 {
399 StringBuilder buf = new StringBuilder(128);
400 buf.append("<img src='")
401 .append(context.getExternalContext().getRequestContextPath())
402 .append(image)
403 .append("' ");
404
405 String style = "border-width:0px;";
406 if (alt != null)
407 {
408 alt = StringUtils.encode(alt);
409 buf.append(" alt='")
410 .append(alt)
411 .append("' title='")
412 .append(alt)
413 .append("'");
414 }
415 else
416 {
417 buf.append(" alt=''");
418 }
419
420 if (verticalAlign != null)
421 {
422 StringBuilder styleBuf = new StringBuilder(40);
423 styleBuf.append(style).append("vertical-align:").append(verticalAlign).append(";");
424 style = styleBuf.toString();
425 }
426
427 buf.append(" style='").append(style).append("'/>");
428
429 return buf.toString();
430 }
431
432
433
434
435
436
437
438
439
440 public static UIForm getParentForm(FacesContext context, UIComponent component)
441 {
442 UIComponent parent = component.getParent();
443 while (parent != null)
444 {
445 if (parent instanceof UIForm)
446 {
447 break;
448 }
449 parent = parent.getParent();
450 }
451 return (UIForm)parent;
452 }
453
454
455
456
457
458
459
460
461
462
463 public static UIComponent getParentNamingContainer(FacesContext context, UIComponent component)
464 {
465 UIComponent parent = component.getParent();
466 while (parent != null)
467 {
468 if (parent instanceof NamingContainer)
469 {
470 break;
471 }
472 parent = parent.getParent();
473 }
474 return (UIComponent)parent;
475 }
476
477
478
479
480
481
482
483 public static boolean isComponentDisabledOrReadOnly(UIComponent component)
484 {
485 boolean disabled = false;
486 boolean readOnly = false;
487
488 Object disabledAttr = component.getAttributes().get("disabled");
489 if (disabledAttr != null)
490 {
491 disabled = disabledAttr.equals(Boolean.TRUE);
492 }
493
494 if (disabled == false)
495 {
496 Object readOnlyAttr = component.getAttributes().get("readonly");
497 if (readOnlyAttr != null)
498 {
499 readOnly = readOnlyAttr.equals(Boolean.TRUE);
500 }
501 }
502
503 return disabled || readOnly;
504 }
505
506
507
508
509
510
511
512
513
514 public static void processActionMethod(FacesContext context, MethodBinding method, ActionEvent event)
515 {
516 try
517 {
518 method.invoke(context, new Object[] {event});
519 }
520 catch (EvaluationException e)
521 {
522 Throwable cause = e.getCause();
523 if (cause instanceof AbortProcessingException)
524 {
525 throw (AbortProcessingException)cause;
526 }
527 else
528 {
529 throw e;
530 }
531 }
532 }
533
534 }