X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.imports%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fimports%2FDistrictImportUtils.java;h=f123855dfc47e32be31d2268a05e6d3401ca33f4;hb=231b9da5dfd8129e24cb5a9d2ec87ff9afa7afd9;hp=7c0d998c20e7f4688d3bed8b2397d2c85054f50f;hpb=31bcbe46c93a576cd6ade673a745ea9cb28672da;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java index 7c0d998c..f123855d 100644 --- a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java +++ b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java @@ -1,156 +1,193 @@ -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.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -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 header = parser.getHeaderMap(); - System.out.println(header); - } - return null; - } - - public static Map readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException { - return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader); - } - - public static Map 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 getSupportedCSVDelimiterFormats() { - Map delimiters = new HashMap<>(); - delimiters.put("COMMA", ','); - delimiters.put("SEMICOMMA", ';'); - return delimiters; - } - - public static List> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException { - return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount); - } - - public static List> 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> results = new ArrayList<>(amount); - Iterator iter = parser.iterator(); - while (start < amount && iter.hasNext()) { - CSVRecord record = iter.next(); - results.add(record.toMap()); - start++; - } - return results; - } - } - - - public static Map> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException { - Map> results = new HashMap<>(); - CSVFormat format = CSVFormat.newFormat(delimiter); - if (firstAsHeader) - format = format.withFirstRecordAsHeader(); - try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { - Map 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 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 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; - } - } - -} +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 header = parser.getHeaderMap(); + System.out.println(header); + } + return null; + } + + public static Map readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException { + return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader); + } + + public static Map 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 getSupportedCSVDelimiterFormats() { + Map delimiters = new HashMap<>(); + delimiters.put("Comma", ','); + delimiters.put("Semicolon", ';'); + return delimiters; + } + + public static List> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException { + return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount); + } + + public static List> 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> results = new ArrayList<>(amount); + Iterator iter = parser.iterator(); + while (start < amount && iter.hasNext()) { + CSVRecord record = iter.next(); + results.add(record.toMap()); + start++; + } + return results; + } + } + + public static List readRows(Path source, char delim, int rowAmount) throws IOException { + List results = new ArrayList<>(); + CSVFormat format = CSVFormat.newFormat(delim); + try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { + Iterator 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> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException { + Map> results = new HashMap<>(); + CSVFormat format = CSVFormat.newFormat(delimiter); + if (firstAsHeader) + format = format.withFirstRecordAsHeader(); + try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { + Map 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 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 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 readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException { + Set results = new HashSet<>(); + CSVFormat format = CSVFormat.newFormat(delim); + try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { + Iterator 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; + } + +}