diff flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java @ 3328:a41f279a66e2

Added parser and import classes to import MINFO sq relations. flys-backend/trunk@4646 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 13 Jun 2012 06:22:04 +0000
parents
children cc8fc6b29649
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java	Wed Jun 13 06:22:04 2012 +0000
@@ -0,0 +1,114 @@
+package de.intevation.flys.importer.parsers;
+
+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;
+
+
+    public SQRelationParser() {
+        relations = new ArrayList<ImportSQRelation>();
+    }
+
+
+    public List<ImportSQRelation> getSQRelations() {
+        return relations;
+    }
+
+    @Override
+    protected void reset() {
+        current = new ImportSQRelation();
+    }
+
+
+    @Override
+    protected void finish() {
+        relations.add(current);
+    }
+
+
+    @Override
+    protected void handleLine(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