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 | 0 | public DelegateConverter(){ |
54 | |
|
55 | 0 | } |
56 | |
|
57 | |
public DelegateConverter(ValueExpression id, ValueExpression binding, String converterIdString) |
58 | |
{ |
59 | 0 | super(); |
60 | 0 | _converterId = id; |
61 | 0 | _binding = binding; |
62 | 0 | _converterIdString = converterIdString; |
63 | 0 | } |
64 | |
|
65 | |
public boolean isTransient() |
66 | |
{ |
67 | 0 | return false; |
68 | |
} |
69 | |
|
70 | |
public void restoreState(FacesContext facesContext, Object state) |
71 | |
{ |
72 | 0 | Object[] values = (Object[]) state; |
73 | 0 | _converterId = (ValueExpression) values[0]; |
74 | 0 | _binding = (ValueExpression) values[1]; |
75 | 0 | _converterIdString = (String) values[2]; |
76 | 0 | } |
77 | |
|
78 | |
public Object saveState(FacesContext facesContext) |
79 | |
{ |
80 | 0 | Object[] values = new Object[3]; |
81 | 0 | values[0] = _converterId; |
82 | 0 | values[1] = _binding; |
83 | 0 | values[2] = _converterIdString; |
84 | 0 | return values; |
85 | |
} |
86 | |
|
87 | |
public void setTransient(boolean arg0) |
88 | |
{ |
89 | |
|
90 | 0 | } |
91 | |
|
92 | |
public Object getAsObject(FacesContext facesContext, UIComponent component, |
93 | |
String value) |
94 | |
{ |
95 | 0 | return _getDelegate().getAsObject(facesContext, component, value); |
96 | |
} |
97 | |
|
98 | |
public String getAsString(FacesContext facesContext, UIComponent component, |
99 | |
Object value) |
100 | |
{ |
101 | 0 | return _getDelegate().getAsString(facesContext, component, value); |
102 | |
} |
103 | |
|
104 | |
private Converter _getDelegate() |
105 | |
{ |
106 | 0 | return _createConverter(); |
107 | |
} |
108 | |
|
109 | |
private Converter _createConverter() |
110 | |
{ |
111 | 0 | Converter converter = null; |
112 | |
|
113 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
114 | 0 | ELContext elContext = facesContext.getELContext(); |
115 | |
|
116 | |
|
117 | |
|
118 | 0 | if (_binding != null) |
119 | |
{ |
120 | |
try |
121 | |
{ |
122 | 0 | converter = (Converter) _binding.getValue(elContext); |
123 | |
|
124 | 0 | if (converter != null) |
125 | |
{ |
126 | 0 | return converter; |
127 | |
} |
128 | |
} |
129 | 0 | catch (Exception e) |
130 | |
{ |
131 | 0 | throw new ConverterException("Exception creating converter using binding", e); |
132 | 0 | } |
133 | |
} |
134 | |
|
135 | 0 | if ((_converterId != null) || (_converterIdString != null)) |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | if (null != _converterIdString) |
140 | |
{ |
141 | 0 | converter = facesContext.getApplication().createConverter(_converterIdString); |
142 | |
} else |
143 | |
{ |
144 | 0 | String converterId = (String) _converterId.getValue(elContext); |
145 | 0 | converter = facesContext.getApplication().createConverter(converterId); |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | 0 | if (converter != null && _binding != null) |
151 | |
{ |
152 | 0 | _binding.setValue(elContext, converter); |
153 | |
} |
154 | |
} |
155 | 0 | catch (Exception e) |
156 | |
{ |
157 | 0 | throw new ConverterException("Exception creating converter with converterId: " + _converterId, e); |
158 | 0 | } |
159 | |
} |
160 | |
|
161 | 0 | return converter; |
162 | |
} |
163 | |
|
164 | |
} |