comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java @ 2816:70b4a31a3306

Implemented the method stubs of the parser for sediment density and made some db schema adaptions. flys-backend/trunk@4233 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Apr 2012 10:59:15 +0000
parents 3febaed762b8
children 8979f2294af9
comparison
equal deleted inserted replaced
2815:3febaed762b8 2816:70b4a31a3306
1 package de.intevation.flys.importer.parsers; 1 package de.intevation.flys.importer.parsers;
2
3 import java.math.BigDecimal;
4
5 import java.text.NumberFormat;
6 import java.text.ParseException;
2 7
3 import java.util.ArrayList; 8 import java.util.ArrayList;
4 import java.util.List; 9 import java.util.List;
10 import java.util.Locale;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
5 13
6 import org.apache.log4j.Logger; 14 import org.apache.log4j.Logger;
7 15
16 import de.intevation.flys.importer.ImportDepth;
8 import de.intevation.flys.importer.ImportSedimentDensity; 17 import de.intevation.flys.importer.ImportSedimentDensity;
18 import de.intevation.flys.importer.ImportSedimentDensityValue;
19 import de.intevation.flys.importer.ImportUnit;
9 20
10 21
11 public class SedimentDensityParser extends LineParser { 22 public class SedimentDensityParser extends LineParser {
12 23
13 private static final Logger log = 24 private static final Logger log =
14 Logger.getLogger(SedimentDensityParser.class); 25 Logger.getLogger(SedimentDensityParser.class);
26
27
28 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
29
30
31 public static final Pattern META_UNIT =
32 Pattern.compile("^Einheit: \\[(.*)\\].*");
33
34 public static final Pattern META_DEPTH =
35 Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
15 36
16 37
17 protected List<ImportSedimentDensity> sedimentDensities; 38 protected List<ImportSedimentDensity> sedimentDensities;
18 39
19 protected ImportSedimentDensity current; 40 protected ImportSedimentDensity current;
38 } 59 }
39 60
40 61
41 @Override 62 @Override
42 protected void handleLine(String line) { 63 protected void handleLine(String line) {
43 log.debug("handle line: '" + 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 if (handleMetaDepth(line)) {
78 return;
79 }
80 else {
81 log.warn("Unknown meta line: '" + line + "'");
82 }
83 }
84
85
86 protected boolean handleMetaUnit(String line) {
87 Matcher m = META_UNIT.matcher(line);
88
89 if (m.matches()) {
90 String unit = m.group(1);
91
92 current.setUnit(new ImportUnit(unit));
93
94 return true;
95 }
96
97 return false;
98 }
99
100
101 protected boolean handleMetaDepth(String line) {
102 Matcher m = META_DEPTH.matcher(line);
103
104 if (m.matches()) {
105 String lo = m.group(1);
106 String up = m.group(2);
107 String unit = m.group(4);
108
109 try {
110 ImportDepth depth = new ImportDepth(
111 new BigDecimal(nf.parse(lo).doubleValue()),
112 new BigDecimal(nf.parse(up).doubleValue()),
113 new ImportUnit(unit)
114 );
115
116 current.setDepth(depth);
117
118 return true;
119 }
120 catch (ParseException pe) {
121 log.warn("Error while parsing numbers in: '" + line + "'");
122 }
123 }
124
125 return false;
126 }
127
128
129 protected void handleDataLine(String line) {
130 String[] vals = line.split(SEPERATOR_CHAR);
131
132 if (vals == null || vals.length < 3) {
133 log.warn("skip invalid data line: '" + line + "'");
134 return;
135 }
136
137 try {
138 BigDecimal km = new BigDecimal(nf.parse(vals[0]).doubleValue());
139 BigDecimal density = new BigDecimal(nf.parse(vals[1]).doubleValue());
140
141 current.addValue(new ImportSedimentDensityValue(
142 km,
143 density,
144 vals[2])
145 );
146 }
147 catch (ParseException pe) {
148 log.warn("Error while parsing numbers in '" + line + "'");
149 }
44 } 150 }
45 151
46 152
47 public List<ImportSedimentDensity> getSedimentDensities() { 153 public List<ImportSedimentDensity> getSedimentDensities() {
48 return sedimentDensities; 154 return sedimentDensities;

http://dive4elements.wald.intevation.org