view flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java @ 487:cf513cbe88c3

Importer: forgot to fetch time interval peer flys-backend/trunk@1815 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 03 May 2011 17:40:02 +0000
parents d37ccb04ab5d
children 9f2204ed79ed
line wrap: on
line source
package de.intevation.flys.importer;

import java.util.HashMap;
import java.util.TreeSet;
import java.util.List;
import java.util.ArrayList;

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

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import de.intevation.flys.utils.FileTools;

public class AnnotationsParser
{
    private static Logger log = Logger.getLogger(AnnotationsParser.class);

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

    public static final String [] TO_SCAN = {
        "Basisdaten",
        "Streckendaten"
    };

    protected HashMap<String, ImportAttribute> attributes;
    protected HashMap<String, ImportPosition>  positions;
    protected TreeSet<ImportAnnotation>        annotations;

    public AnnotationsParser() {
        attributes  = new HashMap<String, ImportAttribute>();
        positions   = new HashMap<String, ImportPosition>();
        annotations = new TreeSet<ImportAnnotation>();
    }

    public void parseFile(File file) throws IOException {
        log.info("parsing km file: '" + file + "'");
        LineNumberReader in = null;
        try {
            in =
                new LineNumberReader(
                new InputStreamReader(
                new FileInputStream(file), ENCODING));

            String line = null;
            while ((line = in.readLine()) != null) {
                if ((line = line.trim()).length() == 0
                || line.startsWith("*")) {
                    continue;
                }

                String [] parts = line.split("\\s*;\\s*");

                if (parts.length < 3) {
                    log.warn("not enough columns in line "
                        + in.getLineNumber());
                    continue;
                }

                ImportPosition position = positions.get(parts[0]);
                if (position == null) {
                    position = new ImportPosition(parts[0]);
                    positions.put(parts[0], position);
                }

                ImportAttribute attribute = attributes.get(parts[1]);
                if (attribute == null) {
                    attribute = new ImportAttribute(parts[1]);
                    attributes.put(parts[1], attribute);
                }

                String [] r = parts[2].replace(",", ".").split("\\s*#\\s*");

                BigDecimal from, to;

                try {
                    from = new BigDecimal(r[0]);
                    to   = r.length < 2 ? null : new BigDecimal(r[1]);
                    if (to != null && from.compareTo(to) > 0) {
                        BigDecimal t = from; from = to; to = t;
                    }
                }
                catch (NumberFormatException nfe) {
                    log.warn("invalid number in line " + in.getLineNumber());
                    continue;
                }

                ImportRange range = new ImportRange(from, to);

                ImportAnnotation annotation = new ImportAnnotation(
                    attribute, position, range);

                if (!annotations.add(annotation)) {
                    log.debug("duplicated annotation in line "
                        + in.getLineNumber());
                }
            }
        }
        finally {
            if (in != null) {
                in.close();
            }
        }
    }

    public void parse(File root) throws IOException {

        for (String toScan: TO_SCAN) {
            File directory = FileTools.repair(new File(root, toScan));
            if (!directory.isDirectory()) {
                log.warn("'" + directory + "' is not a directory.");
                continue;
            }
            File [] files = directory.listFiles();
            if (files == null) {
                log.warn("cannot list directory '" + directory + "'");
                continue;
            }

            for (File file: files) {
                if (file.isFile() && file.canRead()
                && file.getName().toLowerCase().endsWith(".km")) {
                    parseFile(file);
                }
            }
        } // for all directories to scan
    }

    public List<ImportAnnotation> getAnnotations() {
        return new ArrayList<ImportAnnotation>(annotations);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org