view flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java @ 1196:46127af605ba

Added parser for PRF files. TODO: Extract data and km. flys-backend/trunk@2296 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 06 Jul 2011 17:54:49 +0000
parents
children ce3dacc6ea92
line wrap: on
line source
package de.intevation.flys.importer;

import java.util.Map;
import java.util.Stack;
import java.util.TreeMap;

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

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

import org.apache.log4j.Logger;

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

    public static final String ENCODING =
        System.getProperty("flys.backend.prf.encoding", "ISO-8859-1");

    public static final Pattern DATA_PATTERN =
        Pattern.compile(
            "\\((\\d+)x\\s*,\\s*(\\d+)\\(" +
            "\\s*f(\\d+)\\.(\\d+)\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?\\)?");

    public static final Pattern KM_PATTERN =
        Pattern.compile("\\((\\d+)x\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?");

    public static class DataFormat {

        protected int deleteChars;
        protected int maxRepetitions;
        protected int firstIntegerPlaces;
        protected int firstFractionPlaces;
        protected int secondIntegerPlaces;
        protected int secondFractionPlaces;

        public DataFormat() {
        }

        public DataFormat(Matcher m) {
            deleteChars          = Integer.parseInt(m.group(1));
            maxRepetitions       = Integer.parseInt(m.group(2));
            firstIntegerPlaces   = Integer.parseInt(m.group(3));
            firstFractionPlaces  = Integer.parseInt(m.group(4));
            secondIntegerPlaces  = Integer.parseInt(m.group(5));
            secondFractionPlaces = Integer.parseInt(m.group(6));
        }

        public void extractData(String line, Map<Double, Double> dest) {
            //TODO: Implement me!
        }
    } // class DataFormat

    public static class KMFormat {
        protected int deleteChars;
        protected int integerPlaces;
        protected int fractionPlaces;

        public KMFormat() {
        }

        public KMFormat(Matcher m) {
            deleteChars    = Integer.parseInt(m.group(1));
            integerPlaces  = Integer.parseInt(m.group(2));
            fractionPlaces = Integer.parseInt(m.group(3));
        }

        public double extractKm(String line) {
            // TODO: Implement me!
            return 0d;
        }
    } // class KMFormat

    protected Map<Double, Map<Double, Double>> data;

    public PRFParser() {
        data = new TreeMap<Double, Map<Double, Double>>();
    }


    public boolean parse(File file) {

        if (!(file.isFile() && file.canRead())) {
            log.warn("cannot open file '" + file + "'");
            return false;
        }

        log.info("parsing PRF file: '" + file + "'");

        LineNumberReader in = null;

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

            String line = in.readLine();

            if (line == null || (line = line.trim()).length() == 0) {
                log.warn("file is empty.");
                return false;
            }

            Matcher m = DATA_PATTERN.matcher(line);

            if (!m.matches()) {
                log.warn("First line does not look like a PRF data pattern.");
                return false;
            }

            DataFormat dataFormat = new DataFormat(m);

            if ((line = in.readLine()) == null) {
                log.warn("premature EOF. Expected integer in line 2");
                return false;
            }

            try {
                if (Integer.parseInt(line) != dataFormat.maxRepetitions) {
                    log.warn("Expected " +
                        dataFormat.maxRepetitions + " in line 2");
                    return false;
                }
            }
            catch (NumberFormatException nfe) {
                log.warn("invalid integer in line 2", nfe);
                return false;
            }

            if ((line = in.readLine()) == null) {
                log.warn(
                    "premature EOF. Expected pattern for km extraction");
                return false;
            }

            m = KM_PATTERN.matcher(line);

            if (!m.matches()) {
                log.warn(
                    "line 4 does not look like a PRF km extraction pattern.");
                return false;
            }

            while ((line = in.readLine()) != null) {
                if (line.length() == 0) {
                    continue;
                }
                // TODO: Do data and km extraction
            }
        }
        catch (IOException ioe) {
            log.error(ioe);
            return false;
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (IOException ioe) {
                    log.error(ioe);
                }
            }
        }

        return true;
    }

    public void reset() {
        data.clear();
    }

    public static void parsePRFs(File root) {

        PRFParser parser = new PRFParser();

        Stack<File> stack = new Stack<File>();
        stack.push(root);

        while (!stack.empty()) {
            File file = stack.pop();
            if (file.isDirectory()) {
                File [] files = file.listFiles();
                if (files != null) {
                    for (File f: files) {
                        stack.push(f);
                    }
                }
            }
            else if (file.isFile()
                && file.getName().toLowerCase().endsWith(".prf")
            ) {
                parser.reset();
                boolean success = parser.parse(file);
                log.info("parsing " + (success ? "succeeded" : "failed"));
            }
        }
    }

    public static void main(String [] args) {

        for (String arg: args) {
            parsePRFs(new File(arg));
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org