1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.opencsv.bean;
17
18 import com.opencsv.ICSVParser;
19 import com.opencsv.exceptions.CsvDataTypeMismatchException;
20 import org.apache.commons.beanutils.BeanUtilsBean;
21 import org.apache.commons.beanutils.ConversionException;
22 import org.apache.commons.beanutils.ConvertUtilsBean;
23 import org.apache.commons.beanutils.locale.LocaleConvertUtilsBean;
24 import org.apache.commons.lang3.StringUtils;
25
26 import java.util.Locale;
27 import java.util.ResourceBundle;
28
29
30
31
32
33
34
35
36 public class ConverterPrimitiveTypes extends AbstractCsvConverter {
37
38
39
40
41
42
43
44
45
46 protected final ConvertUtilsBean readConverter;
47
48
49
50
51
52
53
54
55
56 protected final LocaleConvertUtilsBean readLocaleConverter;
57
58
59
60
61
62
63
64
65
66 protected final ConvertUtilsBean writeConverter;
67
68
69
70
71
72
73
74
75
76 protected final LocaleConvertUtilsBean writeLocaleConverter;
77
78
79
80
81
82
83
84
85
86 public ConverterPrimitiveTypes(Class<?> type, String locale, String writeLocale, Locale errorLocale) {
87 super(type, locale, writeLocale, errorLocale);
88 if(this.locale == null) {
89 readConverter = BeanUtilsBean.getInstance().getConvertUtils();
90 readConverter.register(true, false, 0);
91 readLocaleConverter = null;
92 }
93 else {
94 readLocaleConverter = new LocaleConvertUtilsBean();
95 readLocaleConverter.setDefaultLocale(this.locale);
96 readConverter = null;
97 }
98 if(this.writeLocale == null) {
99 writeConverter = BeanUtilsBean.getInstance().getConvertUtils();
100 writeConverter.register(true, false, 0);
101 writeLocaleConverter = null;
102 }
103 else {
104 writeLocaleConverter = new LocaleConvertUtilsBean();
105 writeLocaleConverter.setDefaultLocale(this.writeLocale);
106 writeConverter = null;
107 }
108 }
109
110 @Override
111 public Object convertToRead(String value)
112 throws CsvDataTypeMismatchException {
113 Object o = null;
114
115 if (StringUtils.isNotBlank(value) || (value != null && type.equals(String.class))) {
116 try {
117 if(readConverter != null) {
118 synchronized (readConverter) {
119 o = readConverter.convert(value, type);
120 }
121 }
122 else {
123 synchronized (readLocaleConverter) {
124 o = readLocaleConverter.convert(value, type);
125 }
126 }
127 } catch (ConversionException e) {
128 CsvDataTypeMismatchException csve = new CsvDataTypeMismatchException(
129 value, type, String.format(
130 ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("conversion.impossible"),
131 value, type.getCanonicalName()));
132 csve.initCause(e);
133 throw csve;
134 }
135 }
136 return o;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150 @Override
151 public String convertToWrite(Object value)
152 throws CsvDataTypeMismatchException {
153 String result = null;
154 if(value != null) {
155 try {
156 if(writeConverter != null) {
157 synchronized (writeConverter) {
158 result = writeConverter.convert(value);
159 }
160 }
161 else {
162 synchronized (writeLocaleConverter) {
163 result = writeLocaleConverter.convert(value);
164 }
165 }
166 }
167 catch(ConversionException e) {
168 CsvDataTypeMismatchException csve = new CsvDataTypeMismatchException(
169 ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("field.not.primitive"));
170 csve.initCause(e);
171 throw csve;
172 }
173 }
174 return result;
175 }
176 }