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;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.util.HashMap;
25 import java.util.Map;
26 import javax.el.ELException;
27 import javax.faces.FacesException;
28 import javax.faces.view.facelets.ComponentConfig;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.faces.view.facelets.FaceletHandler;
31 import javax.faces.view.facelets.Tag;
32 import javax.faces.view.facelets.TagConfig;
33 import javax.faces.view.facelets.TagHandler;
34
35
36
37
38
39 public class ComponentTagDeclarationLibrary implements TagLibrary
40 {
41 private final Map<String, Map<String, TagHandlerFactory>> _factories;
42
43 public ComponentTagDeclarationLibrary()
44 {
45 _factories = new HashMap<String, Map<String, TagHandlerFactory>>();
46 }
47
48
49
50
51
52
53 public boolean containsNamespace(String ns)
54 {
55 return _factories.containsKey(ns);
56 }
57
58
59
60
61
62
63 public boolean containsTagHandler(String ns, String localName)
64 {
65 if (containsNamespace(ns))
66 {
67 Map<String, TagHandlerFactory> map = _factories.get(ns);
68 if (map == null)
69 {
70 return false;
71 }
72 return map.containsKey(localName);
73 }
74 else
75 {
76 return false;
77 }
78 }
79
80
81
82
83
84
85
86 public TagHandler createTagHandler(String ns, String localName, TagConfig tag) throws FacesException
87 {
88 if (containsNamespace(ns))
89 {
90 Map<String, TagHandlerFactory> map = _factories.get(ns);
91 if (map == null)
92 {
93 map = new HashMap<String, TagHandlerFactory>();
94 _factories.put(ns, map);
95 }
96 TagHandlerFactory f = map.get(localName);
97 if (f != null)
98 {
99 return f.createHandler(tag);
100 }
101 }
102
103 return null;
104 }
105
106
107
108
109
110
111 public boolean containsFunction(String ns, String name)
112 {
113 return false;
114 }
115
116
117
118
119
120
121 public Method createFunction(String ns, String name)
122 {
123 return null;
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 public final void addComponent(String namespace, String name, String componentType, String rendererType)
145 {
146 Map<String, TagHandlerFactory> map = _factories.get(namespace);
147 if (map == null)
148 {
149 map = new HashMap<String, TagHandlerFactory>();
150 _factories.put(namespace, map);
151 }
152 map.put(name, new ComponentHandlerFactory(componentType, rendererType));
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public final void addComponent(String namespace, String name, String componentType, String rendererType,
170 Class<? extends TagHandler> handlerType)
171 {
172 Map<String, TagHandlerFactory> map = _factories.get(namespace);
173 if (map == null)
174 {
175 map = new HashMap<String, TagHandlerFactory>();
176 _factories.put(namespace, map);
177 }
178 map.put(name, new UserComponentHandlerFactory(componentType, rendererType, handlerType));
179 }
180
181 private static class ComponentConfigWrapper implements ComponentConfig
182 {
183
184 protected final TagConfig parent;
185
186 protected final String componentType;
187
188 protected final String rendererType;
189
190 public ComponentConfigWrapper(TagConfig parent, String componentType, String rendererType)
191 {
192 this.parent = parent;
193 this.componentType = componentType;
194 this.rendererType = rendererType;
195 }
196
197 public String getComponentType()
198 {
199 return this.componentType;
200 }
201
202 public String getRendererType()
203 {
204 return this.rendererType;
205 }
206
207 public FaceletHandler getNextHandler()
208 {
209 return this.parent.getNextHandler();
210 }
211
212 public Tag getTag()
213 {
214 return this.parent.getTag();
215 }
216
217 public String getTagId()
218 {
219 return this.parent.getTagId();
220 }
221 }
222
223 private static class ComponentHandlerFactory implements TagHandlerFactory
224 {
225
226 protected final String componentType;
227
228 protected final String renderType;
229
230
231
232
233 public ComponentHandlerFactory(String componentType, String renderType)
234 {
235 this.componentType = componentType;
236 this.renderType = renderType;
237 }
238
239 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
240 {
241 ComponentConfig ccfg = new ComponentConfigWrapper(cfg, this.componentType, this.renderType);
242 return new javax.faces.view.facelets.ComponentHandler(ccfg);
243 }
244 }
245
246 private static class UserComponentHandlerFactory implements TagHandlerFactory
247 {
248
249 private final static Class<?>[] CONS_SIG = new Class[] { ComponentConfig.class };
250
251 protected final String componentType;
252
253 protected final String renderType;
254
255 protected final Class<? extends TagHandler> type;
256
257 protected final Constructor<? extends TagHandler> constructor;
258
259
260
261
262 public UserComponentHandlerFactory(String componentType, String renderType, Class<? extends TagHandler> type)
263 {
264 this.componentType = componentType;
265 this.renderType = renderType;
266 this.type = type;
267 try
268 {
269 this.constructor = this.type.getConstructor(CONS_SIG);
270 }
271 catch (Exception e)
272 {
273 throw new FaceletException("Must have a Constructor that takes in a ComponentConfig", e);
274 }
275 }
276
277 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
278 {
279 try
280 {
281 ComponentConfig ccfg = new ComponentConfigWrapper(cfg, componentType, renderType);
282 return constructor.newInstance(new Object[] { ccfg });
283 }
284 catch (InvocationTargetException e)
285 {
286 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
287 }
288 catch (Exception e)
289 {
290 throw new FaceletException("Error Instantiating ComponentHandler: " + this.type.getName(), e);
291 }
292 }
293 }
294 }