1 package org.simantics.district.imports;
\r
3 import java.io.IOException;
\r
4 import java.nio.file.Files;
\r
5 import java.nio.file.Path;
\r
6 import java.util.ArrayList;
\r
7 import java.util.HashMap;
\r
8 import java.util.Iterator;
\r
9 import java.util.List;
\r
10 import java.util.Map;
\r
12 import org.apache.commons.csv.CSVFormat;
\r
13 import org.apache.commons.csv.CSVParser;
\r
14 import org.apache.commons.csv.CSVRecord;
\r
15 import org.simantics.db.Resource;
\r
17 public class DistrictImportUtils {
\r
19 private DistrictImportUtils() { }
\r
22 public static Resource importCSVAsLayer(Path csvFile) throws IOException {
\r
25 try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
\r
26 Map<String, Integer> header = parser.getHeaderMap();
\r
27 System.out.println(header);
\r
32 public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
\r
33 return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
\r
36 public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
\r
38 format = format.withFirstRecordAsHeader();
\r
39 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
\r
40 return parser.getHeaderMap();
\r
45 public static Map<String, Character> getSupportedCSVDelimiterFormats() {
\r
46 Map<String, Character> delimiters = new HashMap<>();
\r
47 delimiters.put("COMMA", ',');
\r
48 delimiters.put("SEMICOMMA", ';');
\r
52 public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
\r
53 return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
\r
56 public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
\r
58 format = format.withFirstRecordAsHeader();
\r
59 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
\r
61 List<Map<String, String>> results = new ArrayList<>(amount);
\r
62 Iterator<CSVRecord> iter = parser.iterator();
\r
63 while (start < amount && iter.hasNext()) {
\r
64 CSVRecord record = iter.next();
\r
65 results.add(record.toMap());
\r
73 public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
\r
74 Map<CSVHeader, List<String>> results = new HashMap<>();
\r
75 CSVFormat format = CSVFormat.newFormat(delimiter);
\r
77 format = format.withFirstRecordAsHeader();
\r
78 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
\r
79 Map<String, Integer> headers = parser.getHeaderMap();
\r
80 if (headers != null && !headers.isEmpty()) {
\r
81 for (int index = 0; index < headers.size(); index++) {
\r
82 for (String head : headers.keySet()) {
\r
83 results.put(new CSVHeader(head, index), new ArrayList<>());
\r
88 Iterator<CSVRecord> records = parser.iterator();
\r
90 while (rows < amount && records.hasNext()) {
\r
91 CSVRecord record = records.next();
\r
92 for (int j = 0; j < record.size(); j++) {
\r
93 String value = record.get(j);
\r
94 String header = Integer.toString(j);
\r
95 CSVHeader csvHeader = new CSVHeader(header, j);
\r
96 List<String> vals = results.get(csvHeader);
\r
98 vals = new ArrayList<>();
\r
99 results.put(csvHeader, vals);
\r
109 public static class CSVHeader {
\r
111 private final String header;
\r
112 private final int index;
\r
114 public CSVHeader(String header, int index) {
\r
115 this.header = header;
\r
116 this.index = index;
\r
119 public String getHeader() {
\r
123 public int getIndex() {
\r
128 public int hashCode() {
\r
129 final int prime = 31;
\r
131 result = prime * result + ((header == null) ? 0 : header.hashCode());
\r
132 result = prime * result + index;
\r
137 public boolean equals(Object obj) {
\r
142 if (getClass() != obj.getClass())
\r
144 CSVHeader other = (CSVHeader) obj;
\r
145 if (header == null) {
\r
146 if (other.header != null)
\r
148 } else if (!header.equals(other.header))
\r
150 if (index != other.index)
\r