1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.io.dozer;
17
18 import static org.dozer.loader.api.FieldsMappingOptions.hintB;
19 import static org.dozer.loader.api.TypeMappingOptions.mapNull;
20 import static org.dozer.loader.api.TypeMappingOptions.oneWay;
21 import static org.dozer.loader.api.TypeMappingOptions.wildcard;
22
23 import java.io.IOException;
24 import java.io.Reader;
25
26 import org.dozer.DozerBeanMapper;
27 import org.dozer.loader.api.BeanMappingBuilder;
28 import org.dozer.loader.api.TypeMappingBuilder;
29 import org.supercsv.cellprocessor.ift.CellProcessor;
30 import org.supercsv.io.AbstractCsvReader;
31 import org.supercsv.io.CsvBeanReader;
32 import org.supercsv.io.ITokenizer;
33 import org.supercsv.prefs.CsvPreference;
34
35
36
37
38
39
40
41 public class CsvDozerBeanReader extends AbstractCsvReader implements ICsvDozerBeanReader {
42
43 private final DozerBeanMapper dozerBeanMapper;
44
45
46 private final CsvDozerBeanData beanData = new CsvDozerBeanData();
47
48
49
50
51
52
53
54
55
56
57
58
59 public CsvDozerBeanReader(final Reader reader, final CsvPreference preferences) {
60 super(reader, preferences);
61 this.dozerBeanMapper = new DozerBeanMapper();
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public CsvDozerBeanReader(final ITokenizer tokenizer, final CsvPreference preferences) {
77 super(tokenizer, preferences);
78 this.dozerBeanMapper = new DozerBeanMapper();
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public CsvDozerBeanReader(final Reader reader, final CsvPreference preferences,
95 final DozerBeanMapper dozerBeanMapper) {
96 super(reader, preferences);
97 if( dozerBeanMapper == null ) {
98 throw new NullPointerException("dozerBeanMapper should not be null");
99 }
100 this.dozerBeanMapper = dozerBeanMapper;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public CsvDozerBeanReader(final ITokenizer tokenizer, final CsvPreference preferences,
117 final DozerBeanMapper dozerBeanMapper) {
118 super(tokenizer, preferences);
119 if( dozerBeanMapper == null ) {
120 throw new NullPointerException("dozerBeanMapper should not be null");
121 }
122 this.dozerBeanMapper = dozerBeanMapper;
123 }
124
125
126
127
128 public void configureBeanMapping(final Class<?> clazz, final String[] fieldMapping) {
129 dozerBeanMapper.addMapping(new MappingBuilder(clazz, fieldMapping));
130 }
131
132
133
134
135 public void configureBeanMapping(final Class<?> clazz, final String[] fieldMapping, final Class<?>[] hintTypes) {
136 dozerBeanMapper.addMapping(new MappingBuilder(clazz, fieldMapping, hintTypes));
137 }
138
139
140
141
142 public <T> T read(final Class<T> clazz) throws IOException {
143 if( clazz == null ) {
144 throw new NullPointerException("clazz should not be null");
145 }
146
147 return readIntoBean(null, clazz, null);
148 }
149
150
151
152
153 public <T> T read(final Class<T> clazz, final CellProcessor... processors) throws IOException {
154 if( clazz == null ) {
155 throw new NullPointerException("clazz should not be null");
156 } else if( processors == null ) {
157 throw new NullPointerException("processors should not be null");
158 }
159
160 return readIntoBean(null, clazz, processors);
161 }
162
163
164
165
166 public <T> T read(final T bean) throws IOException {
167 if( bean == null ) {
168 throw new NullPointerException("bean should not be null");
169 }
170
171 return readIntoBean(bean, null, null);
172 }
173
174
175
176
177 public <T> T read(final T bean, final CellProcessor... processors) throws IOException {
178 if( bean == null ) {
179 throw new NullPointerException("bean should not be null");
180 } else if( processors == null ) {
181 throw new NullPointerException("processors should not be null");
182 }
183
184 return readIntoBean(bean, null, processors);
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 private <T> T readIntoBean(final T bean, final Class<T> clazz, final CellProcessor[] processors) throws IOException {
204 if( readRow() ) {
205 if( processors == null ) {
206
207 beanData.getColumns().clear();
208 beanData.getColumns().addAll(getColumns());
209 } else {
210
211 executeProcessors(beanData.getColumns(), processors);
212 }
213
214 if( bean != null ) {
215
216 dozerBeanMapper.map(beanData, bean);
217 return bean;
218 } else {
219
220 return dozerBeanMapper.map(beanData, clazz);
221 }
222
223 }
224
225 return null;
226 }
227
228
229
230
231 private static class MappingBuilder extends BeanMappingBuilder {
232
233 private final Class<?> clazz;
234 private final String[] fieldMapping;
235 private final Class<?>[] hintTypes;
236
237
238
239
240
241
242
243
244
245
246
247
248 public MappingBuilder(final Class<?> clazz, final String[] fieldMapping) {
249 if( clazz == null ) {
250 throw new NullPointerException("clazz should not be null");
251 } else if( fieldMapping == null ) {
252 throw new NullPointerException("fieldMapping should not be null");
253 }
254 this.clazz = clazz;
255 this.fieldMapping = fieldMapping;
256 this.hintTypes = null;
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 public MappingBuilder(final Class<?> clazz, final String[] fieldMapping, final Class<?>[] hintTypes) {
273 if( clazz == null ) {
274 throw new NullPointerException("clazz should not be null");
275 } else if( fieldMapping == null ) {
276 throw new NullPointerException("fieldMapping should not be null");
277 } else if( hintTypes == null ) {
278 throw new NullPointerException("fieldMapping should not be null");
279 } else if( fieldMapping.length != hintTypes.length ) {
280 throw new IllegalArgumentException(String.format(
281 "hintTypes length(%d) should match fieldMapping length(%d)", hintTypes.length, fieldMapping.length));
282 }
283 this.clazz = clazz;
284 this.fieldMapping = fieldMapping;
285 this.hintTypes = hintTypes;
286 }
287
288 @Override
289 protected void configure() {
290
291
292
293
294
295
296
297
298 final TypeMappingBuilder mappingBuilder = mapping(CsvDozerBeanData.class, clazz, oneWay(), wildcard(false),
299 mapNull(true));
300
301 for( int i = 0; i < fieldMapping.length; i++ ) {
302
303 final String mapping = fieldMapping[i];
304
305 if( mapping == null ) {
306 continue;
307 }
308
309 if( hintTypes != null && hintTypes[i] != null ) {
310
311 mappingBuilder.fields("columns[" + i + "]", mapping, hintB(hintTypes[i]));
312 } else {
313 mappingBuilder.fields("columns[" + i + "]", mapping);
314 }
315
316 }
317 }
318 }
319
320 }