Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/parsers/SQRelationParser.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-backend/src/main/java/org/dive4elements/river/importer/parsers/SQRelationParser.java@18619c1e7c2a |
children | 4dd33b86dc61 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.importer.parsers; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import java.text.NumberFormat; | |
6 import java.text.ParseException; | |
7 import java.util.ArrayList; | |
8 import java.util.List; | |
9 import java.util.regex.Matcher; | |
10 import java.util.regex.Pattern; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 | |
14 import org.dive4elements.river.importer.ImportSQRelation; | |
15 import org.dive4elements.river.importer.ImportSQRelationValue; | |
16 import org.dive4elements.river.importer.ImportTimeInterval; | |
17 | |
18 | |
19 public class SQRelationParser extends LineParser { | |
20 | |
21 private static final Logger log = | |
22 Logger.getLogger(SQRelationParser.class); | |
23 | |
24 private static final Pattern TIMERANGE_REGEX = | |
25 Pattern.compile(".*Zeitraum.*\\s(\\w*)-(\\w*).*"); | |
26 | |
27 private static final NumberFormat nf = | |
28 NumberFormat.getInstance(DEFAULT_LOCALE); | |
29 | |
30 | |
31 private List<ImportSQRelation> relations; | |
32 | |
33 private ImportSQRelation current; | |
34 | |
35 private String currentDescription; | |
36 | |
37 | |
38 public SQRelationParser() { | |
39 relations = new ArrayList<ImportSQRelation>(); | |
40 } | |
41 | |
42 | |
43 public List<ImportSQRelation> getSQRelations() { | |
44 return relations; | |
45 } | |
46 | |
47 @Override | |
48 public void parse(File file) throws IOException { | |
49 this.currentDescription = file.getName(); | |
50 super.parse(file); | |
51 } | |
52 | |
53 | |
54 @Override | |
55 protected void reset() { | |
56 current = new ImportSQRelation(); | |
57 } | |
58 | |
59 | |
60 @Override | |
61 protected void finish() { | |
62 if (current != null) { | |
63 current.setDescription(currentDescription); | |
64 relations.add(current); | |
65 } | |
66 } | |
67 | |
68 | |
69 @Override | |
70 protected void handleLine(int lineNum, String line) { | |
71 if (line.startsWith(START_META_CHAR)) { | |
72 handleMetaLine(stripMetaLine(line)); | |
73 } | |
74 else { | |
75 handleDataLine(line); | |
76 } | |
77 } | |
78 | |
79 | |
80 protected void handleMetaLine(String line) { | |
81 Matcher m = TIMERANGE_REGEX.matcher(line); | |
82 | |
83 if (m.matches()) { | |
84 String lo = m.group(1); | |
85 String hi = m.group(2); | |
86 | |
87 log.debug("Found timerange " + lo + " - " + hi); | |
88 | |
89 try { | |
90 int low = nf.parse(lo).intValue(); | |
91 int high = nf.parse(hi).intValue(); | |
92 | |
93 current.setTimeInterval(new ImportTimeInterval( | |
94 getDateFromYear(low), | |
95 getDateFromYear(high) | |
96 )); | |
97 } | |
98 catch (ParseException nfe) { | |
99 log.warn("Cannot parse time range.", nfe); | |
100 } | |
101 } | |
102 } | |
103 | |
104 | |
105 protected void handleDataLine(String line) { | |
106 String[] cols = line.split(SEPERATOR_CHAR); | |
107 | |
108 if (cols.length < 14) { | |
109 log.warn("skip invalid data line: '" + line + "'"); | |
110 return; | |
111 } | |
112 | |
113 Double km = parseDouble(cols[3], line); | |
114 Double a = parseDouble(cols[6], line); | |
115 Double b = parseDouble(cols[7], line); | |
116 Double qMax = parseDouble(cols[8], line); | |
117 Double rSq = parseDouble(cols[9], line); | |
118 Integer nTot = parseInteger(cols[10], line); | |
119 Integer nOutlier = parseInteger(cols[11], line); | |
120 Double cFer = parseDouble(cols[12], line); | |
121 Double cDuan = parseDouble(cols[13], line); | |
122 if (km == null || a == null || b == null || | |
123 qMax == null || cols[1].length() == 0) { | |
124 if (km == null) { | |
125 log.error("No km for measurement station: Can not reference measurement station: " | |
126 + line); | |
127 } | |
128 if ( a == null || b == null || | |
129 qMax == null || cols[1].length() == 0) { | |
130 log.error("Incomplete SQ-relation row (missing a, b, Qmax or parameter): " | |
131 + line); | |
132 } | |
133 return; | |
134 } | |
135 current.addValue(new ImportSQRelationValue( | |
136 cols[1], | |
137 km, | |
138 a, | |
139 b, | |
140 qMax, | |
141 rSq, | |
142 nTot, | |
143 nOutlier, | |
144 cFer, | |
145 cDuan)); | |
146 } | |
147 | |
148 private Double parseDouble(String value, String line) { | |
149 Double result = null; | |
150 try { | |
151 result = Double.valueOf(value.replace(",", ".")); | |
152 } | |
153 catch (NumberFormatException nfe) { | |
154 log.warn("Unparseable " + value + " in sq relation row: " + line); | |
155 } | |
156 return result; | |
157 } | |
158 | |
159 private Integer parseInteger(String value, String line) { | |
160 Integer result = null; | |
161 try { | |
162 result = Integer.valueOf(value); | |
163 } | |
164 catch (NumberFormatException nfe) { | |
165 log.warn("Unparseable " + value + " in sq relation row: " + line); | |
166 } | |
167 return result; | |
168 } | |
169 } | |
170 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |