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 | 0 | { |
45 | 0 | _factories = new HashMap<String, Map<String, TagHandlerFactory>>(); |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public boolean containsNamespace(String ns) |
54 | |
{ |
55 | 0 | return _factories.containsKey(ns); |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public boolean containsTagHandler(String ns, String localName) |
64 | |
{ |
65 | 0 | if (containsNamespace(ns)) |
66 | |
{ |
67 | 0 | Map<String, TagHandlerFactory> map = _factories.get(ns); |
68 | 0 | if (map == null) |
69 | |
{ |
70 | 0 | return false; |
71 | |
} |
72 | 0 | return map.containsKey(localName); |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 0 | 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 | 0 | if (containsNamespace(ns)) |
89 | |
{ |
90 | 0 | Map<String, TagHandlerFactory> map = _factories.get(ns); |
91 | 0 | if (map == null) |
92 | |
{ |
93 | 0 | map = new HashMap<String, TagHandlerFactory>(); |
94 | 0 | _factories.put(ns, map); |
95 | |
} |
96 | 0 | TagHandlerFactory f = map.get(localName); |
97 | 0 | if (f != null) |
98 | |
{ |
99 | 0 | return f.createHandler(tag); |
100 | |
} |
101 | |
} |
102 | |
|
103 | 0 | return null; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public boolean containsFunction(String ns, String name) |
112 | |
{ |
113 | 0 | return false; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public Method createFunction(String ns, String name) |
122 | |
{ |
123 | 0 | 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 | 0 | Map<String, TagHandlerFactory> map = _factories.get(namespace); |
147 | 0 | if (map == null) |
148 | |
{ |
149 | 0 | map = new HashMap<String, TagHandlerFactory>(); |
150 | 0 | _factories.put(namespace, map); |
151 | |
} |
152 | 0 | map.put(name, new ComponentHandlerFactory(componentType, rendererType)); |
153 | 0 | } |
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 | 0 | Map<String, TagHandlerFactory> map = _factories.get(namespace); |
173 | 0 | if (map == null) |
174 | |
{ |
175 | 0 | map = new HashMap<String, TagHandlerFactory>(); |
176 | 0 | _factories.put(namespace, map); |
177 | |
} |
178 | 0 | map.put(name, new UserComponentHandlerFactory(componentType, rendererType, handlerType)); |
179 | 0 | } |
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 | 0 | { |
192 | 0 | this.parent = parent; |
193 | 0 | this.componentType = componentType; |
194 | 0 | this.rendererType = rendererType; |
195 | 0 | } |
196 | |
|
197 | |
public String getComponentType() |
198 | |
{ |
199 | 0 | return this.componentType; |
200 | |
} |
201 | |
|
202 | |
public String getRendererType() |
203 | |
{ |
204 | 0 | return this.rendererType; |
205 | |
} |
206 | |
|
207 | |
public FaceletHandler getNextHandler() |
208 | |
{ |
209 | 0 | return this.parent.getNextHandler(); |
210 | |
} |
211 | |
|
212 | |
public Tag getTag() |
213 | |
{ |
214 | 0 | return this.parent.getTag(); |
215 | |
} |
216 | |
|
217 | |
public String getTagId() |
218 | |
{ |
219 | 0 | 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 | 0 | { |
235 | 0 | this.componentType = componentType; |
236 | 0 | this.renderType = renderType; |
237 | 0 | } |
238 | |
|
239 | |
public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException |
240 | |
{ |
241 | 0 | ComponentConfig ccfg = new ComponentConfigWrapper(cfg, this.componentType, this.renderType); |
242 | 0 | return new javax.faces.view.facelets.ComponentHandler(ccfg); |
243 | |
} |
244 | |
} |
245 | |
|
246 | |
private static class UserComponentHandlerFactory implements TagHandlerFactory |
247 | |
{ |
248 | |
|
249 | 0 | 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 | 0 | { |
264 | 0 | this.componentType = componentType; |
265 | 0 | this.renderType = renderType; |
266 | 0 | this.type = type; |
267 | |
try |
268 | |
{ |
269 | 0 | this.constructor = this.type.getConstructor(CONS_SIG); |
270 | |
} |
271 | 0 | catch (Exception e) |
272 | |
{ |
273 | 0 | throw new FaceletException("Must have a Constructor that takes in a ComponentConfig", e); |
274 | 0 | } |
275 | 0 | } |
276 | |
|
277 | |
public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException |
278 | |
{ |
279 | |
try |
280 | |
{ |
281 | 0 | ComponentConfig ccfg = new ComponentConfigWrapper(cfg, componentType, renderType); |
282 | 0 | return constructor.newInstance(new Object[] { ccfg }); |
283 | |
} |
284 | 0 | catch (InvocationTargetException e) |
285 | |
{ |
286 | 0 | throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause()); |
287 | |
} |
288 | 0 | catch (Exception e) |
289 | |
{ |
290 | 0 | throw new FaceletException("Error Instantiating ComponentHandler: " + this.type.getName(), e); |
291 | |
} |
292 | |
} |
293 | |
} |
294 | |
} |