]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Support CSV import from a generic reader 13/4413/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 31 Aug 2020 12:48:15 +0000 (15:48 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Mon, 31 Aug 2020 12:48:15 +0000 (15:48 +0300)
gitlab #93

Change-Id: I552cfac33369bf960342836428bc005ed3b1505f

org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java

index d4acbda8fe7000474ed30582d29a3342f4f95dce..0b6edb5f51785773811c53b33153aff354af2e8c 100644 (file)
@@ -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<CSVRecord, Boolean> 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<CSVRecord, Boolean> 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<CSVRecord> records = parser.iterator();
             while (records.hasNext()) {
                 Boolean cont = consumer.apply(records.next());