changeset 1215:8aef353e54fb

Initial version of the HYK parser. Not ready, yet. flys-backend/trunk@2341 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Jul 2011 16:57:13 +0000
parents 32ee9babe42c
children f8b5c37f15e4
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportHYKFlowZoneType.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java
diffstat 3 files changed, 290 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Fri Jul 15 15:53:33 2011 +0000
+++ b/flys-backend/ChangeLog	Fri Jul 15 16:57:13 2011 +0000
@@ -1,3 +1,11 @@
+2011-07-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/parsers/HYKParser.java:
+	  Initial version of the HYK parser. Not ready, yet.
+
+	* src/main/java/de/intevation/flys/importer/ImportHYKFlowZoneType.java:
+	  Importer model for HYK flow zone types.
+
 2011-07-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* doc/schema/postgresql.sql: Argh! Added distance_{vl|hf|vr} to
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportHYKFlowZoneType.java	Fri Jul 15 16:57:13 2011 +0000
@@ -0,0 +1,41 @@
+package de.intevation.flys.importer;
+
+import de.intevation.flys.model.HYKFlowZoneType;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import java.util.List;
+
+public class ImportHYKFlowZoneType
+{
+    private String          name;
+    private HYKFlowZoneType peer;
+
+    public ImportHYKFlowZoneType() {
+    }
+
+    public ImportHYKFlowZoneType(String name) {
+        this.name = name;
+    }
+
+    public HYKFlowZoneType getPeer() {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance()
+                .getDatabaseSession();
+            Query query = session.createQuery(
+                "from HYKFlowZoneType where name=:name");
+            query.setParameter("name", name);
+            List<HYKFlowZoneType> flowZoneTypes = query.list();
+            if (flowZoneTypes.isEmpty()) {
+                peer = new HYKFlowZoneType(name);
+                session.save(peer);
+            }
+            else {
+                peer = flowZoneTypes.get(0);
+            }
+        }
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java	Fri Jul 15 16:57:13 2011 +0000
@@ -0,0 +1,241 @@
+package de.intevation.flys.importer.parsers;
+
+import de.intevation.flys.importer.ImportHYKFlowZoneType;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import java.math.BigDecimal;
+
+import org.apache.log4j.Logger;
+
+public class HYKParser
+{
+    private static Logger log = Logger.getLogger(HYKParser.class);
+
+    public static enum State {
+        LINE_1, LINE_2, LINE_3, LINE_4, LINE_5, LINE_6
+    };
+
+    private static final String ENCODING = "ISO-8859-1";
+
+    protected Map<String, ImportHYKFlowZoneType> flowZoneTypes;
+
+    public HYKParser() {
+        flowZoneTypes = new HashMap<String, ImportHYKFlowZoneType>();
+    }
+
+    public boolean parse(File file) {
+
+        log.info("Parsing HYK file '" + file + "'");
+
+        LineNumberReader in = null;
+
+        try {
+            in =
+                new LineNumberReader(
+                new InputStreamReader(
+                new FileInputStream(file), ENCODING));
+
+            String line;
+
+            State state = State.LINE_1;
+
+            int numFormations = 0;
+
+            BigDecimal km         = null;
+            BigDecimal top        = null;
+            BigDecimal bottom     = null;
+            BigDecimal distanceVL = null;
+            BigDecimal distanceHF = null;
+            BigDecimal distanceVR = null;
+
+            Integer    year       = null;
+            int        numZones   = 0;
+
+            ImportHYKFlowZoneType [] fzts     = null;
+            BigDecimal            [] coords   = null;
+            int                      coordPos = 0;
+
+            while ((line = in.readLine()) != null) {
+                if ((line = line.trim()).length() == 0) {
+                    continue;
+                }
+                if (line.startsWith("*")) {
+                    continue;
+                }
+                String [] parts = line.split("\\s+");
+
+                switch (state) {
+                    case LINE_1:
+                        if (parts.length < 2) {
+                            log.error("not enough elements in line " +
+                                in.getLineNumber());
+                            return false;
+                        }
+
+                        if (parts.length == 2) {
+                            // no year given
+                            year = null;
+                        }
+                        else {
+                            try {
+                                year = Integer.valueOf(parts[1]);
+                            }
+                            catch (NumberFormatException nfe) {
+                                log.error(
+                                    "year is not an integer in line " +
+                                    in.getLineNumber());
+                                return false;
+                            }
+                        }
+                        try {
+                            km = new BigDecimal(parts[0]);
+                            numFormations = Integer.parseInt(
+                                parts[parts.length > 2 ? 2 : 1]);
+                        }
+                        catch (NumberFormatException nfe) {
+                            log.error(
+                                "parsing number of formations " +
+                                "or km failed in line " + in.getLineNumber());
+                            return false;
+                        }
+                        // TODO: Store HYKEntry
+
+                        state = State.LINE_2;
+                        break;
+
+                    case LINE_2:
+                        if (parts.length < 3) {
+                            log.error("not enough elements in line " +
+                                in.getLineNumber());
+                            return false;
+                        }
+                        try {
+                            numZones = Integer.parseInt(parts[0]);
+                            bottom   = new BigDecimal(parts[1]);
+                            top      = new BigDecimal(parts[2]);
+                        }
+                        catch (NumberFormatException nfe) {
+                            log.error(
+                                "parsing num zones, bottom or top height " +
+                                "failed in line " + in.getLineNumber());
+                            return false;
+                        }
+                        state = State.LINE_3;
+                        break;
+
+                    case LINE_3:
+                        if (parts.length != numZones) {
+                            log.error(
+                                "number of flow zones mismatches " +
+                                "in line " + in.getLineNumber());
+                            return false;
+                        }
+
+                        fzts = new ImportHYKFlowZoneType[parts.length];
+                        for (int i = 0; i < fzts.length; ++i) {
+                            fzts[i] = getFlowZoneType(parts[i]);
+                        }
+                        state = State.LINE_4;
+                        break;
+
+                    case LINE_4:
+                        try {
+                            int N = Math.min(parts.length, coords.length);
+                            for (coordPos = 0; coordPos < N; ++coordPos) {
+                                coords[coordPos] =
+                                    new BigDecimal(parts[coordPos]);
+                            }
+                        }
+                        catch (NumberFormatException nfe) {
+                            log.error("cannot parse number in line " +
+                                in.getLineNumber());
+                            return false;
+                        }
+                        state = State.LINE_5;
+                        break;
+
+                    case LINE_5:
+                        if (parts.length + coordPos < coords.length) {
+                            log.error("not enough elements in line " +
+                                in.getLineNumber());
+                            return false;
+                        }
+                        try {
+                            for (int i = 0; 
+                                i < parts.length && coordPos < coords.length;
+                                ++i, ++coordPos
+                            ) {
+                                coords[coordPos] = new BigDecimal(parts[i]);
+                            }
+                        }
+                        catch (NumberFormatException nfe) {
+                            log.error("cannot parse number in line " + 
+                                in.getLineNumber());
+                            return false;
+                        }
+                        state = State.LINE_6;
+                        break;
+
+                    case LINE_6:
+                        if (parts.length < 3) {
+                            log.error("not enough elements in line " +
+                                in.getLineNumber());
+                            return false;
+                        }
+                        try {
+                            distanceVL = new BigDecimal(parts[0]);
+                            distanceHF = new BigDecimal(parts[1]);
+                            distanceVL = new BigDecimal(parts[2]);
+                        }
+                        catch (NumberFormatException nfe) {
+                            log.error("cannot parse number in line " +
+                                in.getLineNumber());
+                            return false;
+                        }
+                        // TODO: now we have enough information 
+                        //       to store a formation.
+
+                        // continue with next formation.
+                        state = --numFormations > 0 // formations left?
+                            ? State.LINE_2
+                            : State.LINE_1;
+                        break;
+                }
+            }
+        }
+        catch (IOException ioe) {
+            log.error(ioe);
+            return false;
+        }
+        finally {
+            if (in != null) {
+                try {
+                    in.close();
+                }
+                catch (IOException ioe) {
+                    log.error(ioe);
+                }
+            }
+        }
+        return true;
+    }
+
+    protected ImportHYKFlowZoneType getFlowZoneType(String name) {
+        name = name.toUpperCase();
+        ImportHYKFlowZoneType fzt = flowZoneTypes.get(name);
+        if (fzt == null) {
+            fzt = new ImportHYKFlowZoneType(name);
+            flowZoneTypes.put(name, fzt);
+        }
+        return fzt;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org