view backend/src/main/java/org/dive4elements/river/importer/parsers/InfoGewParser.java @ 9650:a2a42a6bac6b

Importer (s/u-info) extensions: outer try/catch for parse and log of line no, catching parsing exception if not enough value fields, parsing error and warning log messages with line number, detecting and rejecting duplicate data series, better differentiation between error and warning log messages
author mschaefer
date Mon, 23 Mar 2020 14:57:03 +0100
parents 5e38e2924c07
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.importer.parsers;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.dive4elements.artifacts.common.utils.FileTools;
import org.dive4elements.river.importer.ImportRiver;


/** Processes files mentioned in an info file for a river. */
public class InfoGewParser
{
    private static Logger log = Logger.getLogger(InfoGewParser.class);

    public static final String ENCODING = "ISO-8859-1";

    public static final Pattern GEWAESSER =
            Pattern.compile("^\\s*Gew\u00e4sser\\s*:\\s*(.+)");

    public static final Pattern WST_DATEI =
            Pattern.compile("^\\s*WSTDatei\\s*:\\s*(.+)");

    public static final Pattern BB_INFO =
            Pattern.compile("^\\s*B\\+B-Info\\s*:\\s*(.+)");

    public static final Pattern GEW_UUID =
            Pattern.compile("^\\s*uuid\\s*:\\s*(.+)");

    protected ArrayList<ImportRiver> rivers;

    protected AnnotationClassifier annotationClassifier;

    public InfoGewParser() {
        this(null);
    }

    public InfoGewParser(final AnnotationClassifier annotationClassifier) {
        this.rivers = new ArrayList<>();
        this.annotationClassifier = annotationClassifier;
    }

    public List<ImportRiver> getRivers() {
        return this.rivers;
    }

    public static final String normalize(final String f) {
        return f.replace("\\", "/").replace("/", File.separator);
    }

    /** Handle a gew, wst, or bb_info file. */
    public void parse(final File file) throws Exception {

        LineNumberReader in = null;

        final File root = file.getParentFile();

        try {
            in =
                    new LineNumberReader(
                            new InputStreamReader(
                                    new FileInputStream(file), ENCODING));

            String line = null;

            String riverName  = null;
            String modelUuid  = null;
            File   wstFile    = null;
            File   bbInfoFile = null;

            while ((line = in.readLine()) != null) {
                if ((line = line.trim()).length() == 0) {
                    continue;
                }
                Matcher m = GEWAESSER.matcher(line);

                if (m.matches()) {
                    final String river = m.group(1);
                    log.info("Found river '" + river + "'");
                    if (riverName != null) {
                        this.rivers.add(new ImportRiver(
                                riverName,
                                modelUuid,
                                wstFile,
                                bbInfoFile,
                                this.annotationClassifier));
                    }
                    riverName  = river;
                    modelUuid  = null;
                    wstFile    = null;
                    bbInfoFile = null;
                }
                else if ((m = WST_DATEI.matcher(line)).matches()) {
                    String wstFilename = m.group(1);
                    File wst = new File(wstFilename = normalize(wstFilename));
                    if (!wst.isAbsolute()) {
                        wst = new File(root, wstFilename);
                    }
                    wst = FileTools.repair(wst);
                    if (!wst.isFile() || !wst.canRead()) {
                        log.error(
                                "cannot access WST file '" + wstFilename + "'");
                        continue;
                    }
                    log.info("Found wst file '" + wst + "'");
                    wstFile = wst;
                }
                else if ((m = GEW_UUID.matcher(line)).matches()) {
                    modelUuid = m.group(1);
                    log.debug("Found model uuid " + modelUuid +
                            " for river " + riverName);
                }
                else if ((m = BB_INFO.matcher(line)).matches()) {
                    //TODO: Make it relative to the wst file.
                    final String bbInfo = m.group(1);
                    bbInfoFile = new File(normalize(bbInfo));
                }
            }
            if (riverName != null && wstFile != null) {
                this.rivers.add(new ImportRiver(
                        riverName,
                        modelUuid,
                        wstFile,
                        bbInfoFile,
                        this.annotationClassifier));
            }
        }
        finally {
            if (in != null) {
                in.close();
            }
        }

        for (final ImportRiver river: this.rivers) {
            river.parseDependencies();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org