KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > AbstractTagLibrary


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag;
16
17 import java.lang.reflect.Constructor JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.el.ELException;
25 import javax.faces.FacesException;
26 import javax.faces.convert.Converter;
27 import javax.faces.validator.Validator;
28
29 import com.sun.facelets.FaceletContext;
30 import com.sun.facelets.FaceletException;
31 import com.sun.facelets.FaceletHandler;
32 import com.sun.facelets.tag.jsf.ComponentConfig;
33 import com.sun.facelets.tag.jsf.ComponentHandler;
34 import com.sun.facelets.tag.jsf.ConvertHandler;
35 import com.sun.facelets.tag.jsf.ConverterConfig;
36 import com.sun.facelets.tag.jsf.ValidateHandler;
37 import com.sun.facelets.tag.jsf.ValidatorConfig;
38
39 /**
40  * Base class for defining TagLibraries in Java
41  *
42  * @author Jacob Hookom
43  * @version $Id: AbstractTagLibrary.java,v 1.8 2005/08/24 04:38:46 jhook Exp $
44  */

45 public abstract class AbstractTagLibrary implements TagLibrary {
46
47     private static class ValidatorConfigWrapper implements ValidatorConfig {
48
49         private final TagConfig parent;
50         private final String JavaDoc validatorId;
51         
52         public ValidatorConfigWrapper(TagConfig parent, String JavaDoc validatorId) {
53             this.parent = parent;
54             this.validatorId = validatorId;
55         }
56         
57         public String JavaDoc getValidatorId() {
58             return this.validatorId;
59         }
60
61         public FaceletHandler getNextHandler() {
62             return this.parent.getNextHandler();
63         }
64
65         public Tag getTag() {
66             return this.parent.getTag();
67         }
68
69         public String JavaDoc getTagId() {
70             return this.parent.getTagId();
71         }
72     }
73     
74     private static class ConverterConfigWrapper implements ConverterConfig {
75         private final TagConfig parent;
76         private final String JavaDoc converterId;
77         
78         public ConverterConfigWrapper(TagConfig parent, String JavaDoc converterId) {
79             this.parent = parent;
80             this.converterId = converterId;
81         }
82         
83         public String JavaDoc getConverterId() {
84             return this.converterId;
85         }
86         public FaceletHandler getNextHandler() {
87             return this.parent.getNextHandler();
88         }
89         public Tag getTag() {
90             return this.parent.getTag();
91         }
92         public String JavaDoc getTagId() {
93             return this.parent.getTagId();
94         }
95     }
96     
97     private static class HandlerFactory implements TagHandlerFactory {
98         private final static Class JavaDoc[] CONSTRUCTOR_SIG = new Class JavaDoc[] { TagConfig.class };
99
100         protected final Class JavaDoc handlerType;
101
102         public HandlerFactory(Class JavaDoc handlerType) {
103             this.handlerType = handlerType;
104         }
105
106         public TagHandler createHandler(TagConfig cfg) throws FacesException,
107                 ELException {
108             try {
109                 return (TagHandler) this.handlerType.getConstructor(
110                         CONSTRUCTOR_SIG).newInstance(new Object JavaDoc[] { cfg });
111             } catch (InvocationTargetException JavaDoc ite) {
112                 Throwable JavaDoc t = ite.getCause();
113                 if (t instanceof FacesException) {
114                     throw (FacesException) t;
115                 } else if (t instanceof ELException) {
116                     throw (ELException) t;
117                 } else {
118                     throw new FacesException("Error Instantiating: "
119                             + this.handlerType.getName(), t);
120                 }
121             } catch (Exception JavaDoc e) {
122                 throw new FacesException("Error Instantiating: "
123                         + this.handlerType.getName(), e);
124             }
125         }
126     }
127
128     private static class ComponentConfigWrapper implements ComponentConfig {
129
130         protected final TagConfig parent;
131
132         protected final String JavaDoc componentType;
133
134         protected final String JavaDoc rendererType;
135
136         public ComponentConfigWrapper(TagConfig parent, String JavaDoc componentType,
137                 String JavaDoc rendererType) {
138             this.parent = parent;
139             this.componentType = componentType;
140             this.rendererType = rendererType;
141         }
142
143         public String JavaDoc getComponentType() {
144             return this.componentType;
145         }
146
147         public String JavaDoc getRendererType() {
148             return this.rendererType;
149         }
150
151         public FaceletHandler getNextHandler() {
152             return this.parent.getNextHandler();
153         }
154
155         public Tag getTag() {
156             return this.parent.getTag();
157         }
158
159         public String JavaDoc getTagId() {
160             return this.parent.getTagId();
161         }
162     }
163
164     private static class UserTagFactory implements TagHandlerFactory {
165         protected final URL JavaDoc location;
166
167         public UserTagFactory(URL JavaDoc location) {
168             this.location = location;
169         }
170
171         public TagHandler createHandler(TagConfig cfg) throws FacesException,
172                 ELException {
173             return new UserTagHandler(cfg, this.location);
174         }
175     }
176
177     private static class ComponentHandlerFactory implements TagHandlerFactory {
178
179         protected final String JavaDoc componentType;
180
181         protected final String JavaDoc renderType;
182
183         /**
184          * @param handlerType
185          */

186         public ComponentHandlerFactory(String JavaDoc componentType, String JavaDoc renderType) {
187             this.componentType = componentType;
188             this.renderType = renderType;
189         }
190
191         public TagHandler createHandler(TagConfig cfg) throws FacesException,
192                 ELException {
193             ComponentConfig ccfg = new ComponentConfigWrapper(cfg,
194                     this.componentType, this.renderType);
195             return new ComponentHandler(ccfg);
196         }
197     }
198
199     private static class UserComponentHandlerFactory implements
200             TagHandlerFactory {
201
202         private final static Class JavaDoc[] CONS_SIG = new Class JavaDoc[] { ComponentConfig.class };
203
204         protected final String JavaDoc componentType;
205
206         protected final String JavaDoc renderType;
207
208         protected final Class JavaDoc type;
209
210         protected final Constructor JavaDoc constructor;
211
212         /**
213          * @param handlerType
214          */

215         public UserComponentHandlerFactory(String JavaDoc componentType,
216                 String JavaDoc renderType, Class JavaDoc type) {
217             this.componentType = componentType;
218             this.renderType = renderType;
219             this.type = type;
220             try {
221                 this.constructor = this.type.getConstructor(CONS_SIG);
222             } catch (Exception JavaDoc e) {
223                 throw new FaceletException(
224                         "Must have a Constructor that takes in a ComponentConfig",
225                         e);
226             }
227         }
228
229         public TagHandler createHandler(TagConfig cfg) throws FacesException,
230                 ELException {
231             try {
232                 ComponentConfig ccfg = new ComponentConfigWrapper(cfg,
233                         this.componentType, this.renderType);
234                 return (TagHandler) this.constructor
235                         .newInstance(new Object JavaDoc[] { ccfg });
236             } catch (InvocationTargetException JavaDoc e) {
237                 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
238             } catch (Exception JavaDoc e) {
239                 throw new FaceletException("Error Instantiating ComponentHandler: "+this.type.getName(), e);
240             }
241         }
242     }
243
244     private static class ValidatorHandlerFactory implements TagHandlerFactory {
245
246         protected final String JavaDoc validatorId;
247
248         public ValidatorHandlerFactory(String JavaDoc validatorId) {
249             this.validatorId = validatorId;
250         }
251
252         public TagHandler createHandler(TagConfig cfg) throws FacesException,
253                 ELException {
254             return new ValidateHandler(new ValidatorConfigWrapper(cfg, this.validatorId));
255         }
256     }
257
258     private static class ConverterHandlerFactory implements TagHandlerFactory {
259
260         protected final String JavaDoc converterId;
261
262         public ConverterHandlerFactory(String JavaDoc converterId) {
263             this.converterId = converterId;
264         }
265
266         public TagHandler createHandler(TagConfig cfg) throws FacesException,
267                 ELException {
268             return new ConvertHandler(new ConverterConfigWrapper(cfg, this.converterId));
269         }
270     }
271
272     private static class UserConverterHandlerFactory implements TagHandlerFactory {
273         private final static Class JavaDoc[] CONS_SIG = new Class JavaDoc[] { ConverterConfig.class };
274         
275         protected final String JavaDoc converterId;
276         
277         protected final Class JavaDoc type;
278
279         protected final Constructor JavaDoc constructor;
280         
281         public UserConverterHandlerFactory(String JavaDoc converterId, Class JavaDoc type) {
282             this.converterId = converterId;
283             this.type = type;
284             try {
285                 this.constructor = this.type.getConstructor(CONS_SIG);
286             } catch (Exception JavaDoc e) {
287                 throw new FaceletException(
288                         "Must have a Constructor that takes in a ConverterConfig",
289                         e);
290             }
291         }
292         
293         public TagHandler createHandler(TagConfig cfg) throws FacesException,
294         ELException {
295             try {
296                 ConverterConfig ccfg = new ConverterConfigWrapper(cfg,
297                         this.converterId);
298                 return (TagHandler) this.constructor
299                         .newInstance(new Object JavaDoc[] { ccfg });
300             } catch (InvocationTargetException JavaDoc e) {
301                 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
302             } catch (Exception JavaDoc e) {
303                 throw new FaceletException("Error Instantiating ConverterHandler: "+this.type.getName(), e);
304             }
305         }
306     }
307     
308     private static class UserValidatorHandlerFactory implements TagHandlerFactory {
309         private final static Class JavaDoc[] CONS_SIG = new Class JavaDoc[] { ValidatorConfig.class };
310         
311         protected final String JavaDoc validatorId;
312         
313         protected final Class JavaDoc type;
314
315         protected final Constructor JavaDoc constructor;
316         
317         public UserValidatorHandlerFactory(String JavaDoc validatorId, Class JavaDoc type) {
318             this.validatorId = validatorId;
319             this.type = type;
320             try {
321                 this.constructor = this.type.getConstructor(CONS_SIG);
322             } catch (Exception JavaDoc e) {
323                 throw new FaceletException(
324                         "Must have a Constructor that takes in a ConverterConfig",
325                         e);
326             }
327         }
328         
329         public TagHandler createHandler(TagConfig cfg) throws FacesException,
330         ELException {
331             try {
332                 ValidatorConfig ccfg = new ValidatorConfigWrapper(cfg,
333                         this.validatorId);
334                 return (TagHandler) this.constructor
335                         .newInstance(new Object JavaDoc[] { ccfg });
336             } catch (InvocationTargetException JavaDoc e) {
337                 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
338             } catch (Exception JavaDoc e) {
339                 throw new FaceletException("Error Instantiating ValidatorHandler: "+this.type.getName(), e);
340             }
341         }
342     }
343     
344     private final Map JavaDoc factories;
345
346     private final String JavaDoc namespace;
347
348     private final Map JavaDoc functions;
349
350     public AbstractTagLibrary(String JavaDoc namespace) {
351         this.namespace = namespace;
352         this.factories = new HashMap JavaDoc();
353         this.functions = new HashMap JavaDoc();
354     }
355
356     /**
357      * Add a ComponentHandler with the specified componentType and rendererType,
358      * aliased by the tag name.
359      *
360      * @see ComponentHandler
361      * @see javax.faces.application.Application#createComponent(java.lang.String)
362      * @param name
363      * name to use, "foo" would be <my:foo />
364      * @param componentType
365      * componentType to use
366      * @param rendererType
367      * rendererType to use
368      */

369     protected final void addComponent(String JavaDoc name, String JavaDoc componentType,
370             String JavaDoc rendererType) {
371         this.factories.put(name, new ComponentHandlerFactory(componentType,
372                 rendererType));
373     }
374
375     /**
376      * Add a ComponentHandler with the specified componentType and rendererType,
377      * aliased by the tag name. The Facelet will be compiled with the specified
378      * HandlerType (which must extend AbstractComponentHandler).
379      *
380      * @see AbstractComponentHandler
381      * @param name
382      * name to use, "foo" would be <my:foo />
383      * @param componentType
384      * componentType to use
385      * @param rendererType
386      * rendererType to use
387      * @param handlerType
388      * a Class that extends AbstractComponentHandler
389      */

390     protected final void addComponent(String JavaDoc name, String JavaDoc componentType,
391             String JavaDoc rendererType, Class JavaDoc handlerType) {
392         this.factories.put(name, new UserComponentHandlerFactory(componentType,
393                 rendererType, handlerType));
394     }
395
396     /**
397      * Add a ConvertHandler for the specified converterId
398      *
399      * @see ConvertHandler
400      * @see javax.faces.application.Application#createConverter(java.lang.String)
401      * @param name
402      * name to use, "foo" would be <my:foo />
403      * @param converterId
404      * id to pass to Application instance
405      */

406     protected final void addConverter(String JavaDoc name, String JavaDoc converterId) {
407         this.factories.put(name, new ConverterHandlerFactory(converterId));
408     }
409     
410     /**
411      * Add a ConvertHandler for the specified converterId of a TagHandler type
412      *
413      * @see ConvertHandler
414      * @see ConverterConfig
415      * @see javax.faces.application.Application#createConverter(java.lang.String)
416      * @param name
417      * name to use, "foo" would be <my:foo />
418      * @param converterId
419      * id to pass to Application instance
420      * @param type
421      * TagHandler type that takes in a ConverterConfig
422      */

423     protected final void addConverter(String JavaDoc name, String JavaDoc converterId, Class JavaDoc type) {
424         this.factories.put(name, new UserConverterHandlerFactory(converterId, type));
425     }
426
427     /**
428      * Add a ValidateHandler for the specified validatorId
429      *
430      * @see ValidateHandler
431      * @see javax.faces.application.Application#createValidator(java.lang.String)
432      * @param name
433      * name to use, "foo" would be <my:foo />
434      * @param validatorId
435      * id to pass to Application instance
436      */

437     protected final void addValidator(String JavaDoc name, String JavaDoc validatorId) {
438         this.factories.put(name, new ValidatorHandlerFactory(validatorId));
439     }
440     
441     /**
442      * Add a ValidateHandler for the specified validatorId
443      *
444      * @see ValidateHandler
445      * @see ValidatorConfig
446      * @see javax.faces.application.Application#createValidator(java.lang.String)
447      * @param name
448      * name to use, "foo" would be <my:foo />
449      * @param validatorId
450      * id to pass to Application instance
451      * @param type
452      * TagHandler type that takes in a ValidatorConfig
453      */

454     protected final void addValidator(String JavaDoc name, String JavaDoc validatorId, Class JavaDoc type) {
455         this.factories.put(name, new UserValidatorHandlerFactory(validatorId, type));
456     }
457
458     /**
459      * Use the specified HandlerType in compiling Facelets. HandlerType must
460      * extend TagHandler.
461      *
462      * @see TagHandler
463      * @param name
464      * name to use, "foo" would be <my:foo />
465      * @param handlerType
466      * must extend TagHandler
467      */

468     protected final void addTagHandler(String JavaDoc name, Class JavaDoc handlerType) {
469         this.factories.put(name, new HandlerFactory(handlerType));
470     }
471
472     /**
473      * Add a UserTagHandler specified a the URL source.
474      *
475      * @see UserTagHandler
476      * @param name
477      * name to use, "foo" would be <my:foo />
478      * @param source source where the Facelet (Tag) source is
479      */

480     protected final void addUserTag(String JavaDoc name, URL JavaDoc source) {
481         this.factories.put(name, new UserTagFactory(source));
482     }
483     
484     
485     /**
486      * Add a Method to be used as a Function at Compilation.
487      *
488      * @see javax.el.FunctionMapper
489      *
490      * @param name (suffix) of function name
491      * @param method method instance
492      */

493     protected final void addFunction(String JavaDoc name, Method JavaDoc method) {
494         this.functions.put(name, method);
495     }
496
497     /*
498      * (non-Javadoc)
499      *
500      * @see com.sun.facelets.tag.TagLibrary#containsNamespace(java.lang.String)
501      */

502     public boolean containsNamespace(String JavaDoc ns) {
503         return this.namespace.equals(ns);
504     }
505
506     /*
507      * (non-Javadoc)
508      *
509      * @see com.sun.facelets.tag.TagLibrary#containsTagHandler(java.lang.String,
510      * java.lang.String)
511      */

512     public boolean containsTagHandler(String JavaDoc ns, String JavaDoc localName) {
513         if (this.namespace.equals(ns)) {
514             if (this.factories.containsKey(localName)) {
515                 return true;
516             }
517         }
518         return false;
519     }
520
521     /*
522      * (non-Javadoc)
523      *
524      * @see com.sun.facelets.tag.TagLibrary#createTagHandler(java.lang.String,
525      * java.lang.String, com.sun.facelets.tag.TagConfig)
526      */

527     public TagHandler createTagHandler(String JavaDoc ns, String JavaDoc localName,
528             TagConfig tag) throws FacesException {
529         if (this.namespace.equals(ns)) {
530             TagHandlerFactory f = (TagHandlerFactory) this.factories
531                     .get(localName);
532             if (f != null) {
533                 return f.createHandler(tag);
534             }
535         }
536         return null;
537     }
538
539     /*
540      * (non-Javadoc)
541      *
542      * @see com.sun.facelets.tag.TagLibrary#containsFunction(java.lang.String,
543      * java.lang.String)
544      */

545     public boolean containsFunction(String JavaDoc ns, String JavaDoc name) {
546         if (this.namespace.equals(ns)) {
547             return this.functions.containsKey(name);
548         }
549         return false;
550     }
551
552     /*
553      * (non-Javadoc)
554      *
555      * @see com.sun.facelets.tag.TagLibrary#createFunction(java.lang.String,
556      * java.lang.String)
557      */

558     public Method JavaDoc createFunction(String JavaDoc ns, String JavaDoc name) {
559         if (this.namespace.equals(ns)) {
560             return (Method JavaDoc) this.functions.get(name);
561         }
562         return null;
563     }
564
565     /*
566      * (non-Javadoc)
567      *
568      * @see java.lang.Object#equals(java.lang.Object)
569      */

570     public boolean equals(Object JavaDoc obj) {
571         return (obj instanceof TagLibrary && obj.hashCode() == this.hashCode());
572     }
573
574     /*
575      * (non-Javadoc)
576      *
577      * @see java.lang.Object#hashCode()
578      */

579     public int hashCode() {
580         return this.namespace.hashCode();
581     }
582
583     public String JavaDoc getNamespace() {
584         return namespace;
585     }
586 }
587
Popular Tags