Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightSingleParser.java @ 2808:b57c95094b68
Finished work on parsing meta information and data specific to single bed heights files in MINFO.
flys-backend/trunk@4216 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 11 Apr 2012 13:12:52 +0000 |
parents | 33f40b23edd8 |
children | f283212966e8 |
comparison
equal
deleted
inserted
replaced
2807:5ac1db5156be | 2808:b57c95094b68 |
---|---|
1 package de.intevation.flys.importer.parsers; | 1 package de.intevation.flys.importer.parsers; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 | 4 |
5 import java.math.BigDecimal; | |
6 | |
7 import java.text.NumberFormat; | |
8 import java.text.ParseException; | |
9 | |
10 import java.util.ArrayList; | |
5 import java.util.List; | 11 import java.util.List; |
6 import java.util.ArrayList; | 12 import java.util.Locale; |
13 | |
14 import java.util.regex.Matcher; | |
15 import java.util.regex.Pattern; | |
7 | 16 |
8 import java.io.IOException; | 17 import java.io.IOException; |
9 import java.io.LineNumberReader; | 18 import java.io.LineNumberReader; |
10 import java.io.FileInputStream; | 19 import java.io.FileInputStream; |
11 import java.io.InputStreamReader; | 20 import java.io.InputStreamReader; |
12 | 21 |
13 import org.apache.log4j.Logger; | 22 import org.apache.log4j.Logger; |
14 | 23 |
15 import de.intevation.flys.importer.ImportBedHeightSingle; | 24 import de.intevation.flys.importer.ImportBedHeightSingle; |
25 import de.intevation.flys.importer.ImportBedHeightSingleValue; | |
26 import de.intevation.flys.importer.ImportBedHeightType; | |
27 import de.intevation.flys.importer.ImportElevationModel; | |
28 import de.intevation.flys.importer.ImportLocationSystem; | |
29 import de.intevation.flys.importer.ImportRange; | |
30 import de.intevation.flys.importer.ImportUnit; | |
16 | 31 |
17 | 32 |
18 public class BedHeightSingleParser { | 33 public class BedHeightSingleParser { |
19 | 34 |
20 public static final String ENCODING = "ISO-8859-1"; | 35 public static final String ENCODING = "ISO-8859-1"; |
21 | 36 |
37 public static final Locale DEFAULT_LOCALE = Locale.GERMAN; | |
38 | |
39 public static final String START_META_CHAR = "#"; | |
40 public static final String SEPERATOR_CHAR = ";"; | |
41 | |
42 public static final Pattern META_YEAR = | |
43 Pattern.compile("^Jahr: (\\d*).*"); | |
44 | |
45 public static final Pattern META_TYPE = | |
46 Pattern.compile("^Aufnahmeart: (.*).*"); | |
47 | |
48 public static final Pattern META_LOCATION_SYSTEM = | |
49 Pattern.compile("^Lagesystem: (.*).*"); | |
50 | |
51 public static final Pattern META_CUR_ELEVATION_SYSTEM = | |
52 Pattern.compile("^H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); | |
53 | |
54 public static final Pattern META_OLD_ELEVATION_SYSTEM = | |
55 Pattern.compile("^urspr.ngliches H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); | |
56 | |
57 public static final Pattern META_SOUNDING_WIDTH = | |
58 Pattern.compile("^ausgewertete Peilbreite: (\\d*).*"); | |
59 | |
60 public static final Pattern META_RANGE = | |
61 Pattern.compile("^Strecke:\\D*(\\d++.\\d*)-(\\d++.\\d*).*"); | |
62 | |
63 public static final Pattern META_EVALUATION_BY = | |
64 Pattern.compile("^Auswerter: (.*).*"); | |
65 | |
66 public static final Pattern META_COMMENTS = | |
67 Pattern.compile("^Weitere Bemerkungen: (.*).*"); | |
22 | 68 |
23 private static final Logger log = | 69 private static final Logger log = |
24 Logger.getLogger(BedHeightSingleParser.class); | 70 Logger.getLogger(BedHeightSingleParser.class); |
25 | 71 |
26 | 72 |
73 protected static NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); | |
74 | |
75 | |
27 protected List<ImportBedHeightSingle> bedHeights; | 76 protected List<ImportBedHeightSingle> bedHeights; |
28 | 77 |
29 | 78 |
30 public BedHeightSingleParser() { | 79 public BedHeightSingleParser() { |
31 bedHeights = new ArrayList<ImportBedHeightSingle>(); | 80 bedHeights = new ArrayList<ImportBedHeightSingle>(); |
37 } | 86 } |
38 | 87 |
39 | 88 |
40 public void parse(File file) throws IOException { | 89 public void parse(File file) throws IOException { |
41 log.info("Parsing bed height single file '" + file + "'"); | 90 log.info("Parsing bed height single file '" + file + "'"); |
91 | |
92 ImportBedHeightSingle obj = new ImportBedHeightSingle(file.getName()); | |
42 | 93 |
43 LineNumberReader in = null; | 94 LineNumberReader in = null; |
44 try { | 95 try { |
45 in = | 96 in = |
46 new LineNumberReader( | 97 new LineNumberReader( |
50 String line = null; | 101 String line = null; |
51 while ((line = in.readLine()) != null) { | 102 while ((line = in.readLine()) != null) { |
52 if ((line = line.trim()).length() == 0) { | 103 if ((line = line.trim()).length() == 0) { |
53 continue; | 104 continue; |
54 } | 105 } |
106 | |
107 if (line.startsWith(START_META_CHAR)) { | |
108 handleMetaLine(obj, line); | |
109 } | |
110 else { | |
111 handleDataLine(obj, line); | |
112 } | |
55 } | 113 } |
56 } | 114 } |
57 finally { | 115 finally { |
58 if (in != null) { | 116 if (in != null) { |
59 in.close(); | 117 in.close(); |
60 } | 118 } |
61 } | 119 } |
62 } | 120 } |
121 | |
122 | |
123 protected void handleMetaLine(ImportBedHeightSingle obj, String line) { | |
124 String meta = stripMetaLine(line); | |
125 | |
126 if (handleMetaYear(obj, meta)) { | |
127 return; | |
128 } | |
129 else if (handleMetaSoundingWidth(obj, meta)) { | |
130 return; | |
131 } | |
132 else if (handleMetaComment(obj, meta)) { | |
133 return; | |
134 } | |
135 else if (handleMetaEvaluationBy(obj, meta)) { | |
136 return; | |
137 } | |
138 else if (handleMetaRange(obj, meta)) { | |
139 return; | |
140 } | |
141 else if (handleMetaType(obj, meta)) { | |
142 return; | |
143 } | |
144 else if (handleMetaLocationSystem(obj, meta)) { | |
145 return; | |
146 } | |
147 else if (handleMetaCurElevationModel(obj, meta)) { | |
148 return; | |
149 } | |
150 else if (handleMetaOldElevationModel(obj, meta)) { | |
151 return; | |
152 } | |
153 else { | |
154 log.warn("Meta line did not match any known type: " + line); | |
155 } | |
156 } | |
157 | |
158 | |
159 protected boolean handleMetaYear(ImportBedHeightSingle obj, String line) { | |
160 Matcher m = META_YEAR.matcher(line); | |
161 | |
162 if (m.matches()) { | |
163 String tmp = m.group(1); | |
164 | |
165 try { | |
166 obj.setYear(Integer.valueOf(tmp)); | |
167 return true; | |
168 } | |
169 catch (NumberFormatException e) { | |
170 log.warn("Error while parsing year!", e); | |
171 } | |
172 } | |
173 | |
174 return false; | |
175 } | |
176 | |
177 | |
178 protected boolean handleMetaSoundingWidth(ImportBedHeightSingle obj, String line) { | |
179 Matcher m = META_SOUNDING_WIDTH.matcher(line); | |
180 | |
181 if (m.matches()) { | |
182 String tmp = m.group(1); | |
183 | |
184 try { | |
185 obj.setSoundingWidth(Integer.valueOf(tmp)); | |
186 return true; | |
187 } | |
188 catch (NumberFormatException e) { | |
189 log.warn("Error while parsing sounding width!", e); | |
190 } | |
191 } | |
192 | |
193 return false; | |
194 } | |
195 | |
196 | |
197 protected boolean handleMetaComment(ImportBedHeightSingle obj, String line) { | |
198 Matcher m = META_COMMENTS.matcher(line); | |
199 | |
200 if (m.matches()) { | |
201 String tmp = m.group(1); | |
202 | |
203 obj.setDescription(tmp); | |
204 | |
205 return true; | |
206 } | |
207 | |
208 return false; | |
209 } | |
210 | |
211 | |
212 protected boolean handleMetaEvaluationBy( | |
213 ImportBedHeightSingle obj, | |
214 String line | |
215 ) { | |
216 Matcher m = META_EVALUATION_BY.matcher(line); | |
217 | |
218 if (m.matches()) { | |
219 String tmp = m.group(1); | |
220 tmp = tmp.replace(";", ""); | |
221 | |
222 obj.setEvaluationBy(tmp); | |
223 | |
224 return true; | |
225 } | |
226 | |
227 return false; | |
228 } | |
229 | |
230 | |
231 protected boolean handleMetaRange(ImportBedHeightSingle obj, String line) { | |
232 Matcher m = META_RANGE.matcher(line); | |
233 | |
234 if (m.matches() && m.groupCount() >= 2) { | |
235 String a = m.group(1).replace(";", ""); | |
236 String b = m.group(2).replace(";", ""); | |
237 | |
238 try { | |
239 BigDecimal lower = new BigDecimal(nf.parse(a).doubleValue()); | |
240 BigDecimal upper = new BigDecimal(nf.parse(b).doubleValue()); | |
241 | |
242 obj.setRange(new ImportRange(lower, upper)); | |
243 | |
244 return true; | |
245 } | |
246 catch (ParseException e) { | |
247 log.warn("Error while parsing range!", e); | |
248 } | |
249 } | |
250 | |
251 return false; | |
252 } | |
253 | |
254 | |
255 protected boolean handleMetaType(ImportBedHeightSingle obj, String line) { | |
256 Matcher m = META_TYPE.matcher(line); | |
257 | |
258 if (m.matches()) { | |
259 String tmp = m.group(1).replace(";", ""); | |
260 | |
261 obj.setType(new ImportBedHeightType( | |
262 ImportBedHeightType.getBedHeightName(tmp), | |
263 tmp)); | |
264 | |
265 return true; | |
266 } | |
267 | |
268 return false; | |
269 } | |
270 | |
271 | |
272 protected boolean handleMetaLocationSystem( | |
273 ImportBedHeightSingle obj, | |
274 String line | |
275 ) { | |
276 Matcher m = META_LOCATION_SYSTEM.matcher(line); | |
277 | |
278 if (m.matches()) { | |
279 String tmp = m.group(1).replace(";", ""); | |
280 | |
281 obj.setLocationSystem(new ImportLocationSystem(tmp, tmp)); | |
282 | |
283 return true; | |
284 } | |
285 | |
286 return false; | |
287 } | |
288 | |
289 | |
290 protected boolean handleMetaCurElevationModel( | |
291 ImportBedHeightSingle obj, | |
292 String line | |
293 ) { | |
294 Matcher m = META_CUR_ELEVATION_SYSTEM.matcher(line); | |
295 | |
296 if (m.matches()) { | |
297 String name = m.group(1); | |
298 String num = m.group(2); | |
299 String unit = m.group(3); | |
300 | |
301 obj.setCurElevationModel(new ImportElevationModel( | |
302 name + " " + num, | |
303 new ImportUnit(unit) | |
304 )); | |
305 | |
306 return true; | |
307 } | |
308 | |
309 return false; | |
310 } | |
311 | |
312 | |
313 protected boolean handleMetaOldElevationModel( | |
314 ImportBedHeightSingle obj, | |
315 String line | |
316 ) { | |
317 Matcher m = META_OLD_ELEVATION_SYSTEM.matcher(line); | |
318 | |
319 if (m.matches()) { | |
320 String name = m.group(1); | |
321 String num = m.group(2); | |
322 String unit = m.group(3); | |
323 | |
324 obj.setOldElevationModel(new ImportElevationModel( | |
325 name + " " + num, | |
326 new ImportUnit(unit) | |
327 )); | |
328 | |
329 return true; | |
330 } | |
331 | |
332 return false; | |
333 } | |
334 | |
335 | |
336 protected void handleDataLine(ImportBedHeightSingle obj, String line) { | |
337 log.debug("Handle data line: '" + line + "'"); | |
338 | |
339 String[] values = line.split(SEPERATOR_CHAR); | |
340 | |
341 if (values == null || values.length < 6) { | |
342 log.warn("Error while parsing data line: '" + line + "'"); | |
343 return; | |
344 } | |
345 | |
346 | |
347 try { | |
348 ImportBedHeightSingleValue value = new ImportBedHeightSingleValue( | |
349 obj, | |
350 new BigDecimal(nf.parse(values[0]).doubleValue()), | |
351 new BigDecimal(nf.parse(values[1]).doubleValue()), | |
352 new BigDecimal(nf.parse(values[2]).doubleValue()), | |
353 new BigDecimal(nf.parse(values[3]).doubleValue()), | |
354 new BigDecimal(nf.parse(values[4]).doubleValue()), | |
355 new BigDecimal(nf.parse(values[5]).doubleValue()) | |
356 ); | |
357 | |
358 obj.addValue(value); | |
359 } | |
360 catch (ParseException e) { | |
361 log.warn("Error while parsing data row.", e); | |
362 } | |
363 } | |
364 | |
365 | |
366 protected static String stripMetaLine(String line) { | |
367 String tmp = line.substring(1, line.length()-1); | |
368 | |
369 if (tmp.startsWith(" ")) { | |
370 return tmp.substring(1, tmp.length()-1); | |
371 } | |
372 else { | |
373 return tmp; | |
374 } | |
375 } | |
63 } | 376 } |
64 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 377 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |