1
2
3
4
5 package org.rcfaces.core.internal.tools;
6
7 import java.io.Externalizable;
8 import java.io.ObjectInput;
9 import java.io.ObjectOutput;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.el.ValueExpression;
18 import javax.faces.FacesException;
19 import javax.faces.application.Application;
20 import javax.faces.component.EditableValueHolder;
21 import javax.faces.component.UICommand;
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.convert.ConverterException;
27
28 import org.rcfaces.core.internal.capability.IConvertValueHolder;
29 import org.rcfaces.core.internal.capability.ISubmittedExternalValue;
30 import org.rcfaces.core.internal.component.Properties;
31 import org.rcfaces.core.internal.util.Convertor;
32
33
34
35
36
37
38 public class ValuesTools extends CollectionTools {
39 private static final String REVISION = "$Revision: 1.17 $";
40
41 private static final String[] STRING_EMPTY_ARRAY = new String[0];
42
43 public static final Object NULL_VALUE = new NullValue();
44
45 private static final IValuesAccessor VALUES_PROVIDER_VALUES_ACCESSOR = new IValuesAccessor() {
46 private static final String REVISION = "$Revision: 1.17 $";
47
48 public int getCount(Object checkProvider) {
49 throw new UnsupportedOperationException("Not implemented !");
50 }
51
52 public Object getFirst(Object checkProvider, Object refValues) {
53 throw new UnsupportedOperationException("Not implemented !");
54 }
55
56 public Object getAdaptedValues(Object values) {
57 throw new UnsupportedOperationException("Not supported !");
58 }
59
60 public void setAdaptedValues(Object value, Object values) {
61 throw new UnsupportedOperationException("Not supported !");
62 }
63
64 public Object[] listValues(Object checkProvider, Object refValues) {
65 return convertToObjectArray(((ValueHolder) checkProvider)
66 .getValue());
67 }
68
69 public Object getComponentValues(UIComponent component) {
70 return ((ValueHolder) component).getValue();
71 }
72
73 public void setComponentValues(UIComponent component, Object values) {
74 ((ValueHolder) component).setValue(values);
75 }
76
77 public Class getComponentValuesType(FacesContext facesContext,
78 UIComponent component) {
79 Object value = getComponentValues(component);
80 if (value != null) {
81 return value.getClass();
82 }
83
84 ValueExpression valueBinding = component
85 .getValueExpression(Properties.VALUE);
86 if (valueBinding == null) {
87 return null;
88 }
89
90 return valueBinding.getType(facesContext.getELContext());
91 }
92 };
93
94 public static final List valueToList(Object value) {
95 if (value == null) {
96 return Collections.EMPTY_LIST;
97 }
98
99 if (value instanceof Object[]) {
100 Object array[] = (Object[]) value;
101 if (array.length < 1) {
102 return Collections.EMPTY_LIST;
103 }
104
105 return Arrays.asList(array);
106 }
107
108 if (value instanceof List) {
109 return (List) value;
110 }
111
112 if (value instanceof Collection) {
113 Collection col = (Collection) value;
114 if (col.isEmpty()) {
115 return Collections.EMPTY_LIST;
116 }
117
118 return new ArrayList(col);
119 }
120
121 return Collections.singletonList(value);
122 }
123
124 protected static final Object[] convertStringsToValues(
125 FacesContext context, UIComponent component, boolean testValue,
126 String values[]) throws ConverterException {
127
128 if (values == null || values.length < 1) {
129 return values;
130 }
131
132 Converter converter = null;
133
134 if (component instanceof IConvertValueHolder) {
135 converter = ((IConvertValueHolder) component).getConverter();
136
137 } else if (component instanceof ValueHolder) {
138 converter = ((ValueHolder) component).getConverter();
139 }
140
141 if (converter == null && testValue) {
142 ValueExpression valueBinding = component
143 .getValueExpression("value");
144
145 if (valueBinding == null) {
146 return values;
147 }
148
149 if (valueBinding.getValue(context.getELContext()) != null) {
150
151 Class converterType = valueBinding.getType(context.getELContext());
152 if (converterType == null || converterType == String.class
153 || converterType == Object.class) {
154 return values;
155 }
156
157 try {
158 Application application = context.getApplication();
159
160 converter = application.createConverter(converterType);
161
162 } catch (Throwable th) {
163 throw new FacesException(
164 "Can not create converter for type '"
165 + converterType + "'.", th);
166 }
167 }
168 }
169
170 if (converter != null) {
171 Object converted[] = new Object[values.length];
172
173 for (int i = 0; i < converted.length; i++) {
174 converted[i] = converter.getAsObject(context, component,
175 values[i]);
176 }
177
178 return converted;
179 }
180
181
182
183 return values;
184 }
185
186 public static Object convertStringToValue(FacesContext context,
187 UIComponent component, Object value, boolean testValue) {
188
189 if ((value instanceof String) == false) {
190 return value;
191 }
192
193 Converter converter = getConverter(component);
194
195 return convertStringToValue(context, component, converter, value,
196 "value", testValue);
197 }
198
199 public static Converter getConverter(UIComponent component) {
200
201 if (component instanceof IConvertValueHolder) {
202 return ((IConvertValueHolder) component).getConverter();
203 }
204
205 if (component instanceof ValueHolder) {
206 return ((ValueHolder) component).getConverter();
207 }
208
209 return null;
210 }
211
212 public static Object convertStringToValue(FacesContext context,
213 UIComponent component, Converter converter, Object value,
214 String attributeName, boolean testValue) {
215
216 if ((value instanceof String) == false) {
217 return value;
218 }
219
220 if (converter == null && attributeName != null && testValue) {
221 ValueExpression valueBinding = component
222 .getValueExpression(attributeName);
223
224 if (valueBinding != null) {
225
226
227
228 Class converterType = valueBinding.getType(context.getELContext());
229 if (converterType == null || converterType == String.class
230 || converterType == Object.class) {
231 return value;
232 }
233
234 try {
235 Application application = context.getApplication();
236
237 converter = application.createConverter(converterType);
238
239 } catch (Exception e) {
240 throw new FacesException(
241 "Can not create converter for type '"
242 + converterType + "'.", e);
243 }
244 }
245 }
246
247
248 if (converter != null) {
249 return converter.getAsObject(context, component, (String) value);
250 }
251
252 return value;
253
254 }
255
256 public static String[] convertValuesToString(Object values[],
257 UIComponent component, FacesContext facesContext) {
258
259 if (values instanceof String[]) {
260 return (String[]) values;
261 }
262 if (values.length == 0) {
263 return STRING_EMPTY_ARRAY;
264 }
265
266 Converter converter = null;
267 if (component instanceof IConvertValueHolder) {
268 converter = ((IConvertValueHolder) component).getConverter();
269
270 } else if (component instanceof ValueHolder) {
271 converter = ((ValueHolder) component).getConverter();
272 }
273
274 if (converter == null && values != null) {
275 if (facesContext == null) {
276 facesContext = FacesContext.getCurrentInstance();
277 }
278
279 Application application = facesContext.getApplication();
280
281 converter = application.createConverter(values[0].getClass());
282 }
283
284 if (converter != null) {
285 if (facesContext == null) {
286 facesContext = FacesContext.getCurrentInstance();
287 }
288
289 String ret[] = new String[values.length];
290
291 for (int i = 0; i < ret.length; i++) {
292 ret[i] = converter.getAsString(facesContext, component,
293 values[i]);
294 }
295
296 return ret;
297 }
298
299 String ret[] = new String[values.length];
300
301 for (int i = 0; i < ret.length; i++) {
302 ret[i] = (String) Convertor.convert(values[i], String.class);
303 }
304
305 return ret;
306 }
307
308 public static String convertValueToString(Object value,
309 UIComponent component, FacesContext facesContext) {
310
311
312
313
314
315 Converter converter = null;
316 if (component instanceof IConvertValueHolder) {
317 converter = ((IConvertValueHolder) component).getConverter();
318
319 } else if (component instanceof ValueHolder) {
320 converter = ((ValueHolder) component).getConverter();
321 }
322
323 return convertValueToString(value, converter, component, facesContext);
324 }
325
326 public static String convertValueToString(Object value,
327 Converter converter, UIComponent component,
328 FacesContext facesContext) {
329
330 if (converter == null
331 && (value != null && (value instanceof String) == false)) {
332 if (facesContext == null) {
333 facesContext = FacesContext.getCurrentInstance();
334 }
335
336 Application application = facesContext.getApplication();
337
338 converter = application.createConverter(value.getClass());
339 }
340
341 if (converter != null) {
342 if (facesContext == null) {
343 facesContext = FacesContext.getCurrentInstance();
344 }
345
346 return converter.getAsString(facesContext, component, value);
347 }
348
349 if (value instanceof String) {
350 return (String) value;
351 }
352
353 return (String) Convertor.convert(value, String.class);
354 }
355
356 public static final boolean valueToBool(ValueHolder valueHolder) {
357 Boolean b = valueToBoolean(valueHolder);
358
359 if (b == null) {
360 return false;
361 }
362
363 return b.booleanValue();
364 }
365
366 public static final Boolean valueToBoolean(ValueHolder valueHolder) {
367 if (valueHolder instanceof ISubmittedExternalValue) {
368 ISubmittedExternalValue submittedExternalValue = (ISubmittedExternalValue) valueHolder;
369
370 if (submittedExternalValue.isSubmittedValueSetted()) {
371 Object submittedValue = ((ISubmittedExternalValue) valueHolder)
372 .getSubmittedExternalValue();
373
374 return (Boolean) submittedValue;
375 }
376
377 } else if (valueHolder instanceof EditableValueHolder) {
378 Object submittedValue = ((EditableValueHolder) valueHolder)
379 .getSubmittedValue();
380 if (submittedValue != null) {
381 return (Boolean) submittedValue;
382 }
383 }
384
385 Object value = valueHolder.getValue();
386
387 if (value == null) {
388 return null;
389 }
390
391 if (value instanceof Boolean) {
392 return (Boolean) value;
393 }
394
395 return (Boolean) Convertor.convert(value, Boolean.class);
396 }
397
398 public static final int valueToInt(ValueHolder valueHolder) {
399 if (valueHolder instanceof ISubmittedExternalValue) {
400 ISubmittedExternalValue submittedExternalValue = (ISubmittedExternalValue) valueHolder;
401
402 if (submittedExternalValue.isSubmittedValueSetted()) {
403 Object submittedValue = ((ISubmittedExternalValue) valueHolder)
404 .getSubmittedExternalValue();
405 if (submittedValue != null) {
406 return ((Integer) submittedValue).intValue();
407 }
408
409 return 0;
410 }
411
412 } else if (valueHolder instanceof EditableValueHolder) {
413 Object submittedValue = ((EditableValueHolder) valueHolder)
414 .getSubmittedValue();
415 if (submittedValue != null) {
416 return ((Integer) submittedValue).intValue();
417 }
418 }
419
420 Object value = valueHolder.getValue();
421
422 if (value == null) {
423 return 0;
424 }
425
426 if (value instanceof Integer) {
427 return ((Integer) value).intValue();
428 }
429
430 Integer i = (Integer) Convertor.convert(value, Integer.TYPE);
431 if (i == null) {
432 return 0;
433 }
434
435 return i.intValue();
436 }
437
438 public static String valueToString(ValueHolder valueHolder,
439 FacesContext facesContext) {
440
441 if (valueHolder instanceof ISubmittedExternalValue) {
442 ISubmittedExternalValue submittedExternalValue = (ISubmittedExternalValue) valueHolder;
443
444 if (submittedExternalValue.isSubmittedValueSetted()) {
445 Object submittedValue = ((ISubmittedExternalValue) valueHolder)
446 .getSubmittedExternalValue();
447 return ValuesTools.valueToString(submittedValue,
448 (UIComponent) valueHolder, facesContext);
449 }
450
451 } else if (valueHolder instanceof EditableValueHolder) {
452 Object submittedValue = ((EditableValueHolder) valueHolder)
453 .getSubmittedValue();
454 if (submittedValue != null) {
455 return ValuesTools.valueToString(submittedValue,
456 (UIComponent) valueHolder, facesContext);
457 }
458 }
459
460 return ValuesTools.valueToString(valueHolder.getValue(),
461 (UIComponent) valueHolder, facesContext);
462 }
463
464 public static String valueToString(UICommand valueHolder,
465 FacesContext facesContext) {
466 return ValuesTools.valueToString(valueHolder.getValue(), valueHolder,
467 facesContext);
468 }
469
470 public static String valueToString(Object value, UIComponent component,
471 FacesContext facesContext) {
472
473
474
475
476
477 Converter converter = null;
478 if (component instanceof IConvertValueHolder) {
479 converter = ((IConvertValueHolder) component).getConverter();
480
481 } else if (component instanceof ValueHolder) {
482 converter = ((ValueHolder) component).getConverter();
483 }
484
485 return valueToString(value, converter, component, facesContext);
486 }
487
488 public static String valueToString(Object value, Converter converter,
489 UIComponent component, FacesContext facesContext) {
490
491 if (converter == null && value != null
492 && (value instanceof String) == false) {
493 if (facesContext == null) {
494 facesContext = FacesContext.getCurrentInstance();
495 }
496
497 Application application = facesContext.getApplication();
498
499 converter = application.createConverter(value.getClass());
500 }
501
502 if (converter != null) {
503 if (facesContext == null) {
504 facesContext = FacesContext.getCurrentInstance();
505 }
506
507 return converter.getAsString(facesContext, component, value);
508 }
509
510 if (value instanceof String) {
511 return (String) value;
512 }
513
514 return (String) Convertor.convert(value, String.class);
515 }
516
517 public static Object getValue(UIComponent component) {
518 if (component instanceof ISubmittedExternalValue) {
519 ISubmittedExternalValue submittedExternalValue = (ISubmittedExternalValue) component;
520
521 if (submittedExternalValue.isSubmittedValueSetted()) {
522 return ((ISubmittedExternalValue) component)
523 .getSubmittedExternalValue();
524 }
525
526 } else if (component instanceof EditableValueHolder) {
527 Object submittedValue = ((EditableValueHolder) component)
528 .getSubmittedValue();
529 if (submittedValue != null) {
530 return submittedValue;
531 }
532 }
533
534 if (component instanceof ValueHolder) {
535 return ((ValueHolder) component).getValue();
536 }
537
538 throw new FacesException("Component '" + component.getId()
539 + "' does not contain value !");
540 }
541
542 public static void setValue(UIComponent component, Object value) {
543 if (component instanceof ISubmittedExternalValue) {
544 ISubmittedExternalValue submittedExternalValue = (ISubmittedExternalValue) component;
545
546 submittedExternalValue.setSubmittedExternalValue(value);
547 return;
548 }
549
550 if (component instanceof EditableValueHolder) {
551 ((EditableValueHolder) component).setSubmittedValue(value);
552 return;
553 }
554
555 if (component instanceof ValueHolder) {
556 ((ValueHolder) component).setValue(value);
557 return;
558 }
559
560 throw new FacesException("Component '" + component.getId()
561 + "' does not support value !");
562 }
563
564 public static void setValues(FacesContext facesContext,
565 UIComponent component, Set values) {
566
567 setValues(component, VALUES_PROVIDER_VALUES_ACCESSOR, values);
568 }
569
570 public static Set valuesToSet(FacesContext facesContext,
571 UIComponent component, boolean immutable) {
572 return valuesToSet(component, VALUES_PROVIDER_VALUES_ACCESSOR,
573 immutable);
574 }
575
576
577
578
579
580
581 public static class NullValue implements Externalizable {
582 private static final String REVISION = "$Revision: 1.17 $";
583
584 private static final long serialVersionUID = -2586084278685773691L;
585
586 public void readExternal(ObjectInput in) {
587 }
588
589 public void writeExternal(ObjectOutput out) {
590 }
591
592 public boolean equals(Object obj) {
593 return (obj instanceof NullValue);
594 }
595
596 public int hashCode() {
597 return 0;
598 }
599
600 }
601
602 }