1
2
3
4
5
6
7
8
9
10
11
12
13
14 package olg.csv.bean.impl;
15
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18
19 import olg.csv.base.Row;
20 import olg.csv.bean.IBeanProcessor;
21 import olg.csv.bean.IPropertyProcessor;
22 import olg.csv.bean.getter.AbstractGetter;
23 import olg.csv.bean.parser.AbstractParser;
24 import olg.csv.bean.parser.ParseException;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public abstract class AbstractPropertyProcessor<B> implements IPropertyProcessor<B> {
40
41
42
43
44 protected Class<B> beanClass = null;
45
46
47
48
49 protected Method method = null;
50
51
52
53
54
55
56 public final Class<B> getBeanClass() {
57 return beanClass;
58 }
59
60
61
62
63
64
65 public abstract B transform(Row line, B bean);
66
67
68
69
70
71
72
73
74
75
76
77 protected final B invoke(Object field, B bean) {
78 B retour = bean;
79
80 try {
81 if (retour == null) {
82 retour = beanClass.newInstance();
83 }
84
85 method.invoke(retour, new Object[]{field});
86
87 } catch (IllegalArgumentException e) {
88 throw new ParseException("Error on method invocation " + method.getName(), e);
89 } catch (IllegalAccessException e) {
90 throw new ParseException("Error on method invocation " + method.getName(), e);
91 } catch (InvocationTargetException e) {
92 throw new ParseException("Error on method invocation " + method.getName(), e);
93 } catch (InstantiationException e) {
94 throw new ParseException("Error on method invocation " + method.getName(), e);
95
96 }
97
98 return retour;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public static final <T, X> IPropertyProcessor<T> getSimplePropertyTransformer(AbstractGetter getter,
127 AbstractParser<X> parser, String propertyName, Class<T> clazz) {
128 if (parser == null) {
129 throw new IllegalArgumentException("getSimplePropertyTransformer: "
130 + "AbstractParser argument must be not null");
131 }
132 if (propertyName == null) {
133 throw new IllegalArgumentException("getSimplePropertyTransformer: propertyName argument must be not null");
134 }
135 if (clazz == null) {
136 throw new IllegalArgumentException("getSimplePropertyTransformer: Class argument must be not null");
137 }
138
139 if (getter == null) {
140 throw new IllegalArgumentException("getSimplePropertyTransformer: "
141 + "AbstractGetter argument must be not null");
142 }
143 return new PropertyProcessor<T>(getter, parser, propertyName, clazz);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public static final <T, P> IPropertyProcessor<T> getComplexPropertyTransformer(IBeanProcessor<P> transformer,
167 Class<T> beanClass, String propertyName) {
168 if (transformer == null) {
169 throw new IllegalArgumentException("Transformer argument must be not null");
170 }
171 if (beanClass == null) {
172 throw new IllegalArgumentException("Class argument must be not null");
173 }
174 if (propertyName == null) {
175 throw new IllegalArgumentException("propertyName argument must be not null");
176 }
177 return new ComplexPropertyProcessor<T>(transformer, beanClass, propertyName);
178 }
179
180 }