1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.taglib.core; |
20 | |
|
21 | |
import javax.el.ELContext; |
22 | |
import javax.el.ValueExpression; |
23 | |
import javax.faces.component.StateHolder; |
24 | |
import javax.faces.component.UIComponent; |
25 | |
import javax.faces.context.FacesContext; |
26 | |
import javax.faces.convert.Converter; |
27 | |
import javax.faces.convert.ConverterException; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class DelegateConverter implements Converter, StateHolder |
47 | |
{ |
48 | |
|
49 | |
private ValueExpression _converterId; |
50 | |
private ValueExpression _binding; |
51 | 0 | private String _converterIdString = null; |
52 | |
|
53 | |
public DelegateConverter() |
54 | 0 | { |
55 | |
|
56 | 0 | } |
57 | |
|
58 | |
public DelegateConverter(ValueExpression id, ValueExpression binding, String converterIdString) |
59 | |
{ |
60 | 0 | super(); |
61 | 0 | _converterId = id; |
62 | 0 | _binding = binding; |
63 | 0 | _converterIdString = converterIdString; |
64 | 0 | } |
65 | |
|
66 | |
public boolean isTransient() |
67 | |
{ |
68 | 0 | return false; |
69 | |
} |
70 | |
|
71 | |
public void restoreState(FacesContext facesContext, Object state) |
72 | |
{ |
73 | 0 | Object[] values = (Object[]) state; |
74 | 0 | _converterId = (ValueExpression) values[0]; |
75 | 0 | _binding = (ValueExpression) values[1]; |
76 | 0 | _converterIdString = (String) values[2]; |
77 | 0 | } |
78 | |
|
79 | |
public Object saveState(FacesContext facesContext) |
80 | |
{ |
81 | 0 | Object[] values = new Object[3]; |
82 | 0 | values[0] = _converterId; |
83 | 0 | values[1] = _binding; |
84 | 0 | values[2] = _converterIdString; |
85 | 0 | return values; |
86 | |
} |
87 | |
|
88 | |
public void setTransient(boolean arg0) |
89 | |
{ |
90 | |
|
91 | 0 | } |
92 | |
|
93 | |
public Object getAsObject(FacesContext facesContext, UIComponent component, |
94 | |
String value) |
95 | |
{ |
96 | 0 | return _getDelegate().getAsObject(facesContext, component, value); |
97 | |
} |
98 | |
|
99 | |
public String getAsString(FacesContext facesContext, UIComponent component, |
100 | |
Object value) |
101 | |
{ |
102 | 0 | return _getDelegate().getAsString(facesContext, component, value); |
103 | |
} |
104 | |
|
105 | |
private Converter _getDelegate() |
106 | |
{ |
107 | 0 | return _createConverter(); |
108 | |
} |
109 | |
|
110 | |
private Converter _createConverter() |
111 | |
{ |
112 | 0 | Converter converter = null; |
113 | |
|
114 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
115 | 0 | ELContext elContext = facesContext.getELContext(); |
116 | |
|
117 | |
|
118 | |
|
119 | 0 | if (_binding != null) |
120 | |
{ |
121 | |
try |
122 | |
{ |
123 | 0 | converter = (Converter) _binding.getValue(elContext); |
124 | |
|
125 | 0 | if (converter != null) |
126 | |
{ |
127 | 0 | return converter; |
128 | |
} |
129 | |
} |
130 | 0 | catch (Exception e) |
131 | |
{ |
132 | 0 | throw new ConverterException("Exception creating converter using binding", e); |
133 | 0 | } |
134 | |
} |
135 | |
|
136 | 0 | if ((_converterId != null) || (_converterIdString != null)) |
137 | |
{ |
138 | |
try |
139 | |
{ |
140 | 0 | if (null != _converterIdString) |
141 | |
{ |
142 | 0 | converter = facesContext.getApplication().createConverter(_converterIdString); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | String converterId = (String) _converterId.getValue(elContext); |
147 | 0 | converter = facesContext.getApplication().createConverter(converterId); |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | 0 | if (converter != null && _binding != null) |
153 | |
{ |
154 | 0 | _binding.setValue(elContext, converter); |
155 | |
} |
156 | |
} |
157 | 0 | catch (Exception e) |
158 | |
{ |
159 | 0 | throw new ConverterException("Exception creating converter with converterId: " + _converterId, e); |
160 | 0 | } |
161 | |
} |
162 | |
|
163 | 0 | if (converter == null) |
164 | |
{ |
165 | 0 | throw new IllegalStateException("Could not create converter. Please specify a valid converterId" + |
166 | |
" or a non-null binding."); |
167 | |
} |
168 | |
|
169 | 0 | return converter; |
170 | |
} |
171 | |
|
172 | |
} |