1
2
3
4
5
6
7
8
9
10
11
12
13
14 package olg.csv.base;
15 import java.io.IOException;
16 import java.util.List;
17 import java.util.NoSuchElementException;
18
19
20
21
22
23
24
25
26 public abstract class AbstractSheetReader implements IReader {
27
28
29
30 protected boolean skipEmptyCell = false;
31
32
33
34 protected int sheetNum;
35
36
37
38 protected int recordIndex = 1;
39
40
41
42 protected int rowSize = 0;
43
44
45
46
47 protected String sheetName = null;
48
49
50
51
52 protected boolean withHeaders;
53
54
55
56 protected Integer beginAtRow = 1;
57
58
59
60 protected Integer endAtRow = null;
61
62
63
64
65 protected Integer beginAtColumn = 0;
66
67
68
69 protected Integer endAtColumn = null;
70
71
72
73
74 public final boolean hasNext() {
75 return getRows() >= recordIndex && (endAtRow == null ? true : endAtRow >= recordIndex);
76 }
77
78
79
80
81 public final Row next() {
82 if (!hasNext()) {
83 throw new NoSuchElementException();
84 }
85 return setNext();
86
87 }
88
89
90
91
92 public final void remove() {
93 throw new UnsupportedOperationException();
94
95 }
96
97
98
99
100 public boolean isWithHeaders() {
101 return this.withHeaders;
102 }
103
104
105
106
107 public abstract void close();
108
109
110
111
112
113
114 protected abstract Row setNext();
115
116
117
118
119
120
121
122
123 protected abstract void doOnInitSheet() throws IOException;
124
125
126
127
128
129
130
131
132
133
134
135
136 protected List<Cell> padding(List<Cell> cells, int columnBegin, int columnEnd) {
137 if (!skipEmptyCell) {
138 for (int j = columnBegin; j < columnEnd; j++) {
139 cells.add(new Cell(j, null));
140
141 }
142 }
143 return cells;
144 }
145
146
147
148
149
150
151 public int getSheetNum() {
152 return sheetNum;
153 }
154
155
156
157
158
159 public String getSheetName() {
160 return sheetName;
161 }
162
163
164
165
166
167
168 protected abstract int getRows();
169
170
171
172
173
174
175
176
177
178
179
180
181 protected void initSheet() throws IOException {
182 doOnInitSheet();
183 this.rowSize = defineRowSize();
184
185 }
186
187
188
189
190
191
192 protected abstract int defineRowSize();
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 private void checkParameters(Integer beginAtRow, Integer endAtRow, String beginAtColumn, String endAtColumn) {
212 if (beginAtRow != null) {
213 this.beginAtRow = beginAtRow;
214 if (this.beginAtRow < 1) {
215 throw new IllegalArgumentException("SheetReader Constructor settings argument: beginAtRow parameter["
216 + beginAtRow
217 + "] must be a positive number or null (in this case reading begins at the first row)");
218
219 }
220 }
221
222 if (endAtRow != null) {
223 this.endAtRow = endAtRow;
224
225 if (this.endAtRow < 1 || this.endAtRow <= this.beginAtRow) {
226 throw new IllegalArgumentException("SheetReader Constructor settings argument endAtRow parameter["
227 + endAtRow + "] must be a positive number and be greater than " + this.beginAtRow
228 + " or null(in this case reading ends at the last row)");
229 }
230 }
231
232 if (beginAtColumn != null) {
233 try {
234 this.beginAtColumn = Cell.fromSheetCellNumber(beginAtColumn);
235 } catch (IllegalArgumentException ex) {
236 throw new IllegalArgumentException(
237 "SheetReader Constructor settings argument: beginAtColumn parameter[" + beginAtColumn
238 + "] must be a positive number greater "
239 + "than 0 or an expression conformed to spreadsheet column Notation or "
240 + "null(in this case rows reading begins at the first column)", ex);
241 }
242
243 }
244
245 if (endAtColumn != null) {
246 try {
247 this.endAtColumn = Cell.fromSheetCellNumber(endAtColumn);
248 } catch (IllegalArgumentException ex) {
249 throw new IllegalArgumentException("SheetReader Constructor settings argument: endAtColumn parameter["
250 + endAtColumn + "] must be a positive number greater than 0 or an expression conformed "
251 + "to spreadsheet column Notation "
252 + "or null (in this case rows reading ends at the last column", ex);
253 }
254
255 if (this.endAtColumn <= this.beginAtColumn) {
256 throw new IllegalArgumentException("SheetReader Constructor settings argument: endAtColumn parameter["
257 + endAtColumn + "] must be greater than " + (beginAtColumn == null ? "A" : beginAtColumn));
258 }
259 }
260 }
261
262
263
264
265
266
267 public AbstractSheetReader(AbstractSheetSettings<? extends AbstractSheetSettings<?>> settings) {
268 super();
269 if (settings == null) {
270 throw new IllegalArgumentException("SheetReader Constructor settings argument must not be null");
271 }
272 checkParameters(settings.getBeginAtRow(), settings.getEndAtRow(), settings.getBeginAtColumn(),
273 settings.getEndAtColumn());
274 this.withHeaders = settings.isWithHeaders();
275 this.sheetNum = settings.getSheetNum();
276 this.sheetName = settings.getSheetName();
277
278 this.recordIndex = this.beginAtRow;
279
280 }
281
282 }