1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
package org.asyrinx.brownie.core.csv;
|
8
|
|
|
9
|
|
import java.io.IOException;
|
10
|
|
import java.io.InputStream;
|
11
|
|
import java.lang.reflect.InvocationTargetException;
|
12
|
|
import java.text.DateFormat;
|
13
|
|
import java.text.ParseException;
|
14
|
|
import java.util.Date;
|
15
|
|
import java.util.HashMap;
|
16
|
|
import java.util.Iterator;
|
17
|
|
import java.util.List;
|
18
|
|
import java.util.Map;
|
19
|
|
import java.util.Properties;
|
20
|
|
import java.util.Set;
|
21
|
|
|
22
|
|
import org.apache.commons.beanutils.BeanUtils;
|
23
|
|
import org.apache.commons.beanutils.PropertyUtils;
|
24
|
|
import org.apache.commons.lang.exception.NestableRuntimeException;
|
25
|
|
import org.asyrinx.brownie.core.io.sf.StreamFactory;
|
26
|
|
import org.asyrinx.brownie.core.io.sf.StreamFactoryFacade;
|
27
|
|
import org.asyrinx.brownie.core.lang.ClassUtils;
|
28
|
|
import org.asyrinx.brownie.core.lang.NumberUtils;
|
29
|
|
import org.asyrinx.brownie.core.lang.enum.ValuedEnum;
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
public class PropertySVBeanMapper implements SVBeanMapper {
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
2
|
public PropertySVBeanMapper(String mappedClassName, Properties properties) {
|
40
|
2
|
super();
|
41
|
2
|
this.properties = properties;
|
42
|
2
|
this.mappedClassName = mappedClassName;
|
43
|
|
}
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
2
|
public PropertySVBeanMapper(String mappedClassName, String propertyContext)
|
49
|
|
throws IOException {
|
50
|
2
|
this(mappedClassName, loadProperties(propertyContext));
|
51
|
|
}
|
52
|
|
|
53
|
2
|
private static Properties loadProperties(String propertyContext)
|
54
|
|
throws IOException {
|
55
|
2
|
final StreamFactory streamFactory = StreamFactoryFacade.newFacade();
|
56
|
2
|
final InputStream inputStream = streamFactory.newInput(propertyContext);
|
57
|
2
|
final Properties properties = new Properties();
|
58
|
2
|
properties.load(inputStream);
|
59
|
2
|
return properties;
|
60
|
|
}
|
61
|
|
|
62
|
|
protected final Properties properties;
|
63
|
|
|
64
|
|
protected final String mappedClassName;
|
65
|
|
|
66
|
|
private DateFormat dateFormat = DateFormat.getDateInstance();
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
3
|
public Object loadBean(List values) {
|
74
|
3
|
final Object bean = getBean();
|
75
|
3
|
loadPropertyValues(values, bean);
|
76
|
3
|
return bean;
|
77
|
|
}
|
78
|
|
|
79
|
3
|
protected void loadPropertyValues(List values, final Object bean) {
|
80
|
3
|
final Set keys = properties.keySet();
|
81
|
3
|
final Map valueMap = new HashMap();
|
82
|
3
|
try {
|
83
|
3
|
for (Iterator iter = keys.iterator(); iter.hasNext();) {
|
84
|
18
|
final Object key = iter.next();
|
85
|
18
|
final String propName = String.valueOf(properties.get(key));
|
86
|
18
|
final int keyIndex = NumberUtils.toInt(key, -1);
|
87
|
18
|
if ((keyIndex < 0) || (keyIndex > values.size() - 1))
|
88
|
0
|
throw new NestableRuntimeException("不正なkeyです: " + key);
|
89
|
18
|
Object value = values.get(keyIndex);
|
90
|
18
|
value = loadPropertyObject(bean, propName, value);
|
91
|
18
|
valueMap.put(propName, value);
|
92
|
|
}
|
93
|
3
|
BeanUtils.populate(bean, valueMap);
|
94
|
|
} catch (Exception e) {
|
95
|
0
|
throw new NestableRuntimeException("bean: '" + bean
|
96
|
|
+ "', valueMap: " + valueMap, e);
|
97
|
|
}
|
98
|
|
}
|
99
|
|
|
100
|
18
|
protected Object loadPropertyObject(final Object bean,
|
101
|
|
final String propName, Object value) throws IllegalAccessException,
|
102
|
|
InvocationTargetException, NoSuchMethodException, ParseException,
|
103
|
|
ClassNotFoundException {
|
104
|
18
|
final Class propType = PropertyUtils.getPropertyType(bean, propName);
|
105
|
18
|
if (Date.class.isAssignableFrom(propType)) {
|
106
|
3
|
value = dateFormat.parse(String.valueOf(value));
|
107
|
15
|
} else if (ValuedEnum.class.isAssignableFrom(propType)) {
|
108
|
|
|
109
|
3
|
Class.forName(propType.getName());
|
110
|
3
|
value = ValuedEnum.getEnumByName(propType, String.valueOf(value));
|
111
|
|
}
|
112
|
18
|
return value;
|
113
|
|
}
|
114
|
|
|
115
|
3
|
protected Object getBean() {
|
116
|
3
|
try {
|
117
|
3
|
return ClassUtils.newObject(mappedClassName, null);
|
118
|
|
} catch (InstantiationException e) {
|
119
|
0
|
throw new NestableRuntimeException(e);
|
120
|
|
}
|
121
|
|
}
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
0
|
public DateFormat getDateFormat() {
|
127
|
0
|
return dateFormat;
|
128
|
|
}
|
129
|
|
|
130
|
|
|
131
|
|
|
132
|
|
|
133
|
0
|
public void setDateFormat(DateFormat format) {
|
134
|
0
|
dateFormat = format;
|
135
|
|
}
|
136
|
|
|
137
|
|
}
|