1 package org.simantics.district.imports;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.Iterator;
12 import org.apache.commons.csv.CSVFormat;
13 import org.apache.commons.csv.CSVParser;
14 import org.apache.commons.csv.CSVRecord;
15 import org.simantics.db.Resource;
17 public class DistrictImportUtils {
19 private DistrictImportUtils() { }
22 public static Resource importCSVAsLayer(Path csvFile) throws IOException {
24 try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
25 Map<String, Integer> header = parser.getHeaderMap();
26 System.out.println(header);
31 public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
32 return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
35 public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
37 format = format.withFirstRecordAsHeader();
38 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
39 return parser.getHeaderMap();
43 public static Map<String, Character> getSupportedCSVDelimiterFormats() {
44 Map<String, Character> delimiters = new HashMap<>();
45 delimiters.put("COMMA", ',');
46 delimiters.put("SEMICOMMA", ';');
50 public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
51 return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
54 public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
56 format = format.withFirstRecordAsHeader();
57 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
59 List<Map<String, String>> results = new ArrayList<>(amount);
60 Iterator<CSVRecord> iter = parser.iterator();
61 while (start < amount && iter.hasNext()) {
62 CSVRecord record = iter.next();
63 results.add(record.toMap());
70 public static List<CSVRecord> readRows(Path source, char delim, int rowAmount) throws IOException {
71 List<CSVRecord> results = new ArrayList<>();
72 CSVFormat format = CSVFormat.newFormat(delim);
73 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
74 Iterator<CSVRecord> records = parser.iterator();
76 if (rowAmount == -1) {
77 while (records.hasNext()) {
78 results.add(records.next());
82 while (rows < rowAmount && records.hasNext()) {
83 results.add(records.next());
92 public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
93 Map<CSVHeader, List<String>> results = new HashMap<>();
94 CSVFormat format = CSVFormat.newFormat(delimiter);
96 format = format.withFirstRecordAsHeader();
97 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
98 Map<String, Integer> headers = parser.getHeaderMap();
99 if (headers != null && !headers.isEmpty()) {
100 for (int index = 0; index < headers.size(); index++) {
101 for (String head : headers.keySet()) {
102 results.put(new CSVHeader(head, index), new ArrayList<>());
107 Iterator<CSVRecord> records = parser.iterator();
109 while (rows < amount && records.hasNext()) {
110 CSVRecord record = records.next();
111 for (int j = 0; j < record.size(); j++) {
112 String value = record.get(j);
113 String header = Integer.toString(j);
114 CSVHeader csvHeader = new CSVHeader(header, j);
115 List<String> vals = results.get(csvHeader);
117 vals = new ArrayList<>();
118 results.put(csvHeader, vals);
128 public static class CSVHeader {
130 private final String header;
131 private final int index;
133 public CSVHeader(String header, int index) {
134 this.header = header;
138 public String getHeader() {
142 public int getIndex() {
147 public int hashCode() {
148 final int prime = 31;
150 result = prime * result + ((header == null) ? 0 : header.hashCode());
151 result = prime * result + index;
156 public boolean equals(Object obj) {
161 if (getClass() != obj.getClass())
163 CSVHeader other = (CSVHeader) obj;
164 if (header == null) {
165 if (other.header != null)
167 } else if (!header.equals(other.header))
169 if (index != other.index)