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 public class ConverterTag extends TagSupport
37 {
38 private static final long serialVersionUID = -6168345066829108081L;
39 private String _converterId;
40 private String _binding;
41
42 public ConverterTag()
43 {
44 super();
45 }
46
47 public void setConverterId(String converterId)
48 {
49 _converterId = converterId;
50 }
51
52 @Override
53 public int doStartTag() throws JspException
54 {
55
56 UIComponentClassicTagBase componentTag =
57 UIComponentClassicTagBase.getParentUIComponentClassicTagBase(pageContext);
58
59 if (componentTag == null)
60 {
61 throw new JspException("no parent UIComponentTag found");
62 }
63 if (!componentTag.getCreated())
64 {
65 return Tag.SKIP_BODY;
66 }
67
68 Converter converter = createConverter();
69
70 UIComponent component = componentTag.getComponentInstance();
71 if (component == null)
72 {
73 throw new JspException("parent UIComponentTag has no UIComponent");
74 }
75 if (!(component instanceof ValueHolder))
76 {
77 throw new JspException("UIComponent is no ValueHolder");
78 }
79 ((ValueHolder)component).setConverter(converter);
80
81 return Tag.SKIP_BODY;
82 }
83
84 @Override
85 public void release()
86 {
87 super.release();
88 _converterId = null;
89 _binding = null;
90 }
91
92
93
94
95 protected Converter createConverter() throws JspException
96 {
97 FacesContext facesContext = FacesContext.getCurrentInstance();
98 Application application = facesContext.getApplication();
99
100 if (_binding != null)
101 {
102 ValueBinding vb = application.createValueBinding(_binding);
103 if (vb != null)
104 {
105 Converter converter = (Converter)vb.getValue(facesContext);
106 if (converter != null)
107 {
108 return converter;
109 }
110 }
111 }
112
113 if (UIComponentTag.isValueReference(_converterId))
114 {
115 ValueBinding vb = facesContext.getApplication().createValueBinding(_converterId);
116 return application.createConverter((String)vb.getValue(facesContext));
117 }
118
119 return application.createConverter(_converterId);
120
121 }
122
123
124
125
126 public void setBinding(String binding) throws JspException
127 {
128 _binding = binding;
129 }
130 }