embulk-parser-csv_guessable 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,512 @@
1
+ package org.embulk.parser.csv_guessable;
2
+
3
+ import com.google.common.base.Preconditions;
4
+ import java.util.List;
5
+ import java.util.ArrayList;
6
+ import java.util.Deque;
7
+ import java.util.ArrayDeque;
8
+ import org.embulk.spi.DataException;
9
+ import org.embulk.spi.util.LineDecoder;
10
+ import org.embulk.config.ConfigException;
11
+
12
+ public class CsvTokenizer
13
+ {
14
+ static enum RecordState
15
+ {
16
+ NOT_END, END,
17
+ }
18
+
19
+ static enum ColumnState
20
+ {
21
+ BEGIN, VALUE, QUOTED_VALUE, AFTER_QUOTED_VALUE, FIRST_TRIM, LAST_TRIM_OR_VALUE,
22
+ }
23
+
24
+ private static final char END_OF_LINE = '\0';
25
+ static final char NO_QUOTE = '\0';
26
+ static final char NO_ESCAPE = '\0';
27
+
28
+ private final char delimiterChar;
29
+ private final String delimiterFollowingString;
30
+ private final char quote;
31
+ private final char escape;
32
+ private final String newline;
33
+ private final boolean trimIfNotQuoted;
34
+ private final long maxQuotedSizeLimit;
35
+ private final String commentLineMarker;
36
+ private final LineDecoder input;
37
+ private final String nullStringOrNull;
38
+
39
+ private RecordState recordState = RecordState.END; // initial state is end of a record. nextRecord() must be called first
40
+ private long lineNumber = 0;
41
+
42
+ private String line = null;
43
+ private int linePos = 0;
44
+ private boolean wasQuotedColumn = false;
45
+ private List<String> quotedValueLines = new ArrayList<>();
46
+ private Deque<String> unreadLines = new ArrayDeque<>();
47
+
48
+ public CsvTokenizer(LineDecoder input, CsvGuessableParserPlugin.PluginTask task)
49
+ {
50
+ String delimiter = task.getDelimiter();
51
+ if (delimiter.length() == 0) {
52
+ throw new ConfigException("Empty delimiter is not allowed");
53
+ } else {
54
+ this.delimiterChar = delimiter.charAt(0);
55
+ if (delimiter.length() > 1) {
56
+ delimiterFollowingString = delimiter.substring(1);
57
+ } else {
58
+ delimiterFollowingString = null;
59
+ }
60
+ }
61
+ quote = task.getQuoteChar().or(CsvGuessableParserPlugin.QuoteCharacter.noQuote()).getCharacter();
62
+ escape = task.getEscapeChar().or(CsvGuessableParserPlugin.EscapeCharacter.noEscape()).getCharacter();
63
+ newline = task.getNewline().getString();
64
+ trimIfNotQuoted = task.getTrimIfNotQuoted();
65
+ maxQuotedSizeLimit = task.getMaxQuotedSizeLimit();
66
+ commentLineMarker = task.getCommentLineMarker().orNull();
67
+ nullStringOrNull = task.getNullString().orNull();
68
+ this.input = input;
69
+ }
70
+
71
+ public long getCurrentLineNumber()
72
+ {
73
+ return lineNumber;
74
+ }
75
+
76
+ public boolean skipHeaderLine()
77
+ {
78
+ boolean skipped = input.poll() != null;
79
+ if (skipped) {
80
+ lineNumber++;
81
+ }
82
+ return skipped;
83
+ }
84
+
85
+ // returns skipped line
86
+ public String skipCurrentLine()
87
+ {
88
+ String skippedLine;
89
+ if (quotedValueLines.isEmpty()) {
90
+ skippedLine = line;
91
+ } else {
92
+ // recover lines of quoted value
93
+ skippedLine = quotedValueLines.remove(0); // TODO optimize performance
94
+ unreadLines.addAll(quotedValueLines);
95
+ lineNumber -= quotedValueLines.size();
96
+ if (line != null) {
97
+ unreadLines.add(line);
98
+ lineNumber -= 1;
99
+ }
100
+ quotedValueLines.clear();
101
+ }
102
+ recordState = RecordState.END;
103
+ return skippedLine;
104
+ }
105
+
106
+ public boolean nextFile()
107
+ {
108
+ boolean next = input.nextFile();
109
+ if (next) {
110
+ lineNumber = 0;
111
+ }
112
+ return next;
113
+ }
114
+
115
+ // used by guess-csv
116
+ public boolean nextRecord()
117
+ {
118
+ return nextRecord(true);
119
+ }
120
+
121
+ public boolean nextRecord(boolean skipEmptyLine)
122
+ {
123
+ // If at the end of record, read the next line and initialize the state
124
+ if (recordState != RecordState.END) {
125
+ throw new TooManyColumnsException("Too many columns");
126
+ }
127
+
128
+ boolean hasNext = nextLine(skipEmptyLine);
129
+ if (hasNext) {
130
+ recordState = RecordState.NOT_END;
131
+ return true;
132
+ } else {
133
+ return false;
134
+ }
135
+ }
136
+
137
+ private boolean nextLine(boolean skipEmptyLine)
138
+ {
139
+ while (true) {
140
+ if (!unreadLines.isEmpty()) {
141
+ line = unreadLines.removeFirst();
142
+ } else {
143
+ line = input.poll();
144
+ if (line == null) {
145
+ return false;
146
+ }
147
+ }
148
+ linePos = 0;
149
+ lineNumber++;
150
+
151
+ boolean skip = skipEmptyLine && (
152
+ line.isEmpty() ||
153
+ (commentLineMarker != null && line.startsWith(commentLineMarker)));
154
+ if (!skip) {
155
+ return true;
156
+ }
157
+ }
158
+ }
159
+
160
+ public boolean hasNextColumn()
161
+ {
162
+ return recordState == RecordState.NOT_END;
163
+ }
164
+
165
+ public String nextColumn()
166
+ {
167
+ if (!hasNextColumn()) {
168
+ throw new TooFewColumnsException("Too few columns");
169
+ }
170
+
171
+ // reset last state
172
+ wasQuotedColumn = false;
173
+ quotedValueLines.clear();
174
+
175
+ // local state
176
+ int valueStartPos = linePos;
177
+ int valueEndPos = 0; // initialized by VALUE state and used by LAST_TRIM_OR_VALUE and
178
+ StringBuilder quotedValue = null; // initial by VALUE or FIRST_TRIM state and used by QUOTED_VALUE state
179
+ ColumnState columnState = ColumnState.BEGIN;
180
+
181
+ while (true) {
182
+ final char c = nextChar();
183
+
184
+ switch (columnState) {
185
+ case BEGIN:
186
+ // TODO optimization: state is BEGIN only at the first character of a column.
187
+ // this block can be out of the looop.
188
+ if (isDelimiter(c)) {
189
+ // empty value
190
+ if (delimiterFollowingString == null) {
191
+ return "";
192
+ } else if (isDelimiterFollowingFrom(linePos)) {
193
+ linePos += delimiterFollowingString.length();
194
+ return "";
195
+ }
196
+ // not a delimiter
197
+ }
198
+ if (isEndOfLine(c)) {
199
+ // empty value
200
+ recordState = RecordState.END;
201
+ return "";
202
+
203
+ } else if (isSpace(c) && trimIfNotQuoted) {
204
+ columnState = ColumnState.FIRST_TRIM;
205
+
206
+ } else if (isQuote(c)) {
207
+ valueStartPos = linePos; // == 1
208
+ wasQuotedColumn = true;
209
+ quotedValue = new StringBuilder();
210
+ columnState = ColumnState.QUOTED_VALUE;
211
+
212
+ } else {
213
+ columnState = ColumnState.VALUE;
214
+ }
215
+ break;
216
+
217
+ case FIRST_TRIM:
218
+ if (isDelimiter(c)) {
219
+ // empty value
220
+ if (delimiterFollowingString == null) {
221
+ return "";
222
+ } else if (isDelimiterFollowingFrom(linePos)) {
223
+ linePos += delimiterFollowingString.length();
224
+ return "";
225
+ }
226
+ // not a delimiter
227
+ }
228
+ if (isEndOfLine(c)) {
229
+ // empty value
230
+ recordState = RecordState.END;
231
+ return "";
232
+
233
+ } else if (isQuote(c)) {
234
+ // column has heading spaces and quoted. TODO should this be rejected?
235
+ valueStartPos = linePos;
236
+ wasQuotedColumn = true;
237
+ quotedValue = new StringBuilder();
238
+ columnState = ColumnState.QUOTED_VALUE;
239
+
240
+ } else if (isSpace(c)) {
241
+ // skip this character
242
+
243
+ } else {
244
+ valueStartPos = linePos - 1;
245
+ columnState = ColumnState.VALUE;
246
+ }
247
+ break;
248
+
249
+ case VALUE:
250
+ if (isDelimiter(c)) {
251
+ if (delimiterFollowingString == null) {
252
+ return line.substring(valueStartPos, linePos - 1);
253
+ } else if (isDelimiterFollowingFrom(linePos)) {
254
+ String value = line.substring(valueStartPos, linePos - 1);
255
+ linePos += delimiterFollowingString.length();
256
+ return value;
257
+ }
258
+ // not a delimiter
259
+ }
260
+ if (isEndOfLine(c)) {
261
+ recordState = RecordState.END;
262
+ return line.substring(valueStartPos, linePos);
263
+
264
+ } else if (isSpace(c) && trimIfNotQuoted) {
265
+ valueEndPos = linePos - 1; // this is possibly end of value
266
+ columnState = ColumnState.LAST_TRIM_OR_VALUE;
267
+
268
+ // TODO not implemented yet foo""bar""baz -> [foo, bar, baz].append
269
+ //} else if (isQuote(c)) {
270
+ // // In RFC4180, If fields are not enclosed with double quotes, then
271
+ // // double quotes may not appear inside the fields. But they are often
272
+ // // included in the fields. We should care about them later.
273
+
274
+ } else {
275
+ // keep VALUE state
276
+ }
277
+ break;
278
+
279
+ case LAST_TRIM_OR_VALUE:
280
+ if (isDelimiter(c)) {
281
+ if (delimiterFollowingString == null) {
282
+ return line.substring(valueStartPos, valueEndPos);
283
+ } else if (isDelimiterFollowingFrom(linePos)) {
284
+ linePos += delimiterFollowingString.length();
285
+ return line.substring(valueStartPos, valueEndPos);
286
+ } else {
287
+ // not a delimiter
288
+ }
289
+ }
290
+ if (isEndOfLine(c)) {
291
+ recordState = RecordState.END;
292
+ return line.substring(valueStartPos, valueEndPos);
293
+
294
+ } else if (isSpace(c)) {
295
+ // keep LAST_TRIM_OR_VALUE state
296
+
297
+ } else {
298
+ // this spaces are not trailing spaces. go back to VALUE state
299
+ columnState = ColumnState.VALUE;
300
+ }
301
+ break;
302
+
303
+ case QUOTED_VALUE:
304
+ if (isEndOfLine(c)) {
305
+ // multi-line quoted value
306
+ quotedValue.append(line.substring(valueStartPos, linePos));
307
+ quotedValue.append(newline);
308
+ quotedValueLines.add(line);
309
+ if (!nextLine(false)) {
310
+ throw new InvalidValueException("Unexpected end of line during parsing a quoted value");
311
+ }
312
+ valueStartPos = 0;
313
+
314
+ } else if (isQuote(c)) {
315
+ char next = peekNextChar();
316
+ if (isQuote(next)) { // escaped quote
317
+ quotedValue.append(line.substring(valueStartPos, linePos));
318
+ valueStartPos = ++linePos;
319
+ } else {
320
+ quotedValue.append(line.substring(valueStartPos, linePos - 1));
321
+ columnState = ColumnState.AFTER_QUOTED_VALUE;
322
+ }
323
+
324
+ } else if (isEscape(c)) { // isQuote must be checked first in case of quote == escape
325
+ // In RFC 4180, CSV's escape char is '\"'. But '\\' is often used.
326
+ char next = peekNextChar();
327
+ if (isEndOfLine(c)) {
328
+ // escape end of line. TODO assuming multi-line quoted value without newline?
329
+ quotedValue.append(line.substring(valueStartPos, linePos));
330
+ quotedValueLines.add(line);
331
+ if (!nextLine(false)) {
332
+ throw new InvalidValueException("Unexpected end of line during parsing a quoted value");
333
+ }
334
+ valueStartPos = 0;
335
+ } else if (isQuote(next) || isEscape(next)) { // escaped quote
336
+ quotedValue.append(line.substring(valueStartPos, linePos - 1));
337
+ quotedValue.append(next);
338
+ valueStartPos = ++linePos;
339
+ }
340
+
341
+ } else {
342
+ if ((linePos - valueStartPos) + quotedValue.length() > maxQuotedSizeLimit) {
343
+ throw new QuotedSizeLimitExceededException("The size of the quoted value exceeds the limit size ("+maxQuotedSizeLimit+")");
344
+ }
345
+ // keep QUOTED_VALUE state
346
+ }
347
+ break;
348
+
349
+ case AFTER_QUOTED_VALUE:
350
+ if (isDelimiter(c)) {
351
+ if (delimiterFollowingString == null) {
352
+ return quotedValue.toString();
353
+ } else if (isDelimiterFollowingFrom(linePos)) {
354
+ linePos += delimiterFollowingString.length();
355
+ return quotedValue.toString();
356
+ }
357
+ // not a delimiter
358
+ }
359
+ if (isEndOfLine(c)) {
360
+ recordState = RecordState.END;
361
+ return quotedValue.toString();
362
+
363
+ } else if (isSpace(c)) {
364
+ // column has trailing spaces and quoted. TODO should this be rejected?
365
+
366
+ } else {
367
+ throw new InvalidValueException(String.format("Unexpected extra character '%c' after a value quoted by '%c'", c, quote));
368
+ }
369
+ break;
370
+
371
+ default:
372
+ assert false;
373
+ }
374
+ }
375
+ }
376
+
377
+ public String nextColumnOrNull()
378
+ {
379
+ String v = nextColumn();
380
+ if (nullStringOrNull == null) {
381
+ if (v.isEmpty()) {
382
+ if (wasQuotedColumn) {
383
+ return "";
384
+ }
385
+ else {
386
+ return null;
387
+ }
388
+ }
389
+ else {
390
+ return v;
391
+ }
392
+ }
393
+ else {
394
+ if (v.equals(nullStringOrNull)) {
395
+ return null;
396
+ }
397
+ else {
398
+ return v;
399
+ }
400
+ }
401
+ }
402
+
403
+ public boolean wasQuotedColumn()
404
+ {
405
+ return wasQuotedColumn;
406
+ }
407
+
408
+ private char nextChar()
409
+ {
410
+ Preconditions.checkState(line != null, "nextColumn is called after end of file");
411
+
412
+ if (linePos >= line.length()) {
413
+ return END_OF_LINE;
414
+ } else {
415
+ return line.charAt(linePos++);
416
+ }
417
+ }
418
+
419
+ private char peekNextChar()
420
+ {
421
+ Preconditions.checkState(line != null, "peekNextChar is called after end of file");
422
+
423
+ if (linePos >= line.length()) {
424
+ return END_OF_LINE;
425
+ } else {
426
+ return line.charAt(linePos);
427
+ }
428
+ }
429
+
430
+ private boolean isSpace(char c)
431
+ {
432
+ return c == ' ';
433
+ }
434
+
435
+ private boolean isDelimiterFollowingFrom(int pos)
436
+ {
437
+ if (line.length() < pos + delimiterFollowingString.length()) {
438
+ return false;
439
+ }
440
+ for (int i = 0; i < delimiterFollowingString.length(); i++) {
441
+ if (delimiterFollowingString.charAt(i) != line.charAt(pos + i)) {
442
+ return false;
443
+ }
444
+ }
445
+ return true;
446
+ }
447
+
448
+ private boolean isDelimiter(char c)
449
+ {
450
+ return c == delimiterChar;
451
+ }
452
+
453
+ private boolean isEndOfLine(char c)
454
+ {
455
+ return c == END_OF_LINE;
456
+ }
457
+
458
+ private boolean isQuote(char c)
459
+ {
460
+ return quote != NO_QUOTE && c == quote;
461
+ }
462
+
463
+ private boolean isEscape(char c)
464
+ {
465
+ return escape != NO_ESCAPE && c == escape;
466
+ }
467
+
468
+ public static class InvalidFormatException
469
+ extends DataException
470
+ {
471
+ public InvalidFormatException(String message)
472
+ {
473
+ super(message);
474
+ }
475
+ }
476
+
477
+ public static class InvalidValueException
478
+ extends DataException
479
+ {
480
+ public InvalidValueException(String message)
481
+ {
482
+ super(message);
483
+ }
484
+ }
485
+
486
+ public static class QuotedSizeLimitExceededException
487
+ extends InvalidValueException
488
+ {
489
+ public QuotedSizeLimitExceededException(String message)
490
+ {
491
+ super(message);
492
+ }
493
+ }
494
+
495
+ public class TooManyColumnsException
496
+ extends InvalidFormatException
497
+ {
498
+ public TooManyColumnsException(String message)
499
+ {
500
+ super(message);
501
+ }
502
+ }
503
+
504
+ public class TooFewColumnsException
505
+ extends InvalidFormatException
506
+ {
507
+ public TooFewColumnsException(String message)
508
+ {
509
+ super(message);
510
+ }
511
+ }
512
+ }