# HG changeset patch # User Sascha L. Teichmann # Date 1304442578 0 # Node ID 6b231041dc1829cdd3631b3c64ac74e7b269d8ce # Parent a9e9a8a44d19406372f7f16a781f130819d29e61 Importer: Try to extract time ranges from at files. flys-backend/trunk@1811 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r a9e9a8a44d19 -r 6b231041dc18 flys-backend/ChangeLog --- a/flys-backend/ChangeLog Tue May 03 14:18:43 2011 +0000 +++ b/flys-backend/ChangeLog Tue May 03 17:09:38 2011 +0000 @@ -1,3 +1,8 @@ +2011-05-03 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/importer/AtFileParser.java: + Try to extract time ranges from at files. + 2011-05-03 Sascha L. Teichmann * src/main/java/de/intevation/flys/importer/ImportRiver.java: diff -r a9e9a8a44d19 -r 6b231041dc18 flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java --- 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 :