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 3e2ef22
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class MassImportForm extends BaseForm {
private ImportConfiguration importConfiguration;
private UploadedFile file;
private String csvSeparator = ";";
private String previousCsvSeparator = null;
private List<String> metadataKeys = new LinkedList<>(Collections.singletonList("ID"));
private List<CsvRecord> records = new LinkedList<>();
private String importedCsvHeaderLine = "";
Expand Down Expand Up @@ -131,7 +130,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 Expand Up @@ -304,7 +307,6 @@ public String getCsvSeparator() {
* @param csvSeparator as java.lang.String
*/
public void setCsvSeparator(String csvSeparator) {
this.previousCsvSeparator = this.csvSeparator;
this.csvSeparator = csvSeparator;
}

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,8 @@
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,27 +84,19 @@ 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));
}
records.add(new CsvRecord(cells));
}
cells.add(new CsvCell(currentCell.toString()));
records.add(new CsvRecord(cells));
}
}
return records;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ public List<ProcessDTO> findByMetadata(Map<String, String> metadata, boolean exa
* @param title
* the title
* @return a list of processes
* @throws DataException
* @throws DataException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
* when there is an error on conversion
*/
public List<ProcessDTO> findByTitle(String title) throws DataException {
Expand Down

0 comments on commit 3e2ef22

Please sign in to comment.