1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.component;
20
21 import java.io.IOException;
22 import java.io.Serializable;
23 import java.lang.reflect.Array;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.faces.FactoryFinder;
31 import javax.faces.FacesException;
32 import javax.faces.context.FacesContext;
33 import javax.faces.el.ValueBinding;
34 import javax.faces.event.AbortProcessingException;
35 import javax.faces.event.FacesEvent;
36 import javax.faces.event.FacesListener;
37 import javax.faces.render.RenderKit;
38 import javax.faces.render.RenderKitFactory;
39 import javax.faces.render.Renderer;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public abstract class UIComponentBase
72 extends UIComponent
73 {
74 private static Log log = LogFactory.getLog(UIComponentBase.class);
75
76 private static final Iterator _EMPTY_UICOMPONENT_ITERATOR =
77 new _EmptyIterator();
78
79 private _ComponentAttributesMap _attributesMap = null;
80 private Map _valueBindingMap = null;
81 private List _childrenList = null;
82 private Map _facetMap = null;
83 private List _facesListeners = null;
84 private String _clientId = null;
85 private String _id = null;
86 private UIComponent _parent = null;
87 private boolean _transient = false;
88
89 private transient FacesContext _facesContext;
90
91 public UIComponentBase()
92 {
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public Map getAttributes()
142 {
143 if (_attributesMap == null)
144 {
145 _attributesMap = new _ComponentAttributesMap(this);
146 }
147 return _attributesMap;
148 }
149
150
151
152
153
154
155
156
157
158 public ValueBinding getValueBinding(String name)
159 {
160 if (name == null) throw new NullPointerException("name");
161 if (_valueBindingMap == null)
162 {
163 return null;
164 }
165 else
166 {
167 return (ValueBinding)_valueBindingMap.get(name);
168 }
169 }
170
171
172
173
174
175 public void setValueBinding(String name,
176 ValueBinding binding)
177 {
178 if (name == null) throw new NullPointerException("name");
179 if (_valueBindingMap == null)
180 {
181 _valueBindingMap = new HashMap();
182 }
183 _valueBindingMap.put(name, binding);
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 public String getClientId(FacesContext context)
209 {
210 if (context == null) throw new NullPointerException("context");
211
212 if (_clientId != null) return _clientId;
213
214 boolean idWasNull = false;
215 String id = getId();
216 if (id == null)
217 {
218
219
220 UIViewRoot viewRoot = context.getViewRoot();
221 if (viewRoot != null)
222 {
223 id = viewRoot.createUniqueId();
224 }
225 else
226 {
227 context.getExternalContext().log("ERROR: Cannot automatically create an id for component of type " + getClass().getName() + " because there is no viewRoot in the current facesContext!");
228 id = "ERROR";
229 }
230 setId(id);
231
232 idWasNull = true;
233 }
234
235 UIComponent namingContainer = findParentNamingContainer(this, false);
236 if (namingContainer != null)
237 {
238 _clientId = namingContainer.getClientId(context) + NamingContainer.SEPARATOR_CHAR + id;
239 }
240 else
241 {
242 _clientId = id;
243 }
244
245 Renderer renderer = getRenderer(context);
246 if (renderer != null)
247 {
248 _clientId = renderer.convertClientId(context, _clientId);
249 }
250
251 if (idWasNull)
252 {
253 context.getExternalContext().log("WARNING: Component " + _clientId + " just got an automatic id, because there was no id assigned yet. " +
254 "If this component was created dynamically (i.e. not by a JSP tag) you should assign it an " +
255 "explicit static id or assign it the id you get from the createUniqueId from the current UIViewRoot " +
256 "component right after creation!");
257 }
258
259 return _clientId;
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 public String getId()
279 {
280 return _id;
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 public void setId(String id)
303 {
304 isIdValid(id);
305 _id = id;
306 _clientId = null;
307
308 UIComponent parent = getParent();
309
310 if(parent != null) {
311 List li = getParent().getChildren();
312
313 if(li instanceof _ComponentChildrenList) {
314 ((_ComponentChildrenList) li).updateId(_id,this);
315 }
316 }
317 }
318
319 public UIComponent getParent()
320 {
321 return _parent;
322 }
323
324 public void setParent(UIComponent parent)
325 {
326 _parent = parent;
327 }
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 public boolean getRendersChildren()
346 {
347 Renderer renderer = getRenderer(getFacesContext());
348 if (renderer != null)
349 {
350 return renderer.getRendersChildren();
351 }
352 else
353 {
354 return false;
355 }
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 public List getChildren()
374 {
375 if (_childrenList == null)
376 {
377 _childrenList = new _ComponentChildrenList(this);
378 }
379 return _childrenList;
380 }
381
382
383
384
385
386
387
388
389 public int getChildCount()
390 {
391 return _childrenList == null ? 0 : _childrenList.size();
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 public UIComponent findComponent(String expr)
426 {
427 if (expr == null) throw new NullPointerException("expr");
428 if (expr.length() == 0) throw new IllegalArgumentException("empty expr");
429
430 UIComponent findBase;
431 if (expr.charAt(0) == NamingContainer.SEPARATOR_CHAR)
432 {
433 findBase = getRootComponent(this);
434 expr = expr.substring(1);
435 }
436 else
437 {
438 if (this instanceof NamingContainer)
439 {
440 findBase = this;
441 }
442 else
443 {
444 findBase = findParentNamingContainer(this, true
445 }
446 }
447
448 int separator = expr.indexOf(NamingContainer.SEPARATOR_CHAR);
449 if (separator == -1)
450 {
451 return findComponent(findBase, expr);
452 }
453 else
454 {
455 String id = expr.substring(0, separator);
456 findBase = findComponent(findBase, id);
457 if (findBase == null)
458 {
459 return null;
460 }
461 else
462 {
463 if (!(findBase instanceof NamingContainer))
464 throw new IllegalArgumentException("Intermediate identifier " + id + " in search expression " +
465 expr + " identifies a UIComponent that is not a NamingContainer");
466 return findBase.findComponent(expr.substring(separator + 1));
467 }
468 }
469 }
470
471 static UIComponent findParentNamingContainer(UIComponent component,
472 boolean returnRootIfNotFound)
473 {
474 UIComponent parent = component.getParent();
475 if (returnRootIfNotFound && parent == null)
476 {
477 return component;
478 }
479 while (parent != null)
480 {
481 if (parent instanceof NamingContainer) return parent;
482 if (returnRootIfNotFound)
483 {
484 UIComponent nextParent = parent.getParent();
485 if (nextParent == null)
486 {
487 return parent;
488 }
489 parent = nextParent;
490 }
491 else
492 {
493 parent = parent.getParent();
494 }
495 }
496 return null;
497 }
498
499 static UIComponent getRootComponent(UIComponent component)
500 {
501 UIComponent parent;
502 for(;;)
503 {
504 parent = component.getParent();
505 if (parent == null) return component;
506 component = parent;
507 }
508 }
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 private UIComponent findComponent(UIComponent findBase, String id)
525 {
526 if (idsAreEqual(id, findBase)){
527 return findBase;
528 }
529
530
531
532
533
534
535 return findComponentNormal(id, findBase);
536 }
537
538 private UIComponent findComponentCached(String id, UIComponent findBase) {
539
540 if(!(findBase.getChildren() instanceof _ComponentChildrenList)) {
541 return null;
542 }
543
544 _ComponentChildrenList li = (_ComponentChildrenList) findBase.getChildren();
545
546 UIComponent comp = li.get(id);
547
548 if(comp != null)
549 return comp;
550
551 for (Iterator it = findBase.getFacetsAndChildren(); it.hasNext(); )
552 {
553 UIComponent childOrFacet = (UIComponent)it.next();
554 UIComponent find = findComponentCached(id,childOrFacet);
555 if (find != null) return find;
556 }
557
558 return null;
559 }
560
561 private UIComponent findComponentNormal(String id, UIComponent findBase) {
562 if (idsAreEqual(id, findBase))
563 {
564 return findBase;
565 }
566
567 for (Iterator it = findBase.getFacetsAndChildren(); it.hasNext(); )
568 {
569 UIComponent childOrFacet = (UIComponent)it.next();
570
571
572
573
574 if (childOrFacet instanceof NamingContainer){
575 if (idsAreEqual(id, childOrFacet))
576 {
577 return childOrFacet;
578 }
579 }else{
580 UIComponent find = findComponentNormal(id, childOrFacet);
581 if (find != null) return find;
582 }
583 }
584
585 return null;
586 }
587
588
589
590
591
592
593
594 private boolean idsAreEqual(String id, UIComponent cmp)
595 {
596 if(id.equals(cmp.getId()))
597 return true;
598
599 if(cmp instanceof UIData)
600 {
601 UIData uiData = ((UIData) cmp);
602
603 if(uiData.getRowIndex()==-1)
604 {
605 return dynamicIdIsEqual(id,cmp.getId());
606 }
607 else
608 {
609 return id.equals(cmp.getId()+NamingContainer.SEPARATOR_CHAR+uiData.getRowIndex());
610 }
611 }
612
613 return false;
614 }
615
616 private boolean dynamicIdIsEqual(String dynamicId, String id)
617 {
618 return dynamicId.matches(id+":[0-9]*");
619 }
620
621
622 public Map getFacets()
623 {
624 if (_facetMap == null)
625 {
626 _facetMap = new _ComponentFacetMap(this);
627 }
628 return _facetMap;
629 }
630
631 public UIComponent getFacet(String name)
632 {
633 return _facetMap == null ? null : (UIComponent)_facetMap.get(name);
634 }
635
636 public Iterator getFacetsAndChildren()
637 {
638 if (_facetMap == null)
639 {
640 if (_childrenList == null)
641 return _EMPTY_UICOMPONENT_ITERATOR;
642
643 if (_childrenList.size() == 0)
644 return _EMPTY_UICOMPONENT_ITERATOR;
645
646 return _childrenList.iterator();
647 }
648 else
649 {
650 if (_facetMap.size() == 0)
651 {
652 if (_childrenList == null)
653 return _EMPTY_UICOMPONENT_ITERATOR;
654
655 if (_childrenList.size() == 0)
656 return _EMPTY_UICOMPONENT_ITERATOR;
657
658 return _childrenList.iterator();
659 }
660 else
661 {
662 if (_childrenList == null)
663 return _facetMap.values().iterator();
664
665 if (_childrenList.size() == 0)
666 return _facetMap.values().iterator();
667
668 return new _FacetsAndChildrenIterator(_facetMap, _childrenList);
669 }
670 }
671 }
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696 public void broadcast(FacesEvent event)
697 throws AbortProcessingException
698 {
699 if (event == null) throw new NullPointerException("event");
700 try {
701 if (_facesListeners == null) return;
702 for (Iterator it = _facesListeners.iterator(); it.hasNext(); )
703 {
704 FacesListener facesListener = (FacesListener)it.next();
705 if (event.isAppropriateListener(facesListener))
706 {
707 event.processListener(facesListener);
708 }
709 }
710 } catch (Exception e) {
711 if (e instanceof AbortProcessingException) {
712 throw (AbortProcessingException) e;
713 }
714 throw new FacesException("Exception while calling broadcast on : "+getPathToComponent(this), e);
715 }
716 }
717
718
719
720
721
722
723 public void decode(FacesContext context)
724 {
725 if (context == null) throw new NullPointerException("context");
726 try {
727 Renderer renderer = getRenderer(context);
728 if (renderer != null)
729 {
730 renderer.decode(context, this);
731 }
732 } catch (Exception e) {
733 if (e instanceof AbortProcessingException)
734 throw new FacesException("Exception while calling decode on : "+getPathToComponent(this), e);
735 }
736 }
737
738 public void encodeBegin(FacesContext context)
739 throws IOException
740 {
741 if (context == null) throw new NullPointerException("context");
742 try {
743 setCachedFacesContext(context);
744
745 if (!isRendered()) return;
746 Renderer renderer = getRenderer(context);
747 if (renderer != null)
748 {
749 renderer.encodeBegin(context, this);
750 }
751 } catch (Exception e) {
752 throw new FacesException("Exception while calling encodeBegin on : "+getPathToComponent(this), e);
753 }
754 finally
755 {
756 setCachedFacesContext(null);
757 }
758 }
759
760 public void encodeChildren(FacesContext context)
761 throws IOException
762 {
763 if (context == null) throw new NullPointerException("context");
764
765 boolean isCachedFacesContext = isCachedFacesContext();
766 try
767 {
768 if (!isCachedFacesContext)
769 {
770 setCachedFacesContext(context);
771 }
772 if (!isRendered()) return;
773 Renderer renderer = getRenderer(context);
774 if (renderer != null)
775 {
776 renderer.encodeChildren(context, this);
777 }
778 }
779 finally
780 {
781 if (!isCachedFacesContext)
782 {
783 setCachedFacesContext(null);
784 }
785 }
786 }
787
788 public void encodeEnd(FacesContext context)
789 throws IOException
790 {
791 if (context == null) throw new NullPointerException("context");
792 try {
793 setCachedFacesContext(context);
794 if (!isRendered()) return;
795
796 Renderer renderer = getRenderer(context);
797 if (renderer != null)
798 {
799 renderer.encodeEnd(context, this);
800 }
801 } catch (Exception e) {
802 throw new FacesException("Exception while calling encodeEnd on : "+getPathToComponent(this), e);
803 }
804 finally
805 {
806 setCachedFacesContext(null);
807 }
808 }
809
810 protected void addFacesListener(FacesListener listener)
811 {
812 if (listener == null) throw new NullPointerException("listener");
813 if (_facesListeners == null)
814 {
815 _facesListeners = new ArrayList();
816 }
817 _facesListeners.add(listener);
818 }
819
820 protected FacesListener[] getFacesListeners(Class clazz)
821 {
822 if (_facesListeners == null)
823 {
824 return (FacesListener[])Array.newInstance(clazz, 0);
825 }
826 List lst = null;
827 for (Iterator it = _facesListeners.iterator(); it.hasNext(); )
828 {
829 FacesListener facesListener = (FacesListener)it.next();
830 if (clazz.isAssignableFrom(facesListener.getClass()))
831 {
832 if (lst == null) lst = new ArrayList();
833 lst.add(facesListener);
834 }
835 }
836 if (lst == null)
837 {
838 return (FacesListener[])Array.newInstance(clazz, 0);
839 }
840 else
841 {
842 return (FacesListener[])lst.toArray((FacesListener[])Array.newInstance(clazz, lst.size()));
843 }
844 }
845
846 protected void removeFacesListener(FacesListener listener)
847 {
848 if (_facesListeners != null)
849 {
850 _facesListeners.remove(listener);
851 }
852 }
853
854 public void queueEvent(FacesEvent event)
855 {
856 if (event == null) throw new NullPointerException("event");
857 UIComponent parent = getParent();
858 if (parent == null)
859 {
860 throw new IllegalStateException("component is not a descendant of a UIViewRoot");
861 }
862 parent.queueEvent(event);
863 }
864
865 public void processDecodes(FacesContext context)
866 {
867 if (context == null)
868 throw new NullPointerException("context");
869
870 try
871 {
872 setCachedFacesContext(context);
873
874 if (!isRendered()) return;
875
876 for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
877 {
878 UIComponent childOrFacet = (UIComponent)it.next();
879 childOrFacet.processDecodes(context);
880 }
881 try
882 {
883 decode(context);
884 }
885 catch (RuntimeException e)
886 {
887 context.renderResponse();
888 throw e;
889 }
890 }
891 finally
892 {
893 setCachedFacesContext(null);
894 }
895 }
896
897
898 public void processValidators(FacesContext context)
899 {
900 if (context == null) throw new NullPointerException("context");
901
902 try
903 {
904 setCachedFacesContext(context);
905 if (!isRendered()) return;
906
907 for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
908 {
909 UIComponent childOrFacet = (UIComponent)it.next();
910 childOrFacet.processValidators(context);
911 }
912 }
913 finally
914 {
915 setCachedFacesContext(null);
916 }
917 }
918
919
920
921
922
923
924
925
926
927
928 public void processUpdates(FacesContext context)
929 {
930 if (context == null) throw new NullPointerException("context");
931
932 try
933 {
934 setCachedFacesContext(context);
935
936 if (!isRendered()) return;
937
938 for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
939 {
940 UIComponent childOrFacet = (UIComponent)it.next();
941 childOrFacet.processUpdates(context);
942 }
943 }
944 finally
945 {
946 setCachedFacesContext(null);
947 }
948 }
949
950 public Object processSaveState(FacesContext context)
951 {
952 if (context == null) throw new NullPointerException("context");
953 if (isTransient()) return null;
954 Map facetMap = null;
955 for (Iterator it = getFacets().entrySet().iterator(); it.hasNext(); )
956 {
957 Map.Entry entry = (Map.Entry)it.next();
958 if (facetMap == null) facetMap = new HashMap();
959 UIComponent component = (UIComponent)entry.getValue();
960 if (!component.isTransient())
961 {
962 facetMap.put(entry.getKey(), component.processSaveState(context));
963 }
964 }
965 List childrenList = null;
966 if (getChildCount() > 0)
967 {
968 for (Iterator it = getChildren().iterator(); it.hasNext(); )
969 {
970 UIComponent child = (UIComponent)it.next();
971 if (childrenList == null) {
972 childrenList = new ArrayList(getChildCount());
973 }
974 Object childState = child.processSaveState(context);
975 if (childState != null) {
976 childrenList.add(childState);
977 }
978 }
979 }
980 Object savedState;
981 try {
982 savedState = saveState(context);
983 } catch (Exception e) {
984 throw new FacesException("Exception while saving state of component : "+getPathToComponent(this), e);
985 }
986
987 return new Object[] {savedState,
988 facetMap,
989 childrenList};
990 }
991
992 public void processRestoreState(FacesContext context,
993 Object state)
994 {
995 if (context == null) throw new NullPointerException("context");
996 Object myState = ((Object[])state)[0];
997 Map facetMap = (Map)((Object[])state)[1];
998 List childrenList = (List)((Object[])state)[2];
999 if(facetMap != null)
1000 {
1001 for (Iterator it = getFacets().entrySet().iterator(); it.hasNext(); )
1002 {
1003 Map.Entry entry = (Map.Entry)it.next();
1004 Object facetState = facetMap.get(entry.getKey());
1005 if (facetState != null)
1006 {
1007 UIComponent component = (UIComponent)entry.getValue();
1008 component.processRestoreState(context, facetState);
1009 }
1010 else
1011 {
1012 context.getExternalContext().log("No state found to restore facet " + entry.getKey());
1013 }
1014 }
1015 }
1016 if (childrenList != null && getChildCount() > 0)
1017 {
1018 int idx = 0;
1019 for (Iterator it = getChildren().iterator(); it.hasNext(); )
1020 {
1021 UIComponent child = (UIComponent)it.next();
1022 if(!child.isTransient())
1023 {
1024 Object childState = childrenList.get(idx++);
1025 if (childState != null)
1026 {
1027 child.processRestoreState(context, childState);
1028 }
1029 else
1030 {
1031 context.getExternalContext().log("No state found to restore child of component " + getId());
1032 }
1033 }
1034 }
1035 }
1036 try {
1037 restoreState(context, myState);
1038 } catch (Exception e) {
1039 throw new FacesException("Exception while restoring state of component : "+getPathToComponent(this), e);
1040 }
1041 }
1042
1043 protected FacesContext getFacesContext()
1044 {
1045 if (_facesContext == null)
1046 {
1047 return FacesContext.getCurrentInstance();
1048 }
1049 else
1050 {
1051 return _facesContext;
1052 }
1053 }
1054
1055 protected Renderer getRenderer(FacesContext context)
1056 {
1057 if (context == null) throw new NullPointerException("context");
1058 String rendererType = getRendererType();
1059 if (rendererType == null) return null;
1060
1061 RenderKit renderKit = context.getRenderKit();
1062 Renderer renderer = renderKit.getRenderer(getFamily(), rendererType);
1063 if (renderer == null)
1064 {
1065 getFacesContext().getExternalContext().log("No Renderer found for component " + getPathToComponent(this) + " (component-family=" + getFamily() + ", renderer-type=" + rendererType + ")");
1066 log.warn("No Renderer found for component " + getPathToComponent(this) + " (component-family=" + getFamily() + ", renderer-type=" + rendererType + ")");
1067 }
1068 return renderer;
1069 }
1070
1071 private String getPathToComponent(UIComponent component)
1072 {
1073 StringBuffer buf = new StringBuffer();
1074
1075 if(component == null)
1076 {
1077 buf.append("{Component-Path : ");
1078 buf.append("[null]}");
1079 return buf.toString();
1080 }
1081
1082 getPathToComponent(component,buf);
1083
1084 buf.insert(0,"{Component-Path : ");
1085 buf.append("}");
1086
1087 return buf.toString();
1088 }
1089
1090 private static void getPathToComponent(UIComponent component, StringBuffer buf)
1091 {
1092 if(component == null)
1093 return;
1094
1095 StringBuffer intBuf = new StringBuffer();
1096
1097 intBuf.append("[Class: ");
1098 intBuf.append(component.getClass().getName());
1099 if(component instanceof UIViewRoot)
1100 {
1101 intBuf.append(",ViewId: ");
1102 intBuf.append(((UIViewRoot) component).getViewId());
1103 }
1104 else
1105 {
1106 intBuf.append(",Id: ");
1107 intBuf.append(component.getId());
1108 }
1109 intBuf.append("]");
1110
1111 buf.insert(0,intBuf.toString());
1112
1113 getPathToComponent(component.getParent(), buf);
1114 }
1115
1116
1117
1118
1119
1120
1121
1122 public boolean isTransient()
1123 {
1124 return _transient;
1125 }
1126
1127 public void setTransient(boolean transientFlag)
1128 {
1129 _transient = transientFlag;
1130 }
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157 public static Object saveAttachedState(FacesContext context,
1158 Object attachedObject)
1159 {
1160 if (attachedObject == null) return null;
1161 if (attachedObject instanceof List)
1162 {
1163 List lst = new ArrayList(((List)attachedObject).size());
1164 for (Iterator it = ((List)attachedObject).iterator(); it.hasNext(); )
1165 {
1166 Object value = it.next();
1167 if (value != null)
1168 {
1169 lst.add(saveAttachedState(context, value));
1170 }
1171 }
1172 return new _AttachedListStateWrapper(lst);
1173 }
1174 else if (attachedObject instanceof StateHolder)
1175 {
1176 if (((StateHolder)attachedObject).isTransient())
1177 {
1178 return null;
1179 }
1180 else
1181 {
1182 return new _AttachedStateWrapper(attachedObject.getClass(),
1183 ((StateHolder)attachedObject).saveState(context));
1184 }
1185 }
1186 else if (attachedObject instanceof Serializable)
1187 {
1188 return attachedObject;
1189 }
1190 else
1191 {
1192 return new _AttachedStateWrapper(attachedObject.getClass(), null);
1193 }
1194 }
1195
1196 public static Object restoreAttachedState(FacesContext context,
1197 Object stateObj)
1198 throws IllegalStateException
1199 {
1200 if (context == null) throw new NullPointerException("context");
1201 if (stateObj == null) return null;
1202 if (stateObj instanceof _AttachedListStateWrapper)
1203 {
1204 List lst = ((_AttachedListStateWrapper)stateObj).getWrappedStateList();
1205 List restoredList = new ArrayList(lst.size());
1206 for (Iterator it = lst.iterator(); it.hasNext(); )
1207 {
1208 restoredList.add(restoreAttachedState(context, it.next()));
1209 }
1210 return restoredList;
1211 }
1212 else if (stateObj instanceof _AttachedStateWrapper)
1213 {
1214 Class clazz = ((_AttachedStateWrapper)stateObj).getClazz();
1215 Object restoredObject;
1216 try
1217 {
1218 restoredObject = clazz.newInstance();
1219 }
1220 catch (InstantiationException e)
1221 {
1222 throw new RuntimeException("Could not restore StateHolder of type " + clazz.getName() + " (missing no-args constructor?)", e);
1223 }
1224 catch (IllegalAccessException e)
1225 {
1226 throw new RuntimeException(e);
1227 }
1228 if (restoredObject instanceof StateHolder)
1229 {
1230 Object wrappedState = ((_AttachedStateWrapper)stateObj).getWrappedStateObject();
1231 ((StateHolder)restoredObject).restoreState(context, wrappedState);
1232 }
1233 return restoredObject;
1234 }
1235 else
1236 {
1237 return stateObj;
1238 }
1239 }
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249 public Object saveState(FacesContext context)
1250 {
1251 Object values[] = new Object[7];
1252 values[0] = _id;
1253 values[1] = _rendered;
1254 values[2] = _rendererType;
1255 values[3] = _clientId;
1256 values[4] = saveAttributesMap();
1257 values[5] = saveAttachedState(context, _facesListeners);
1258 values[6] = saveValueBindingMap(context);
1259 return values;
1260 }
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 public void restoreState(FacesContext context, Object state)
1271 {
1272 Object values[] = (Object[])state;
1273 _id = (String)values[0];
1274 _rendered = (Boolean)values[1];
1275 _rendererType = (String)values[2];
1276 _clientId = (String)values[3];
1277 restoreAttributesMap(values[4]);
1278 _facesListeners = (List)restoreAttachedState(context, values[5]);
1279 restoreValueBindingMap(context, values[6]);
1280 }
1281
1282
1283 private Object saveAttributesMap()
1284 {
1285 if (_attributesMap != null)
1286 {
1287 return _attributesMap.getUnderlyingMap();
1288 }
1289 else
1290 {
1291 return null;
1292 }
1293 }
1294
1295 private void restoreAttributesMap(Object stateObj)
1296 {
1297 if (stateObj != null)
1298 {
1299 _attributesMap = new _ComponentAttributesMap(this, (Map)stateObj);
1300 }
1301 else
1302 {
1303 _attributesMap = null;
1304 }
1305 }
1306
1307 private Object saveValueBindingMap(FacesContext context)
1308 {
1309 if (_valueBindingMap != null)
1310 {
1311 int initCapacity = (_valueBindingMap.size() * 4 + 3) / 3;
1312 HashMap stateMap = new HashMap(initCapacity);
1313 for (Iterator it = _valueBindingMap.entrySet().iterator(); it.hasNext(); )
1314 {
1315 Map.Entry entry = (Map.Entry)it.next();
1316 stateMap.put(entry.getKey(),
1317 saveAttachedState(context, entry.getValue()));
1318 }
1319 return stateMap;
1320 }
1321 else
1322 {
1323 return null;
1324 }
1325 }
1326
1327 private void restoreValueBindingMap(FacesContext context, Object stateObj)
1328 {
1329 if (stateObj != null)
1330 {
1331 Map stateMap = (Map)stateObj;
1332 int initCapacity = (stateMap.size() * 4 + 3) / 3;
1333 _valueBindingMap = new HashMap(initCapacity);
1334 for (Iterator it = stateMap.entrySet().iterator(); it.hasNext(); )
1335 {
1336 Map.Entry entry = (Map.Entry)it.next();
1337 _valueBindingMap.put(entry.getKey(),
1338 restoreAttachedState(context, entry.getValue()));
1339 }
1340 }
1341 else
1342 {
1343 _valueBindingMap = null;
1344 }
1345 }
1346
1347
1348
1349
1350
1351 private void isIdValid(String string)
1352 {
1353
1354
1355 if(string == null)
1356 return;
1357
1358
1359
1360 if(string.length()==0)
1361 {
1362 throw new IllegalArgumentException("component identifier must not be a zero-length String");
1363 }
1364
1365
1366 char[] chars = string.toCharArray();
1367
1368
1369 if(!Character.isLetter(chars[0]) && chars[0] !='_')
1370 {
1371 throw new IllegalArgumentException("component identifier's first character must be a letter or an underscore ('_')! But it is \""+chars[0]+"\"");
1372 }
1373 for (int i = 1; i < chars.length; i++)
1374 {
1375
1376 if(!Character.isDigit(chars[i]) && !Character.isLetter(chars[i]) && chars[i] !='-' && chars[i] !='_')
1377 {
1378 throw new IllegalArgumentException("Subsequent characters of component identifier must be a letter, a digit, an underscore ('_'), or a dash ('-')! But component identifier contains \""+chars[i]+"\"");
1379 }
1380 }
1381 }
1382
1383 boolean isCachedFacesContext()
1384 {
1385 return _facesContext != null;
1386 }
1387
1388 void setCachedFacesContext(FacesContext facesContext)
1389 {
1390 _facesContext = facesContext;
1391 }
1392
1393
1394
1395 private static final boolean DEFAULT_RENDERED = true;
1396
1397 private Boolean _rendered = null;
1398 private String _rendererType = null;
1399
1400
1401
1402 public void setRendered(boolean rendered)
1403 {
1404 _rendered = Boolean.valueOf(rendered);
1405 }
1406
1407
1408
1409
1410
1411
1412
1413 public boolean isRendered()
1414 {
1415 if (_rendered != null) return _rendered.booleanValue();
1416 ValueBinding vb = getValueBinding("rendered");
1417 Boolean v = vb != null ? (Boolean)vb.getValue(getFacesContext()) : null;
1418 return v != null ? v.booleanValue() : DEFAULT_RENDERED;
1419 }
1420
1421 public void setRendererType(String rendererType)
1422 {
1423 _rendererType = rendererType;
1424 }
1425
1426 public String getRendererType()
1427 {
1428 if (_rendererType != null) return _rendererType;
1429 ValueBinding vb = getValueBinding("rendererType");
1430 return vb != null ? _ComponentUtils.getStringValue(getFacesContext(), vb) : null;
1431 }
1432
1433
1434
1435 }