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.net.URL;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.el.ELException;
29 import javax.faces.FacesException;
30 import javax.faces.application.Resource;
31 import javax.faces.application.ResourceHandler;
32 import javax.faces.context.FacesContext;
33 import javax.faces.view.facelets.BehaviorConfig;
34 import javax.faces.view.facelets.BehaviorHandler;
35 import javax.faces.view.facelets.ComponentConfig;
36 import javax.faces.view.facelets.ComponentHandler;
37 import javax.faces.view.facelets.ConverterConfig;
38 import javax.faces.view.facelets.ConverterHandler;
39 import javax.faces.view.facelets.FaceletException;
40 import javax.faces.view.facelets.FaceletHandler;
41 import javax.faces.view.facelets.Tag;
42 import javax.faces.view.facelets.TagConfig;
43 import javax.faces.view.facelets.TagHandler;
44 import javax.faces.view.facelets.ValidatorConfig;
45 import javax.faces.view.facelets.ValidatorHandler;
46 import org.apache.myfaces.shared.config.MyfacesConfig;
47 import org.apache.myfaces.view.facelets.tag.composite.CompositeComponentResourceTagHandler;
48 import org.apache.myfaces.view.facelets.tag.composite.CompositeResouceWrapper;
49
50
51
52
53
54
55
56 public abstract class AbstractTagLibrary implements TagLibrary
57 {
58 private final Map<String, TagHandlerFactory> _factories;
59
60 private final Map<String, Method> _functions;
61
62 private final String _namespace;
63 private final String _aliasNamespace;
64 private Boolean _strictJsf2FaceletsCompatibility;
65
66 public AbstractTagLibrary(String namespace, String aliasNamespace)
67 {
68 _namespace = namespace;
69 _aliasNamespace = aliasNamespace;
70 _factories = new HashMap<String, TagHandlerFactory>();
71 _functions = new HashMap<String, Method>();
72 }
73
74 public AbstractTagLibrary(String namespace)
75 {
76 this(namespace,null);
77 }
78
79
80
81
82
83
84
85 public boolean containsNamespace(String ns)
86 {
87 return _namespace.equals(ns) || (_aliasNamespace != null && _aliasNamespace.equals(ns));
88 }
89
90
91
92
93
94
95 public boolean containsTagHandler(String ns, String localName)
96 {
97 return containsNamespace(ns) && _factories.containsKey(localName);
98 }
99
100
101
102
103
104
105
106 public TagHandler createTagHandler(String ns, String localName, TagConfig tag) throws FacesException
107 {
108 if (containsNamespace(ns))
109 {
110 TagHandlerFactory f = _factories.get(localName);
111 if (f != null)
112 {
113 return f.createHandler(tag);
114 }
115 }
116
117 return null;
118 }
119
120
121
122
123
124
125 public boolean containsFunction(String ns, String name)
126 {
127 return containsNamespace(ns) && _functions.containsKey(name);
128 }
129
130
131
132
133
134
135 public Method createFunction(String ns, String name)
136 {
137 return containsNamespace(ns) ? _functions.get(name) : null;
138 }
139
140 public String getNamespace()
141 {
142 return _namespace;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157 protected final void addComponent(String name, String componentType, String rendererType)
158 {
159 _factories.put(name, new ComponentHandlerFactory(componentType, rendererType));
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 protected final void addComponent(String name, String componentType, String rendererType,
178 Class<? extends TagHandler> handlerType)
179 {
180 _factories.put(name, new UserComponentHandlerFactory(componentType, rendererType, handlerType));
181 }
182
183 protected final void addComponentFromResourceId(String name, String resourceId)
184 {
185 _factories.put(name, new UserComponentFromResourceIdHandlerFactory(resourceId));
186 }
187
188
189
190
191
192
193
194
195
196
197
198 protected final void addConverter(String name, String converterId)
199 {
200 _factories.put(name, new ConverterHandlerFactory(converterId));
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 protected final void addConverter(String name, String converterId, Class<? extends TagHandler> type)
218 {
219 _factories.put(name, new UserConverterHandlerFactory(converterId, type));
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233 protected final void addValidator(String name, String validatorId)
234 {
235 _factories.put(name, new ValidatorHandlerFactory(validatorId));
236 }
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 protected final void addValidator(String name, String validatorId, Class<? extends TagHandler> type)
252 {
253 _factories.put(name, new UserValidatorHandlerFactory(validatorId, type));
254 }
255
256
257
258
259
260
261
262
263
264
265 protected final void addTagHandler(String name, Class<? extends TagHandler> handlerType)
266 {
267 _factories.put(name, new HandlerFactory(handlerType));
268 }
269
270
271
272
273
274
275
276
277
278
279 protected final void addUserTag(String name, URL source)
280 {
281 if (_strictJsf2FaceletsCompatibility == null)
282 {
283 MyfacesConfig config = MyfacesConfig.getCurrentInstance(
284 FacesContext.getCurrentInstance().getExternalContext());
285
286 _strictJsf2FaceletsCompatibility = config.isStrictJsf2FaceletsCompatibility();
287 }
288 if (Boolean.TRUE.equals(_strictJsf2FaceletsCompatibility))
289 {
290 _factories.put(name, new LegacyUserTagFactory(source));
291 }
292 else
293 {
294 _factories.put(name, new UserTagFactory(source));
295 }
296 }
297
298
299
300
301
302
303
304
305
306
307
308 protected final void addFunction(String name, Method method)
309 {
310 _functions.put(name, method);
311 }
312
313
314
315
316
317
318 protected final void addBehavior(String name, String behaviorId)
319 {
320 _factories.put(name, new BehaviorHandlerFactory(behaviorId));
321 }
322
323
324
325
326
327
328
329 protected final void addBehavior(String name, String behaviorId,
330 Class<? extends TagHandler> handlerType)
331 {
332 _factories.put(name, new UserBehaviorHandlerFactory(behaviorId,handlerType));
333 }
334
335 private static class ValidatorConfigWrapper implements ValidatorConfig
336 {
337
338 private final TagConfig parent;
339 private final String validatorId;
340
341 public ValidatorConfigWrapper(TagConfig parent, String validatorId)
342 {
343 this.parent = parent;
344 this.validatorId = validatorId;
345 }
346
347 public String getValidatorId()
348 {
349 return this.validatorId;
350 }
351
352 public FaceletHandler getNextHandler()
353 {
354 return this.parent.getNextHandler();
355 }
356
357 public Tag getTag()
358 {
359 return this.parent.getTag();
360 }
361
362 public String getTagId()
363 {
364 return this.parent.getTagId();
365 }
366 }
367
368 private static class ConverterConfigWrapper implements ConverterConfig
369 {
370 private final TagConfig parent;
371 private final String converterId;
372
373 public ConverterConfigWrapper(TagConfig parent, String converterId)
374 {
375 this.parent = parent;
376 this.converterId = converterId;
377 }
378
379 public String getConverterId()
380 {
381 return this.converterId;
382 }
383
384 public FaceletHandler getNextHandler()
385 {
386 return this.parent.getNextHandler();
387 }
388
389 public Tag getTag()
390 {
391 return this.parent.getTag();
392 }
393
394 public String getTagId()
395 {
396 return this.parent.getTagId();
397 }
398 }
399
400 private static class HandlerFactory implements TagHandlerFactory
401 {
402 private final static Class<?>[] CONSTRUCTOR_SIG = new Class[]{TagConfig.class};
403
404 protected final Class<? extends TagHandler> handlerType;
405
406 public HandlerFactory(Class<? extends TagHandler> handlerType)
407 {
408 this.handlerType = handlerType;
409 }
410
411 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
412 {
413 try
414 {
415 return handlerType.getConstructor(CONSTRUCTOR_SIG).newInstance(new Object[] { cfg });
416 }
417 catch (InvocationTargetException ite)
418 {
419 Throwable t = ite.getCause();
420 if (t instanceof FacesException)
421 {
422 throw (FacesException) t;
423 }
424 else if (t instanceof ELException)
425 {
426 throw (ELException) t;
427 }
428 else
429 {
430 throw new FacesException("Error Instantiating: " + handlerType.getName(), t);
431 }
432 }
433 catch (Exception e)
434 {
435 throw new FacesException("Error Instantiating: " + handlerType.getName(), e);
436 }
437 }
438 }
439
440 private static class ComponentConfigWrapper implements ComponentConfig
441 {
442
443 protected final TagConfig parent;
444
445 protected final String componentType;
446
447 protected final String rendererType;
448
449 public ComponentConfigWrapper(TagConfig parent, String componentType, String rendererType)
450 {
451 this.parent = parent;
452 this.componentType = componentType;
453 this.rendererType = rendererType;
454 }
455
456 public String getComponentType()
457 {
458 return this.componentType;
459 }
460
461 public String getRendererType()
462 {
463 return this.rendererType;
464 }
465
466 public FaceletHandler getNextHandler()
467 {
468 return this.parent.getNextHandler();
469 }
470
471 public Tag getTag()
472 {
473 return this.parent.getTag();
474 }
475
476 public String getTagId()
477 {
478 return this.parent.getTagId();
479 }
480 }
481
482 private static class UserTagFactory implements TagHandlerFactory
483 {
484 protected final URL location;
485
486 public UserTagFactory(URL location)
487 {
488 this.location = location;
489 }
490
491 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
492 {
493 return new UserTagHandler(cfg, this.location);
494 }
495 }
496
497 private static class LegacyUserTagFactory implements TagHandlerFactory
498 {
499 protected final URL location;
500
501 public LegacyUserTagFactory(URL location)
502 {
503 this.location = location;
504 }
505
506 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
507 {
508 return new LegacyUserTagHandler(cfg, this.location);
509 }
510 }
511
512 private static class ComponentHandlerFactory implements TagHandlerFactory
513 {
514
515 protected final String componentType;
516
517 protected final String renderType;
518
519
520
521
522 public ComponentHandlerFactory(String componentType, String renderType)
523 {
524 this.componentType = componentType;
525 this.renderType = renderType;
526 }
527
528 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
529 {
530 ComponentConfig ccfg = new ComponentConfigWrapper(cfg, this.componentType, this.renderType);
531 return new ComponentHandler(ccfg);
532 }
533 }
534
535 private static class UserComponentHandlerFactory implements TagHandlerFactory
536 {
537
538 private final static Class<?>[] CONS_SIG = new Class[] { ComponentConfig.class };
539
540 protected final String componentType;
541
542 protected final String renderType;
543
544 protected final Class<? extends TagHandler> type;
545
546 protected final Constructor<? extends TagHandler> constructor;
547
548
549
550
551 public UserComponentHandlerFactory(String componentType, String renderType, Class<? extends TagHandler> type)
552 {
553 this.componentType = componentType;
554 this.renderType = renderType;
555 this.type = type;
556 try
557 {
558 this.constructor = this.type.getConstructor(CONS_SIG);
559 }
560 catch (Exception e)
561 {
562 throw new FaceletException("Must have a Constructor that takes in a ComponentConfig", e);
563 }
564 }
565
566 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
567 {
568 try
569 {
570 ComponentConfig ccfg = new ComponentConfigWrapper(cfg, componentType, renderType);
571 return constructor.newInstance(new Object[] { ccfg });
572 }
573 catch (InvocationTargetException e)
574 {
575 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
576 }
577 catch (Exception e)
578 {
579 throw new FaceletException("Error Instantiating ComponentHandler: " + this.type.getName(), e);
580 }
581 }
582 }
583
584 private static class ValidatorHandlerFactory implements TagHandlerFactory
585 {
586
587 protected final String validatorId;
588
589 public ValidatorHandlerFactory(String validatorId)
590 {
591 this.validatorId = validatorId;
592 }
593
594 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
595 {
596 return new ValidatorHandler(new ValidatorConfigWrapper(cfg, this.validatorId));
597 }
598 }
599
600 private static class ConverterHandlerFactory implements TagHandlerFactory
601 {
602
603 protected final String converterId;
604
605 public ConverterHandlerFactory(String converterId)
606 {
607 this.converterId = converterId;
608 }
609
610 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
611 {
612 return new ConverterHandler(new ConverterConfigWrapper(cfg, this.converterId));
613 }
614 }
615
616 private static class UserConverterHandlerFactory implements TagHandlerFactory
617 {
618 private final static Class<?>[] CONS_SIG = new Class[] { ConverterConfig.class };
619
620 protected final String converterId;
621
622 protected final Class<? extends TagHandler> type;
623
624 protected final Constructor<? extends TagHandler> constructor;
625
626 public UserConverterHandlerFactory(String converterId, Class<? extends TagHandler> type)
627 {
628 this.converterId = converterId;
629 this.type = type;
630 try
631 {
632 this.constructor = this.type.getConstructor(CONS_SIG);
633 }
634 catch (Exception e)
635 {
636 throw new FaceletException("Must have a Constructor that takes in a ConverterConfig", e);
637 }
638 }
639
640 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
641 {
642 try
643 {
644 ConverterConfig ccfg = new ConverterConfigWrapper(cfg, converterId);
645 return constructor.newInstance(new Object[] { ccfg });
646 }
647 catch (InvocationTargetException e)
648 {
649 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
650 }
651 catch (Exception e)
652 {
653 throw new FaceletException("Error Instantiating ConverterHandler: " + type.getName(), e);
654 }
655 }
656 }
657
658 private static class UserValidatorHandlerFactory implements TagHandlerFactory
659 {
660 private final static Class<?>[] CONS_SIG = new Class[] { ValidatorConfig.class };
661
662 protected final String validatorId;
663
664 protected final Class<? extends TagHandler> type;
665
666 protected final Constructor<? extends TagHandler> constructor;
667
668 public UserValidatorHandlerFactory(String validatorId, Class<? extends TagHandler> type)
669 {
670 this.validatorId = validatorId;
671 this.type = type;
672 try
673 {
674 this.constructor = this.type.getConstructor(CONS_SIG);
675 }
676 catch (Exception e)
677 {
678 throw new FaceletException("Must have a Constructor that takes in a ConverterConfig", e);
679 }
680 }
681
682 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
683 {
684 try
685 {
686 ValidatorConfig ccfg = new ValidatorConfigWrapper(cfg, validatorId);
687 return constructor.newInstance(new Object[] { ccfg });
688 }
689 catch (InvocationTargetException e)
690 {
691 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
692 }
693 catch (Exception e)
694 {
695 throw new FaceletException("Error Instantiating ValidatorHandler: " + type.getName(), e);
696 }
697 }
698 }
699
700 private static class BehaviorConfigWrapper implements BehaviorConfig
701 {
702 protected final TagConfig parent;
703
704 protected final String behaviorId;
705
706 public BehaviorConfigWrapper(TagConfig parent, String behaviorId)
707 {
708 this.parent = parent;
709 this.behaviorId = behaviorId;
710 }
711
712 public FaceletHandler getNextHandler()
713 {
714 return this.parent.getNextHandler();
715 }
716
717 public Tag getTag()
718 {
719 return this.parent.getTag();
720 }
721
722 public String getTagId()
723 {
724 return this.parent.getTagId();
725 }
726
727 public String getBehaviorId()
728 {
729 return this.behaviorId;
730 }
731 }
732
733 private static class BehaviorHandlerFactory implements TagHandlerFactory
734 {
735 protected final String behaviorId;
736
737 public BehaviorHandlerFactory(String behaviorId)
738 {
739 super();
740 this.behaviorId = behaviorId;
741 }
742
743 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
744 {
745 BehaviorConfig bcfg = new BehaviorConfigWrapper(cfg,this.behaviorId);
746 return new BehaviorHandler(bcfg);
747 }
748 }
749
750 private static class UserBehaviorHandlerFactory implements TagHandlerFactory
751 {
752 private final static Class<?>[] CONS_SIG = new Class[] { BehaviorConfig.class };
753
754 protected final String behaviorId;
755
756 protected final Class<? extends TagHandler> type;
757
758 protected final Constructor<? extends TagHandler> constructor;
759
760 public UserBehaviorHandlerFactory(String behaviorId, Class<? extends TagHandler> type)
761 {
762 this.behaviorId = behaviorId;
763 this.type = type;
764 try
765 {
766 this.constructor = this.type.getConstructor(CONS_SIG);
767 }
768 catch (Exception e)
769 {
770 throw new FaceletException("Must have a Constructor that takes in a BehaviorConfig", e);
771 }
772 }
773
774 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
775 {
776 try
777 {
778 BehaviorConfig bcfg = new BehaviorConfigWrapper(cfg,this.behaviorId);
779 return constructor.newInstance(new Object[] { bcfg });
780 }
781 catch (InvocationTargetException e)
782 {
783 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause());
784 }
785 catch (Exception e)
786 {
787 throw new FaceletException("Error Instantiating BehaviorHandler: " + this.type.getName(), e);
788 }
789 }
790 }
791
792 private static class UserComponentFromResourceIdHandlerFactory implements TagHandlerFactory
793 {
794 protected final String resourceId;
795
796 public UserComponentFromResourceIdHandlerFactory(String resourceId)
797 {
798 this.resourceId = resourceId;
799 }
800
801 public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
802 {
803 FacesContext facesContext = FacesContext.getCurrentInstance();
804 ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
805 Resource compositeComponentResourceWrapped = resourceHandler.createResourceFromId(resourceId);
806 if (compositeComponentResourceWrapped != null)
807 {
808 Resource compositeComponentResource
809 = new CompositeResouceWrapper(compositeComponentResourceWrapped);
810 ComponentConfig componentConfig = new ComponentConfigWrapper(cfg,
811 "javax.faces.NamingContainer", null);
812
813 return new CompositeComponentResourceTagHandler(componentConfig,
814 compositeComponentResource);
815 }
816 else
817 {
818 throw new FaceletException(
819 "Error Instantiating Component from <resource-id> declaration: " + this.resourceId);
820 }
821 }
822 }
823 }