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 jakarta.el.ELException;
26 import jakarta.el.ValueExpression;
27 import jakarta.faces.FacesException;
28 import jakarta.faces.application.Application;
29 import jakarta.faces.component.ActionSource;
30 import jakarta.faces.component.EditableValueHolder;
31 import jakarta.faces.component.UIComponent;
32 import jakarta.faces.component.UIViewRoot;
33 import jakarta.faces.component.ValueHolder;
34 import jakarta.faces.context.FacesContext;
35 import jakarta.faces.view.facelets.ComponentConfig;
36 import jakarta.faces.view.facelets.FaceletContext;
37 import jakarta.faces.view.facelets.MetaRuleset;
38 import jakarta.faces.view.facelets.MetaTagHandler;
39 import jakarta.faces.view.facelets.TagAttribute;
40 import jakarta.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
101 public final void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException
102 {
103
104 if (parent == null)
105 {
106 throw new TagException(this.tag, "Parent UIComponent was null");
107 }
108
109
110 String facetName = this.getFacetName(ctx, parent);
111
112
113 String id = ctx.generateUniqueId(this.tagId);
114
115
116 UIComponent c = ComponentSupport.findChildByTagId(parent, id);
117 boolean componentFound = false;
118 if (c != null)
119 {
120 componentFound = true;
121
122 if (log.isLoggable(Level.FINE))
123 {
124 log.fine(this.tag + " Component[" + id + "] Found, marking children for cleanup");
125 }
126 ComponentSupport.markForDeletion(c);
127 }
128 else
129 {
130 c = this.createComponent(ctx);
131 if (log.isLoggable(Level.FINE))
132 {
133 log.fine(this.tag + " Component[" + id + "] Created: " + c.getClass().getName());
134 }
135 this.setAttributes(ctx, c);
136
137
138 c.getAttributes().put(ComponentSupport.MARK_CREATED, id);
139
140
141 if (this.id != null)
142 {
143 c.setId(this.id.getValue(ctx));
144 }
145 else
146 {
147 UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
148 if (root != null)
149 {
150 String uid = root.createUniqueId();
151 c.setId(uid);
152 }
153 }
154
155 if (this.rendererType != null)
156 {
157 c.setRendererType(this.rendererType);
158 }
159
160
161 this.onComponentCreated(ctx, c, parent);
162 }
163
164
165 this.applyNextHandler(ctx, c);
166
167
168 if (componentFound)
169 {
170 ComponentSupport.finalizeForDeletion(c);
171
172 if (facetName == null)
173 {
174 parent.getChildren().remove(c);
175 }
176 }
177
178 this.onComponentPopulated(ctx, c, parent);
179
180
181
182
183 if (facetName == null)
184 {
185 parent.getChildren().add(c);
186 }
187 else
188 {
189 parent.getFacets().put(facetName, c);
190 }
191 }
192
193
194
195
196
197
198
199 protected final String getFacetName(FaceletContext ctx, UIComponent parent)
200 {
201
202
203 return (String) parent.getAttributes().get("facelets.FACET_NAME");
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218 protected UIComponent createComponent(FaceletContext ctx)
219 {
220 UIComponent c = null;
221 FacesContext faces = ctx.getFacesContext();
222 Application app = faces.getApplication();
223 if (this.binding != null)
224 {
225 ValueExpression ve = this.binding.getValueExpression(ctx, Object.class);
226
227 c = app.createComponent(ve, faces, this.componentType);
228 if (c != null)
229 {
230 c.setValueExpression("binding", ve);
231 }
232 }
233 else
234 {
235 c = app.createComponent(this.componentType);
236 }
237 return c;
238 }
239
240
241
242
243
244
245
246
247
248 protected String getId(FaceletContext ctx)
249 {
250 if (this.id != null)
251 {
252 return this.id.getValue(ctx);
253 }
254 return ctx.generateUniqueId(this.tagId);
255 }
256
257 @Override
258 protected MetaRuleset createMetaRuleset(Class type)
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
291
292 MetaRuleset m = new MetaRulesetImpl(this.tag, type);
293
294 m.ignore("binding").ignore("id");
295
296
297 m.addRule(ComponentRule.INSTANCE);
298
299
300 if (ActionSource.class.isAssignableFrom(type))
301 {
302 m.addRule(ActionSourceRule.INSTANCE);
303 }
304
305
306 if (ValueHolder.class.isAssignableFrom(type))
307 {
308 m.addRule(ValueHolderRule.INSTANCE);
309
310
311 if (EditableValueHolder.class.isAssignableFrom(type))
312 {
313 m.ignore("submittedValue");
314 m.ignore("valid");
315 m.addRule(EditableValueHolderRule.INSTANCE);
316 }
317 }
318
319 return m;
320 }
321
322
323
324
325
326
327
328
329
330
331 protected void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent)
332 {
333
334 }
335
336 protected void onComponentPopulated(FaceletContext ctx, UIComponent c, UIComponent parent)
337 {
338
339 }
340
341 protected void applyNextHandler(FaceletContext ctx, UIComponent c) throws IOException, FacesException, ELException
342 {
343
344 this.nextHandler.apply(ctx, c);
345 }
346 }