1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.renderkit;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.Serializable;
26 import java.lang.reflect.Array;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Date;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 import javax.el.ValueExpression;
40 import javax.faces.FacesException;
41 import javax.faces.FactoryFinder;
42 import javax.faces.application.FacesMessage;
43 import javax.faces.application.ProjectStage;
44 import javax.faces.application.Resource;
45 import javax.faces.application.ResourceHandler;
46 import javax.faces.component.EditableValueHolder;
47 import javax.faces.component.NamingContainer;
48 import javax.faces.component.UIComponent;
49 import javax.faces.component.UIForm;
50 import javax.faces.component.UIInput;
51 import javax.faces.component.UINamingContainer;
52 import javax.faces.component.UIOutput;
53 import javax.faces.component.UISelectMany;
54 import javax.faces.component.UISelectOne;
55 import javax.faces.component.UIViewRoot;
56 import javax.faces.component.ValueHolder;
57 import javax.faces.component.html.HtmlInputText;
58 import javax.faces.context.FacesContext;
59 import javax.faces.convert.Converter;
60 import javax.faces.convert.ConverterException;
61 import javax.faces.el.PropertyNotFoundException;
62 import javax.faces.el.ValueBinding;
63 import javax.faces.event.PhaseId;
64 import javax.faces.model.SelectItem;
65 import javax.faces.render.RenderKit;
66 import javax.faces.render.RenderKitFactory;
67 import javax.faces.render.ResponseStateManager;
68
69 import org.apache.myfaces.shared.renderkit.html.util.FormInfo;
70 import org.apache.myfaces.shared.util.HashMapUtils;
71 import org.apache.myfaces.shared.util.SelectItemsIterator;
72
73
74
75
76
77 public final class RendererUtils
78 {
79 private RendererUtils()
80 {
81
82 }
83
84
85 private static final Logger log = Logger.getLogger(RendererUtils.class
86 .getName());
87
88 public static final String SELECT_ITEM_LIST_ATTR = RendererUtils.class
89 .getName() + ".LIST";
90 public static final String EMPTY_STRING = "";
91
92 public static final Object NOTHING = new Serializable()
93 {
94 public boolean equals(final Object o)
95 {
96 if (o != null)
97 {
98 if (o.getClass().equals(this.getClass()))
99 {
100 return true;
101 }
102 }
103 return false;
104 }
105
106 @Override
107 public int hashCode()
108 {
109 return super.hashCode();
110 }
111 };
112
113 public static final String ACTION_FOR_LIST = "org.apache.myfaces.ActionForList";
114 public static final String ACTION_FOR_PHASE_LIST = "org.apache.myfaces.ActionForPhaseList";
115
116 public static final String SEQUENCE_PARAM = "jsf_sequence";
117
118 private static final String RENDER_KIT_IMPL = RendererUtils.class.getName()
119 + ".RenderKitImpl";
120
121
122 public static final String RES_NOT_FOUND = "RES_NOT_FOUND";
123
124 public static String getPathToComponent(UIComponent component)
125 {
126 StringBuilder buf = new StringBuilder();
127
128 if (component == null)
129 {
130 buf.append("{Component-Path : ");
131 buf.append("[null]}");
132 return buf.toString();
133 }
134
135 getPathToComponent(component, buf);
136
137 buf.insert(0, "{Component-Path : ");
138 Object location = component.getAttributes().get(
139 UIComponent.VIEW_LOCATION_KEY);
140 if (location != null)
141 {
142 buf.append(" Location: ").append(location);
143 }
144 buf.append("}");
145
146 return buf.toString();
147 }
148
149 private static void getPathToComponent(UIComponent component,
150 StringBuilder buf)
151 {
152 if (component == null)
153 {
154 return;
155 }
156
157 StringBuilder intBuf = new StringBuilder();
158
159 intBuf.append("[Class: ");
160 intBuf.append(component.getClass().getName());
161 if (component instanceof UIViewRoot)
162 {
163 intBuf.append(",ViewId: ");
164 intBuf.append(((UIViewRoot) component).getViewId());
165 }
166 else
167 {
168 intBuf.append(",Id: ");
169 intBuf.append(component.getId());
170 }
171 intBuf.append("]");
172
173 buf.insert(0, intBuf.toString());
174
175 getPathToComponent(component.getParent(), buf);
176 }
177
178 public static String getConcatenatedId(FacesContext context,
179 UIComponent container, String clientId)
180 {
181 UIComponent child = container.findComponent(clientId);
182
183 if (child == null)
184 {
185 return clientId;
186 }
187
188 return getConcatenatedId(context, child);
189 }
190
191 public static String getConcatenatedId(FacesContext context,
192 UIComponent component)
193 {
194 if (context == null)
195 {
196 throw new NullPointerException("context");
197 }
198
199 StringBuilder idBuf = new StringBuilder();
200
201 idBuf.append(component.getId());
202
203 UIComponent parent;
204
205 while ((parent = component.getParent()) != null)
206 {
207 if (parent instanceof NamingContainer)
208 {
209 idBuf.insert(0, UINamingContainer.getSeparatorChar(context));
210 idBuf.insert(0, parent.getId());
211 }
212 }
213
214 return idBuf.toString();
215 }
216
217 public static Boolean getBooleanValue(UIComponent component)
218 {
219 Object value = getObjectValue(component);
220
221 if (value instanceof String)
222 {
223 value = Boolean.valueOf((String) value);
224 }
225
226 if (value == null || value instanceof Boolean)
227 {
228 return (Boolean) value;
229 }
230
231 throw new IllegalArgumentException(
232 "Expected submitted value of type Boolean for Component : "
233 + getPathToComponent(component));
234
235 }
236
237 public static Date getDateValue(UIComponent component)
238 {
239 Object value = getObjectValue(component);
240 if (value == null || value instanceof Date)
241 {
242 return (Date) value;
243 }
244
245 throw new IllegalArgumentException(
246 "Expected submitted value of type Date for component : "
247 + getPathToComponent(component));
248 }
249
250 public static Object getObjectValue(UIComponent component)
251 {
252 if (!(component instanceof ValueHolder))
253 {
254 throw new IllegalArgumentException("Component : "
255 + getPathToComponent(component) + "is not a ValueHolder");
256 }
257
258 if (component instanceof EditableValueHolder)
259 {
260 Object value = ((EditableValueHolder) component)
261 .getSubmittedValue();
262 if (value != null)
263 {
264 return value;
265 }
266 }
267
268 return ((ValueHolder) component).getValue();
269 }
270
271 @Deprecated
272 public static String getStringValue(FacesContext context, ValueBinding vb)
273 {
274 Object value = vb.getValue(context);
275 if (value != null)
276 {
277 return value.toString();
278 }
279 return null;
280 }
281
282 public static String getStringValue(FacesContext context, ValueExpression ve)
283 {
284 Object value = ve.getValue(context.getELContext());
285 if (value != null)
286 {
287 return value.toString();
288 }
289 return null;
290 }
291
292 public static String getStringValue(FacesContext facesContext,
293 UIComponent component)
294 {
295 if (!(component instanceof ValueHolder))
296 {
297 throw new IllegalArgumentException("Component : "
298 + getPathToComponent(component)
299 + "is not a ValueHolder");
300 }
301
302 if (component instanceof EditableValueHolder)
303 {
304 Object submittedValue = ((EditableValueHolder) component)
305 .getSubmittedValue();
306 if (submittedValue != null)
307 {
308 if (log.isLoggable(Level.FINE))
309 {
310 log.fine("returning 1 '" + submittedValue + "'");
311 }
312 return submittedValue.toString();
313 }
314 }
315
316 Object value;
317
318 if (component instanceof EditableValueHolder)
319 {
320
321 EditableValueHolder holder = (EditableValueHolder) component;
322
323 if (holder.isLocalValueSet())
324 {
325 value = holder.getLocalValue();
326 }
327 else
328 {
329 value = getValue(component);
330 }
331 }
332 else
333 {
334 value = getValue(component);
335 }
336
337 Converter converter = ((ValueHolder) component).getConverter();
338 if (converter == null && value != null)
339 {
340
341 try
342 {
343 converter = facesContext.getApplication().createConverter(
344 value.getClass());
345 if (log.isLoggable(Level.FINE))
346 {
347 log.fine("the created converter is " + converter);
348 }
349 }
350 catch (FacesException e)
351 {
352 log.log(Level.SEVERE, "No converter for class "
353 + value.getClass().getName()
354 + " found (component id=" + component.getId()
355 + ").", e);
356
357 }
358 }
359
360 if (converter == null)
361 {
362 if (value == null)
363 {
364 if (log.isLoggable(Level.FINE))
365 {
366 log.fine("returning an empty string");
367 }
368 return "";
369 }
370
371 if (log.isLoggable(Level.FINE))
372 {
373 log.fine("returning an .toString");
374 }
375 return value.toString();
376
377 }
378
379 if (log.isLoggable(Level.FINE))
380 {
381 log.fine("returning converter get as string " + converter);
382 }
383 return converter.getAsString(facesContext, component, value);
384 }
385
386 public static String getStringFromSubmittedValueOrLocalValueReturnNull(
387 FacesContext facesContext, UIComponent component)
388 {
389 try
390 {
391 if (!(component instanceof ValueHolder))
392 {
393 throw new IllegalArgumentException("Component : "
394 + getPathToComponent(component)
395 + "is not a ValueHolder");
396 }
397
398 if (component instanceof EditableValueHolder)
399 {
400 Object submittedValue = ((EditableValueHolder) component)
401 .getSubmittedValue();
402 if (submittedValue != null)
403 {
404 if (log.isLoggable(Level.FINE))
405 {
406 log.fine("returning 1 '" + submittedValue + "'");
407 }
408 return submittedValue.toString();
409 }
410 }
411
412 Object value;
413
414 if (component instanceof EditableValueHolder)
415 {
416
417 EditableValueHolder holder = (EditableValueHolder) component;
418
419 if (holder.isLocalValueSet())
420 {
421 value = holder.getLocalValue();
422 }
423 else
424 {
425 value = getValue(component);
426 }
427 }
428 else
429 {
430 value = getValue(component);
431 }
432
433 Converter converter = ((ValueHolder) component).getConverter();
434 if (converter == null && value != null)
435 {
436
437 try
438 {
439 converter = facesContext.getApplication().createConverter(
440 value.getClass());
441 if (log.isLoggable(Level.FINE))
442 {
443 log.fine("the created converter is " + converter);
444 }
445 }
446 catch (FacesException e)
447 {
448 log.log(Level.SEVERE, "No converter for class "
449 + value.getClass().getName()
450 + " found (component id=" + component.getId()
451 + ").", e);
452
453 }
454 }
455
456 if (converter == null)
457 {
458 if (value == null)
459 {
460
461
462 return null;
463 }
464
465 if (log.isLoggable(Level.FINE))
466 {
467 log.fine("returning an .toString");
468 }
469 return value.toString();
470
471 }
472
473 if (log.isLoggable(Level.FINE))
474 {
475 log.fine("returning converter get as string " + converter);
476 }
477 return converter.getAsString(facesContext, component, value);
478
479 }
480 catch (PropertyNotFoundException ex)
481 {
482 log.log(Level.SEVERE, "Property not found - called by component : "
483 + getPathToComponent(component), ex);
484
485 throw ex;
486 }
487 }
488
489 private static Object getValue(UIComponent component)
490 {
491 Object value = ((ValueHolder) component).getValue();
492 return value;
493 }
494
495
496
497
498
499
500 public static boolean isDefaultAttributeValue(Object value)
501 {
502 if (value == null)
503 {
504 return true;
505 }
506 else if (value instanceof Boolean)
507 {
508 return !((Boolean) value).booleanValue();
509 }
510 else if (value instanceof Number)
511 {
512 if (value instanceof Integer)
513 {
514 return ((Number) value).intValue() == Integer.MIN_VALUE;
515 }
516 else if (value instanceof Double)
517 {
518 return ((Number) value).doubleValue() == Double.MIN_VALUE;
519 }
520 else if (value instanceof Long)
521 {
522 return ((Number) value).longValue() == Long.MIN_VALUE;
523 }
524 else if (value instanceof Byte)
525 {
526 return ((Number) value).byteValue() == Byte.MIN_VALUE;
527 }
528 else if (value instanceof Float)
529 {
530 return ((Number) value).floatValue() == Float.MIN_VALUE;
531 }
532 else if (value instanceof Short)
533 {
534 return ((Number) value).shortValue() == Short.MIN_VALUE;
535 }
536 }
537 return false;
538 }
539
540
541
542
543
544
545 public static Converter findUIOutputConverter(FacesContext facesContext,
546 UIOutput component) throws FacesException
547 {
548 return _SharedRendererUtils.findUIOutputConverter(facesContext,
549 component);
550 }
551
552
553
554
555
556
557
558 public static Converter findUISelectManyConverter(
559 FacesContext facesContext, UISelectMany component)
560 {
561 return findUISelectManyConverter(facesContext, component, false);
562 }
563
564
565
566
567
568
569
570
571
572
573 public static Converter findUISelectManyConverter(
574 FacesContext facesContext, UISelectMany component,
575 boolean considerValueType)
576 {
577
578 Converter converter = component.getConverter();
579 if (converter != null)
580 {
581 return converter;
582 }
583
584 if (considerValueType)
585 {
586
587 converter = _SharedRendererUtils.getValueTypeConverter(
588 facesContext, component);
589 if (converter != null)
590 {
591 return converter;
592 }
593 }
594
595
596 ValueExpression ve = component.getValueExpression("value");
597 if (ve == null)
598 {
599 return null;
600 }
601
602
603
604 Class<?> valueType = null;
605 Object value = ve.getValue(facesContext.getELContext());
606 valueType = (value != null) ? value.getClass() : ve
607 .getType(facesContext.getELContext());
608
609 if (valueType == null)
610 {
611 return null;
612 }
613
614
615
616 if (Collection.class.isAssignableFrom(valueType)
617 || Object.class.equals(valueType))
618 {
619
620 return _SharedRendererUtils.getSelectItemsValueConverter(
621 new SelectItemsIterator(component, facesContext),
622 facesContext);
623 }
624
625 if (!valueType.isArray())
626 {
627 throw new IllegalArgumentException(
628 "ValueExpression for UISelectMany : "
629 + getPathToComponent(component)
630 + " must be of type Collection or Array");
631 }
632
633 Class<?> arrayComponentType = valueType.getComponentType();
634 if (String.class.equals(arrayComponentType))
635 {
636 return null;
637 }
638
639 if (Object.class.equals(arrayComponentType))
640 {
641
642
643 return _SharedRendererUtils.getSelectItemsValueConverter(
644 new SelectItemsIterator(component, facesContext),
645 facesContext);
646 }
647
648 try
649 {
650 return facesContext.getApplication().createConverter(
651 arrayComponentType);
652 }
653 catch (FacesException e)
654 {
655 log.log(Level.SEVERE,
656 "No Converter for type " + arrayComponentType.getName()
657 + " found", e);
658 return null;
659 }
660 }
661
662 public static void checkParamValidity(FacesContext facesContext,
663 UIComponent uiComponent, Class compClass)
664 {
665 if (facesContext == null)
666 {
667 throw new NullPointerException("facesContext may not be null");
668 }
669 if (uiComponent == null)
670 {
671 throw new NullPointerException("uiComponent may not be null");
672 }
673
674
675
676 if (compClass != null && !(compClass.isInstance(uiComponent)))
677 {
678 throw new IllegalArgumentException("uiComponent : "
679 + getPathToComponent(uiComponent) + " is not instance of "
680 + compClass.getName() + " as it should be");
681 }
682 }
683
684 public static void renderChildren(FacesContext facesContext,
685 UIComponent component) throws IOException
686 {
687 if (component.getChildCount() > 0)
688 {
689 for (int i = 0; i < component.getChildCount(); i++)
690 {
691 UIComponent child = component.getChildren().get(i);
692
693 child.encodeAll(facesContext);
694 }
695 }
696 }
697
698
699
700
701
702
703
704
705 @Deprecated
706 public static void renderChild(FacesContext facesContext, UIComponent child)
707 throws IOException
708 {
709
710
711
712 if (!isRendered(facesContext, child))
713 {
714 return;
715 }
716
717 child.encodeBegin(facesContext);
718 if (child.getRendersChildren())
719 {
720 child.encodeChildren(facesContext);
721 }
722 else
723 {
724 renderChildren(facesContext, child);
725 }
726 child.encodeEnd(facesContext);
727 }
728
729
730
731
732
733
734 public static boolean isRendered(FacesContext facesContext,
735 UIComponent uiComponent)
736 {
737
738
739 try
740 {
741 uiComponent.pushComponentToEL(facesContext, uiComponent);
742 return uiComponent.isRendered();
743 }
744 finally
745 {
746 uiComponent.popComponentFromEL(facesContext);
747 }
748 }
749
750 public static List getSelectItemList(UISelectOne uiSelectOne)
751 {
752 return internalGetSelectItemList(uiSelectOne,
753 FacesContext.getCurrentInstance());
754 }
755
756
757
758
759
760
761 public static List getSelectItemList(UISelectOne uiSelectOne,
762 FacesContext facesContext)
763 {
764 return internalGetSelectItemList(uiSelectOne, facesContext);
765 }
766
767 public static List getSelectItemList(UISelectMany uiSelectMany)
768 {
769 return internalGetSelectItemList(uiSelectMany,
770 FacesContext.getCurrentInstance());
771 }
772
773
774
775
776
777
778 public static List getSelectItemList(UISelectMany uiSelectMany,
779 FacesContext facesContext)
780 {
781 return internalGetSelectItemList(uiSelectMany, facesContext);
782 }
783
784 private static List internalGetSelectItemList(UIComponent uiComponent,
785 FacesContext facesContext)
786 {
787
788
789
790
791
792
793
794
795 List list = new ArrayList();
796
797 for (Iterator iter = new SelectItemsIterator(uiComponent, facesContext); iter
798 .hasNext();)
799 {
800 list.add(iter.next());
801 }
802 return list;
803 }
804
805
806
807
808
809
810
811
812
813
814 public static Set getSubmittedValuesAsSet(FacesContext context,
815 UIComponent component, Converter converter,
816 UISelectMany uiSelectMany)
817 {
818 Object submittedValues = uiSelectMany.getSubmittedValue();
819 if (submittedValues == null)
820 {
821 return null;
822 }
823
824 if (converter != null)
825 {
826 converter = new PassThroughAsStringConverter(converter);
827 }
828
829 return internalSubmittedOrSelectedValuesAsSet(context, component,
830 converter, uiSelectMany, submittedValues, false);
831 }
832
833
834
835
836
837
838
839
840
841
842 public static Set getSelectedValuesAsSet(FacesContext context,
843 UIComponent component, Converter converter,
844 UISelectMany uiSelectMany)
845 {
846 Object selectedValues = uiSelectMany.getValue();
847
848 return internalSubmittedOrSelectedValuesAsSet(context, component,
849 converter, uiSelectMany, selectedValues, true);
850 }
851
852
853
854
855
856
857 public static String getConvertedStringValue(FacesContext context,
858 UIComponent component, Converter converter, Object value)
859 {
860 if (converter == null)
861 {
862 if (value == null)
863 {
864 return "";
865 }
866 else if (value instanceof String)
867 {
868 return (String) value;
869 }
870 else
871 {
872 return value.toString();
873 }
874 }
875
876 return converter.getAsString(context, component, value);
877 }
878
879
880
881
882
883
884 public static String getConvertedStringValue(FacesContext context,
885 UIComponent component, Converter converter, SelectItem selectItem)
886 {
887 return getConvertedStringValue(context, component, converter,
888 selectItem.getValue());
889 }
890
891 private static Set internalSubmittedOrSelectedValuesAsSet(
892 FacesContext context, UIComponent component, Converter converter,
893 UISelectMany uiSelectMany, Object values,
894 boolean allowNonArrayOrCollectionValue)
895 {
896 if (values == null || EMPTY_STRING.equals(values))
897 {
898 return Collections.EMPTY_SET;
899 }
900 else if (values instanceof Object[])
901 {
902
903 Object[] ar = (Object[]) values;
904 if (ar.length == 0)
905 {
906 return Collections.EMPTY_SET;
907 }
908
909 HashSet set = new HashSet(HashMapUtils.calcCapacity(ar.length));
910 for (int i = 0; i < ar.length; i++)
911 {
912 set.add(getConvertedStringValue(context, component, converter,
913 ar[i]));
914 }
915 return set;
916 }
917 else if (values.getClass().isArray())
918 {
919
920 int len = Array.getLength(values);
921 HashSet set = new HashSet(
922 org.apache.myfaces.shared.util.HashMapUtils
923 .calcCapacity(len));
924 for (int i = 0; i < len; i++)
925 {
926 set.add(getConvertedStringValue(context, component, converter,
927 Array.get(values, i)));
928 }
929 return set;
930 }
931 else if (values instanceof Collection)
932 {
933 Collection col = (Collection) values;
934 if (col.size() == 0)
935 {
936 return Collections.EMPTY_SET;
937 }
938
939 HashSet set = new HashSet(HashMapUtils.calcCapacity(col.size()));
940 for (Iterator i = col.iterator(); i.hasNext();)
941 {
942 set.add(getConvertedStringValue(context, component, converter,
943 i.next()));
944 }
945
946 return set;
947
948 }
949 else if (allowNonArrayOrCollectionValue)
950 {
951 HashSet set = new HashSet(HashMapUtils.calcCapacity(1));
952 set.add(values);
953 return set;
954 }
955 else
956 {
957 throw new IllegalArgumentException(
958 "Value of UISelectMany component with path : "
959 + getPathToComponent(uiSelectMany)
960 + " is not of type Array or List");
961 }
962 }
963
964 public static Object getConvertedUISelectOneValue(
965 FacesContext facesContext, UISelectOne output, Object submittedValue)
966 {
967 if (submittedValue != null && !(submittedValue instanceof String))
968 {
969 throw new IllegalArgumentException(
970 "Submitted value of type String for component : "
971 + getPathToComponent(output) + "expected");
972 }
973
974
975
976
977 if ((submittedValue != null) && (submittedValue instanceof String)
978 && ("".equals(submittedValue)))
979 {
980
981 submittedValue = null;
982 }
983
984 Converter converter;
985 try
986 {
987 converter = findUIOutputConverter(facesContext, output);
988 }
989 catch (FacesException e)
990 {
991 throw new ConverterException(e);
992 }
993
994 return converter == null ? submittedValue : converter.getAsObject(
995 facesContext, output, (String) submittedValue);
996 }
997
998 public static Object getConvertedUIOutputValue(FacesContext facesContext,
999 UIOutput output, Object submittedValue) throws ConverterException
1000 {
1001 if (submittedValue != null && !(submittedValue instanceof String))
1002 {
1003 submittedValue = submittedValue.toString();
1004 }
1005
1006 Converter converter;
1007 try
1008 {
1009 converter = findUIOutputConverter(facesContext, output);
1010 }
1011 catch (FacesException e)
1012 {
1013 throw new ConverterException(e);
1014 }
1015
1016 return converter == null ? submittedValue : converter.getAsObject(
1017 facesContext, output, (String) submittedValue);
1018 }
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030 public static Object getConvertedUISelectManyValue(
1031 FacesContext facesContext, UISelectMany selectMany,
1032 Object submittedValue) throws ConverterException
1033 {
1034
1035 return getConvertedUISelectManyValue(facesContext, selectMany,
1036 submittedValue, false);
1037 }
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050 public static Object getConvertedUISelectManyValue(
1051 FacesContext facesContext, UISelectMany selectMany,
1052 Object submittedValue, boolean considerValueType)
1053 throws ConverterException
1054 {
1055 if (submittedValue == null)
1056 {
1057 return null;
1058 }
1059
1060 if (!(submittedValue instanceof String[]))
1061 {
1062 throw new ConverterException(
1063 "Submitted value of type String[] for component : "
1064 + getPathToComponent(selectMany) + "expected");
1065 }
1066
1067 return _SharedRendererUtils.getConvertedUISelectManyValue(facesContext,
1068 selectMany, (String[]) submittedValue, considerValueType);
1069 }
1070
1071 public static boolean getBooleanAttribute(UIComponent component,
1072 String attrName, boolean defaultValue)
1073 {
1074 Boolean b = (Boolean) component.getAttributes().get(attrName);
1075 return b != null ? b.booleanValue() : defaultValue;
1076 }
1077
1078 public static int getIntegerAttribute(UIComponent component,
1079 String attrName, int defaultValue)
1080 {
1081 Integer i = (Integer) component.getAttributes().get(attrName);
1082 return i != null ? i.intValue() : defaultValue;
1083 }
1084
1085 private static final String TRINIDAD_FORM_COMPONENT_FAMILY = "org.apache.myfaces.trinidad.Form";
1086 private static final String ADF_FORM_COMPONENT_FAMILY = "oracle.adf.Form";
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 public static FormInfo findNestingForm(UIComponent uiComponent,
1104 FacesContext facesContext)
1105 {
1106 UIComponent parent = uiComponent.getParent();
1107 while (parent != null
1108 && (!ADF_FORM_COMPONENT_FAMILY.equals(parent.getFamily())
1109 && !TRINIDAD_FORM_COMPONENT_FAMILY.equals(parent
1110 .getFamily()) && !(parent instanceof UIForm)))
1111 {
1112 parent = parent.getParent();
1113 }
1114
1115 if (parent != null)
1116 {
1117
1118 String formName = parent.getClientId(facesContext);
1119 return new FormInfo(parent, formName);
1120 }
1121
1122 return null;
1123 }
1124
1125 public static boolean getBooleanValue(String attribute, Object value,
1126 boolean defaultValue)
1127 {
1128 if (value instanceof Boolean)
1129 {
1130 return ((Boolean) value).booleanValue();
1131 }
1132 else if (value instanceof String)
1133 {
1134 return Boolean.valueOf((String) value).booleanValue();
1135 }
1136 else if (value != null)
1137 {
1138 log.severe("value for attribute "
1139 + attribute
1140 + " must be instanceof 'Boolean' or 'String', is of type : "
1141 + value.getClass());
1142
1143 return defaultValue;
1144 }
1145
1146 return defaultValue;
1147 }
1148
1149 public static void copyHtmlInputTextAttributes(HtmlInputText src,
1150 HtmlInputText dest)
1151 {
1152 dest.setId(src.getId());
1153 boolean forceId = getBooleanValue(JSFAttr.FORCE_ID_ATTR, src
1154 .getAttributes().get(JSFAttr.FORCE_ID_ATTR), false);
1155 if (forceId)
1156 {
1157 dest.getAttributes().put(JSFAttr.FORCE_ID_ATTR, Boolean.TRUE);
1158 }
1159 dest.setImmediate(src.isImmediate());
1160 dest.setTransient(src.isTransient());
1161 dest.setAccesskey(src.getAccesskey());
1162 dest.setAlt(src.getAlt());
1163 dest.setConverter(src.getConverter());
1164 dest.setDir(src.getDir());
1165 dest.setDisabled(src.isDisabled());
1166 dest.setLang(src.getLang());
1167 dest.setLocalValueSet(src.isLocalValueSet());
1168 dest.setMaxlength(src.getMaxlength());
1169 dest.setOnblur(src.getOnblur());
1170 dest.setOnchange(src.getOnchange());
1171 dest.setOnclick(src.getOnclick());
1172 dest.setOndblclick(src.getOndblclick());
1173 dest.setOnfocus(src.getOnfocus());
1174 dest.setOnkeydown(src.getOnkeydown());
1175 dest.setOnkeypress(src.getOnkeypress());
1176 dest.setOnkeyup(src.getOnkeyup());
1177 dest.setOnmousedown(src.getOnmousedown());
1178 dest.setOnmousemove(src.getOnmousemove());
1179 dest.setOnmouseout(src.getOnmouseout());
1180 dest.setOnmouseover(src.getOnmouseover());
1181 dest.setOnmouseup(src.getOnmouseup());
1182 dest.setOnselect(src.getOnselect());
1183 dest.setReadonly(src.isReadonly());
1184 dest.setRendered(src.isRendered());
1185 dest.setRequired(src.isRequired());
1186 dest.setSize(src.getSize());
1187 dest.setStyle(src.getStyle());
1188 dest.setStyleClass(src.getStyleClass());
1189 dest.setTabindex(src.getTabindex());
1190 dest.setTitle(src.getTitle());
1191 dest.setValidator(src.getValidator());
1192 }
1193
1194
1195
1196
1197 @Deprecated
1198 public static Integer getViewSequence(FacesContext facescontext)
1199 {
1200 Map map = facescontext.getExternalContext().getRequestMap();
1201 Integer sequence = (Integer) map.get(SEQUENCE_PARAM);
1202 if (sequence == null)
1203 {
1204 sequence = new Integer(1);
1205 map.put(SEQUENCE_PARAM, sequence);
1206
1207 synchronized (facescontext.getExternalContext().getSession(true))
1208 {
1209 facescontext.getExternalContext().getSessionMap()
1210 .put(RendererUtils.SEQUENCE_PARAM, sequence);
1211 }
1212 }
1213 return sequence;
1214 }
1215
1216 public static UIComponent findComponent(UIComponent headerComp, Class clazz)
1217 {
1218 if (clazz.isAssignableFrom(headerComp.getClass()))
1219 {
1220 return headerComp;
1221 }
1222
1223 List li = headerComp.getChildren();
1224
1225 for (int i = 0; i < li.size(); i++)
1226 {
1227 UIComponent comp = (UIComponent) li.get(i);
1228
1229
1230 UIComponent lookupComp = findComponent(comp, clazz);
1231
1232 if (lookupComp != null)
1233 {
1234 return lookupComp;
1235 }
1236 }
1237
1238 return null;
1239 }
1240
1241 public static void addOrReplaceChild(UIInput component, UIComponent child)
1242 {
1243 List li = component.getChildren();
1244
1245 for (int i = 0; i < li.size(); i++)
1246 {
1247 UIComponent oldChild = (UIComponent) li.get(i);
1248
1249 if (oldChild.getId() != null
1250 && oldChild.getId().equals(child.getId()))
1251 {
1252 li.set(i, child);
1253 return;
1254 }
1255 }
1256
1257 component.getChildren().add(child);
1258 }
1259
1260 public static String getClientId(FacesContext facesContext,
1261 UIComponent uiComponent, String forAttr)
1262 {
1263 UIComponent forComponent = uiComponent.findComponent(forAttr);
1264 if (forComponent == null)
1265 {
1266 final char separatorChar = UINamingContainer
1267 .getSeparatorChar(facesContext);
1268 if (log.isLoggable(Level.INFO))
1269 {
1270 log.info("Unable to find component '"
1271 + forAttr
1272 + "' (calling findComponent on component '"
1273 + uiComponent.getClientId(facesContext)
1274 + "')."
1275 + " We'll try to return a guessed client-id anyways -"
1276 + " this will be a problem if you put the referenced component"
1277 + " into a different naming-container. If this is the case, "
1278 + "you can always use the full client-id.");
1279 }
1280 if (forAttr.length() > 0 && forAttr.charAt(0) == separatorChar)
1281 {
1282
1283 return forAttr.substring(1);
1284 }
1285
1286
1287 String labelClientId = uiComponent.getClientId(facesContext);
1288 int colon = labelClientId.lastIndexOf(separatorChar);
1289
1290 return colon == -1 ? forAttr : labelClientId
1291 .substring(0, colon + 1) + forAttr;
1292
1293 }
1294
1295 return forComponent.getClientId(facesContext);
1296
1297 }
1298
1299 public static List convertIdsToClientIds(String actionFor,
1300 FacesContext facesContext, UIComponent component)
1301 {
1302 List li = new ArrayList();
1303
1304 String[] ids = actionFor.split(",");
1305
1306 for (int i = 0; i < ids.length; i++)
1307 {
1308 String trimedId = ids[i].trim();
1309 if (trimedId.equals("none"))
1310 {
1311 li.add(trimedId);
1312 }
1313 else
1314 {
1315 li.add(RendererUtils.getClientId(facesContext, component,
1316 trimedId));
1317 }
1318 }
1319 return li;
1320 }
1321
1322 public static List convertPhasesToPhasesIds(String actionForPhase)
1323 {
1324 List li = new ArrayList();
1325
1326 if (actionForPhase == null)
1327 {
1328 return li;
1329 }
1330
1331 String[] ids = actionForPhase.split(",");
1332
1333 for (int i = 0; i < ids.length; i++)
1334 {
1335 if (ids[i].equals("PROCESS_VALIDATIONS"))
1336 {
1337 li.add(PhaseId.PROCESS_VALIDATIONS);
1338 }
1339 else if (ids[i].equals("UPDATE_MODEL_VALUES"))
1340 {
1341 li.add(PhaseId.UPDATE_MODEL_VALUES);
1342 }
1343 }
1344 return li;
1345 }
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355 public static String loadResourceFile(FacesContext ctx, String file)
1356 {
1357
1358 ByteArrayOutputStream content = new ByteArrayOutputStream(10240);
1359
1360 InputStream in = null;
1361 try
1362 {
1363 in = ctx.getExternalContext().getResourceAsStream(file);
1364 if (in == null)
1365 {
1366 return null;
1367 }
1368
1369 byte[] fileBuffer = new byte[10240];
1370 int read;
1371 while ((read = in.read(fileBuffer)) > -1)
1372 {
1373 content.write(fileBuffer, 0, read);
1374 }
1375 }
1376 catch (FileNotFoundException e)
1377 {
1378 if (log.isLoggable(Level.WARNING))
1379 {
1380 log.log(Level.WARNING, "no such file " + file, e);
1381 }
1382 content = null;
1383 }
1384 catch (IOException e)
1385 {
1386 if (log.isLoggable(Level.WARNING))
1387 {
1388 log.log(Level.WARNING, "problems during processing resource "
1389 + file, e);
1390 }
1391 content = null;
1392 }
1393 finally
1394 {
1395 try
1396 {
1397 if (content != null)
1398 {
1399 content.close();
1400 }
1401 }
1402 catch (IOException e)
1403 {
1404 log.log(Level.WARNING, e.getLocalizedMessage(), e);
1405 }
1406 if (in != null)
1407 {
1408 try
1409 {
1410 in.close();
1411 }
1412 catch (IOException e)
1413 {
1414 log.log(Level.WARNING, e.getLocalizedMessage(), e);
1415 }
1416 }
1417 }
1418
1419 return content.toString();
1420 }
1421
1422
1423
1424
1425
1426
1427 public static void initPartialValidationAndModelUpdate(
1428 UIComponent component, FacesContext facesContext)
1429 {
1430 String actionFor = (String) component.getAttributes().get("actionFor");
1431
1432 if (actionFor != null)
1433 {
1434 List li = convertIdsToClientIds(actionFor, facesContext, component);
1435
1436 facesContext.getExternalContext().getRequestMap()
1437 .put(ACTION_FOR_LIST, li);
1438
1439 String actionForPhase = (String) component.getAttributes().get(
1440 "actionForPhase");
1441
1442 if (actionForPhase != null)
1443 {
1444 List phaseList = convertPhasesToPhasesIds(actionForPhase);
1445
1446 facesContext.getExternalContext().getRequestMap()
1447 .put(ACTION_FOR_PHASE_LIST, phaseList);
1448 }
1449 }
1450 }
1451
1452 public static boolean isAdfOrTrinidadForm(UIComponent component)
1453 {
1454 if (component == null)
1455 {
1456 return false;
1457 }
1458 return ADF_FORM_COMPONENT_FAMILY.equals(component.getFamily())
1459 || TRINIDAD_FORM_COMPONENT_FAMILY.equals(component.getFamily());
1460 }
1461
1462
1463
1464
1465
1466
1467
1468 @Deprecated
1469 public static ResponseStateManager getResponseStateManager(
1470 FacesContext facesContext, String renderKitId)
1471 throws FacesException
1472 {
1473 RenderKit renderKit = facesContext.getRenderKit();
1474
1475 if (renderKit == null)
1476 {
1477
1478 Map attributesMap = facesContext.getAttributes();
1479 RenderKitFactory factory = (RenderKitFactory) attributesMap
1480 .get(RENDER_KIT_IMPL);
1481
1482 if (factory != null)
1483 {
1484 renderKit = factory.getRenderKit(facesContext, renderKitId);
1485 }
1486 else
1487 {
1488 factory = (RenderKitFactory) FactoryFinder
1489 .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
1490
1491 if (factory == null)
1492 {
1493 throw new IllegalStateException("Factory is null");
1494 }
1495
1496 attributesMap.put(RENDER_KIT_IMPL, factory);
1497
1498 renderKit = factory.getRenderKit(facesContext, renderKitId);
1499 }
1500 }
1501
1502 if (renderKit == null)
1503 {
1504 throw new IllegalArgumentException(
1505 "Could not find a RenderKit for \"" + renderKitId + "\"");
1506 }
1507
1508 return renderKit.getResponseStateManager();
1509 }
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523 public static String getIconSrc(final FacesContext facesContext,
1524 final UIComponent component, final String attributeName)
1525 {
1526
1527
1528 final Map<String, Object> attributes = component.getAttributes();
1529 final String resourceName = (String) attributes.get(JSFAttr.NAME_ATTR);
1530 if (resourceName != null && (resourceName.length() > 0))
1531 {
1532
1533 final ResourceHandler resourceHandler = facesContext
1534 .getApplication().getResourceHandler();
1535 final Resource resource;
1536
1537 final String libraryName = (String) component.getAttributes().get(
1538 JSFAttr.LIBRARY_ATTR);
1539 if ((libraryName != null) && (libraryName.length() > 0))
1540 {
1541 resource = resourceHandler.createResource(resourceName,
1542 libraryName);
1543 }
1544 else
1545 {
1546 resource = resourceHandler.createResource(resourceName);
1547 }
1548
1549 if (resource == null)
1550 {
1551
1552
1553 if (facesContext.isProjectStage(ProjectStage.Development))
1554 {
1555 String summary = "Unable to find resource: " + resourceName;
1556 if (libraryName != null)
1557 {
1558 summary = summary + " from library: " + libraryName;
1559 }
1560 facesContext.addMessage(
1561 component.getClientId(facesContext),
1562 new FacesMessage(FacesMessage.SEVERITY_WARN,
1563 summary, summary));
1564 }
1565
1566 return RES_NOT_FOUND;
1567 }
1568 else
1569 {
1570 return resource.getRequestPath();
1571 }
1572 }
1573 else
1574 {
1575 String value = (String) component.getAttributes()
1576 .get(attributeName);
1577 return toResourceUri(facesContext, value);
1578 }
1579 }
1580
1581
1582
1583
1584 static public String toResourceUri(FacesContext facesContext, Object o)
1585 {
1586 if (o == null)
1587 {
1588 return null;
1589 }
1590
1591 String uri = o.toString();
1592
1593
1594
1595
1596
1597
1598
1599 if (uri.length() == 0)
1600 {
1601 return null;
1602 }
1603
1604
1605
1606
1607 if (uri.contains(ResourceHandler.RESOURCE_IDENTIFIER))
1608 {
1609 return uri;
1610 }
1611
1612
1613 if (uri.startsWith("//"))
1614 {
1615 return uri.substring(1);
1616 }
1617 else
1618 {
1619
1620
1621
1622 String resourceURL = facesContext.getApplication().getViewHandler()
1623 .getResourceURL(facesContext, uri);
1624 return facesContext.getExternalContext().encodeResourceURL(
1625 resourceURL);
1626 }
1627 }
1628
1629
1630
1631
1632
1633
1634
1635 private static class PassThroughAsStringConverter implements Converter
1636 {
1637 private final Converter converter;
1638
1639 public PassThroughAsStringConverter(Converter converter)
1640 {
1641 this.converter = converter;
1642 }
1643
1644 public Object getAsObject(FacesContext context, UIComponent component,
1645 String value) throws ConverterException
1646 {
1647 return converter.getAsObject(context, component, value);
1648 }
1649
1650 public String getAsString(FacesContext context, UIComponent component,
1651 Object value) throws ConverterException
1652 {
1653 return (String) value;
1654 }
1655
1656 }
1657 }