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