Skip to content

Commit

Permalink
update openCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Mar 23, 2024
1 parent 754acd9 commit 8fd8d1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kitodo.production.forms.massimport;

import com.opencsv.exceptions.CsvValidationException;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -114,7 +116,7 @@ public void handleFileUpload(FileUploadEvent event) {
records = massImportService.parseLines(importedCsvLines, csvSeparator);
}
}
} catch (IOException e) {
} catch (IOException | CsvValidationException e) {
Helper.setErrorMessage(e);
}
}
Expand All @@ -132,7 +134,7 @@ public void changeSeparator() {
metadataKeys = new LinkedList<>(Arrays.asList(importedCsvHeaderLine.split(csvSeparator, -1)));
try {
records = massImportService.parseLines(importedCsvLines, csvSeparator);
} catch (IOException e) {
} catch (IOException | CsvValidationException e) {
Helper.setErrorMessage(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

package org.kitodo.production.services.data;

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -84,12 +88,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) throws IOException {
public List<CsvRecord> parseLines(List<String> lines, String separator) throws IOException, CsvValidationException {
List<CsvRecord> records = new LinkedList<>();
for (String line : lines) {
if (!Objects.isNull(line) && !line.isBlank()) {
List<CsvCell> cells = new LinkedList<>();
CSVReader csvReader = new CSVReader(new StringReader(line), separator.charAt(0), '\"');
CSVParser parser = new CSVParserBuilder()
.withSeparator(separator.charAt(0))
.withQuoteChar('\"')
.build();
CSVReader csvReader = new CSVReaderBuilder(new StringReader(line))
.withSkipLines(0)
.withCSVParser(parser)
.build();
String[] values = csvReader.readNext();
if (!Objects.isNull(values)) {
for (String value : values) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.10</version>
<version>5.9</version>
</dependency>
<dependency>
<groupId>com.xebialabs.restito</groupId>
Expand Down

0 comments on commit 8fd8d1c

Please sign in to comment.