comparison backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.java @ 7730:e1b831fe435a slt-simplify-cross-sections

Merged default into slt-simplify-cross-sections branch and updated package and class names.
author Tom Gottfried <tom@intevation.de>
date Mon, 20 Jan 2014 14:04:20 +0100
parents 4c3ccf2b0304
children 5e38e2924c07
comparison
equal deleted inserted replaced
5084:ca45dd039b54 7730:e1b831fe435a
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.importer.parsers;
10
11 import java.math.BigDecimal;
12
13 import java.text.NumberFormat;
14 import java.text.ParseException;
15
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.apache.log4j.Logger;
22
23 import org.dive4elements.river.importer.ImportMorphWidth;
24 import org.dive4elements.river.importer.ImportMorphWidthValue;
25 import org.dive4elements.river.importer.ImportUnit;
26
27
28 public class MorphologicalWidthParser extends LineParser {
29
30 private static final Logger log =
31 Logger.getLogger(MorphologicalWidthParser.class);
32
33 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
34
35 public static final Pattern META_UNIT =
36 Pattern.compile("^Einheit: \\[(.*)\\].*");
37
38 protected List<ImportMorphWidth> morphWidths;
39
40 protected ImportMorphWidth current;
41
42
43 public MorphologicalWidthParser() {
44 morphWidths = new ArrayList<ImportMorphWidth>();
45 }
46
47
48 @Override
49 protected void reset() {
50 current = new ImportMorphWidth();
51 }
52
53
54 @Override
55 protected void finish() {
56 if (current != null) {
57 morphWidths.add(current);
58 }
59 }
60
61
62 @Override
63 protected void handleLine(int lineNum, String line) {
64 if (line.startsWith(START_META_CHAR)) {
65 handleMetaLine(stripMetaLine(line));
66 }
67 else {
68 handleDataLine(line);
69 }
70 }
71
72
73 protected void handleMetaLine(String line) {
74 if (handleMetaUnit(line)) {
75 return;
76 }
77 else {
78 log.warn("MWP: Unknown meta line: '" + line + "'");
79 }
80 }
81
82
83 protected boolean handleMetaUnit(String line) {
84 Matcher m = META_UNIT.matcher(line);
85
86 if (m.matches()) {
87 String unit = m.group(1);
88
89 current.setUnit(new ImportUnit(unit));
90
91 return true;
92 }
93
94 return false;
95 }
96
97
98 protected void handleDataLine(String line) {
99 String[] vals = line.split(SEPERATOR_CHAR);
100
101 if (vals == null || vals.length < 2) {
102 log.warn("MWP: skip invalid data line: '" + line + "'");
103 return;
104 }
105
106 try {
107 BigDecimal km = new BigDecimal(nf.parse(vals[0]).doubleValue());
108 BigDecimal width = new BigDecimal(nf.parse(vals[1]).doubleValue());
109
110 String desc = vals.length > 2 ? vals[2] : null;
111
112 current.addValue(new ImportMorphWidthValue(
113 km,
114 width,
115 desc
116 ));
117 }
118 catch (ParseException pe) {
119 log.warn("MWP: unparseable number in data row: " + line);
120 }
121 }
122
123
124 public List<ImportMorphWidth> getMorphologicalWidths() {
125 return morphWidths;
126 }
127 }
128 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org