diff flys-backend/src/main/java/de/intevation/flys/importer/parsers/LineParser.java @ 2815:3febaed762b8

Added new parser (stub) to read MINFO sediment density files; prepared import process to handle those files. flys-backend/trunk@4232 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Apr 2012 09:57:37 +0000
parents
children f63b39799d2d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/LineParser.java	Fri Apr 13 09:57:37 2012 +0000
@@ -0,0 +1,93 @@
+package de.intevation.flys.importer.parsers;
+
+import java.io.File;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+
+import org.apache.log4j.Logger;
+
+
+public abstract class LineParser {
+
+    private static final Logger log = Logger.getLogger(LineParser.class);
+
+    public static final String ENCODING = "ISO-8859-1";
+
+    public static final Locale DEFAULT_LOCALE = Locale.GERMAN;
+
+    public static final String START_META_CHAR = "#";
+    public static final String SEPERATOR_CHAR  = ";";
+
+
+    protected abstract void handleLine(String line);
+
+    protected abstract void reset();
+
+    protected abstract void finish();
+
+
+    /**
+     * This method reads each line of <i>file</i>. At the beginning,
+     * <i>reset()</i> is called; afterwars for each line <i>handleLine()</i> is
+     * called; at the end <i>finish</i> is called.
+     *
+     * @param file The file which should be parsed.
+     */
+    public void parse(File file) throws IOException {
+        log.info("Parsing file '" + file + "'");
+
+        reset();
+
+        LineNumberReader in = null;
+        try {
+            in =
+                new LineNumberReader(
+                new InputStreamReader(
+                new FileInputStream(file), ENCODING));
+
+            String line = null;
+            while ((line = in.readLine()) != null) {
+                if ((line = line.trim()).length() == 0) {
+                    continue;
+                }
+
+                handleLine(line);
+            }
+        }
+        finally {
+            if (in != null) {
+                in.close();
+            }
+        }
+
+        finish();
+    }
+
+
+    protected static String stripMetaLine(String line) {
+        String tmp = line.substring(1, line.length());
+
+        if (tmp.startsWith(" ")) {
+            return tmp.substring(1, tmp.length());
+        }
+        else {
+            return tmp;
+        }
+    }
+
+
+    public static Date getDateFromYear(int year) {
+        Calendar cal = Calendar.getInstance();
+        cal.set(year, 0, 1);
+
+        return cal.getTime();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org