1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.core.convert;
17
18 import java.net.URL;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.apache.commons.beanutils.ConvertUtils;
23 import org.apache.commons.beanutils.Converter;
24 import org.apache.commons.beanutils.converters.ArrayConverter;
25
26 import de.smartics.properties.api.core.domain.PropertyDescriptor;
27 import de.smartics.properties.api.core.domain.PropertyValueConversionException;
28
29
30
31
32
33 public final class BeanUtilsPropertyValueConverter implements
34 PropertyValueConverter
35 {
36
37
38
39
40
41
42
43 private static final long serialVersionUID = 1L;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @Override
62 public Object convert(final PropertyDescriptor descriptor, final Object value)
63 throws PropertyValueConversionException
64 {
65 try
66 {
67 final Class<?> type = descriptor.getType().getType();
68
69 final Object convertedValue;
70 if (List.class.isAssignableFrom(type))
71 {
72 final ListPropertyHelper helper = new ListPropertyHelper(descriptor);
73 final Class<?> elementType = helper.getElementType();
74 ConverterUtils.ensureConverter(descriptor, elementType);
75 final Converter elementConverter = ConvertUtils.lookup(elementType);
76 final Class<?> arrayType = helper.getArrayTypeOfElementType();
77 final ArrayConverter converter =
78 new ArrayConverter(arrayType, elementConverter);
79 configure(converter, elementType);
80 final Object[] array = (Object[]) converter.convert(arrayType, value);
81 convertedValue = Arrays.asList(array);
82 }
83 else
84 {
85 ConverterUtils.ensureConverter(descriptor, type);
86 convertedValue = ConvertUtils.convert(value, type);
87 }
88
89 return convertedValue;
90 }
91 catch (final RuntimeException e)
92 {
93 throw new PropertyValueConversionException(e, descriptor, value);
94 }
95 }
96
97 private void configure(final ArrayConverter converter,
98 final Class<?> elementType)
99 {
100 if (URL.class == elementType)
101 {
102 converter.setAllowedChars(new char[] { '.', ':', '-', '_', '#', '+', '*',
103 '~', '?', '/', '!', '"', '$', '%',
104 '&', '(', ')', '[', ']', '{', '}',
105 '=', ',', ';', '@' });
106 converter.setDelimiter('|');
107 }
108
109 }
110
111
112
113 }