changeset 8042:9342d7fe0ee7

Introduce LineParserException for CSV file parsing.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 16 Jul 2014 18:33:28 +0200
parents 01ad09af0975
children bd0dea643440 86fa217c24d5
files backend/src/main/java/org/dive4elements/river/importer/parsers/LineParser.java backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java
diffstat 2 files changed, 22 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/LineParser.java	Wed Jul 16 17:09:02 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/LineParser.java	Wed Jul 16 18:33:28 2014 +0200
@@ -42,8 +42,17 @@
     public static final String START_META_CHAR = "#";
     public static final String SEPERATOR_CHAR  = ";";
 
+    public static class LineParserException extends Exception {
+        public LineParserException() {
+        }
 
-    protected abstract void handleLine(int lineNum, String line);
+        public LineParserException(String msg) {
+            super(msg);
+        }
+    } // class LineParserException
+
+
+    protected abstract void handleLine(int lineNum, String line) throws LineParserException;
 
     protected abstract void reset();
 
@@ -88,8 +97,9 @@
 
                 handleLine(lineNum++, line);
             }
-        }
-        finally {
+        } catch (LineParserException lpe) {
+            log.error("Error while parsing file '" + file + "'", lpe);
+        } finally {
             if (in != null) {
                 in.close();
             }
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java	Wed Jul 16 17:09:02 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java	Wed Jul 16 18:33:28 2014 +0200
@@ -116,7 +116,7 @@
 
 
     @Override
-    protected void handleLine(int lineNum, String line) {
+    protected void handleLine(int lineNum, String line) throws LineParserException {
         if (line.startsWith(START_META_CHAR)) {
             handleMetaLine(stripMetaLine(line));
         }
@@ -126,22 +126,20 @@
     }
 
 
-    protected void handleMetaLine(String line) {
+    protected void handleMetaLine(String line) throws LineParserException {
         if (handleMetaUnit(line)) {
             return;
         }
-        else if (handleMetaFraction(line)) {
-            return;
-        }
-        else if (handleMetaFractionName(line)) {
+        if (handleMetaFraction(line)) {
             return;
         }
-        else if (handleColumnNames(line)) {
+        if (handleMetaFractionName(line)) {
             return;
         }
-        else {
-            log.warn("SLLSP: Unknown meta line: '" + line + "'");
+        if (handleColumnNames(line)) {
+            return;
         }
+        log.warn("SLLSP: Unknown meta line: '" + line + "'");
     }
 
 
@@ -221,7 +219,7 @@
     }
 
 
-    public boolean handleColumnNames(String line) {
+    public boolean handleColumnNames(String line) throws LineParserException {
         Matcher m = META_COLUMN_NAMES.matcher(line);
 
         if (m.matches()) {
@@ -229,8 +227,7 @@
 
             // 'Fluss-km', 'Hinweise' and at least one data column required
             if (columnNames.length < 3) {
-                log.error("SLLSP: missing columns.");
-                return true;
+                throw new LineParserException("SLLSP: missing columns.");
             }
 
             initializeSedimentLoadLSs();

http://dive4elements.wald.intevation.org