view flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.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 13596605e81f
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.ImportSQRelation;
import de.intevation.flys.importer.ImportSQRelationValue;
import de.intevation.flys.importer.ImportTimeInterval;


public class SQRelationParser extends LineParser {

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

    private static final Pattern TIMERANGE_REGEX =
        Pattern.compile(".*Zeitraum.*\\s(\\w*)-(\\w*).*");

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


    private List<ImportSQRelation> relations;

    private ImportSQRelation current;

    private String currentDescription;


    public SQRelationParser() {
        relations = new ArrayList<ImportSQRelation>();
    }


    public List<ImportSQRelation> getSQRelations() {
        return relations;
    }

    @Override
    public void parse(File file) throws IOException {
        this.currentDescription = file.getName();
        super.parse(file);
    }


    @Override
    protected void reset() {
        current = new ImportSQRelation();
    }


    @Override
    protected void finish() {
        if (current != null) {
            current.setDescription(currentDescription);
            relations.add(current);
        }
    }


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


    protected void handleMetaLine(String line) {
        Matcher m = TIMERANGE_REGEX.matcher(line);

        if (m.matches()) {
            String lo = m.group(1);
            String hi = m.group(2);

            log.debug("Found timerange " + lo + " - " + hi);

            try {
                int low  = nf.parse(lo).intValue();
                int high = nf.parse(hi).intValue();

                current.setTimeInterval(new ImportTimeInterval(
                    getDateFromYear(low),
                    getDateFromYear(high)
                ));
            }
            catch (ParseException nfe) {
                log.warn("Cannot parse time range.", nfe);
            }
        }
    }


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

        if (cols.length < 8) {
            log.warn("skip invalid data line: '" + line + "'");
            return;
        }

        try {
            current.addValue(new ImportSQRelationValue(
                cols[1],
                cols[2],
                cols[4],
                nf.parse(cols[3]).doubleValue(),
                nf.parse(cols[6]).doubleValue(),
                nf.parse(cols[7]).doubleValue()
            ));
        }
        catch (ParseException pe) {
            log.warn("Error while parsing sq relation row: '" + line + "'", pe);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org