Skip to content

Commit

Permalink
delegate csv line parsing to OpenCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Mar 23, 2024
1 parent 914ceaa commit 9c73289
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ private void resetValues() {
*/
public void changeSeparator() {
metadataKeys = new LinkedList<>(Arrays.asList(importedCsvHeaderLine.split(csvSeparator, -1)));
records = massImportService.parseLines(importedCsvLines, csvSeparator);
try {
records = massImportService.parseLines(importedCsvLines, csvSeparator);
} catch (IOException e) {
Helper.setErrorMessage(e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -26,6 +27,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import com.opencsv.CSVReader;
import org.apache.commons.lang3.StringUtils;
import org.kitodo.api.Metadata;
import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewInterface;
Expand Down Expand Up @@ -81,26 +83,18 @@ public List<String> getLines(UploadedFile file) throws IOException {
* @param separator String used to split lines into individual parts
* @return list of CsvRecord
*/
public List<CsvRecord> parseLines(List<String> lines, String separator) {
public List<CsvRecord> parseLines(List<String> lines, String separator) throws IOException {
List<CsvRecord> records = new LinkedList<>();
for (String line : lines) {
List<CsvCell> cells = new LinkedList<>();
if (!Objects.isNull(line) && !line.isBlank()) {
StringBuilder currentCell = new StringBuilder();
boolean inQuotes = false;
for (char c : line.toCharArray()) {
if ((c == '\"' || c == '\'') && !inQuotes) {
inQuotes = true;
} else if (c == '\"' || c == '\'') {
inQuotes = false;
} else if (String.valueOf(c).equals(separator) && !inQuotes) {
cells.add(new CsvCell(currentCell.toString()));
currentCell.setLength(0); // Reset currentCell
} else {
currentCell.append(c);
List<CsvCell> cells = new LinkedList<>();
CSVReader csvReader = new CSVReader(new StringReader(line), separator.charAt(0), '\"');
String[] values = csvReader.readNext();
if (!Objects.isNull(values)) {
for (String value : values) {
cells.add(new CsvCell(value));
}
}
}
cells.add(new CsvCell(currentCell.toString()));
records.add(new CsvRecord(cells));
}
}
Expand Down

0 comments on commit 9c73289

Please sign in to comment.