| 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 | 8 | private Map<Class<?>, PropertyEditor> editorMap = null; |
| 34 | |
|
| 35 | 8 | public CsvToBean() { |
| 36 | 8 | } |
| 37 | |
|
| 38 | |
public List<T> parse(MappingStrategy<T> mapper, Reader reader) { |
| 39 | 7 | return parse(mapper, new CSVReader(reader)); |
| 40 | |
} |
| 41 | |
|
| 42 | |
public List<T> parse(MappingStrategy<T> mapper, CSVReader csv) { |
| 43 | |
try { |
| 44 | 8 | mapper.captureHeader(csv); |
| 45 | |
String[] line; |
| 46 | 7 | List<T> list = new ArrayList<T>(); |
| 47 | 21 | while (null != (line = csv.readNext())) { |
| 48 | 14 | T obj = processLine(mapper, line); |
| 49 | 14 | list.add(obj); |
| 50 | 14 | } |
| 51 | 7 | return list; |
| 52 | 1 | } catch (Exception e) { |
| 53 | 1 | 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 | 14 | T bean = mapper.createBean(); |
| 59 | 60 | for (int col = 0; col < line.length; col++) { |
| 60 | 46 | PropertyDescriptor prop = mapper.findDescriptor(col); |
| 61 | 46 | if (null != prop) { |
| 62 | 46 | String value = checkForTrim(line[col], prop); |
| 63 | 46 | Object obj = convertValue(value, prop); |
| 64 | 46 | prop.getWriteMethod().invoke(bean, obj); |
| 65 | |
} |
| 66 | |
} |
| 67 | 14 | return bean; |
| 68 | |
} |
| 69 | |
|
| 70 | |
private String checkForTrim(String s, PropertyDescriptor prop) { |
| 71 | 46 | return trimmableProperty(prop) ? s.trim() : s; |
| 72 | |
} |
| 73 | |
|
| 74 | |
private boolean trimmableProperty(PropertyDescriptor prop) { |
| 75 | 46 | return !prop.getPropertyType().getName().contains("String"); |
| 76 | |
} |
| 77 | |
|
| 78 | |
protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException, IllegalAccessException { |
| 79 | 46 | PropertyEditor editor = getPropertyEditor(prop); |
| 80 | 46 | Object obj = value; |
| 81 | 46 | if (null != editor) { |
| 82 | 46 | editor.setAsText(value); |
| 83 | 46 | obj = editor.getValue(); |
| 84 | |
} |
| 85 | 46 | return obj; |
| 86 | |
} |
| 87 | |
|
| 88 | |
private PropertyEditor getPropertyEditorValue(Class<?> cls) { |
| 89 | 46 | if (editorMap == null) { |
| 90 | 7 | editorMap = new HashMap<Class<?>, PropertyEditor>(); |
| 91 | |
} |
| 92 | |
|
| 93 | 46 | PropertyEditor editor = editorMap.get(cls); |
| 94 | |
|
| 95 | 46 | if (editor == null) { |
| 96 | 11 | editor = PropertyEditorManager.findEditor(cls); |
| 97 | 11 | addEditorToMap(cls, editor); |
| 98 | |
} |
| 99 | |
|
| 100 | 46 | return editor; |
| 101 | |
} |
| 102 | |
|
| 103 | |
private void addEditorToMap(Class<?> cls, PropertyEditor editor) { |
| 104 | 11 | if (editor != null) { |
| 105 | 11 | editorMap.put(cls, editor); |
| 106 | |
} |
| 107 | 11 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws InstantiationException, IllegalAccessException { |
| 114 | 46 | Class<?> cls = desc.getPropertyEditorClass(); |
| 115 | 46 | if (null != cls) return (PropertyEditor) cls.newInstance(); |
| 116 | 46 | return getPropertyEditorValue(desc.getPropertyType()); |
| 117 | |
} |
| 118 | |
|
| 119 | |
} |