diff flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java @ 485:6b231041dc18

Importer: Try to extract time ranges from at files. flys-backend/trunk@1811 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 03 May 2011 17:09:38 +0000
parents d980e545ccab
children 8ea09ec7f0c8
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java	Tue May 03 14:18:43 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java	Tue May 03 17:09:38 2011 +0000
@@ -14,6 +14,11 @@
 import de.intevation.flys.importer.ImportDischargeTable;
 import de.intevation.flys.importer.ImportDischargeTableValue;
 
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import java.util.Date;
+import java.util.Calendar;
 
 public class AtFileParser {
 
@@ -24,6 +29,12 @@
     private static NumberFormat nf = NumberFormat.getInstance();
 
 
+    // regular expression from hell to find out time range
+    public static final Pattern DATE_LINE = Pattern.compile(
+        "^\\*\\s*Abflu[^t]+tafel?\\s*([^\\d]+)"  + 
+        "(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4})\\s*(?:(?:bis)|-)?\\s*" +
+        "(?:(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4}))?\\s*.*$");
+
     public AtFileParser() {
     }
 
@@ -42,18 +53,32 @@
 
         ImportDischargeTable dischargeTable = new ImportDischargeTable();
 
+        Date from = null;
+        Date to   = null;
+
         try {
             br = new BufferedReader(
                  new InputStreamReader(
                  new FileInputStream(file), ENCODING));
 
             while ((line = br.readLine()) != null) {
+
                 String tmp = line.trim();
 
                 if (tmp.length() == 0) {
                     continue;
                 }
 
+                Matcher m = DATE_LINE.matcher(tmp);
+                if (m.matches()) {
+                    from = guessDate(m.group(1), m.group(2), m.group(3));
+                    to   = guessDate(m.group(4), m.group(5), m.group(6));
+                    if (from == null) {
+                        Date t = from; from = to; to = t;
+                    }
+                    continue;
+                }
+
                 if (tmp.startsWith("#! name=")) {
                     // XXX Skip the name,  because we don't know where to save
                     // it at the moment
@@ -113,5 +138,50 @@
 
         return dischargeTable;
     }
+
+    public static Date guessDate(String day, String month, String year) {
+        if (day == null && month == null && year == null) {
+            return null;
+        }
+
+        int dayI = 15;
+        if (day != null) {
+            try {
+                dayI = Integer.parseInt(day.trim());
+            }
+            catch (NumberFormatException nfe) {
+            }
+        }
+
+        int monthI = 6;
+        if (month != null) {
+            try {
+                monthI = Integer.parseInt(month.trim());
+            }
+            catch (NumberFormatException nfe) {
+            }
+        }
+
+        int yearI = 1900;
+        if (year != null) {
+            try {
+                yearI = Integer.parseInt(year.trim());
+                if (yearI < 100) {
+                    if (yearI < 20) {
+                        yearI += 2000;
+                    }
+                    else {
+                        yearI += 1900;
+                    }
+                }
+            }
+            catch (NumberFormatException nfe) {
+            }
+        }
+
+        Calendar cal = Calendar.getInstance();
+        cal.set(yearI, monthI-1, dayI);
+        return cal.getTime();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org