Skip to content

Commit

Permalink
fix(reader-gtfs): fix filtering of empty-lines
Browse files Browse the repository at this point in the history
line filtering in opencsv not possible, we therefore read the row and check if it's produced from an empty line
  • Loading branch information
kschrab committed Dec 5, 2024
1 parent 213b075 commit 455de55
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions reader-gtfs/src/main/java/com/conveyal/gtfs/util/CsvReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;
import com.opencsv.validators.LineValidator;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
Expand Down Expand Up @@ -34,7 +33,6 @@ public CsvReader(Reader reader) {
public CsvReader(Reader reader, char delimiter) {
this.reader = new CSVReaderBuilder(reader)
.withCSVParser(new CSVParserBuilder().withSeparator(delimiter).build())
.withLineValidator(new SkipEmptyLines())
.build();
}

Expand Down Expand Up @@ -67,24 +65,27 @@ public String get(String column) {

public boolean readRecord() {
try {
currenRecord = this.reader.readNext();
return currenRecord != null;
} catch (Exception e) {
String[] nextRecord;
do {
if (this.reader.peek() == null) {
return false;
}
nextRecord = this.reader.readNextSilently();
} while (isEmptyLine(nextRecord));

currenRecord = nextRecord;
return true;
} catch (IOException e) {
return false;
}
}

static class SkipEmptyLines implements LineValidator {
@Override
public boolean isValid(String s) {
return s != null && !s.isEmpty();
}

@Override
public void validate(String s) throws CsvValidationException {
if (!isValid(s)) {
throw new CsvValidationException();
}
}
/*
* Checks if the provided record was produced by an empty line.
*/
private boolean isEmptyLine(String[] record) {
// an empty line results in a non-empty record consisting of one empty string: [""]
return record.length == 1 && (record[0] == null || record[0].isEmpty());
}

}

0 comments on commit 455de55

Please sign in to comment.