1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.compiler;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import java.util.Set;
30 import javax.el.ValueExpression;
31 import javax.faces.FacesException;
32 import javax.faces.component.ContextCallback;
33 import javax.faces.component.UIComponent;
34 import javax.faces.component.UIViewRoot;
35 import javax.faces.component.UniqueIdVendor;
36 import javax.faces.component.visit.VisitCallback;
37 import javax.faces.component.visit.VisitContext;
38 import javax.faces.context.FacesContext;
39 import javax.faces.el.ValueBinding;
40 import javax.faces.event.AbortProcessingException;
41 import javax.faces.event.ComponentSystemEvent;
42 import javax.faces.event.FacesEvent;
43 import javax.faces.event.FacesListener;
44 import javax.faces.render.Renderer;
45
46 import javax.faces.view.Location;
47 import org.apache.commons.collections.iterators.EmptyIterator;
48 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
49
50 class UILeaf extends UIComponent implements Map<String, Object>
51 {
52
53 private static final String _STRING_BUILDER_KEY
54 = "javax.faces.component.UIComponentBase.SHARED_STRING_BUILDER";
55
56 private String _clientId = null;
57
58 private String _id = null;
59
60 public String getClientId(FacesContext context)
61 {
62 if (context == null)
63 {
64 throw new NullPointerException("context");
65 }
66
67 if (_clientId != null)
68 {
69 return _clientId;
70 }
71
72
73 String id = getId();
74 if (id == null)
75 {
76
77
78
79
80
81
82
83
84 UniqueIdVendor parentUniqueIdVendor = _ComponentUtils.findParentUniqueIdVendor(this);
85 if (parentUniqueIdVendor == null)
86 {
87 UIViewRoot viewRoot = context.getViewRoot();
88 if (viewRoot != null)
89 {
90 id = viewRoot.createUniqueId();
91 }
92 else
93 {
94
95 String location = getComponentLocation(this);
96 throw new FacesException("Cannot create clientId. No id is assigned for component"
97 + " to create an id and UIViewRoot is not defined: "
98 + getPathToComponent(this)
99 + (location != null ? " created from: " + location : ""));
100 }
101 }
102 else
103 {
104 id = parentUniqueIdVendor.createUniqueId(context, null);
105 }
106 setId(id);
107
108
109 }
110
111 UIComponent namingContainer = _ComponentUtils.findParentNamingContainer(this, false);
112 if (namingContainer != null)
113 {
114 String containerClientId = namingContainer.getContainerClientId(context);
115 if (containerClientId != null)
116 {
117 StringBuilder bld = _getSharedStringBuilder(context);
118 _clientId = bld.append(containerClientId).append(
119 context.getNamingContainerSeparatorChar()).append(id).toString();
120 }
121 else
122 {
123 _clientId = id;
124 }
125 }
126 else
127 {
128 _clientId = id;
129 }
130
131 Renderer renderer = getRenderer(context);
132 if (renderer != null)
133 {
134 _clientId = renderer.convertClientId(context, _clientId);
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 return _clientId;
151 }
152
153 public String getId()
154 {
155 return _id;
156 }
157
158 @Override
159 public void setId(String id)
160 {
161
162
163
164
165 _id = id;
166 _clientId = null;
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
209
210
211
212 private String getComponentLocation(UIComponent component)
213 {
214 Location location = (Location) component.getAttributes()
215 .get(UIComponent.VIEW_LOCATION_KEY);
216 if (location != null)
217 {
218 return location.toString();
219 }
220 return null;
221 }
222
223 private String getPathToComponent(UIComponent component)
224 {
225 StringBuffer buf = new StringBuffer();
226
227 if (component == null)
228 {
229 buf.append("{Component-Path : ");
230 buf.append("[null]}");
231 return buf.toString();
232 }
233
234 getPathToComponent(component, buf);
235
236 buf.insert(0, "{Component-Path : ");
237 buf.append("}");
238
239 return buf.toString();
240 }
241
242 private void getPathToComponent(UIComponent component, StringBuffer buf)
243 {
244 if (component == null)
245 {
246 return;
247 }
248
249 StringBuffer intBuf = new StringBuffer();
250
251 intBuf.append("[Class: ");
252 intBuf.append(component.getClass().getName());
253 if (component instanceof UIViewRoot)
254 {
255 intBuf.append(",ViewId: ");
256 intBuf.append(((UIViewRoot) component).getViewId());
257 }
258 else
259 {
260 intBuf.append(",Id: ");
261 intBuf.append(component.getId());
262 }
263 intBuf.append("]");
264
265 buf.insert(0, intBuf.toString());
266
267 getPathToComponent(component.getParent(), buf);
268 }
269
270 static StringBuilder _getSharedStringBuilder(FacesContext facesContext)
271 {
272 Map<Object, Object> attributes = facesContext.getAttributes();
273
274 StringBuilder sb = (StringBuilder) attributes.get(_STRING_BUILDER_KEY);
275
276 if (sb == null)
277 {
278 sb = new StringBuilder();
279 attributes.put(_STRING_BUILDER_KEY, sb);
280 }
281 else
282 {
283
284
285 sb.setLength(0);
286 }
287
288 return sb;
289 }
290
291
292
293
294 private static Map<String, UIComponent> facets = new HashMap<String, UIComponent>()
295 {
296
297 @Override
298 public void putAll(Map<? extends String, ? extends UIComponent> map)
299 {
300
301 }
302
303 @Override
304 public UIComponent put(String name, UIComponent value)
305 {
306 return null;
307 }
308 };
309
310 private UIComponent parent;
311
312
313
314 @Override
315 public Map<String, Object> getAttributes()
316 {
317
318
319
320
321
322
323
324 return this;
325 }
326
327 @Override
328 public void clearInitialState()
329 {
330
331 }
332
333 @Override
334 public boolean initialStateMarked()
335 {
336
337 return false;
338 }
339
340 @Override
341 public void markInitialState()
342 {
343
344 }
345
346
347 @Override
348 public void processEvent(ComponentSystemEvent event)
349 throws AbortProcessingException
350 {
351
352 }
353
354 @Override
355 @SuppressWarnings("deprecation")
356 public ValueBinding getValueBinding(String binding)
357 {
358 return null;
359 }
360
361 @Override
362 @SuppressWarnings("deprecation")
363 public void setValueBinding(String name, ValueBinding binding)
364 {
365
366 }
367
368 @Override
369 public ValueExpression getValueExpression(String name)
370 {
371 return null;
372 }
373
374 @Override
375 public void setValueExpression(String name, ValueExpression arg1)
376 {
377
378 }
379
380 public String getFamily()
381 {
382 return "facelets.LiteralText";
383 }
384
385 @Override
386 public UIComponent getParent()
387 {
388 return this.parent;
389 }
390
391 @Override
392 public void setParent(UIComponent parent)
393 {
394 this.parent = parent;
395 }
396
397 @Override
398 public boolean isRendered()
399 {
400 return true;
401 }
402
403 @Override
404 public void setRendered(boolean rendered)
405 {
406
407 }
408
409 @Override
410 public String getRendererType()
411 {
412 return null;
413 }
414
415 @Override
416 public void setRendererType(String rendererType)
417 {
418
419 }
420
421 @Override
422 public boolean getRendersChildren()
423 {
424 return true;
425 }
426
427 @Override
428 public List<UIComponent> getChildren()
429 {
430 List<UIComponent> children = Collections.emptyList();
431 return children;
432 }
433
434 @Override
435 public int getChildCount()
436 {
437 return 0;
438 }
439
440 @Override
441 public UIComponent findComponent(String id)
442 {
443 return null;
444 }
445
446 @Override
447 public Map<String, UIComponent> getFacets()
448 {
449 return facets;
450 }
451
452 @Override
453 public int getFacetCount()
454 {
455 return 0;
456 }
457
458 @Override
459 public UIComponent getFacet(String name)
460 {
461 return null;
462 }
463
464 @SuppressWarnings("unchecked")
465 @Override
466 public Iterator<UIComponent> getFacetsAndChildren()
467 {
468
469
470
471 return EmptyIterator.INSTANCE;
472 }
473
474 @Override
475 public void broadcast(FacesEvent event) throws AbortProcessingException
476 {
477
478 }
479
480 @Override
481 public void decode(FacesContext faces)
482 {
483
484 }
485
486 @Override
487 public void encodeBegin(FacesContext faces) throws IOException
488 {
489
490 }
491
492 @Override
493 public void encodeChildren(FacesContext faces) throws IOException
494 {
495
496 }
497
498 @Override
499 public void encodeEnd(FacesContext faces) throws IOException
500 {
501
502 }
503
504 @Override
505 public void encodeAll(FacesContext faces) throws IOException
506 {
507 this.encodeBegin(faces);
508 }
509
510 @Override
511 protected void addFacesListener(FacesListener faces)
512 {
513
514 }
515
516 @Override
517 protected FacesListener[] getFacesListeners(Class faces)
518 {
519 return null;
520 }
521
522 @Override
523 protected void removeFacesListener(FacesListener faces)
524 {
525
526 }
527
528 @Override
529 public void queueEvent(FacesEvent event)
530 {
531
532 }
533
534 @Override
535 public void processRestoreState(FacesContext faces, Object state)
536 {
537
538 }
539
540 @Override
541 public void processDecodes(FacesContext faces)
542 {
543
544 }
545
546 @Override
547 public void processValidators(FacesContext faces)
548 {
549
550 }
551
552 @Override
553 public void processUpdates(FacesContext faces)
554 {
555
556 }
557
558 @Override
559 public Object processSaveState(FacesContext faces)
560 {
561 return null;
562 }
563
564 @Override
565 protected FacesContext getFacesContext()
566 {
567 return FacesContext.getCurrentInstance();
568 }
569
570 @Override
571 protected Renderer getRenderer(FacesContext faces)
572 {
573 return null;
574 }
575
576 public Object saveState(FacesContext faces)
577 {
578 return null;
579 }
580
581 public void restoreState(FacesContext faces, Object state)
582 {
583
584 }
585
586 public boolean isTransient()
587 {
588 return true;
589 }
590
591 public void setTransient(boolean tranzient)
592 {
593
594 }
595
596 @Override
597 public boolean invokeOnComponent(FacesContext context, String clientId, ContextCallback callback)
598 throws FacesException
599 {
600
601 return false;
602 }
603
604 @Override
605 public boolean visitTree(VisitContext context, VisitCallback callback)
606 {
607
608
609 return false;
610 }
611
612
613
614 private Map<String, Object> _attributes = null;
615 private String _markCreated = null;
616
617 public void setMarkCreated(String markCreated)
618 {
619 _markCreated = markCreated;
620 }
621
622 public int size()
623 {
624 return _attributes == null ? 0 : _attributes.size();
625 }
626
627 public void clear()
628 {
629 if (_attributes != null)
630 {
631 _attributes.clear();
632 _markCreated = null;
633 }
634 }
635
636 public boolean isEmpty()
637 {
638 if (_markCreated == null)
639 {
640 return _attributes == null ? false : _attributes.isEmpty();
641 }
642 else
643 {
644 return false;
645 }
646 }
647
648 public boolean containsKey(Object key)
649 {
650 checkKey(key);
651
652 if (ComponentSupport.MARK_CREATED.equals(key))
653 {
654 return _markCreated != null;
655 }
656 else
657 {
658 return (_attributes == null ? false :_attributes.containsKey(key));
659 }
660 }
661
662 public boolean containsValue(Object value)
663 {
664 if (_markCreated != null && _markCreated.equals(value))
665 {
666 return true;
667 }
668 return (_attributes == null) ? false : _attributes.containsValue(value);
669 }
670
671 public Collection<Object> values()
672 {
673 return getUnderlyingMap().values();
674 }
675
676 public void putAll(Map<? extends String, ? extends Object> t)
677 {
678 for (Map.Entry<? extends String, ? extends Object> entry : t.entrySet())
679 {
680 put(entry.getKey(), entry.getValue());
681 }
682 }
683
684 public Set<Entry<String, Object>> entrySet()
685 {
686 return getUnderlyingMap().entrySet();
687 }
688
689 public Set<String> keySet()
690 {
691 return getUnderlyingMap().keySet();
692 }
693
694 public Object get(Object key)
695 {
696 checkKey(key);
697
698 if ("rendered".equals(key))
699 {
700 return true;
701 }
702 if ("transient".equals(key))
703 {
704 return true;
705 }
706 if (ComponentSupport.MARK_CREATED.equals(key))
707 {
708 return _markCreated;
709 }
710 return (_attributes == null) ? null : _attributes.get(key);
711 }
712
713 public Object remove(Object key)
714 {
715 checkKey(key);
716
717 if (ComponentSupport.MARK_CREATED.equals(key))
718 {
719 _markCreated = null;
720 }
721 return (_attributes == null) ? null : _attributes.remove(key);
722 }
723
724 public Object put(String key, Object value)
725 {
726 checkKey(key);
727
728 if (ComponentSupport.MARK_CREATED.equals(key))
729 {
730 String old = _markCreated;
731 _markCreated = (String) value;
732 return old;
733 }
734 return getUnderlyingMap().put(key, value);
735 }
736
737 private void checkKey(Object key)
738 {
739 if (key == null)
740 {
741 throw new NullPointerException("key");
742 }
743 if (!(key instanceof String))
744 {
745 throw new ClassCastException("key is not a String");
746 }
747 }
748
749 Map<String, Object> getUnderlyingMap()
750 {
751 if (_attributes == null)
752 {
753 _attributes = new HashMap<String, Object>(2,1);
754 }
755 return _attributes;
756 }
757
758 @Override
759 public Map<String, Object> getPassThroughAttributes(boolean create)
760 {
761 if (create)
762 {
763
764
765 return Collections.emptyMap();
766 }
767 else
768 {
769 return null;
770 }
771 }
772
773 }