]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
Pushing some (very) old changes to remote.. should have done long ago
[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.Collection;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import org.apache.commons.csv.CSVFormat;
16 import org.apache.commons.csv.CSVParser;
17 import org.apache.commons.csv.CSVRecord;
18 import org.simantics.db.Resource;
19
20 public class DistrictImportUtils {
21
22     private DistrictImportUtils() { }
23
24     public static Resource importCSVAsLayer(Path csvFile) throws IOException {
25         
26         try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
27             Map<String, Integer> header = parser.getHeaderMap();
28             System.out.println(header);
29         }
30         return null;
31     }
32     
33     public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
34         return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
35     }
36     
37     public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
38         if (firstAsHeader)
39             format = format.withFirstRecordAsHeader();
40         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
41             return parser.getHeaderMap();
42         }
43     }
44
45     public static Map<String, Character> getSupportedCSVDelimiterFormats() {
46         Map<String, Character> delimiters = new HashMap<>();
47         delimiters.put("Comma", ',');
48         delimiters.put("Semicolon", ';');
49         delimiters.put("Tabulator", '\t');
50         return delimiters;
51     }
52
53     public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
54         return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
55     }
56
57     public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
58         if (firstAsHeader)
59             format = format.withFirstRecordAsHeader();
60         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
61             int start = 0;
62             List<Map<String, String>> results = new ArrayList<>(amount);
63             Iterator<CSVRecord> iter = parser.iterator();
64             while (start < amount && iter.hasNext()) {
65                 CSVRecord record = iter.next();
66                 results.add(record.toMap());
67                 start++;
68             }
69             return results;
70         }
71     }
72
73     public static List<CSVRecord> readRows(Path source, char delim, int rowAmount) throws IOException {
74         List<CSVRecord> results = new ArrayList<>();
75         CSVFormat format = CSVFormat.newFormat(delim);
76         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
77             Iterator<CSVRecord> records = parser.iterator();
78             int rows = 0;
79             if (rowAmount == -1) {
80                 while (records.hasNext()) {
81                     results.add(records.next());
82                     rows++;
83                 }
84             } else {
85                 while (rows < rowAmount && records.hasNext()) {
86                     results.add(records.next());
87                     rows++;
88                 }
89             }
90         }
91         return results;
92     }
93
94     
95     public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
96         Map<CSVHeader, List<String>> results = new HashMap<>();
97         CSVFormat format = CSVFormat.newFormat(delimiter);
98         if (firstAsHeader)
99             format = format.withFirstRecordAsHeader();
100         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
101             Map<String, Integer> headers = parser.getHeaderMap();
102             if (headers != null && !headers.isEmpty()) {
103                 for (int index = 0; index < headers.size(); index++) {
104                     for (String head : headers.keySet()) {
105                         results.put(new CSVHeader(head, index), new ArrayList<>());
106                     }
107                 }
108             }
109             
110             Iterator<CSVRecord> records = parser.iterator();
111             int rows = 0;
112             while (rows < amount && records.hasNext()) {
113                 CSVRecord record = records.next();
114                 for (int j = 0; j < record.size(); j++) {
115                     String value = record.get(j);
116                     String header = Integer.toString(j);
117                     CSVHeader csvHeader = new CSVHeader(header, j);
118                     List<String> vals = results.get(csvHeader);
119                     if (vals == null) {
120                         vals = new ArrayList<>();
121                         results.put(csvHeader, vals);
122                     }
123                     vals.add(value);
124                 }
125                 rows++;
126             }
127         }
128         return results;
129     }
130     
131     public static class CSVHeader {
132
133         private final String header;
134         private final int index;
135         
136         public CSVHeader(String header, int index) {
137             this.header = header;
138             this.index = index;
139         }
140
141         public String getHeader() {
142             return header;
143         }
144
145         public int getIndex() {
146             return index;
147         }
148         
149         @Override
150         public int hashCode() {
151             final int prime = 31;
152             int result = 1;
153             result = prime * result + ((header == null) ? 0 : header.hashCode());
154             result = prime * result + index;
155             return result;
156         }
157
158         @Override
159         public boolean equals(Object obj) {
160             if (this == obj)
161                 return true;
162             if (obj == null)
163                 return false;
164             if (getClass() != obj.getClass())
165                 return false;
166             CSVHeader other = (CSVHeader) obj;
167             if (header == null) {
168                 if (other.header != null)
169                     return false;
170             } else if (!header.equals(other.header))
171                 return false;
172             if (index != other.index)
173                 return false;
174             return true;
175         }
176     }
177
178     public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
179         Set<String> results = new HashSet<>();
180         CSVFormat format = CSVFormat.newFormat(delim);
181         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
182             Iterator<CSVRecord> records = parser.iterator();
183             if (records.hasNext())
184                 records.next();
185             while (records.hasNext()) {
186                 CSVRecord row = records.next();
187                 String value = row.get(mappingIndex);
188                 results.add(value);
189             }
190         }
191         return results;
192     }
193
194 }