comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/BedHeightSingleParser.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/BedHeightSingleParser.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.ParseException;
6
7 import org.apache.log4j.Logger;
8
9 import de.intevation.flys.importer.ImportBedHeight;
10 import de.intevation.flys.importer.ImportBedHeightSingle;
11 import de.intevation.flys.importer.ImportBedHeightSingleValue;
12
13
14 public class BedHeightSingleParser extends BedHeightParser {
15
16 private static final Logger log =
17 Logger.getLogger(BedHeightSingleParser.class);
18
19
20
21 @Override
22 protected ImportBedHeight newImportBedHeight(String description) {
23 return new ImportBedHeightSingle(description);
24 }
25
26
27
28 /**
29 * Create ImportBedHeightSingleValue from a line of csv file, add
30 * it to the ImportBedHeight.
31 */
32 @Override
33 protected void handleDataLine(ImportBedHeight obj, String line) {
34 String[] values = line.split(SEPERATOR_CHAR);
35
36 if (values == null || (values.length != 1 && values.length < 6)) {
37 //log.warn("BSP: Error while parsing data line: '" + line + "'");
38 return;
39 }
40
41 BigDecimal km;
42
43 try {
44 km = new BigDecimal(nf.parse(values[0]).doubleValue());
45 }
46 catch (ParseException e) {
47 // We expect a lot of ";;;;;;" lines.
48 //log.warn("BSP: Error while parsing km of data row.", e);
49 return;
50 }
51
52 // Handle gaps like "10,0;;;;;".
53 if (values.length == 1) {
54 ImportBedHeightSingleValue value = new ImportBedHeightSingleValue(
55 (ImportBedHeightSingle) obj,
56 km,
57 null, null, null, null, null);
58 obj.addValue(value);
59 return;
60 }
61
62 // Because we cannot enforce consistency of values with complete data
63 // via null constraints in the database (as there are "gap" values),
64 // do this checks manually.
65 if (values[3].length() == 0 || values[4].length() == 0
66 || values[5].length() == 0) {
67 //log.warn("BSP: Error while parsing data row (manual null constraint violated).");
68 return;
69 }
70
71 try {
72 ImportBedHeightSingleValue value = new ImportBedHeightSingleValue(
73 (ImportBedHeightSingle) obj,
74 km,
75 new BigDecimal(nf.parse(values[1]).doubleValue()),
76 new BigDecimal(nf.parse(values[2]).doubleValue()),
77 new BigDecimal(nf.parse(values[3]).doubleValue()),
78 parseBigDecimal(values[4], line),
79 new BigDecimal(nf.parse(values[5]).doubleValue())
80 );
81
82 obj.addValue(value);
83 }
84 catch (ParseException e) {
85 log.warn("BSP: unparseable value in data row.", e);
86 }
87 }
88
89 private BigDecimal parseBigDecimal(String value, String line) {
90 BigDecimal result = null;
91 try {
92 Double dValue = Double.valueOf(value.replace(",", "."));
93 result = new BigDecimal(dValue.doubleValue());
94 }
95 catch (NumberFormatException nfe) {
96 log.warn(
97 "Could not parse " +
98 value +
99 " in bed heigt single row: "
100 + line);
101 }
102 return result;
103 }
104 }
105 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org