1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.controller.impl; |
17 | |
|
18 | |
import java.lang.reflect.Array; |
19 | |
import java.util.Collection; |
20 | |
|
21 | |
import org.seasar.cubby.controller.FormWrapper; |
22 | |
import org.seasar.cubby.controller.FormWrapperFactory; |
23 | |
import org.seasar.cubby.converter.ConversionHelper; |
24 | |
import org.seasar.cubby.converter.Converter; |
25 | |
import org.seasar.cubby.converter.ConverterFactory; |
26 | |
import org.seasar.framework.beans.BeanDesc; |
27 | |
import org.seasar.framework.beans.PropertyDesc; |
28 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 90 | public class FormWrapperFactoryImpl implements FormWrapperFactory { |
37 | |
|
38 | |
|
39 | |
private ConverterFactory converterFactory; |
40 | |
|
41 | |
|
42 | |
private ConversionHelper conversionHelper; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public void setConverterFactory(final ConverterFactory converterFactory) { |
51 | 76 | this.converterFactory = converterFactory; |
52 | 76 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public void setConversionHelper(final ConversionHelper conversionHelper) { |
61 | 76 | this.conversionHelper = conversionHelper; |
62 | 76 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public FormWrapper create(final Object form) { |
68 | 8 | final FormWrapper formObject = new FormWrapperImpl(form); |
69 | 8 | return formObject; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 8 | private class FormWrapperImpl implements FormWrapper { |
79 | |
|
80 | |
|
81 | |
private final Object form; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 8 | private FormWrapperImpl(final Object form) { |
92 | 8 | this.form = form; |
93 | 8 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public String[] getValues(final String name) { |
99 | 12 | if (this.form == null) { |
100 | 0 | return null; |
101 | |
} |
102 | 12 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(this.form |
103 | |
.getClass()); |
104 | 12 | if (!beanDesc.hasPropertyDesc(name)) { |
105 | 2 | return null; |
106 | |
} |
107 | 10 | final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(name); |
108 | 10 | final Object value = propertyDesc.getValue(this.form); |
109 | 10 | if (value == null) { |
110 | 2 | return null; |
111 | 8 | } else if (value instanceof String[]) { |
112 | 0 | return (String[]) value; |
113 | |
} else { |
114 | 8 | if (value.getClass().isArray()) { |
115 | 2 | final int length = Array.getLength(value); |
116 | 2 | final String[] array = (String[]) Array.newInstance( |
117 | |
String.class, length); |
118 | 8 | for (int i = 0; i < length; i++) { |
119 | 6 | final Object element = Array.get(value, i); |
120 | 6 | final String converted = convert(element); |
121 | 6 | Array.set(array, i, converted); |
122 | |
} |
123 | 2 | return array; |
124 | 6 | } else if (value instanceof Collection) { |
125 | 2 | final Collection<?> collection = (Collection<?>) value; |
126 | 2 | final String[] array = (String[]) Array.newInstance( |
127 | |
String.class, collection.size()); |
128 | 2 | int i = 0; |
129 | 2 | for (final Object element : collection) { |
130 | 4 | final String converted = convert(element); |
131 | 4 | Array.set(array, i++, converted); |
132 | 4 | } |
133 | 2 | return array; |
134 | |
} else { |
135 | 4 | final String[] array = (String[]) Array.newInstance( |
136 | |
String.class, 1); |
137 | 4 | final String converted = convert(value); |
138 | 4 | Array.set(array, 0, converted); |
139 | 4 | return array; |
140 | |
} |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private String convert(final Object value) { |
152 | 14 | if (value == null) { |
153 | 5 | return null; |
154 | |
} |
155 | 9 | final Converter converter = converterFactory.getConverter(null, |
156 | |
value.getClass()); |
157 | 9 | if (converter == null) { |
158 | 4 | if (value == null) { |
159 | 0 | return null; |
160 | |
} else { |
161 | 4 | return value.toString(); |
162 | |
} |
163 | |
} else { |
164 | 5 | return converter.convertToString(value, conversionHelper); |
165 | |
} |
166 | |
} |
167 | |
|
168 | |
} |
169 | |
} |