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