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