1 | |
package au.com.bytecode.opencsv.bean; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import au.com.bytecode.opencsv.CSVReader; |
20 | |
|
21 | |
import java.beans.IntrospectionException; |
22 | |
import java.beans.PropertyDescriptor; |
23 | |
import java.beans.PropertyEditor; |
24 | |
import java.beans.PropertyEditorManager; |
25 | |
import java.io.Reader; |
26 | |
import java.lang.reflect.InvocationTargetException; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.HashMap; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
public class CsvToBean<T> { |
33 | 0 | private Map <Class<?>, PropertyEditor> editorMap = null; |
34 | 0 | public CsvToBean() { |
35 | 0 | } |
36 | |
|
37 | |
public List<T> parse(MappingStrategy<T> mapper, Reader reader) { |
38 | |
|
39 | 0 | return parse(mapper, new CSVReader(reader)); |
40 | |
} |
41 | |
|
42 | |
public List<T> parse(MappingStrategy<T> mapper, CSVReader csv) { |
43 | |
try { |
44 | 0 | mapper.captureHeader(csv); |
45 | |
String[] line; |
46 | 0 | List<T> list = new ArrayList<T>(); |
47 | 0 | while(null != (line = csv.readNext())) { |
48 | 0 | T obj = processLine(mapper, line); |
49 | 0 | list.add(obj); |
50 | 0 | } |
51 | 0 | return list; |
52 | 0 | } catch (Exception e) { |
53 | 0 | throw new RuntimeException("Error parsing CSV!", e); |
54 | |
} |
55 | |
} |
56 | |
|
57 | |
protected T processLine(MappingStrategy<T> mapper, String[] line) throws IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException { |
58 | 0 | T bean = mapper.createBean(); |
59 | 0 | for(int col = 0; col < line.length; col++) { |
60 | 0 | String value = line[col]; |
61 | 0 | PropertyDescriptor prop = mapper.findDescriptor(col); |
62 | 0 | if (null != prop) { |
63 | 0 | Object obj = convertValue(value, prop); |
64 | 0 | prop.getWriteMethod().invoke(bean, obj); |
65 | |
} |
66 | |
} |
67 | 0 | return bean; |
68 | |
} |
69 | |
|
70 | |
protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException, IllegalAccessException { |
71 | 0 | PropertyEditor editor = getPropertyEditor(prop); |
72 | 0 | Object obj = value; |
73 | 0 | if (null != editor) { |
74 | 0 | editor.setAsText(value.trim()); |
75 | 0 | obj = editor.getValue(); |
76 | |
} |
77 | 0 | return obj; |
78 | |
} |
79 | |
|
80 | |
private PropertyEditor getPropertyEditorValue(Class<?> cls) |
81 | |
{ |
82 | 0 | if (editorMap == null) |
83 | |
{ |
84 | 0 | editorMap = new HashMap<Class<?>, PropertyEditor>(); |
85 | |
} |
86 | |
|
87 | 0 | PropertyEditor editor = editorMap.get(cls); |
88 | |
|
89 | 0 | if (editor == null) |
90 | |
{ |
91 | 0 | editor = PropertyEditorManager.findEditor(cls); |
92 | 0 | addEditorToMap(cls, editor); |
93 | |
} |
94 | |
|
95 | 0 | return editor; |
96 | |
} |
97 | |
|
98 | |
private void addEditorToMap(Class<?> cls, PropertyEditor editor) |
99 | |
{ |
100 | 0 | if (editor != null) |
101 | |
{ |
102 | 0 | editorMap.put(cls, editor); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws InstantiationException, IllegalAccessException { |
111 | 0 | Class<?> cls = desc.getPropertyEditorClass(); |
112 | 0 | if (null != cls) return (PropertyEditor) cls.newInstance(); |
113 | 0 | return getPropertyEditorValue(desc.getPropertyType()); |
114 | |
} |
115 | |
|
116 | |
} |