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.tag.composite;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import javax.el.ValueExpression;
27 import javax.faces.component.UIComponent;
28 import javax.faces.context.FacesContext;
29 import javax.faces.view.AttachedObjectTarget;
30
31 import org.apache.myfaces.shared.util.StringUtils;
32 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
33
34
35
36
37
38
39 public class AttachedObjectTargetImpl implements AttachedObjectTarget, Serializable
40 {
41
42
43
44 private static final long serialVersionUID = -7214478234269252354L;
45
46 protected ValueExpression _name;
47
48 protected ValueExpression _targets;
49
50 public AttachedObjectTargetImpl()
51 {
52 }
53
54 public String getName()
55 {
56 if (_name != null)
57 {
58 return (String) _name.getValue(FacesContext.getCurrentInstance().getELContext());
59 }
60 return null;
61 }
62
63 public List<UIComponent> getTargets(UIComponent topLevelComponent)
64 {
65 FacesContext facesContext = FacesContext.getCurrentInstance();
66 String [] targetsArray = getTargets(facesContext);
67
68 if (targetsArray.length > 0)
69 {
70 List<UIComponent> targetsList = new ArrayList<UIComponent>(targetsArray.length);
71 final char separatorChar = facesContext.getNamingContainerSeparatorChar();
72 UIComponent facetBase = topLevelComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME);
73 for (String target : targetsArray)
74 {
75
76
77 int separator = target.indexOf(separatorChar);
78 UIComponent innerComponent = null;
79 if (separator == -1)
80 {
81 innerComponent = ComponentSupport.findComponentChildOrFacetFrom(
82 facetBase, target, null);
83 }
84 else
85 {
86 innerComponent = ComponentSupport.findComponentChildOrFacetFrom(
87 facetBase, target.substring(0,separator), target);
88 }
89
90 if (innerComponent != null)
91 {
92 targetsList.add(innerComponent);
93 }
94 }
95 return targetsList;
96 }
97 else
98 {
99
100
101
102
103 String name = getName();
104 if (name != null)
105 {
106
107
108 UIComponent innerComponent = ComponentSupport.findComponentChildOrFacetFrom(
109 topLevelComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME),
110 name, null);
111 if (innerComponent != null)
112 {
113 List<UIComponent> targetsList = new ArrayList<UIComponent>(1);
114 targetsList.add(innerComponent);
115 return targetsList;
116 }
117 }
118 return Collections.emptyList();
119 }
120 }
121
122 public String [] getTargets(FacesContext context)
123 {
124 if (_targets != null)
125 {
126 return StringUtils.splitShortString((String) _targets.getValue(context.getELContext()), ' ');
127 }
128 return org.apache.myfaces.shared.util.ArrayUtils.EMPTY_STRING_ARRAY;
129 }
130
131 public void setName(ValueExpression ve)
132 {
133 _name = ve;
134 }
135
136 public void setTargets(ValueExpression ve)
137 {
138 _targets = ve;
139 }
140 }