comparison 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
comparison
equal deleted inserted replaced
484:a9e9a8a44d19 485:6b231041dc18
12 import org.apache.log4j.Logger; 12 import org.apache.log4j.Logger;
13 13
14 import de.intevation.flys.importer.ImportDischargeTable; 14 import de.intevation.flys.importer.ImportDischargeTable;
15 import de.intevation.flys.importer.ImportDischargeTableValue; 15 import de.intevation.flys.importer.ImportDischargeTableValue;
16 16
17 import java.util.regex.Pattern;
18 import java.util.regex.Matcher;
19
20 import java.util.Date;
21 import java.util.Calendar;
17 22
18 public class AtFileParser { 23 public class AtFileParser {
19 24
20 public static final String ENCODING = "ISO-8859-1"; 25 public static final String ENCODING = "ISO-8859-1";
21 26
22 private static Logger logger = Logger.getLogger(AtFileParser.class); 27 private static Logger logger = Logger.getLogger(AtFileParser.class);
23 28
24 private static NumberFormat nf = NumberFormat.getInstance(); 29 private static NumberFormat nf = NumberFormat.getInstance();
25 30
31
32 // regular expression from hell to find out time range
33 public static final Pattern DATE_LINE = Pattern.compile(
34 "^\\*\\s*Abflu[^t]+tafel?\\s*([^\\d]+)" +
35 "(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4})\\s*(?:(?:bis)|-)?\\s*" +
36 "(?:(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4}))?\\s*.*$");
26 37
27 public AtFileParser() { 38 public AtFileParser() {
28 } 39 }
29 40
30 41
40 51
41 boolean beginning = true; 52 boolean beginning = true;
42 53
43 ImportDischargeTable dischargeTable = new ImportDischargeTable(); 54 ImportDischargeTable dischargeTable = new ImportDischargeTable();
44 55
56 Date from = null;
57 Date to = null;
58
45 try { 59 try {
46 br = new BufferedReader( 60 br = new BufferedReader(
47 new InputStreamReader( 61 new InputStreamReader(
48 new FileInputStream(file), ENCODING)); 62 new FileInputStream(file), ENCODING));
49 63
50 while ((line = br.readLine()) != null) { 64 while ((line = br.readLine()) != null) {
65
51 String tmp = line.trim(); 66 String tmp = line.trim();
52 67
53 if (tmp.length() == 0) { 68 if (tmp.length() == 0) {
69 continue;
70 }
71
72 Matcher m = DATE_LINE.matcher(tmp);
73 if (m.matches()) {
74 from = guessDate(m.group(1), m.group(2), m.group(3));
75 to = guessDate(m.group(4), m.group(5), m.group(6));
76 if (from == null) {
77 Date t = from; from = to; to = t;
78 }
54 continue; 79 continue;
55 } 80 }
56 81
57 if (tmp.startsWith("#! name=")) { 82 if (tmp.startsWith("#! name=")) {
58 // XXX Skip the name, because we don't know where to save 83 // XXX Skip the name, because we don't know where to save
111 136
112 logger.info("Finished parsing AT file: " + file); 137 logger.info("Finished parsing AT file: " + file);
113 138
114 return dischargeTable; 139 return dischargeTable;
115 } 140 }
141
142 public static Date guessDate(String day, String month, String year) {
143 if (day == null && month == null && year == null) {
144 return null;
145 }
146
147 int dayI = 15;
148 if (day != null) {
149 try {
150 dayI = Integer.parseInt(day.trim());
151 }
152 catch (NumberFormatException nfe) {
153 }
154 }
155
156 int monthI = 6;
157 if (month != null) {
158 try {
159 monthI = Integer.parseInt(month.trim());
160 }
161 catch (NumberFormatException nfe) {
162 }
163 }
164
165 int yearI = 1900;
166 if (year != null) {
167 try {
168 yearI = Integer.parseInt(year.trim());
169 if (yearI < 100) {
170 if (yearI < 20) {
171 yearI += 2000;
172 }
173 else {
174 yearI += 1900;
175 }
176 }
177 }
178 catch (NumberFormatException nfe) {
179 }
180 }
181
182 Calendar cal = Calendar.getInstance();
183 cal.set(yearI, monthI-1, dayI);
184 return cal.getTime();
185 }
116 } 186 }
117 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 187 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org