changeset 8991:c43d8c1a4455

Parse via decimalformat instead of bigdecimal constructor (to cope with strings with chars after the actual number)
author mschaefer
date Tue, 10 Apr 2018 16:43:32 +0200
parents 07dcedddf839
children d046997281bc
files backend/src/main/java/org/dive4elements/river/importer/common/AbstractParser.java backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.java backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.java backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java
diffstat 7 files changed, 26 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/common/AbstractParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/common/AbstractParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -17,6 +17,7 @@
 import java.io.InputStreamReader;
 import java.io.LineNumberReader;
 import java.math.BigDecimal;
+import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.text.ParseException;
 import java.util.ArrayList;
@@ -73,6 +74,13 @@
 
     private static NumberFormat numberFormat = NumberFormat.getInstance(Locale.ROOT);
 
+    private static DecimalFormat bigDecimalFormat;
+
+    static {
+        bigDecimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.ROOT);
+        bigDecimalFormat.setParseBigDecimal(true);
+    }
+
     /**
      * Path of the file or directory to import from
      */
@@ -265,8 +273,8 @@
     /**
      * Parses a number string as a BigDecimal, replacing a comma with a dot first
      */
-    public static BigDecimal parseDecimal(final String text) throws NumberFormatException {
-        return new BigDecimal(text.replace(',', '.'));
+    public static BigDecimal parseDecimal(final String text) throws ParseException {
+        return (BigDecimal) bigDecimalFormat.parse(text.replace(',', '.'));
     }
 
     /**
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.text.NumberFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.TreeSet;
@@ -235,7 +236,7 @@
                         name,
                         AbstractParser.parseDecimal(qStr) };
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Could not parse Q value: '" + qStr + "'");
             }
         }
@@ -271,7 +272,7 @@
 
             this.kmExists.add(key);
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Unparseable flow velocity values:", pe);
         }
     }
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -126,7 +126,7 @@
             }
 
         }
-        catch (final NumberFormatException e) {
+        catch (final ParseException e) {
             log.error("Unparseable station in line " + lineNum +
                     ". Error: " + e.getMessage());
             return null;
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -10,6 +10,7 @@
 
 import java.math.BigDecimal;
 import java.text.NumberFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -113,7 +114,7 @@
                     desc
                     ));
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("MWP: unparseable number in data row: " + line);
         }
     }
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.text.NumberFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -113,7 +114,7 @@
 
                 return true;
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Unparseable numbers in: '" + line + "'");
             }
         }
@@ -159,7 +160,7 @@
                 shoreOffset = AbstractParser.parseDecimal(vals[1]);
             }
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Unparseable numbers in '" + line + "'");
         }
 
@@ -173,7 +174,7 @@
             try {
                 year = AbstractParser.parseDecimal(this.yearString);
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Unparseable year string");
             }
         }
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.text.NumberFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -243,13 +244,13 @@
                                 station,
                                 AbstractParser.parseDecimal(value));
                     }
-                    catch (final NumberFormatException pe) {
+                    catch (final ParseException pe) {
                         log.warn("Could not parse value: '" + value + "'");
                     }
                 }
             }
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Could not parse station: '" + line + "'");
         }
     }
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.text.NumberFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -195,7 +196,7 @@
 
                 return true;
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Unparseable Q range: '" + line + "'");
             }
         }
@@ -223,7 +224,7 @@
                 this.currentRange.setA(station);
             }
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Unparseable number in data row: " + line);
         }
     }

http://dive4elements.wald.intevation.org