diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/massimport/MassImportForm.java b/Kitodo/src/main/java/org/kitodo/production/forms/massimport/MassImportForm.java index 01d6610188c..b93c6b4d02b 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/massimport/MassImportForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/massimport/MassImportForm.java @@ -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; @@ -114,7 +116,7 @@ public void handleFileUpload(FileUploadEvent event) { records = massImportService.parseLines(importedCsvLines, csvSeparator); } } - } catch (IOException e) { + } catch (IOException | CsvValidationException e) { Helper.setErrorMessage(e); } } @@ -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); } } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java index 8538c316078..1687d5c6def 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/MassImportService.java @@ -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; @@ -84,12 +88,19 @@ public List getLines(UploadedFile file) throws IOException { * @param separator String used to split lines into individual parts * @return list of CsvRecord */ - public List parseLines(List lines, String separator) throws IOException { + public List parseLines(List lines, String separator) throws IOException, CsvValidationException { List records = new LinkedList<>(); for (String line : lines) { if (!Objects.isNull(line) && !line.isBlank()) { List 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) { diff --git a/pom.xml b/pom.xml index bf972a0c0ee..19e93ca7010 100644 --- a/pom.xml +++ b/pom.xml @@ -129,7 +129,7 @@ com.opencsv opencsv - 3.10 + 5.9 com.xebialabs.restito