Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TsvHelper.java @ 9709:b74f817435fe
comment removed
author | dnt_bjoernsen <d.tironi@bjoernsen.de> |
---|---|
date | Wed, 27 Jan 2021 11:47:38 +0100 |
parents | 694062b1875a |
children |
rev | line source |
---|---|
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 | |
9559
ba0561906f81
Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents:
9481
diff
changeset
|
37 public static List<String[]> readTsv(final File inputFile, final int maxLength) throws IOException, TsvReaderException { |
9481 | 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 | |
9559
ba0561906f81
Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents:
9481
diff
changeset
|
53 if (line.length > maxLength) { |
9481 | 54 reader.close(); |
9634
694062b1875a
zu Pos 21: WMS-Konfiguration
dnt_bjoernsen <d.tironi@bjoernsen.de>
parents:
9559
diff
changeset
|
55 throw new TsvReaderException("Invalid number of Tokens; should not be more than " + maxLength + "!"); |
9481 | 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 } |