1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.io;
17
18 import java.io.IOException;
19 import java.io.Reader;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.supercsv.cellprocessor.ift.CellProcessor;
24 import org.supercsv.exception.SuperCsvConstraintViolationException;
25 import org.supercsv.exception.SuperCsvException;
26 import org.supercsv.prefs.CsvPreference;
27 import org.supercsv.util.Util;
28
29
30
31
32
33
34
35 public abstract class AbstractCsvReader implements ICsvReader {
36
37 private final ITokenizer tokenizer;
38
39 private final CsvPreference preferences;
40
41
42 private final List<String> columns = new ArrayList<String>();
43
44
45 private int rowNumber = 0;
46
47
48
49
50
51
52
53
54
55
56
57 public AbstractCsvReader(final Reader reader, final CsvPreference preferences) {
58 if( reader == null ) {
59 throw new NullPointerException("reader should not be null");
60 } else if( preferences == null ) {
61 throw new NullPointerException("preferences should not be null");
62 }
63
64 this.preferences = preferences;
65 this.tokenizer = new Tokenizer(reader, preferences);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public AbstractCsvReader(final ITokenizer tokenizer, final CsvPreference preferences) {
81 if( tokenizer == null ) {
82 throw new NullPointerException("tokenizer should not be null");
83 } else if( preferences == null ) {
84 throw new NullPointerException("preferences should not be null");
85 }
86
87 this.preferences = preferences;
88 this.tokenizer = tokenizer;
89 }
90
91
92
93
94 public void close() throws IOException {
95 tokenizer.close();
96 }
97
98
99
100
101 public String get(final int n) {
102 return columns.get(n - 1);
103 }
104
105
106
107
108 public String[] getHeader(final boolean firstLineCheck) throws IOException {
109
110 if( firstLineCheck && tokenizer.getLineNumber() != 0 ) {
111 throw new SuperCsvException(String.format(
112 "CSV header must be fetched as the first read operation, but %d lines have already been read",
113 tokenizer.getLineNumber()));
114 }
115
116 if( readRow() ) {
117 return columns.toArray(new String[columns.size()]);
118 }
119
120 return null;
121 }
122
123
124
125
126 public int getLineNumber() {
127 return tokenizer.getLineNumber();
128 }
129
130
131
132
133 public String getUntokenizedRow() {
134 return tokenizer.getUntokenizedRow();
135 }
136
137
138
139
140 public int getRowNumber() {
141 return rowNumber;
142 }
143
144
145
146
147 public int length() {
148 return columns.size();
149 }
150
151
152
153
154
155
156 protected List<String> getColumns() {
157 return columns;
158 }
159
160
161
162
163
164
165 protected CsvPreference getPreferences() {
166 return preferences;
167 }
168
169
170
171
172
173
174
175
176
177
178 protected boolean readRow() throws IOException {
179 if( tokenizer.readColumns(columns) ) {
180 rowNumber++;
181 return true;
182 }
183 return false;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 protected List<Object> executeProcessors(final List<Object> processedColumns, final CellProcessor[] processors) {
203 Util.executeCellProcessors(processedColumns, getColumns(), processors, getLineNumber(), getRowNumber());
204 return processedColumns;
205 }
206
207 }