[PATCH 1 of 3] Handle empty strings as missing values without warning when parsing SQ-relations

Wald Commits scm-commit at wald.intevation.org
Fri Apr 17 19:32:41 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1429275008 -7200
# Node ID 11c88a2f695af4a5a6da841a1b87777e9c4c0f21
# Parent  ea676691e53382d7acd5fe8dacb17da274240196
Handle empty strings as missing values without warning when parsing SQ-relations.

diff -r ea676691e533 -r 11c88a2f695a backend/src/main/java/org/dive4elements/river/importer/parsers/SQRelationParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/SQRelationParser.java	Thu Apr 16 18:38:19 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/SQRelationParser.java	Fri Apr 17 14:50:08 2015 +0200
@@ -119,30 +119,26 @@
     protected void handleDataLine(String line) {
         String[] cols = line.split(SEPERATOR_CHAR);
 
-        if (cols.length < 14) {
-            log.warn("skip invalid data line: '" + line + "'");
-            return;
-        }
-
-        Double km = parseDouble(cols[3], line);
-        Double a = parseDouble(cols[6], line);
-        Double b = parseDouble(cols[7], line);
-        Double qMax = parseDouble(cols[8], line);
-        Double rSq = parseDouble(cols[9], line);
-        Integer nTot = parseInteger(cols[10], line);
-        Integer nOutlier = parseInteger(cols[11], line);
-        Double cFer = parseDouble(cols[12], line);
-        Double cDuan = parseDouble(cols[13], line);
+        String parameter = cols[1].trim();
+        Double km = parseDouble(cols, 3);
+        Double a = parseDouble(cols, 6);
+        Double b = parseDouble(cols, 7);
+        Double qMax = parseDouble(cols, 8);
+        Double rSq = parseDouble(cols, 9);
+        Integer nTot = parseInteger(cols, 10);
+        Integer nOutlier = parseInteger(cols, 11);
+        Double cFer = parseDouble(cols, 12);
+        Double cDuan = parseDouble(cols, 13);
 
         if (km == null || a == null || b == null
-        || qMax == null || cols[1].length() == 0
+        || qMax == null || parameter.length() == 0
         ) {
             if (km == null) {
                 log.error("No km for measurement station: Can not reference measurement station: "
                     + line);
             }
             if (a == null || b == null
-            || qMax == null || cols[1].length() == 0
+            || qMax == null || parameter.length() == 0
             ) {
                 log.error("Incomplete SQ-relation row (missing a, b, Qmax or parameter): "
                     + line);
@@ -170,26 +166,28 @@
         }
     }
 
-    private Double parseDouble(String value, String line) {
-        Double result = null;
-        try {
-            result = Double.valueOf(value.replace(",", "."));
+    private Double parseDouble(String[] values, int idx) {
+        if (idx >= 0 && idx < values.length && !values[idx].isEmpty()) {
+            try {
+                return nf.parse(values[idx]).doubleValue();
+            }
+            catch (ParseException e) {
+                log.warn("Unparseable value '" + values[idx] + "'");
+            }
         }
-        catch (NumberFormatException nfe) {
-            log.warn("Unparseable " + value + " in sq relation row: " + line);
-        }
-        return result;
+        return null;
     }
 
-    private Integer parseInteger(String value, String line) {
-        Integer result = null;
-        try {
-            result = Integer.valueOf(value);
+    private Integer parseInteger(String[] values, int idx) {
+        if (idx >= 0 && idx < values.length && !values[idx].isEmpty()) {
+            try {
+                return nf.parse(values[idx]).intValue();
+            }
+            catch (ParseException e) {
+                log.warn("Unparseable value '" + values[idx] + "'");
+            }
         }
-        catch (NumberFormatException nfe) {
-            log.warn("Unparseable " + value + " in sq relation row: " + line);
-        }
-        return result;
+        return null;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4Elements-commits mailing list