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