view flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java @ 769:321b9e480f72

Added a method to river to find the gauge with the max overlap with a given interval. flys-backend/trunk@2235 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 26 Jun 2011 16:13:08 +0000
parents aa9e3da95c31
children
line wrap: on
line source
package de.intevation.flys.importer;

import java.io.File;

import java.util.List;
import java.util.ArrayList;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import de.intevation.flys.utils.FileTools;

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*(.+)");

    protected ArrayList<ImportRiver> rivers;

    protected AnnotationClassifier annotationClassifier;

    public InfoGewParser() {
        this(null);
    }

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

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

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

    public void parse(File file) throws IOException {

        LineNumberReader in = null;

        File root = file.getParentFile();

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

            String line = null;

            String riverName  = 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()) {
                    String river = m.group(1);
                    log.info("Found river '" + river + "'");
                    if (riverName != null) {
                        rivers.add(new ImportRiver(
                            riverName,
                            wstFile,
                            bbInfoFile,
                            annotationClassifier));
                    }
                    riverName  = river;
                    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);
                    log.info("Found wst file '" + wst + "'");
                    if (!wst.isFile() || !wst.canRead()) {
                        log.warn("cannot access WST file '" + wstFilename + "'");
                        continue;
                    }
                    wstFile = wst;
                }
                else if ((m = BB_INFO.matcher(line)).matches()) {
                    //TODO: Make it relative to the wst file.
                    String bbInfo = m.group(1);
                    bbInfoFile = new File(normalize(bbInfo));
                }
            }
            if (riverName != null) {
                rivers.add(new ImportRiver(
                    riverName,
                    wstFile,
                    bbInfoFile,
                    annotationClassifier));
            }
        }
        finally {
            if (in != null) {
                in.close();
            }
        }

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

http://dive4elements.wald.intevation.org