comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightParser.java @ 2811:8926571e47fb

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

http://dive4elements.wald.intevation.org