-package org.simantics.district.imports;\r
-\r
-import java.io.IOException;\r
-import java.nio.file.Files;\r
-import java.nio.file.Path;\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.apache.commons.csv.CSVFormat;\r
-import org.apache.commons.csv.CSVParser;\r
-import org.apache.commons.csv.CSVRecord;\r
-import org.simantics.db.Resource;\r
-\r
-public class DistrictImportUtils {\r
-\r
- private DistrictImportUtils() { }\r
- \r
- \r
- public static Resource importCSVAsLayer(Path csvFile) throws IOException {\r
- \r
- \r
- try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {\r
- Map<String, Integer> header = parser.getHeaderMap();\r
- System.out.println(header);\r
- }\r
- return null;\r
- }\r
- \r
- public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {\r
- return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);\r
- }\r
- \r
- public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {\r
- if (firstAsHeader)\r
- format = format.withFirstRecordAsHeader();\r
- try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {\r
- return parser.getHeaderMap();\r
- }\r
- }\r
-\r
-\r
- public static Map<String, Character> getSupportedCSVDelimiterFormats() {\r
- Map<String, Character> delimiters = new HashMap<>();\r
- delimiters.put("COMMA", ',');\r
- delimiters.put("SEMICOMMA", ';');\r
- return delimiters;\r
- }\r
-\r
- public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {\r
- return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);\r
- }\r
-\r
- public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {\r
- if (firstAsHeader)\r
- format = format.withFirstRecordAsHeader();\r
- try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {\r
- int start = 0;\r
- List<Map<String, String>> results = new ArrayList<>(amount);\r
- Iterator<CSVRecord> iter = parser.iterator();\r
- while (start < amount && iter.hasNext()) {\r
- CSVRecord record = iter.next();\r
- results.add(record.toMap());\r
- start++;\r
- }\r
- return results;\r
- }\r
- }\r
-\r
-\r
- public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {\r
- Map<CSVHeader, List<String>> results = new HashMap<>();\r
- CSVFormat format = CSVFormat.newFormat(delimiter);\r
- if (firstAsHeader)\r
- format = format.withFirstRecordAsHeader();\r
- try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {\r
- Map<String, Integer> headers = parser.getHeaderMap();\r
- if (headers != null && !headers.isEmpty()) {\r
- for (int index = 0; index < headers.size(); index++) {\r
- for (String head : headers.keySet()) {\r
- results.put(new CSVHeader(head, index), new ArrayList<>());\r
- }\r
- }\r
- }\r
- \r
- Iterator<CSVRecord> records = parser.iterator();\r
- int rows = 0;\r
- while (rows < amount && records.hasNext()) {\r
- CSVRecord record = records.next();\r
- for (int j = 0; j < record.size(); j++) {\r
- String value = record.get(j);\r
- String header = Integer.toString(j);\r
- CSVHeader csvHeader = new CSVHeader(header, j);\r
- List<String> vals = results.get(csvHeader);\r
- if (vals == null) {\r
- vals = new ArrayList<>();\r
- results.put(csvHeader, vals);\r
- }\r
- vals.add(value);\r
- }\r
- rows++;\r
- }\r
- }\r
- return results;\r
- }\r
- \r
- public static class CSVHeader {\r
-\r
- private final String header;\r
- private final int index;\r
- \r
- public CSVHeader(String header, int index) {\r
- this.header = header;\r
- this.index = index;\r
- }\r
-\r
- public String getHeader() {\r
- return header;\r
- }\r
-\r
- public int getIndex() {\r
- return index;\r
- }\r
- \r
- @Override\r
- public int hashCode() {\r
- final int prime = 31;\r
- int result = 1;\r
- result = prime * result + ((header == null) ? 0 : header.hashCode());\r
- result = prime * result + index;\r
- return result;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj)\r
- return true;\r
- if (obj == null)\r
- return false;\r
- if (getClass() != obj.getClass())\r
- return false;\r
- CSVHeader other = (CSVHeader) obj;\r
- if (header == null) {\r
- if (other.header != null)\r
- return false;\r
- } else if (!header.equals(other.header))\r
- return false;\r
- if (index != other.index)\r
- return false;\r
- return true;\r
- }\r
- }\r
-\r
-}\r
+package org.simantics.district.imports;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVParser;
+import org.apache.commons.csv.CSVRecord;
+import org.simantics.db.Resource;
+
+public class DistrictImportUtils {
+
+ private DistrictImportUtils() { }
+
+ public static Resource importCSVAsLayer(Path csvFile) throws IOException {
+
+ try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
+ Map<String, Integer> header = parser.getHeaderMap();
+ System.out.println(header);
+ }
+ return null;
+ }
+
+ public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
+ return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
+ }
+
+ public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
+ if (firstAsHeader)
+ format = format.withFirstRecordAsHeader();
+ try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
+ return parser.getHeaderMap();
+ }
+ }
+
+ public static Map<String, Character> getSupportedCSVDelimiterFormats() {
+ Map<String, Character> delimiters = new HashMap<>();
+ delimiters.put("Comma", ',');
+ delimiters.put("Semicolon", ';');
+ delimiters.put("Tabulator", '\t');
+ return delimiters;
+ }
+
+ public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
+ return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
+ }
+
+ public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
+ if (firstAsHeader)
+ format = format.withFirstRecordAsHeader();
+ try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
+ int start = 0;
+ List<Map<String, String>> results = new ArrayList<>(amount);
+ Iterator<CSVRecord> iter = parser.iterator();
+ while (start < amount && iter.hasNext()) {
+ CSVRecord record = iter.next();
+ results.add(record.toMap());
+ start++;
+ }
+ return results;
+ }
+ }
+
+ public static List<CSVRecord> readRows(Path source, char delim, int rowAmount) throws IOException {
+ List<CSVRecord> results = new ArrayList<>();
+ CSVFormat format = CSVFormat.newFormat(delim);
+ try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
+ Iterator<CSVRecord> records = parser.iterator();
+ int rows = 0;
+ if (rowAmount == -1) {
+ while (records.hasNext()) {
+ results.add(records.next());
+ rows++;
+ }
+ } else {
+ while (rows < rowAmount && records.hasNext()) {
+ results.add(records.next());
+ rows++;
+ }
+ }
+ }
+ return results;
+ }
+
+
+ public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
+ Map<CSVHeader, List<String>> results = new HashMap<>();
+ CSVFormat format = CSVFormat.newFormat(delimiter);
+ if (firstAsHeader)
+ format = format.withFirstRecordAsHeader();
+ try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
+ Map<String, Integer> headers = parser.getHeaderMap();
+ if (headers != null && !headers.isEmpty()) {
+ for (int index = 0; index < headers.size(); index++) {
+ for (String head : headers.keySet()) {
+ results.put(new CSVHeader(head, index), new ArrayList<>());
+ }
+ }
+ }
+
+ Iterator<CSVRecord> records = parser.iterator();
+ int rows = 0;
+ while (rows < amount && records.hasNext()) {
+ CSVRecord record = records.next();
+ for (int j = 0; j < record.size(); j++) {
+ String value = record.get(j);
+ String header = Integer.toString(j);
+ CSVHeader csvHeader = new CSVHeader(header, j);
+ List<String> vals = results.get(csvHeader);
+ if (vals == null) {
+ vals = new ArrayList<>();
+ results.put(csvHeader, vals);
+ }
+ vals.add(value);
+ }
+ rows++;
+ }
+ }
+ return results;
+ }
+
+ public static class CSVHeader {
+
+ private final String header;
+ private final int index;
+
+ public CSVHeader(String header, int index) {
+ this.header = header;
+ this.index = index;
+ }
+
+ public String getHeader() {
+ return header;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((header == null) ? 0 : header.hashCode());
+ result = prime * result + index;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ CSVHeader other = (CSVHeader) obj;
+ if (header == null) {
+ if (other.header != null)
+ return false;
+ } else if (!header.equals(other.header))
+ return false;
+ if (index != other.index)
+ return false;
+ return true;
+ }
+ }
+
+ public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
+ Set<String> results = new HashSet<>();
+ CSVFormat format = CSVFormat.newFormat(delim);
+ try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
+ Iterator<CSVRecord> records = parser.iterator();
+ if (records.hasNext())
+ records.next();
+ while (records.hasNext()) {
+ CSVRecord row = records.next();
+ String value = row.get(mappingIndex);
+ results.add(value);
+ }
+ }
+ return results;
+ }
+
+}