comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.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/MorphologicalWidthParser.java@b3dd14fc13a6
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
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 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
26
27 public static final Pattern META_UNIT =
28 Pattern.compile("^Einheit: \\[(.*)\\].*");
29
30 protected List<ImportMorphWidth> morphWidths;
31
32 protected ImportMorphWidth current;
33
34
35 public MorphologicalWidthParser() {
36 morphWidths = new ArrayList<ImportMorphWidth>();
37 }
38
39
40 @Override
41 protected void reset() {
42 current = new ImportMorphWidth();
43 }
44
45
46 @Override
47 protected void finish() {
48 if (current != null) {
49 morphWidths.add(current);
50 }
51 }
52
53
54 @Override
55 protected void handleLine(int lineNum, String line) {
56 if (line.startsWith(START_META_CHAR)) {
57 handleMetaLine(stripMetaLine(line));
58 }
59 else {
60 handleDataLine(line);
61 }
62 }
63
64
65 protected void handleMetaLine(String line) {
66 if (handleMetaUnit(line)) {
67 return;
68 }
69 else {
70 log.warn("MWP: Unknown meta line: '" + line + "'");
71 }
72 }
73
74
75 protected boolean handleMetaUnit(String line) {
76 Matcher m = META_UNIT.matcher(line);
77
78 if (m.matches()) {
79 String unit = m.group(1);
80
81 current.setUnit(new ImportUnit(unit));
82
83 return true;
84 }
85
86 return false;
87 }
88
89
90 protected void handleDataLine(String line) {
91 String[] vals = line.split(SEPERATOR_CHAR);
92
93 if (vals == null || vals.length < 2) {
94 log.warn("MWP: skip invalid data line: '" + line + "'");
95 return;
96 }
97
98 try {
99 BigDecimal km = new BigDecimal(nf.parse(vals[0]).doubleValue());
100 BigDecimal width = new BigDecimal(nf.parse(vals[1]).doubleValue());
101
102 String desc = vals.length > 2 ? vals[2] : null;
103
104 current.addValue(new ImportMorphWidthValue(
105 km,
106 width,
107 desc
108 ));
109 }
110 catch (ParseException pe) {
111 log.warn("MWP: unparseable number in data row: " + line);
112 }
113 }
114
115
116 public List<ImportMorphWidth> getMorphologicalWidths() {
117 return morphWidths;
118 }
119 }
120 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org