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