1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.component; |
20 | |
|
21 | |
import javax.el.ValueExpression; |
22 | |
import javax.faces.FacesException; |
23 | |
import javax.faces.context.FacesContext; |
24 | |
import javax.faces.convert.Converter; |
25 | |
import javax.faces.convert.ConverterException; |
26 | |
import java.lang.reflect.Array; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.List; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | class _SharedRendererUtils |
39 | |
{ |
40 | |
static Converter findUIOutputConverter(FacesContext facesContext, UIOutput component) |
41 | |
{ |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | Converter converter = component.getConverter(); |
47 | 0 | if (converter != null) return converter; |
48 | |
|
49 | |
|
50 | 0 | ValueExpression expression = component.getValueExpression("value"); |
51 | 0 | if (expression == null) return null; |
52 | |
|
53 | 0 | Class valueType = expression.getType(facesContext.getELContext()); |
54 | 0 | if (valueType == null) return null; |
55 | |
|
56 | 0 | if (Object.class.equals(valueType)) return null; |
57 | |
|
58 | |
try |
59 | |
{ |
60 | 0 | return facesContext.getApplication().createConverter(valueType); |
61 | |
} |
62 | 0 | catch (FacesException e) |
63 | |
{ |
64 | 0 | log(facesContext, "No Converter for type " + valueType.getName() + " found", e); |
65 | 0 | return null; |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
static Object getConvertedUISelectManyValue(FacesContext facesContext, |
70 | |
UISelectMany component, |
71 | |
String[] submittedValue) |
72 | |
throws ConverterException |
73 | |
{ |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 0 | if (submittedValue == null) throw new NullPointerException("submittedValue"); |
79 | |
|
80 | 0 | ValueExpression expression = component.getValueExpression("value"); |
81 | 0 | Class valueType = null; |
82 | 0 | Class arrayComponentType = null; |
83 | 0 | if (expression != null) |
84 | |
{ |
85 | |
|
86 | |
|
87 | |
|
88 | 0 | Object value = expression.getValue(facesContext.getELContext()); |
89 | 0 | valueType = (value != null) ? value.getClass() : |
90 | |
expression.getType(facesContext.getELContext()) ; |
91 | |
|
92 | 0 | if (valueType != null && valueType.isArray()) |
93 | |
{ |
94 | 0 | arrayComponentType = valueType.getComponentType(); |
95 | |
} |
96 | |
} |
97 | |
|
98 | 0 | Converter converter = component.getConverter(); |
99 | 0 | if (converter == null) |
100 | |
{ |
101 | 0 | if (valueType == null) |
102 | |
{ |
103 | |
|
104 | |
|
105 | 0 | return submittedValue; |
106 | |
} |
107 | |
|
108 | 0 | if (List.class.isAssignableFrom(valueType)) |
109 | |
{ |
110 | |
|
111 | |
|
112 | |
|
113 | 0 | int len = submittedValue.length; |
114 | 0 | List lst = new ArrayList(len); |
115 | 0 | for (int i = 0; i < len; i++) |
116 | |
{ |
117 | 0 | lst.add(submittedValue[i]); |
118 | |
} |
119 | 0 | return lst; |
120 | |
} |
121 | |
|
122 | 0 | if (arrayComponentType == null) |
123 | |
{ |
124 | 0 | throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array"); |
125 | |
} |
126 | |
|
127 | 0 | if (Object.class.equals(arrayComponentType)) return submittedValue; |
128 | |
|
129 | 0 | converter = facesContext.getApplication().createConverter(arrayComponentType); |
130 | |
|
131 | 0 | if (converter == null) |
132 | |
{ |
133 | 0 | return submittedValue; |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | 0 | if (expression != null) |
140 | |
{ |
141 | 0 | valueType = expression.getType(facesContext.getELContext()); |
142 | 0 | if (valueType != null && valueType.isArray()) |
143 | |
{ |
144 | 0 | if (submittedValue.length > 0) |
145 | |
{ |
146 | 0 | arrayComponentType = converter.getAsObject(facesContext, component, submittedValue[0]).getClass(); |
147 | |
} |
148 | |
} |
149 | |
} |
150 | |
|
151 | 0 | if (valueType == null) |
152 | |
{ |
153 | |
|
154 | |
|
155 | 0 | int len = submittedValue.length; |
156 | 0 | Object [] convertedValues = (Object []) Array.newInstance( |
157 | |
arrayComponentType==null?Object.class:arrayComponentType,len); |
158 | 0 | for (int i = 0; i < len; i++) |
159 | |
{ |
160 | 0 | convertedValues[i] |
161 | |
= converter.getAsObject(facesContext, component, submittedValue[i]); |
162 | |
} |
163 | 0 | return convertedValues; |
164 | |
} |
165 | |
|
166 | 0 | if (List.class.isAssignableFrom(valueType)) |
167 | |
{ |
168 | |
|
169 | |
|
170 | |
|
171 | 0 | int len = submittedValue.length; |
172 | 0 | List lst = new ArrayList(len); |
173 | 0 | for (int i = 0; i < len; i++) |
174 | |
{ |
175 | 0 | lst.add(converter.getAsObject(facesContext, component, submittedValue[i])); |
176 | |
} |
177 | 0 | return lst; |
178 | |
} |
179 | |
|
180 | 0 | if (arrayComponentType == null) |
181 | |
{ |
182 | 0 | throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array"); |
183 | |
} |
184 | |
|
185 | 0 | if (arrayComponentType.isPrimitive()) |
186 | |
{ |
187 | |
|
188 | 0 | int len = submittedValue.length; |
189 | 0 | Object convertedValues = Array.newInstance(arrayComponentType, len); |
190 | 0 | for (int i = 0; i < len; i++) |
191 | |
{ |
192 | 0 | Array.set(convertedValues, i, |
193 | |
converter.getAsObject(facesContext, component, submittedValue[i])); |
194 | |
} |
195 | 0 | return convertedValues; |
196 | |
} |
197 | |
|
198 | |
|
199 | 0 | int len = submittedValue.length; |
200 | 0 | ArrayList convertedValues = new ArrayList(len); |
201 | 0 | for (int i = 0; i < len; i++) |
202 | |
{ |
203 | 0 | convertedValues.add(i, converter.getAsObject(facesContext, component, submittedValue[i])); |
204 | |
} |
205 | 0 | return convertedValues.toArray((Object[]) Array.newInstance(arrayComponentType, len)); |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
private static void log(FacesContext context, String msg, Exception e) |
212 | |
{ |
213 | 0 | context.getExternalContext().log(msg, e); |
214 | 0 | } |
215 | |
} |