view flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java @ 4198:1cdbd8a0c994

Added two new tables ClickableQDTable and ClickableWTable and made Ws and Qs clickable in historical discharge calculation. The new tables define listener interfaces (clicked lower or upper icon) to listen to user clicks. In addition to this, there is an enum ClickMode with NONE, SINGLE and RANGE options, which allows to specifiy, which icons are displayed in the tables. NONE means no icon for user clicks, SINGLE has 1 icon, RANGE 2 icons for lower and upper.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 22 Oct 2012 13:31:25 +0200
parents f63b39799d2d
children 016616df35dc
line wrap: on
line source
package de.intevation.flys.importer.parsers;

import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import de.intevation.flys.importer.ImportUnit;
import de.intevation.flys.importer.ImportWaterlevelDifference;
import de.intevation.flys.importer.ImportWaterlevelDifferenceColumn;
import de.intevation.flys.importer.ImportWaterlevelDifferenceValue;


public class WaterlevelDifferencesParser extends LineParser {

    private static final Logger log =
        Logger.getLogger(WaterlevelDifferencesParser.class);

    private static final NumberFormat nf =
        NumberFormat.getInstance(DEFAULT_LOCALE);

    public static final Pattern META_UNIT =
        Pattern.compile("^Einheit: \\[(.*)\\].*");


    private List<ImportWaterlevelDifference> differences;

    private ImportWaterlevelDifferenceColumn[] columns;

    private ImportWaterlevelDifference current;


    public WaterlevelDifferencesParser() {
        differences = new ArrayList<ImportWaterlevelDifference>();
    }


    public List<ImportWaterlevelDifference> getDifferences() {
        return differences;
    }


    @Override
    public void parse(File file) throws IOException {
        current = new ImportWaterlevelDifference(file.getName());

        super.parse(file);
    }


    @Override
    protected void reset() {
    }


    @Override
    protected void finish() {
        if (columns != null && current != null) {
            for (ImportWaterlevelDifferenceColumn col: columns) {
                current.addValue(col);
            }

            differences.add(current);
        }

        current = null;
        columns = null;
    }

    @Override
    protected void handleLine(int lineNum, String line) {
        if (line.startsWith(START_META_CHAR)) {
            handleMetaLine(stripMetaLine(line));
        }
        else {
            handleDataLine(line);
        }
    }


    private void handleMetaLine(String meta) {
        if (handleMetaUnit(meta)) {
            return;
        }
        else {
            handleMetaColumnNames(meta);
        }
    }


    private boolean handleMetaUnit(String meta) {
        Matcher m = META_UNIT.matcher(meta);

        if (m.matches()) {
            String unit = m.group(1);
            log.debug("Found unit: '" + unit + "'");

            current.setUnit(new ImportUnit(unit));

            return true;
        }

        return false;
    }


    private boolean handleMetaColumnNames(String meta) {
        Pattern META_COLUMN_NAMES = Pattern.compile("Fluss-km;(.*)");
        Matcher m = META_COLUMN_NAMES.matcher(meta);

        if (m.matches()) {
            String colStr = m.group(1);
            String[] cols = colStr.split(SEPERATOR_CHAR);

            log.debug("Found " + cols.length + " columns.");

            initColumns(cols);

            return true;
        }

        return false;
    }


    private void initColumns(String[] cols) {
        columns = new ImportWaterlevelDifferenceColumn[cols.length];

        for (int i = 0; i < cols.length; i++) {
            String name = cols[i].replace("\"", "");

            log.debug("Create new column '" + name + "'");
            columns[i] = new ImportWaterlevelDifferenceColumn(name);
        }
    }


    private void handleDataLine(String line) {
        String[] cols = line.split(SEPERATOR_CHAR);

        if (cols == null || cols.length < 2) {
            log.warn("skip invalid waterlevel line: '" + line + "'");
            return;
        }

        try {
            Double station = nf.parse(cols[0]).doubleValue();

            for (int i = 0; i < columns.length; i++) {
                int idx = i+1;

                if (idx >= cols.length) {
                    log.warn("Insufficient column numbers: " + line);
                    continue;
                }

                String value = cols[idx];

                try {
                    columns[i].addValue(new ImportWaterlevelDifferenceValue(
                        station,
                        nf.parse(value).doubleValue()
                    ));
                }
                catch (ParseException pe) {
                    log.warn("Error while parsing value: '" + value + "'");
                }
            }
        }
        catch (ParseException pe) {
            log.warn("Error while parsing station: '" + line + "'");
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org