comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/MorphologicalWidthParser.java @ 2821:0a536eb5d668

Added parser for parsing morphological widths. flys-backend/trunk@4238 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Apr 2012 13:22:54 +0000
parents
children 0d27d02b1208
comparison
equal deleted inserted replaced
2820:7dffd28271d0 2821:0a536eb5d668
1 package de.intevation.flys.importer.parsers;
2
3 import java.math.BigDecimal;
4
5 import java.text.NumberFormat;
6 import java.text.ParseException;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import org.apache.log4j.Logger;
14
15 import de.intevation.flys.importer.ImportMorphWidth;
16 import de.intevation.flys.importer.ImportMorphWidthValue;
17 import de.intevation.flys.importer.ImportUnit;
18
19
20 public class MorphologicalWidthParser extends LineParser {
21
22 private static final Logger log =
23 Logger.getLogger(MorphologicalWidthParser.class);
24
25
26 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
27
28
29 public static final Pattern META_UNIT =
30 Pattern.compile("^Einheit: \\[(.*)\\].*");
31
32
33 protected List<ImportMorphWidth> morphWidths;
34
35 protected ImportMorphWidth current;
36
37
38 public MorphologicalWidthParser() {
39 morphWidths = new ArrayList<ImportMorphWidth>();
40 }
41
42
43 @Override
44 protected void reset() {
45 current = new ImportMorphWidth();
46 }
47
48
49 @Override
50 protected void finish() {
51 if (current != null) {
52 morphWidths.add(current);
53 }
54 }
55
56
57 @Override
58 protected void handleLine(String line) {
59 if (line.startsWith(START_META_CHAR)) {
60 handleMetaLine(stripMetaLine(line));
61 }
62 else {
63 handleDataLine(line);
64 }
65 }
66
67
68 protected void handleMetaLine(String line) {
69 if (handleMetaUnit(line)) {
70 return;
71 }
72 else {
73 log.warn("Unknown meta line: '" + line + "'");
74 }
75 }
76
77
78 protected boolean handleMetaUnit(String line) {
79 Matcher m = META_UNIT.matcher(line);
80
81 if (m.matches()) {
82 String unit = m.group(1);
83
84 current.setUnit(new ImportUnit(unit));
85
86 return true;
87 }
88
89 return false;
90 }
91
92
93 protected void handleDataLine(String line) {
94 String[] vals = line.split(SEPERATOR_CHAR);
95
96 if (vals == null || vals.length < 2) {
97 log.warn("skip invalid data line: '" + line + "'");
98 return;
99 }
100
101 try {
102 BigDecimal km = new BigDecimal(nf.parse(vals[0]).doubleValue());
103 BigDecimal width = new BigDecimal(nf.parse(vals[1]).doubleValue());
104
105 String desc = vals.length > 2 ? vals[2] : null;
106
107 current.addValue(new ImportMorphWidthValue(
108 km,
109 width,
110 desc
111 ));
112 }
113 catch (ParseException pe) {
114 log.warn("Error while parsing numbers in '" + line + "'");
115 }
116 }
117
118
119 public List<ImportMorphWidth> getMorphologicalWidths() {
120 return morphWidths;
121 }
122 }
123 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org