]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
1ad5b9a9487c0667d91a553cd6ecc461b1851156
[simantics/district.git] / org.simantics.district.imports / src / org / simantics / district / imports / DistrictImportUtils.java
1 package org.simantics.district.imports;
2
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;
9 import java.util.List;
10 import java.util.Map;
11
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;
16
17 public class DistrictImportUtils {
18
19     private DistrictImportUtils() { }
20     
21     
22     public static Resource importCSVAsLayer(Path csvFile) throws IOException {
23         
24         try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
25             Map<String, Integer> header = parser.getHeaderMap();
26             System.out.println(header);
27         }
28         return null;
29     }
30     
31     public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
32         return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
33     }
34     
35     public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
36         if (firstAsHeader)
37             format = format.withFirstRecordAsHeader();
38         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
39             return parser.getHeaderMap();
40         }
41     }
42
43     public static Map<String, Character> getSupportedCSVDelimiterFormats() {
44         Map<String, Character> delimiters = new HashMap<>();
45         delimiters.put("COMMA", ',');
46         delimiters.put("SEMICOMMA", ';');
47         return delimiters;
48     }
49
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);
52     }
53
54     public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
55         if (firstAsHeader)
56             format = format.withFirstRecordAsHeader();
57         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
58             int start = 0;
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());
64                 start++;
65             }
66             return results;
67         }
68     }
69
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();
75             int rows = 0;
76             if (rowAmount == -1) {
77                 while (records.hasNext()) {
78                     results.add(records.next());
79                     rows++;
80                 }
81             } else {
82                 while (rows < rowAmount && records.hasNext()) {
83                     results.add(records.next());
84                     rows++;
85                 }
86             }
87         }
88         return results;
89     }
90
91     
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);
95         if (firstAsHeader)
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<>());
103                     }
104                 }
105             }
106             
107             Iterator<CSVRecord> records = parser.iterator();
108             int rows = 0;
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);
116                     if (vals == null) {
117                         vals = new ArrayList<>();
118                         results.put(csvHeader, vals);
119                     }
120                     vals.add(value);
121                 }
122                 rows++;
123             }
124         }
125         return results;
126     }
127     
128     public static class CSVHeader {
129
130         private final String header;
131         private final int index;
132         
133         public CSVHeader(String header, int index) {
134             this.header = header;
135             this.index = index;
136         }
137
138         public String getHeader() {
139             return header;
140         }
141
142         public int getIndex() {
143             return index;
144         }
145         
146         @Override
147         public int hashCode() {
148             final int prime = 31;
149             int result = 1;
150             result = prime * result + ((header == null) ? 0 : header.hashCode());
151             result = prime * result + index;
152             return result;
153         }
154
155         @Override
156         public boolean equals(Object obj) {
157             if (this == obj)
158                 return true;
159             if (obj == null)
160                 return false;
161             if (getClass() != obj.getClass())
162                 return false;
163             CSVHeader other = (CSVHeader) obj;
164             if (header == null) {
165                 if (other.header != null)
166                     return false;
167             } else if (!header.equals(other.header))
168                 return false;
169             if (index != other.index)
170                 return false;
171             return true;
172         }
173     }
174
175 }