Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java @ 3328:a41f279a66e2
Added parser and import classes to import MINFO sq relations.
flys-backend/trunk@4646 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 13 Jun 2012 06:22:04 +0000 |
parents | |
children | cc8fc6b29649 |
comparison
equal
deleted
inserted
replaced
3327:bf8d9a4f4cd4 | 3328:a41f279a66e2 |
---|---|
1 package de.intevation.flys.importer.parsers; | |
2 | |
3 import java.text.NumberFormat; | |
4 import java.text.ParseException; | |
5 import java.util.ArrayList; | |
6 import java.util.List; | |
7 import java.util.regex.Matcher; | |
8 import java.util.regex.Pattern; | |
9 | |
10 import org.apache.log4j.Logger; | |
11 | |
12 import de.intevation.flys.importer.ImportSQRelation; | |
13 import de.intevation.flys.importer.ImportSQRelationValue; | |
14 import de.intevation.flys.importer.ImportTimeInterval; | |
15 | |
16 | |
17 public class SQRelationParser extends LineParser { | |
18 | |
19 private static final Logger log = | |
20 Logger.getLogger(SQRelationParser.class); | |
21 | |
22 private static final Pattern TIMERANGE_REGEX = | |
23 Pattern.compile(".*Zeitraum.*\\s(\\w*)-(\\w*).*"); | |
24 | |
25 private static final NumberFormat nf = | |
26 NumberFormat.getInstance(DEFAULT_LOCALE); | |
27 | |
28 | |
29 private List<ImportSQRelation> relations; | |
30 | |
31 private ImportSQRelation current; | |
32 | |
33 | |
34 public SQRelationParser() { | |
35 relations = new ArrayList<ImportSQRelation>(); | |
36 } | |
37 | |
38 | |
39 public List<ImportSQRelation> getSQRelations() { | |
40 return relations; | |
41 } | |
42 | |
43 @Override | |
44 protected void reset() { | |
45 current = new ImportSQRelation(); | |
46 } | |
47 | |
48 | |
49 @Override | |
50 protected void finish() { | |
51 relations.add(current); | |
52 } | |
53 | |
54 | |
55 @Override | |
56 protected void handleLine(String line) { | |
57 if (line.startsWith(START_META_CHAR)) { | |
58 handleMetaLine(stripMetaLine(line)); | |
59 } | |
60 else { | |
61 handleDataLine(line); | |
62 } | |
63 } | |
64 | |
65 | |
66 protected void handleMetaLine(String line) { | |
67 Matcher m = TIMERANGE_REGEX.matcher(line); | |
68 | |
69 if (m.matches()) { | |
70 String lo = m.group(1); | |
71 String hi = m.group(2); | |
72 | |
73 log.debug("Found timerange " + lo + " - " + hi); | |
74 | |
75 try { | |
76 int low = nf.parse(lo).intValue(); | |
77 int high = nf.parse(hi).intValue(); | |
78 | |
79 current.setTimeInterval(new ImportTimeInterval( | |
80 getDateFromYear(low), | |
81 getDateFromYear(high) | |
82 )); | |
83 } | |
84 catch (ParseException nfe) { | |
85 log.warn("Cannot parse time range.", nfe); | |
86 } | |
87 } | |
88 } | |
89 | |
90 | |
91 protected void handleDataLine(String line) { | |
92 String[] cols = line.split(SEPERATOR_CHAR); | |
93 | |
94 if (cols.length < 8) { | |
95 log.warn("skip invalid data line: '" + line + "'"); | |
96 return; | |
97 } | |
98 | |
99 try { | |
100 current.addValue(new ImportSQRelationValue( | |
101 cols[1], | |
102 cols[2], | |
103 cols[4], | |
104 nf.parse(cols[3]).doubleValue(), | |
105 nf.parse(cols[6]).doubleValue(), | |
106 nf.parse(cols[7]).doubleValue() | |
107 )); | |
108 } | |
109 catch (ParseException pe) { | |
110 log.warn("Error while parsing sq relation row: '" + line + "'", pe); | |
111 } | |
112 } | |
113 } | |
114 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |