diff flys-backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java@b3dd14fc13a6
children 18619c1e7c2a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java	Thu Apr 25 11:53:11 2013 +0200
@@ -0,0 +1,197 @@
+package de.intevation.flys.importer.parsers;
+
+import java.io.File;
+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;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.importer.ImportUnit;
+
+import de.intevation.flys.importer.ImportWst;
+import de.intevation.flys.importer.ImportWstColumn;
+
+
+/**
+ * Parse WaterlevelDifferences CSV file.
+ */
+public class WaterlevelDifferencesParser extends LineParser {
+
+    private static final Logger log =
+        Logger.getLogger(WaterlevelDifferencesParser.class);
+
+    private static final NumberFormat nf =
+        NumberFormat.getInstance(DEFAULT_LOCALE);
+
+    public static final Pattern META_UNIT =
+        Pattern.compile("^Einheit: \\[(.*)\\].*");
+
+    /** List of parsed differences as ImportWst s. */
+    private List<ImportWst> differences;
+
+    private ImportWstColumn[] columns;
+
+    /** The currently processed dataset. */
+    private ImportWst current;
+
+
+    public WaterlevelDifferencesParser() {
+        differences = new ArrayList<ImportWst>();
+    }
+
+
+    /** Get the differences as wst parsed so far. */
+    public List<ImportWst> getDifferences() {
+        return differences;
+    }
+
+
+    /**
+     * Parse a csv waterleveldifferenceparser and create a ImportWst object
+     * from it.
+     */
+    @Override
+    public void parse(File file) throws IOException {
+        current = new ImportWst(file.getName());
+        current.setKind(7);
+
+        super.parse(file);
+    }
+
+
+    /** No rewind implemented. */
+    @Override
+    protected void reset() {
+    }
+
+
+    @Override
+    protected void finish() {
+        if (columns != null && current != null) {
+            // TODO figure out if its needed, as the columns
+            //      are registered at their construction time.
+            for (ImportWstColumn col: columns) {
+                // TODO place a current.addColumn(col); here?
+            }
+
+            differences.add(current);
+        }
+
+        current = null;
+        columns = null;
+    }
+
+
+    @Override
+    protected void handleLine(int lineNum, String line) {
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+        }
+        else {
+            handleDataLine(line);
+        }
+    }
+
+
+    private void handleMetaLine(String meta) {
+        if (handleMetaUnit(meta)) {
+            return;
+        }
+        else {
+            handleMetaColumnNames(meta);
+        }
+    }
+
+
+    private boolean handleMetaUnit(String meta) {
+        Matcher m = META_UNIT.matcher(meta);
+
+        if (m.matches()) {
+            String unit = m.group(1);
+            log.debug("Found unit: '" + unit + "'");
+
+            current.setUnit(new ImportUnit(unit));
+
+            return true;
+        }
+
+        return false;
+    }
+
+
+    private boolean handleMetaColumnNames(String meta) {
+        Pattern META_COLUMN_NAMES = Pattern.compile("Fluss-km;(.*)");
+        Matcher m = META_COLUMN_NAMES.matcher(meta);
+
+        if (m.matches()) {
+            String colStr = m.group(1);
+            String[] cols = colStr.split(SEPERATOR_CHAR);
+
+            log.debug("Found " + cols.length + " columns.");
+
+            initColumns(cols);
+
+            return true;
+        }
+
+        return false;
+    }
+
+
+    private void initColumns(String[] cols) {
+        current.setNumberColumns(cols.length);
+        columns = current.getColumns().toArray(new ImportWstColumn[cols.length]);
+
+        for (int i = 0; i < cols.length; i++) {
+            String name = cols[i].replace("\"", "");
+
+            log.debug("Create new column '" + name + "'");
+            current.getColumn(i).setName(name);
+            current.getColumn(i).setDescription(name);
+        }
+    }
+
+
+    private void handleDataLine(String line) {
+        String[] cols = line.split(SEPERATOR_CHAR);
+
+        if (cols == null || cols.length < 2) {
+            log.warn("skip invalid waterlevel-diff line: '" + line + "'");
+            return;
+        }
+
+        try {
+            Double station = nf.parse(cols[0]).doubleValue();
+
+            for (int i = 0; i < columns.length; i++) {
+                int idx = i+1;
+
+                if (idx >= cols.length) {
+                    log.warn("Insufficient column numbers: " + line);
+                    continue;
+                }
+
+                String value = cols[idx];
+
+                try {
+                    columns[i].addColumnValue(
+                        new BigDecimal(station),
+                        new BigDecimal(nf.parse(value).doubleValue()));
+                }
+                catch (ParseException pe) {
+                    log.warn("Could not parse value: '" + value + "'");
+                }
+            }
+        }
+        catch (ParseException pe) {
+            log.warn("Could not parse station: '" + line + "'");
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org