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.jsf;
20
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 import javax.el.ELException;
26 import javax.el.ValueExpression;
27 import javax.faces.FacesException;
28 import javax.faces.application.Application;
29 import javax.faces.component.ActionSource;
30 import javax.faces.component.EditableValueHolder;
31 import javax.faces.component.UIComponent;
32 import javax.faces.component.UIViewRoot;
33 import javax.faces.component.ValueHolder;
34 import javax.faces.context.FacesContext;
35 import javax.faces.view.facelets.ComponentConfig;
36 import javax.faces.view.facelets.FaceletContext;
37 import javax.faces.view.facelets.MetaRuleset;
38 import javax.faces.view.facelets.MetaTagHandler;
39 import javax.faces.view.facelets.TagAttribute;
40 import javax.faces.view.facelets.TagException;
41
42 import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl;
43
44
45
46
47
48
49
50
51
52 @Deprecated
53 public class ComponentHandler extends MetaTagHandler
54 {
55
56
57 private final static Logger log = Logger.getLogger(ComponentHandler.class.getName());
58
59 private final TagAttribute binding;
60
61 private final String componentType;
62
63 private final TagAttribute id;
64
65 private final String rendererType;
66
67 public ComponentHandler(ComponentConfig config)
68 {
69 super(config);
70 this.componentType = config.getComponentType();
71 this.rendererType = config.getRendererType();
72 this.id = this.getAttribute("id");
73 this.binding = this.getAttribute("binding");
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public final void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException
101 {
102
103 if (parent == null)
104 {
105 throw new TagException(this.tag, "Parent UIComponent was null");
106 }
107
108
109 String facetName = this.getFacetName(ctx, parent);
110
111
112 String id = ctx.generateUniqueId(this.tagId);
113
114
115 UIComponent c = ComponentSupport.findChildByTagId(parent, id);
116 boolean componentFound = false;
117 if (c != null)
118 {
119 componentFound = true;
120
121 if (log.isLoggable(Level.FINE))
122 {
123 log.fine(this.tag + " Component[" + id + "] Found, marking children for cleanup");
124 }
125 ComponentSupport.markForDeletion(c);
126 }
127 else
128 {
129 c = this.createComponent(ctx);
130 if (log.isLoggable(Level.FINE))
131 {
132 log.fine(this.tag + " Component[" + id + "] Created: " + c.getClass().getName());
133 }
134 this.setAttributes(ctx, c);
135
136
137 c.getAttributes().put(ComponentSupport.MARK_CREATED, id);
138
139
140 if (this.id != null)
141 {
142 c.setId(this.id.getValue(ctx));
143 }
144 else
145 {
146 UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
147 if (root != null)
148 {
149 String uid = root.createUniqueId();
150 c.setId(uid);
151 }
152 }
153
154 if (this.rendererType != null)
155 {
156 c.setRendererType(this.rendererType);
157 }
158
159
160 this.onComponentCreated(ctx, c, parent);
161 }
162
163
164 this.applyNextHandler(ctx, c);
165
166
167 if (componentFound)
168 {
169 ComponentSupport.finalizeForDeletion(c);
170
171 if (facetName == null)
172 {
173 parent.getChildren().remove(c);
174 }
175 }
176
177 this.onComponentPopulated(ctx, c, parent);
178
179
180
181
182 if (facetName == null)
183 {
184 parent.getChildren().add(c);
185 }
186 else
187 {
188 parent.getFacets().put(facetName, c);
189 }
190 }
191
192
193
194
195
196
197
198 protected final String getFacetName(FaceletContext ctx, UIComponent parent)
199 {
200
201
202 return (String) parent.getAttributes().get("facelets.FACET_NAME");
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216 protected UIComponent createComponent(FaceletContext ctx)
217 {
218 UIComponent c = null;
219 FacesContext faces = ctx.getFacesContext();
220 Application app = faces.getApplication();
221 if (this.binding != null)
222 {
223 ValueExpression ve = this.binding.getValueExpression(ctx, Object.class);
224
225 c = app.createComponent(ve, faces, this.componentType);
226 if (c != null)
227 {
228 c.setValueExpression("binding", ve);
229 }
230 }
231 else
232 {
233 c = app.createComponent(this.componentType);
234 }
235 return c;
236 }
237
238
239
240
241
242
243
244
245
246 protected String getId(FaceletContext ctx)
247 {
248 if (this.id != null)
249 {
250 return this.id.getValue(ctx);
251 }
252 return ctx.generateUniqueId(this.tagId);
253 }
254
255 @Override
256 protected MetaRuleset createMetaRuleset(Class type)
257 {
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 MetaRuleset m = new MetaRulesetImpl(this.tag, type);
291
292 m.ignore("binding").ignore("id");
293
294
295 m.addRule(ComponentRule.INSTANCE);
296
297
298 if (ActionSource.class.isAssignableFrom(type))
299 {
300 m.addRule(ActionSourceRule.INSTANCE);
301 }
302
303
304 if (ValueHolder.class.isAssignableFrom(type))
305 {
306 m.addRule(ValueHolderRule.INSTANCE);
307
308
309 if (EditableValueHolder.class.isAssignableFrom(type))
310 {
311 m.ignore("submittedValue");
312 m.ignore("valid");
313 m.addRule(EditableValueHolderRule.INSTANCE);
314 }
315 }
316
317 return m;
318 }
319
320
321
322
323
324
325
326
327
328
329 protected void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent)
330 {
331
332 }
333
334 protected void onComponentPopulated(FaceletContext ctx, UIComponent c, UIComponent parent)
335 {
336
337 }
338
339 protected void applyNextHandler(FaceletContext ctx, UIComponent c) throws IOException, FacesException, ELException
340 {
341
342 this.nextHandler.apply(ctx, c);
343 }
344 }