9481
|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by |
|
3 * Björnsen Beratende Ingenieure GmbH |
|
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt |
|
5 * |
|
6 * This file is Free Software under the GNU AGPL (>=v3) |
|
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
8 * documentation coming with Dive4Elements River for details. |
|
9 */ |
|
10 package org.dive4elements.river.artifacts.sinfo.tkhstate; |
|
11 |
|
12 import java.io.BufferedReader; |
|
13 import java.io.File; |
|
14 import java.io.FileReader; |
|
15 import java.io.IOException; |
|
16 import java.util.ArrayList; |
|
17 import java.util.List; |
|
18 |
|
19 import org.dive4elements.artifacts.common.utils.Config; |
|
20 |
|
21 import au.com.bytecode.opencsv.CSVReader; |
|
22 |
|
23 /** |
|
24 * @author Domenico Nardi Tironi |
|
25 * |
|
26 */ |
|
27 public class TsvHelper { |
|
28 |
|
29 public static class TsvReaderException extends Exception { |
|
30 private static final long serialVersionUID = 1L; |
|
31 |
|
32 public TsvReaderException(final String message) { |
|
33 super(message); |
|
34 } |
|
35 } |
|
36 |
|
37 public static List<String[]> readTsv(final File inputFile, final int requiredLength) throws IOException, TsvReaderException { |
|
38 |
|
39 final List<String[]> result = new ArrayList<>(); |
|
40 |
|
41 final byte[] BOM = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; |
|
42 final String bomChar = new String(BOM, "UTF-8"); |
|
43 |
|
44 try (final BufferedReader bReader = new BufferedReader(new FileReader(inputFile))) { |
|
45 |
|
46 try (final CSVReader reader = new CSVReader(bReader, '\t')) { |
|
47 String[] line; |
|
48 while ((line = reader.readNext()) != null) { |
|
49 if (line == null || line[0].startsWith("#") || line[0].trim().equals("") || (line[0].startsWith(bomChar) && line[0].contains("#"))) { |
|
50 continue; |
|
51 } |
|
52 |
|
53 if (line.length != requiredLength) { |
|
54 reader.close(); |
|
55 throw new TsvReaderException("Invalid number of Tokens; should be " + requiredLength + "!"); |
|
56 } |
|
57 result.add(line); |
|
58 } |
|
59 } |
|
60 return result; |
|
61 } |
|
62 } |
|
63 |
|
64 public static final File makeFile2(final String fileNameRaw, final String rivername) { |
|
65 final File configDir = Config.getModulesConfigDirectory(); |
|
66 final String filename = String.format(fileNameRaw, rivername); |
|
67 return new File(configDir, filename); |
|
68 } |
|
69 |
|
70 public static final File checkFile(final File file) { |
|
71 if (!file.canRead() && !file.isFile()) { |
|
72 return null; // no config-file specified or spelling mistake etc. (not necessarily an error) |
|
73 } |
|
74 return file; |
|
75 } |
|
76 } |