[Dive4elements-commits] [PATCH 1 of 3] Added BundesWasserStrassenParser

Wald Commits scm-commit at wald.intevation.org
Tue Feb 19 11:57:00 CET 2013


# HG changeset patch
# User Felix Wolfsteller <felix.wolfsteller at intevation.de>
# Date 1361271709 -3600
# Node ID c872249ec9a9268471dd24f9e742e32de7f9b3b7
# Parent  e939098ee272a6cc237473f1cf8f573cf3496374
Added BundesWasserStrassenParser.

diff -r e939098ee272 -r c872249ec9a9 flys-backend/src/main/java/de/intevation/flys/importer/parsers/BundesWasserStrassenParser.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BundesWasserStrassenParser.java	Tue Feb 19 12:01:49 2013 +0100
@@ -0,0 +1,85 @@
+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.HashMap;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.importer.ImportMorphWidth;
+import de.intevation.flys.importer.ImportMorphWidthValue;
+import de.intevation.flys.importer.ImportUnit;
+
+/** Parse CSV file that contains official numbers for rivers. */
+public class BundesWasserStrassenParser extends LineParser {
+
+    /** Private logger. */
+    private static final Logger logger =
+        Logger.getLogger(BundesWasserStrassenParser.class);
+
+    /** Map from rivernames to Official numbers. */
+    private HashMap<String,Long> numberMap;
+
+
+    public BundesWasserStrassenParser() {
+        numberMap = new HashMap<String,Long>();
+    }
+
+
+    /** No need to reset. */
+    @Override
+    protected void reset() {
+    }
+
+
+    /** No action needed on eof. */
+    @Override
+    protected void finish() {
+    }
+
+
+    /** Handle a line of the bwastr-id file. */
+    @Override
+    protected void handleLine(int lineNum, String line) {
+        String[] vals = line.split(",");
+        if (vals.length != 2) {
+            logger.warn("Invalid bwastr-id line:\n" + line);
+            return;
+        }
+        try{
+            String name = unwrap(vals[0].toLowerCase());
+            String numberStr = unwrap(vals[1]);
+            Long number = Long.valueOf(numberStr);
+            numberMap.put(name, number);
+        }
+        catch (NumberFormatException e) {
+            logger.warn("Invalid number in bwastr-id line:\n" + line);
+        }
+    }
+
+
+    /** Get river->official number mapping. */
+    public HashMap<String,Long> getMap() {
+        return numberMap;
+    }
+
+
+    /** Remove leading and trailing quotes. */
+    protected String unwrap(String input) {
+        if (input.startsWith("\"")) {
+            input = input.substring(1);
+        }
+        if (input.endsWith("\"")) {
+            input = input.substring(0, input.length() - 1);
+        }
+        return input;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list