Mercurial > dive4elements > river
annotate flys-backend/src/main/java/de/intevation/flys/importer/StaFileParser.java @ 192:f1fce41347ea
Added missing ImportGauge.getPeer() method
flys-backend/trunk@1530 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 21 Mar 2011 10:27:14 +0000 |
parents | bc3747a371cc |
children | 8d3533a03e10 |
rev | line source |
---|---|
189
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.importer; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.io.File; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 import java.io.IOException; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import java.io.LineNumberReader; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 import java.io.FileInputStream; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 import java.io.InputStreamReader; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 import java.math.BigDecimal; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import java.util.regex.Pattern; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 import java.util.regex.Matcher; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 import java.util.HashMap; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import org.apache.log4j.Logger; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 public class StaFileParser |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 private static Logger log = Logger.getLogger(StaFileParser.class); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 public static final String ENCODING = "ISO-8859-1"; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 public static final Pattern QWTD_ = |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([QWTD-]).*"); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 public StaFileParser() { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 public boolean parse(ImportGauge gauge) throws IOException { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 File file = gauge.getStaFile(); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 log.info("parsing STA file: " + file); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 LineNumberReader in = null; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 try { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 in = |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 new LineNumberReader( |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 new InputStreamReader( |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 new FileInputStream(file), ENCODING)); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 String line = in.readLine(); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 if (line == null) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 log.warn("STA file is empty."); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 return false; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 if (line.length() < 37) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 log.warn("first line in STA file is too short."); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 return false; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 gauge.setName(line.substring(16, 37).trim()); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 String [] values = line.substring(38).trim().split("\\s+", 2); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 if (values.length < 2) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 log.warn("Not enough columns for aeo and datum"); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 try { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 gauge.setAeo(new BigDecimal(values[0].replace(",", "."))); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 gauge.setDatum(new BigDecimal(values[1].replace(",", "."))); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 catch (NumberFormatException nfe) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 log.warn("cannot parse aeo or datum"); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 return false; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 line = in.readLine(); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 if (line == null) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 log.warn("STA file has not enough lines"); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 return false; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 if (line.length() < 36) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 log.warn("second line is too short"); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 return false; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 try { |
192
f1fce41347ea
Added missing ImportGauge.getPeer() method
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
189
diff
changeset
|
83 gauge.setStation( |
189
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 new BigDecimal(line.substring(29, 36).trim())); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 catch (NumberFormatException nfe) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 log.warn("parsing of the datum of the gauge failed"); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 return false; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 // overread the next six lines |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 for (int i = 0; i < 6; ++i) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 if ((line = in.readLine()) == null) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 log.warn("STA file is too short"); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 return false; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 HashMap<String, ImportMainValueType> types = |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 new HashMap<String, ImportMainValueType>(); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 while ((line = in.readLine()) != null) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 Matcher m = QWTD_.matcher(line); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 if (m.matches()) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 BigDecimal value; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 try { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 value = new BigDecimal(m.group(2).replace(",", ".")); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 catch (NumberFormatException nfe) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
110 log.warn("value not parseable in line " |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 + in.getLineNumber()); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 continue; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 String typeString = m.group(3); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 log.debug("\t type: " + typeString); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 ImportMainValueType type = types.get(typeString); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 if (type == null) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 type = new ImportMainValueType(typeString); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 types.put(typeString, type); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 else { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 // TODO: treat as a comment |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
126 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 finally { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 if (in != null) { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 in.close(); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
132 log.info("finished parsing STA file: " + file); |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
133 return true; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
134 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
135 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
136 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |