diff artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TsvHelper.java @ 9481:787fc085459b

TSV introduced; uinfo.inundationWMS-Config
author gernotbelger
date Wed, 12 Sep 2018 10:55:09 +0200
parents
children ba0561906f81
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TsvHelper.java	Wed Sep 12 10:55:09 2018 +0200
@@ -0,0 +1,76 @@
+/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
+ * Software engineering by
+ *  Björnsen Beratende Ingenieure GmbH
+ *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+package org.dive4elements.river.artifacts.sinfo.tkhstate;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.dive4elements.artifacts.common.utils.Config;
+
+import au.com.bytecode.opencsv.CSVReader;
+
+/**
+ * @author Domenico Nardi Tironi
+ *
+ */
+public class TsvHelper {
+
+    public static class TsvReaderException extends Exception {
+        private static final long serialVersionUID = 1L;
+
+        public TsvReaderException(final String message) {
+            super(message);
+        }
+    }
+
+    public static List<String[]> readTsv(final File inputFile, final int requiredLength) throws IOException, TsvReaderException {
+
+        final List<String[]> result = new ArrayList<>();
+
+        final byte[] BOM = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
+        final String bomChar = new String(BOM, "UTF-8");
+
+        try (final BufferedReader bReader = new BufferedReader(new FileReader(inputFile))) {
+
+            try (final CSVReader reader = new CSVReader(bReader, '\t')) {
+                String[] line;
+                while ((line = reader.readNext()) != null) {
+                    if (line == null || line[0].startsWith("#") || line[0].trim().equals("") || (line[0].startsWith(bomChar) && line[0].contains("#"))) {
+                        continue;
+                    }
+
+                    if (line.length != requiredLength) {
+                        reader.close();
+                        throw new TsvReaderException("Invalid number of Tokens; should be " + requiredLength + "!");
+                    }
+                    result.add(line);
+                }
+            }
+            return result;
+        }
+    }
+
+    public static final File makeFile2(final String fileNameRaw, final String rivername) {
+        final File configDir = Config.getModulesConfigDirectory();
+        final String filename = String.format(fileNameRaw, rivername);
+        return new File(configDir, filename);
+    }
+
+    public static final File checkFile(final File file) {
+        if (!file.canRead() && !file.isFile()) {
+            return null; // no config-file specified or spelling mistake etc. (not necessarily an error)
+        }
+        return file;
+    }
+}
\ No newline at end of file

http://dive4elements.wald.intevation.org