1
2
3
4
5
6
7
8
9
10
11
12
13
14 package olg.csv.bean;
15
16 import java.io.Closeable;
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.Iterator;
20
21 import olg.csv.base.IReader;
22 import olg.csv.base.IgnoreNullReader;
23 import olg.csv.base.ReaderException;
24 import olg.csv.base.Row;
25 import olg.csv.bean.loader.BeanProcessorLoader;
26 import olg.csv.bean.loader.LoadException;
27 import olg.csv.bean.parser.ParseException;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class BeanReader<E> implements Iterator<E>, Closeable {
46
47
48
49
50 private static final Logger LOGGER = LoggerFactory.getLogger(BeanReader.class);
51
52
53
54
55 private IReader baseReader = null;
56
57
58
59
60 private IBeanProcessor<E> beanProcessor = null;
61
62
63
64
65 public void close() {
66 try {
67 baseReader.close();
68 } catch (IOException e) {
69 LOGGER.info("Error on closing IReader", e);
70 }
71
72 }
73
74
75
76 public boolean hasNext() {
77 return baseReader.hasNext();
78 }
79
80
81
82
83 public E next() throws ParseException {
84 E retour = null;
85
86 Row row = baseReader.next();
87 retour = beanProcessor.transform(row);
88
89 return retour;
90 }
91
92
93
94
95 public void remove() {
96 throw new UnsupportedOperationException();
97
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public BeanReader(File configFile, IReader reader) throws LoadException {
113 this(configFile, reader, false);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public BeanReader(File configFile, IReader reader, boolean skipEmptyRow) throws LoadException {
131 super();
132 if (configFile == null) {
133 throw new IllegalArgumentException("Constructor configFile argument must be not null");
134 }
135 if (reader == null) {
136 throw new IllegalArgumentException("Constructor reader argument must be not null");
137 }
138 if (skipEmptyRow) {
139 this.baseReader = new IgnoreNullReader(reader);
140 } else {
141 this.baseReader = reader;
142 }
143
144 if (baseReader.isWithHeaders()) {
145 if (baseReader.hasNext()) {
146 baseReader.next();
147 } else {
148 throw new ReaderException("No headers");
149 }
150 }
151 this.beanProcessor = new BeanProcessorLoader<E>().load(configFile);
152 }
153
154
155
156
157
158
159
160
161
162
163
164 public BeanReader(IBeanProcessor<E> beanProcessor, IReader reader) {
165 super();
166 if (beanProcessor == null) {
167 throw new IllegalArgumentException("Constructor beanProcessor argument must be not null");
168 }
169 if (reader == null) {
170 throw new IllegalArgumentException("Constructor reader argument must be not null");
171 }
172 this.baseReader = reader;
173 this.beanProcessor = beanProcessor;
174 if (baseReader.isWithHeaders()) {
175 if (baseReader.hasNext()) {
176 baseReader.next();
177 } else {
178 throw new ReaderException("No headers");
179 }
180 }
181 }
182
183 }