view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TsvHelper.java @ 9559:ba0561906f81

Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
author gernotbelger
date Wed, 24 Oct 2018 18:40:38 +0200
parents 787fc085459b
children 694062b1875a
line wrap: on
line source
/** 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 maxLength) 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 > maxLength) {
                        reader.close();
                        throw new TsvReaderException("Invalid number of Tokens; should not be more than" + maxLength + "!");
                    }
                    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;
    }
}

http://dive4elements.wald.intevation.org