From: Reino Ruusu Date: Mon, 31 Aug 2020 12:48:15 +0000 (+0300) Subject: Support CSV import from a generic reader X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F13%2F4413%2F1;p=simantics%2Fdistrict.git Support CSV import from a generic reader gitlab #93 Change-Id: I552cfac33369bf960342836428bc005ed3b1505f --- diff --git a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java index d4acbda8..0b6edb5f 100644 --- a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java +++ b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java @@ -1,6 +1,7 @@ package org.simantics.district.imports; import java.io.IOException; +import java.io.Reader; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -105,11 +106,16 @@ public class DistrictImportUtils { } public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function consumer) throws IOException { - CSVFormat format = CSVFormat.newFormat(delim); + consumeCSV(Files.newBufferedReader(source), delim, firstAsHeader, consumer); + } + + public static void consumeCSV(Reader reader, char delim, boolean firstAsHeader, + Function consumer) throws IOException { + CSVFormat format = CSVFormat.newFormat(delim).withQuote('"'); if (firstAsHeader) { format = format.withFirstRecordAsHeader(); } - try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { + try (CSVParser parser = format.parse(reader)) { Iterator records = parser.iterator(); while (records.hasNext()) { Boolean cont = consumer.apply(records.next());