comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/BedHeightParser.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightParser.java@2c6f4a3c5a25
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
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 import de.intevation.flys.importer.ImporterSession;
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: [^0-9]*(\\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 /** Handle a line of file that contains data (in contrast to comments, meta). */
88 protected abstract void handleDataLine(
89 ImportBedHeight importBedHeight,
90 String line
91 );
92
93
94
95 public BedHeightParser() {
96 this.bedHeights = new ArrayList<ImportBedHeight>();
97 }
98
99
100 public List<ImportBedHeight> getBedHeights() {
101 return bedHeights;
102 }
103
104
105 public void parse(File file) throws IOException {
106 log.info("Parsing bed height single file '" + file + "'");
107
108 ImportBedHeight obj = newImportBedHeight(file.getName());
109
110 LineNumberReader in = null;
111 try {
112 in =
113 new LineNumberReader(
114 new InputStreamReader(
115 new FileInputStream(file), ENCODING));
116
117 String line = null;
118 while ((line = in.readLine()) != null) {
119 if ((line = line.trim()).length() == 0) {
120 continue;
121 }
122
123 if (line.startsWith(START_META_CHAR)) {
124 handleMetaLine(obj, line);
125 }
126 else {
127 handleDataLine(obj, line);
128 }
129 }
130
131 log.info("File contained " + obj.getValueCount() + " values.");
132 bedHeights.add(obj);
133 }
134 finally {
135 if (in != null) {
136 in.close();
137 }
138 }
139 }
140
141
142 protected static String stripMetaLine(String line) {
143 String tmp = line.substring(1, line.length());
144
145 if (tmp.startsWith(" ")) {
146 return tmp.substring(1, tmp.length());
147 }
148 else {
149 return tmp;
150 }
151 }
152
153
154 public static Date getDateFromYear(int year) {
155 Calendar cal = Calendar.getInstance();
156 cal.set(year, 0, 1);
157
158 return cal.getTime();
159 }
160
161
162 protected void handleMetaLine(ImportBedHeight obj, String line) {
163 String meta = stripMetaLine(line);
164
165 if (handleMetaYear(obj, meta)) {
166 return;
167 }
168 else if (handleMetaTimeInterval(obj, meta)) {
169 return;
170 }
171 else if (handleMetaSoundingWidth(obj, meta)) {
172 return;
173 }
174 else if (handleMetaComment(obj, meta)) {
175 return;
176 }
177 else if (handleMetaEvaluationBy(obj, meta)) {
178 return;
179 }
180 else if (handleMetaRange(obj, meta)) {
181 return;
182 }
183 else if (handleMetaType(obj, meta)) {
184 return;
185 }
186 else if (handleMetaLocationSystem(obj, meta)) {
187 return;
188 }
189 else if (handleMetaCurElevationModel(obj, meta)) {
190 return;
191 }
192 else if (handleMetaOldElevationModel(obj, meta)) {
193 return;
194 }
195 else {
196 log.warn("BHP: Meta line did not match any known type: " + line);
197 }
198 }
199
200
201 protected boolean handleMetaYear(ImportBedHeight obj, String line) {
202 Matcher m = META_YEAR.matcher(line);
203
204 if (m.matches()) {
205 String tmp = m.group(1);
206 if (tmp.length() > 0) {
207 obj.setYear(Integer.parseInt(tmp));
208 }
209 else {
210 log.warn("BHP: No year given.");
211 }
212 return true;
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("BHP: could not parse 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("BHP: Could not parse sounding width: " + line, e);
260 log.warn("-> Set default value '0'");
261 }
262 obj.setSoundingWidth(0);
263 }
264
265 return false;
266 }
267
268
269 protected boolean handleMetaComment(ImportBedHeight obj, String line) {
270 Matcher m = META_COMMENTS.matcher(line);
271
272 if (m.matches()) {
273 String tmp = m.group(1);
274
275 obj.setDescription(tmp);
276
277 return true;
278 }
279
280 return false;
281 }
282
283
284 protected boolean handleMetaEvaluationBy(
285 ImportBedHeight obj,
286 String line
287 ) {
288 Matcher m = META_EVALUATION_BY.matcher(line);
289
290 if (m.matches()) {
291 String tmp = m.group(1);
292 tmp = tmp.replace(";", "");
293
294 obj.setEvaluationBy(tmp);
295
296 return true;
297 }
298
299 return false;
300 }
301
302
303 protected boolean handleMetaRange(ImportBedHeight obj, String line) {
304 Matcher m = META_RANGE.matcher(line);
305
306 if (m.matches() && m.groupCount() >= 2) {
307 String a = m.group(1).replace(";", "");
308 String b = m.group(2).replace(";", "");
309
310 try {
311 BigDecimal lower = new BigDecimal(nf.parse(a).doubleValue());
312 BigDecimal upper = new BigDecimal(nf.parse(b).doubleValue());
313
314 obj.setRange(new ImportRange(lower, upper));
315
316 return true;
317 }
318 catch (ParseException e) {
319 log.warn("BHP: could not parse range", e);
320 }
321 }
322
323 return false;
324 }
325
326
327 protected boolean handleMetaType(ImportBedHeight obj, String line) {
328 Matcher m = META_TYPE.matcher(line);
329
330 if (m.matches()) {
331 String tmp = m.group(1).replace(";", "");
332
333 BedHeightType bht = BedHeightType.fetchBedHeightTypeForType(
334 tmp, ImporterSession.getInstance().getDatabaseSession());
335
336 if (bht != null) {
337 obj.setType(new ImportBedHeightType(bht));
338 return true;
339 }
340
341 log.warn("Unknown bed height type: '" + tmp + "'");
342 }
343
344 return false;
345 }
346
347
348 protected boolean handleMetaLocationSystem(
349 ImportBedHeight obj,
350 String line
351 ) {
352 Matcher m = META_LOCATION_SYSTEM.matcher(line);
353
354 if (m.matches()) {
355 String tmp = m.group(1).replace(";", "");
356
357 obj.setLocationSystem(new ImportLocationSystem(tmp, tmp));
358
359 return true;
360 }
361
362 return false;
363 }
364
365
366 protected boolean handleMetaCurElevationModel(
367 ImportBedHeight obj,
368 String line
369 ) {
370 Matcher m = META_CUR_ELEVATION_SYSTEM.matcher(line);
371
372 if (m.matches()) {
373 String name = m.group(1);
374 String num = m.group(2);
375 String unit = m.group(3);
376
377 obj.setCurElevationModel(new ImportElevationModel(
378 name + " " + num,
379 new ImportUnit(unit)
380 ));
381
382 return true;
383 }
384
385 return false;
386 }
387
388
389 protected boolean handleMetaOldElevationModel(
390 ImportBedHeight obj,
391 String line
392 ) {
393 Matcher m = META_OLD_ELEVATION_SYSTEM.matcher(line);
394
395 if (m.matches()) {
396 String name = m.group(1);
397 String num = m.group(2);
398 String unit = m.group(3);
399
400 obj.setOldElevationModel(new ImportElevationModel(
401 name + " " + num,
402 new ImportUnit(unit)
403 ));
404
405 return true;
406 }
407
408 return false;
409 }
410 }
411 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org