Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/StaFileParser.java @ 5735:c75be5205a69
Backend: Parse time intervals for main values.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Tue, 16 Apr 2013 18:07:05 +0200 |
parents | dc2daadbdd0c |
children | ab4419948655 |
comparison
equal
deleted
inserted
replaced
5734:06adfba17091 | 5735:c75be5205a69 |
---|---|
9 import java.math.BigDecimal; | 9 import java.math.BigDecimal; |
10 | 10 |
11 import java.util.regex.Pattern; | 11 import java.util.regex.Pattern; |
12 import java.util.regex.Matcher; | 12 import java.util.regex.Matcher; |
13 | 13 |
14 import java.util.Date; | |
14 import java.util.HashMap; | 15 import java.util.HashMap; |
15 import java.util.ArrayList; | 16 import java.util.ArrayList; |
17 import java.util.List; | |
16 | 18 |
17 import org.apache.log4j.Logger; | 19 import org.apache.log4j.Logger; |
18 | 20 |
19 import de.intevation.flys.importer.ImportMainValueType; | 21 import de.intevation.flys.importer.ImportMainValueType; |
20 import de.intevation.flys.importer.ImportMainValue; | 22 import de.intevation.flys.importer.ImportMainValue; |
21 import de.intevation.flys.importer.ImportNamedMainValue; | 23 import de.intevation.flys.importer.ImportNamedMainValue; |
22 import de.intevation.flys.importer.ImportGauge; | 24 import de.intevation.flys.importer.ImportGauge; |
25 import de.intevation.flys.importer.ImportTimeInterval; | |
26 import de.intevation.flys.utils.DateGuesser; | |
23 | 27 |
24 public class StaFileParser | 28 public class StaFileParser |
25 { | 29 { |
26 private static Logger log = Logger.getLogger(StaFileParser.class); | 30 private static Logger log = Logger.getLogger(StaFileParser.class); |
27 | 31 |
34 Boolean.getBoolean("flys.backend.sta.not.parse.gauge.numbers"); | 38 Boolean.getBoolean("flys.backend.sta.not.parse.gauge.numbers"); |
35 | 39 |
36 public static final Pattern QWTD_ = | 40 public static final Pattern QWTD_ = |
37 Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([" + | 41 Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([" + |
38 Pattern.quote(TYPES) + "]).*"); | 42 Pattern.quote(TYPES) + "]).*"); |
43 | |
44 public static final class NameAndTimeInterval { | |
45 private String name; | |
46 private ImportTimeInterval timeInterval; | |
47 | |
48 public NameAndTimeInterval(String name) { | |
49 this(name, null); | |
50 } | |
51 | |
52 public NameAndTimeInterval(String name, ImportTimeInterval timeInterval) { | |
53 this.name = name; | |
54 this.timeInterval = timeInterval; | |
55 } | |
56 | |
57 public String getName() { | |
58 return name; | |
59 } | |
60 | |
61 public ImportTimeInterval getTimeInterval() { | |
62 return timeInterval; | |
63 } | |
64 } // class NameAndTimeInterval | |
39 | 65 |
40 public StaFileParser() { | 66 public StaFileParser() { |
41 } | 67 } |
42 | 68 |
43 public boolean parse(ImportGauge gauge) throws IOException { | 69 public boolean parse(ImportGauge gauge) throws IOException { |
158 if (type == null) { | 184 if (type == null) { |
159 type = new ImportMainValueType(typeString); | 185 type = new ImportMainValueType(typeString); |
160 types.put(typeString, type); | 186 types.put(typeString, type); |
161 } | 187 } |
162 String name = m.group(1); | 188 String name = m.group(1); |
189 NameAndTimeInterval nat = parseName(name); | |
163 ImportNamedMainValue namedMainValue = | 190 ImportNamedMainValue namedMainValue = |
164 new ImportNamedMainValue(type, name); | 191 new ImportNamedMainValue(type, nat.getName()); |
165 namedMainValues.add(namedMainValue); | 192 namedMainValues.add(namedMainValue); |
166 | 193 |
167 ImportMainValue mainValue = | 194 ImportMainValue mainValue = new ImportMainValue( |
168 new ImportMainValue(gauge, namedMainValue, value); | 195 gauge, |
196 namedMainValue, | |
197 value, | |
198 nat.getTimeInterval()); | |
169 | 199 |
170 mainValues.add(mainValue); | 200 mainValues.add(mainValue); |
171 } | 201 } |
172 else { | 202 else { |
173 // TODO: treat as a comment | 203 // TODO: treat as a comment |
184 } | 214 } |
185 } | 215 } |
186 log.info("finished parsing STA file: " + file); | 216 log.info("finished parsing STA file: " + file); |
187 return true; | 217 return true; |
188 } | 218 } |
219 | |
220 protected NameAndTimeInterval parseName(String name) { | |
221 List<String> result = new ArrayList<String>(); | |
222 | |
223 unbracket(name, 0, result); | |
224 | |
225 int length = result.size(); | |
226 | |
227 if (length < 1) { // Should not happen. | |
228 return new NameAndTimeInterval(name); | |
229 } | |
230 | |
231 if (length == 1) { // No date at all -> use first part. | |
232 return new NameAndTimeInterval(result.get(0).trim()); | |
233 } | |
234 | |
235 if (length == 2) { // e.g. W(1994) or W(1994 - 1999) | |
236 String type = result.get(0).trim(); | |
237 | |
238 ImportTimeInterval timeInterval = getTimeInterval( | |
239 result.get(1).trim()); | |
240 | |
241 if (timeInterval == null) { // No date at all. | |
242 type = name; | |
243 } | |
244 | |
245 return new NameAndTimeInterval(type, timeInterval); | |
246 } | |
247 | |
248 if (length == 3) { // e.g W(Q(1994)) or W(Q(1994 - 1999)) | |
249 | |
250 String type = | |
251 result.get(0).trim() + "(" + | |
252 result.get(1).trim() + ")"; | |
253 | |
254 ImportTimeInterval timeInterval = getTimeInterval( | |
255 result.get(2).trim()); | |
256 | |
257 if (timeInterval == null) { // No date at all. | |
258 type = name; | |
259 } | |
260 | |
261 return new NameAndTimeInterval(type, timeInterval); | |
262 } | |
263 | |
264 return null; | |
265 } | |
266 | |
267 private static ImportTimeInterval getTimeInterval(String datePart) { | |
268 | |
269 int minus = datePart.indexOf('-'); | |
270 | |
271 if (minus < 0) { // '-' not found | |
272 | |
273 Date date = null; | |
274 try { | |
275 date = DateGuesser.guessDate(datePart); | |
276 } | |
277 catch (IllegalArgumentException iae) { | |
278 log.warn("STA: Invalid date '" + datePart + "'"); | |
279 return null; | |
280 } | |
281 | |
282 return new ImportTimeInterval(date); | |
283 } | |
284 | |
285 // Found '-' so we have <from> - <to> | |
286 String startPart = datePart.substring(0, minus).trim(); | |
287 String endPart = datePart.substring(minus).trim(); | |
288 | |
289 Date startDate = null; | |
290 Date endDate = null; | |
291 | |
292 try { | |
293 startDate = DateGuesser.guessDate(startPart); | |
294 } | |
295 catch (IllegalArgumentException iae) { | |
296 log.warn("STA: Invalid start date '" + startPart + "'"); | |
297 } | |
298 | |
299 try { | |
300 endDate = DateGuesser.guessDate(endPart); | |
301 } | |
302 catch (IllegalArgumentException iae) { | |
303 log.warn("STA: Invalid end date '" + endPart + "'"); | |
304 } | |
305 | |
306 if (startDate == null) { | |
307 log.warn("STA: Need start date."); | |
308 return null; | |
309 } | |
310 | |
311 return new ImportTimeInterval(startDate, endDate); | |
312 } | |
313 | |
314 private static int unbracket(String s, int index, List<String> result) { | |
315 StringBuilder sb = new StringBuilder(); | |
316 int length = s.length(); | |
317 while (index < length) { | |
318 char c = s.charAt(index); | |
319 switch (c) { | |
320 case '(': | |
321 index = unbracket(s, index, result); | |
322 break; | |
323 case ')': | |
324 result.add(0, sb.toString()); | |
325 return index+1; | |
326 default: | |
327 sb.append(c); | |
328 } | |
329 } | |
330 result.add(0, sb.toString()); | |
331 | |
332 return index; | |
333 } | |
189 } | 334 } |
190 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 335 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |