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