1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.webapp;
20
21 import javax.faces.application.Application;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.ValueHolder;
24 import javax.faces.context.FacesContext;
25 import javax.faces.convert.Converter;
26 import javax.faces.el.ValueBinding;
27 import javax.servlet.jsp.JspException;
28 import javax.servlet.jsp.tagext.Tag;
29 import javax.servlet.jsp.tagext.TagSupport;
30
31
32
33
34
35
36
37
38 public class ConverterTag
39 extends TagSupport
40 {
41 private static final long serialVersionUID = -6168345066829108081L;
42 private String _converterId;
43 private String _binding;
44
45 public ConverterTag()
46 {
47 super();
48 }
49
50 public void setConverterId(String converterId)
51 {
52 _converterId = converterId;
53 }
54
55 public int doStartTag()
56 throws JspException
57 {
58
59 UIComponentClassicTagBase componentTag = UIComponentClassicTagBase.getParentUIComponentClassicTagBase(pageContext);
60
61 if (componentTag == null)
62 {
63 throw new JspException("no parent UIComponentTag found");
64 }
65 if (!componentTag.getCreated())
66 {
67 return Tag.SKIP_BODY;
68 }
69
70 Converter converter = createConverter();
71
72 UIComponent component = componentTag.getComponentInstance();
73 if (component == null)
74 {
75 throw new JspException("parent UIComponentTag has no UIComponent");
76 }
77 if (!(component instanceof ValueHolder))
78 {
79 throw new JspException("UIComponent is no ValueHolder");
80 }
81 ((ValueHolder)component).setConverter(converter);
82
83 return Tag.SKIP_BODY;
84 }
85
86 public void release()
87 {
88 super.release();
89 _converterId = null;
90 _binding = null;
91 }
92
93 protected Converter createConverter()
94 throws JspException
95 {
96 FacesContext facesContext = FacesContext.getCurrentInstance();
97 Application application = facesContext.getApplication();
98
99 if(_binding != null) {
100 ValueBinding vb = application.createValueBinding(_binding);
101 if(vb != null) {
102 Converter converter = (Converter) vb.getValue(facesContext);
103 if(converter != null) {
104 return converter;
105 }
106 }
107 }
108
109 if (UIComponentTag.isValueReference(_converterId))
110 {
111 ValueBinding vb = facesContext.getApplication().createValueBinding(_converterId);
112 return application.createConverter((String)vb.getValue(facesContext));
113 }
114
115 return application.createConverter(_converterId);
116
117 }
118
119 public void setBinding(String binding) throws javax.servlet.jsp.JspException
120 {
121 _binding = binding;
122 }
123 }