1 | package org.springframework.batch.item.file.transform; |
2 | |
3 | import java.text.DateFormat; |
4 | import java.text.NumberFormat; |
5 | |
6 | /** |
7 | * Default implementation of {@link FieldSetFactory} with no special knowledge |
8 | * of the {@link FieldSet} required. Returns a {@link DefaultFieldSet} from both |
9 | * factory methods. |
10 | * |
11 | * @author Dave Syer |
12 | * |
13 | */ |
14 | public class DefaultFieldSetFactory implements FieldSetFactory { |
15 | |
16 | private DateFormat dateFormat; |
17 | |
18 | private NumberFormat numberFormat; |
19 | |
20 | /** |
21 | * The {@link NumberFormat} to use for parsing numbers. If unset the default |
22 | * locale will be used. |
23 | * @param numberFormat the {@link NumberFormat} to use for number parsing |
24 | */ |
25 | public void setNumberFormat(NumberFormat numberFormat) { |
26 | this.numberFormat = numberFormat; |
27 | } |
28 | |
29 | /** |
30 | * The {@link DateFormat} to use for parsing numbers. If unset the default |
31 | * pattern is ISO standard <code>yyyy/MM/dd</code>. |
32 | * @param dateFormat the {@link DateFormat} to use for date parsing |
33 | */ |
34 | public void setDateFormat(DateFormat dateFormat) { |
35 | this.dateFormat = dateFormat; |
36 | } |
37 | |
38 | /** |
39 | * {@inheritDoc} |
40 | */ |
41 | @Override |
42 | public FieldSet create(String[] values, String[] names) { |
43 | DefaultFieldSet fieldSet = new DefaultFieldSet(values, names); |
44 | return enhance(fieldSet); |
45 | } |
46 | |
47 | /** |
48 | * {@inheritDoc} |
49 | */ |
50 | @Override |
51 | public FieldSet create(String[] values) { |
52 | DefaultFieldSet fieldSet = new DefaultFieldSet(values); |
53 | return enhance(fieldSet); |
54 | } |
55 | |
56 | private FieldSet enhance(DefaultFieldSet fieldSet) { |
57 | if (dateFormat!=null) { |
58 | fieldSet.setDateFormat(dateFormat); |
59 | } |
60 | if (numberFormat!=null) { |
61 | fieldSet.setNumberFormat(numberFormat); |
62 | } |
63 | return fieldSet; |
64 | } |
65 | |
66 | } |