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.beans.BeanDescriptor; |
22 | |
import java.beans.IntrospectionException; |
23 | |
import java.beans.PropertyDescriptor; |
24 | |
import java.io.IOException; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
import java.util.logging.Level; |
28 | |
import java.util.logging.Logger; |
29 | |
|
30 | |
import javax.faces.application.ProjectStage; |
31 | |
import javax.faces.component.UIComponent; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
import javax.faces.view.facelets.FaceletContext; |
34 | |
import javax.faces.view.facelets.TagAttribute; |
35 | |
import javax.faces.view.facelets.TagConfig; |
36 | |
import javax.faces.view.facelets.TagException; |
37 | |
import javax.faces.view.facelets.TagHandler; |
38 | |
|
39 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute; |
40 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
@JSFFaceletTag(name="composite:facet") |
54 | |
public class FacetHandler extends TagHandler implements InterfaceDescriptorCreator |
55 | |
{ |
56 | |
|
57 | |
|
58 | 0 | private static final Logger log = Logger.getLogger(FacetHandler.class.getName()); |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | private static final String[] STANDARD_ATTRIBUTES_SORTED = new String[] |
65 | |
{ |
66 | |
"displayName", |
67 | |
"expert", |
68 | |
"hidden", |
69 | |
"name", |
70 | |
"preferred", |
71 | |
"required", |
72 | |
"shortDescription" |
73 | |
}; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
@JSFFaceletAttribute(name="name", |
79 | |
className="javax.el.ValueExpression", |
80 | |
deferredValueType="java.lang.String") |
81 | |
private final TagAttribute _name; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
@JSFFaceletAttribute(name="displayName", |
87 | |
className="javax.el.ValueExpression", |
88 | |
deferredValueType="java.lang.String") |
89 | |
private final TagAttribute _displayName; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
@JSFFaceletAttribute(name="required", |
99 | |
className="javax.el.ValueExpression", |
100 | |
deferredValueType="boolean") |
101 | |
private final TagAttribute _required; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@JSFFaceletAttribute(name="preferred", |
107 | |
className="javax.el.ValueExpression", |
108 | |
deferredValueType="boolean") |
109 | |
private final TagAttribute _preferred; |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
@JSFFaceletAttribute(name="expert", |
115 | |
className="javax.el.ValueExpression", |
116 | |
deferredValueType="boolean") |
117 | |
private final TagAttribute _expert; |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
@JSFFaceletAttribute(name="shortDescription", |
123 | |
className="javax.el.ValueExpression", |
124 | |
deferredValueType="java.lang.String") |
125 | |
private final TagAttribute _shortDescription; |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
@JSFFaceletAttribute(name="hidden", |
133 | |
className="javax.el.ValueExpression", |
134 | |
deferredValueType="boolean") |
135 | |
protected final TagAttribute _hidden; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
private boolean _cacheable; |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private PropertyDescriptor _propertyDescriptor; |
151 | |
|
152 | |
public FacetHandler(TagConfig config) |
153 | |
{ |
154 | 0 | super(config); |
155 | 0 | _name = getRequiredAttribute("name"); |
156 | 0 | _displayName = getAttribute("displayName"); |
157 | 0 | _required = getAttribute("required"); |
158 | 0 | _preferred = getAttribute("preferred"); |
159 | 0 | _expert = getAttribute("expert"); |
160 | 0 | _shortDescription = getAttribute("shortDescription"); |
161 | 0 | _hidden = getAttribute("hidden"); |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | 0 | final boolean development = FacesContext.getCurrentInstance() |
169 | |
.isProjectStage(ProjectStage.Development); |
170 | |
|
171 | 0 | if (_name.isLiteral() |
172 | |
&& (!development || _areDevelopmentAttributesLiteral())) |
173 | |
{ |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | 0 | _cacheable = true; |
181 | 0 | if (_required == null && |
182 | |
!CompositeTagAttributeUtils.containsUnspecifiedAttributes(tag, |
183 | |
STANDARD_ATTRIBUTES_SORTED)) |
184 | |
{ |
185 | 0 | _propertyDescriptor = _createFacetPropertyDescriptor(development); |
186 | |
} |
187 | |
} |
188 | |
else |
189 | |
{ |
190 | 0 | _cacheable = false; |
191 | |
} |
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
private boolean _areDevelopmentAttributesLiteral() |
200 | |
{ |
201 | 0 | return CompositeTagAttributeUtils.areAttributesLiteral( |
202 | |
_displayName, _shortDescription, _expert, _hidden, _preferred); |
203 | |
} |
204 | |
|
205 | |
@SuppressWarnings("unchecked") |
206 | |
public void apply(FaceletContext ctx, UIComponent parent) |
207 | |
throws IOException |
208 | |
{ |
209 | 0 | CompositeComponentBeanInfo beanInfo = |
210 | |
(CompositeComponentBeanInfo) parent.getAttributes() |
211 | |
.get(UIComponent.BEANINFO_KEY); |
212 | |
|
213 | 0 | if (beanInfo == null) |
214 | |
{ |
215 | 0 | if (log.isLoggable(Level.SEVERE)) |
216 | |
{ |
217 | 0 | log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY "); |
218 | |
} |
219 | 0 | return; |
220 | |
} |
221 | |
|
222 | 0 | BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); |
223 | |
|
224 | 0 | Map<String, PropertyDescriptor> facetPropertyDescriptorMap = (Map<String, PropertyDescriptor>) |
225 | |
beanDescriptor.getValue(UIComponent.FACETS_KEY); |
226 | |
|
227 | 0 | if (facetPropertyDescriptorMap == null) |
228 | |
{ |
229 | 0 | facetPropertyDescriptorMap = new HashMap<String, PropertyDescriptor>(); |
230 | 0 | beanDescriptor.setValue(UIComponent.FACETS_KEY, facetPropertyDescriptorMap); |
231 | |
} |
232 | |
|
233 | 0 | String facetName = _name.getValue(ctx); |
234 | |
|
235 | 0 | if (isCacheable()) |
236 | |
{ |
237 | 0 | if (_propertyDescriptor == null) |
238 | |
{ |
239 | 0 | _propertyDescriptor = _createFacetPropertyDescriptor(facetName, ctx); |
240 | |
} |
241 | 0 | facetPropertyDescriptorMap.put(facetName, _propertyDescriptor); |
242 | |
} |
243 | |
else |
244 | |
{ |
245 | 0 | PropertyDescriptor facetDescriptor = _createFacetPropertyDescriptor(facetName, ctx); |
246 | 0 | facetPropertyDescriptorMap.put(facetName, facetDescriptor); |
247 | |
} |
248 | |
|
249 | 0 | nextHandler.apply(ctx, parent); |
250 | 0 | } |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
private PropertyDescriptor _createFacetPropertyDescriptor(boolean development) |
259 | |
{ |
260 | |
try |
261 | |
{ |
262 | 0 | CompositeComponentPropertyDescriptor facetPropertyDescriptor = |
263 | |
new CompositeComponentPropertyDescriptor(_name.getValue()); |
264 | |
|
265 | |
|
266 | |
|
267 | 0 | if (development) |
268 | |
{ |
269 | 0 | CompositeTagAttributeUtils.addDevelopmentAttributesLiteral(facetPropertyDescriptor, |
270 | |
_displayName, _shortDescription, _expert, _hidden, _preferred); |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | 0 | return facetPropertyDescriptor; |
277 | |
} |
278 | 0 | catch (IntrospectionException e) |
279 | |
{ |
280 | 0 | if (log.isLoggable(Level.SEVERE)) |
281 | |
{ |
282 | 0 | log.log(Level.SEVERE, "Cannot create PropertyDescriptor for facet ",e); |
283 | |
} |
284 | 0 | throw new TagException(tag,e); |
285 | |
} |
286 | |
} |
287 | |
|
288 | |
private PropertyDescriptor _createFacetPropertyDescriptor(String facetName, FaceletContext ctx) |
289 | |
throws TagException, IOException |
290 | |
{ |
291 | |
try |
292 | |
{ |
293 | 0 | CompositeComponentPropertyDescriptor facetPropertyDescriptor = |
294 | |
new CompositeComponentPropertyDescriptor(facetName); |
295 | |
|
296 | 0 | if (_required != null) |
297 | |
{ |
298 | 0 | facetPropertyDescriptor.setValue("required", _required.getValueExpression(ctx, Boolean.class)); |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | 0 | if (ctx.getFacesContext().isProjectStage(ProjectStage.Development)) |
304 | |
{ |
305 | 0 | CompositeTagAttributeUtils.addDevelopmentAttributes(facetPropertyDescriptor, ctx, |
306 | |
_displayName, _shortDescription, _expert, _hidden, _preferred); |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | 0 | CompositeTagAttributeUtils.addUnspecifiedAttributes(facetPropertyDescriptor, tag, |
312 | |
STANDARD_ATTRIBUTES_SORTED, ctx); |
313 | |
|
314 | 0 | return facetPropertyDescriptor; |
315 | |
} |
316 | 0 | catch (IntrospectionException e) |
317 | |
{ |
318 | 0 | if (log.isLoggable(Level.SEVERE)) |
319 | |
{ |
320 | 0 | log.log(Level.SEVERE, "Cannot create PropertyDescriptor for attribute ",e); |
321 | |
} |
322 | 0 | throw new TagException(tag,e); |
323 | |
} |
324 | |
} |
325 | |
|
326 | |
public boolean isCacheable() |
327 | |
{ |
328 | 0 | return _cacheable; |
329 | |
} |
330 | |
|
331 | |
public void setCacheable(boolean cacheable) |
332 | |
{ |
333 | 0 | _cacheable = cacheable; |
334 | 0 | } |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
} |