diff flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java @ 2816:70b4a31a3306

Implemented the method stubs of the parser for sediment density and made some db schema adaptions. flys-backend/trunk@4233 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Apr 2012 10:59:15 +0000
parents 3febaed762b8
children 8979f2294af9
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java	Fri Apr 13 09:57:37 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java	Fri Apr 13 10:59:15 2012 +0000
@@ -1,11 +1,22 @@
 package de.intevation.flys.importer.parsers;
 
+import java.math.BigDecimal;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.log4j.Logger;
 
+import de.intevation.flys.importer.ImportDepth;
 import de.intevation.flys.importer.ImportSedimentDensity;
+import de.intevation.flys.importer.ImportSedimentDensityValue;
+import de.intevation.flys.importer.ImportUnit;
 
 
 public class SedimentDensityParser extends LineParser {
@@ -14,6 +25,16 @@
         Logger.getLogger(SedimentDensityParser.class);
 
 
+    public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
+
+
+    public static final Pattern META_UNIT =
+        Pattern.compile("^Einheit: \\[(.*)\\].*");
+
+    public static final Pattern META_DEPTH =
+        Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
+
+
     protected List<ImportSedimentDensity> sedimentDensities;
 
     protected ImportSedimentDensity current;
@@ -40,7 +61,92 @@
 
     @Override
     protected void handleLine(String line) {
-        log.debug("handle line: '" + line + "'");
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+        }
+        else {
+            handleDataLine(line);
+        }
+    }
+
+
+    protected void handleMetaLine(String line) {
+        if (handleMetaUnit(line)) {
+            return;
+        }
+        else if (handleMetaDepth(line)) {
+            return;
+        }
+        else {
+            log.warn("Unknown meta line: '" + line + "'");
+        }
+    }
+
+
+    protected boolean handleMetaUnit(String line) {
+        Matcher m = META_UNIT.matcher(line);
+
+        if (m.matches()) {
+            String unit = m.group(1);
+
+            current.setUnit(new ImportUnit(unit));
+
+            return true;
+        }
+
+        return false;
+    }
+
+
+    protected boolean handleMetaDepth(String line) {
+        Matcher m = META_DEPTH.matcher(line);
+
+        if (m.matches()) {
+            String lo   = m.group(1);
+            String up   = m.group(2);
+            String unit = m.group(4);
+
+            try {
+                ImportDepth depth = new ImportDepth(
+                    new BigDecimal(nf.parse(lo).doubleValue()),
+                    new BigDecimal(nf.parse(up).doubleValue()),
+                    new ImportUnit(unit)
+                );
+
+                current.setDepth(depth);
+
+                return true;
+            }
+            catch (ParseException pe) {
+                log.warn("Error while parsing numbers in: '" + line + "'");
+            }
+        }
+
+        return false;
+    }
+
+
+    protected void handleDataLine(String line) {
+        String[] vals = line.split(SEPERATOR_CHAR);
+
+        if (vals == null || vals.length < 3) {
+            log.warn("skip invalid data line: '" + line + "'");
+            return;
+        }
+
+        try {
+            BigDecimal km      = new BigDecimal(nf.parse(vals[0]).doubleValue());
+            BigDecimal density = new BigDecimal(nf.parse(vals[1]).doubleValue());
+
+            current.addValue(new ImportSedimentDensityValue(
+                km,
+                density,
+                vals[2])
+            );
+        }
+        catch (ParseException pe) {
+            log.warn("Error while parsing numbers in '" + line + "'");
+        }
     }
 
 

http://dive4elements.wald.intevation.org