[PATCH] WST-Parser and doc: reject files with wrong number of columns

Wald Commits scm-commit at wald.intevation.org
Thu Oct 31 15:25:35 CET 2013


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1383229531 -3600
# Node ID bdb12632c5f58182a2b79fcc1949b9d7854f6896
# Parent  2a2e89c015887679cbace0a94c709dcf7bc4deaa
WST-Parser and doc: reject files with wrong number of columns.

diff -r 2a2e89c01588 -r bdb12632c5f5 backend/doc/documentation/de/importer-hydr-morph.tex
--- a/backend/doc/documentation/de/importer-hydr-morph.tex	Thu Oct 31 13:11:03 2013 +0100
+++ b/backend/doc/documentation/de/importer-hydr-morph.tex	Thu Oct 31 15:25:31 2013 +0100
@@ -399,10 +399,14 @@
 obwohl sich das Gewässer noch nicht in der Datenbank befindet 
 (siehe Kapitel \ref{import_data}).
 
-\textbf{Stations in 'XYZ' near line \# not ordered. File rejected.}
+\textbf{WST: Stations in 'XYZ' near line \# not ordered. File rejected.}
 \\Die Stationen in einer WST-Datei sind nicht konsequent auf- oder 
 absteigend geordnet. Die Datei wird verworfen.
 
+\textbf{WST: number of columns is less than expected. File rejected.}
+\\Die Anzahl der Spalten in einer Zeile einer WST-Datei stimmt nicht
+mit der Angabe im Datei-Kopf überein. Die Datei wird verworfen.
+
 \textbf{File 'XYZ' is broken!}
 \\Die Datei XYZ ist inkonsistent und führt zu Fehlern.
 
diff -r 2a2e89c01588 -r bdb12632c5f5 backend/src/main/java/org/dive4elements/river/importer/parsers/WstParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/WstParser.java	Thu Oct 31 13:11:03 2013 +0100
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/WstParser.java	Thu Oct 31 15:25:31 2013 +0100
@@ -188,8 +188,14 @@
 
                 // handle Q-lines
                 if (line.startsWith("*\u001f")) {
-                    BigDecimal [] data =
-                        parseLineAsDouble(line, columnCount, false, true);
+                    BigDecimal [] data = null;
+                    try {
+                        data = parseLineAsDouble(line, columnCount, false, true);
+                    }
+                    catch(IllegalArgumentException iae) {
+                        log.error(iae.getMessage());
+                        return;
+                    }
 
                     if (aktAbfluesse != null) { // add Q-ranges obtained from previous lines
                         if (kmHist1 != null && kmHist2 != null
@@ -329,8 +335,14 @@
                         columnHeaderChecked = true;
                     }
 
-                    BigDecimal [] data =
-                        parseLineAsDouble(line, columnCount, true, false);
+                    BigDecimal [] data = null;
+                    try {
+                        data = parseLineAsDouble(line, columnCount, true, false);
+                    }
+                    catch(IllegalArgumentException iae) {
+                        log.error(iae.getMessage());
+                        return;
+                    }
 
                     BigDecimal kaem = data[0];
 
@@ -345,7 +357,7 @@
                     // check consistence of station ordering in file
                     if (kmHist2 != null && 
                         kmHist2.compareTo(kmHist1) != kmHist1.compareTo(kaem)) {
-                        throw new ParseException("Stations in " + file + 
+                        throw new ParseException("WST: Stations in " + file + 
                             " near line " + in.getLineNumber() + 
                             " not ordered. File rejected.");
                     }
@@ -529,11 +541,15 @@
             strings.add(line.substring(0, 8));
         }
 
-        int pos = 9;
+        int pos = 0;
         for (int i = 0; i < tokenCount; ++i) {
+            pos += 9;
+            if (pos >= line.length()) {
+                throw new IllegalArgumentException(
+                    "WST: number of columns is less than expected. File rejected.");
+            }
             strings.add(line.substring(pos, 
                 Math.min(pos + 8, line.length())));
-            pos += 9;
         }
 
         return strings.toArray(new String[strings.size()]);


More information about the Dive4elements-commits mailing list