Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java @ 1219:d80997bd94ce
HYKParser: Create data structures while parsing.
flys-backend/trunk@2345 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 17 Jul 2011 22:30:09 +0000 |
parents | f8b5c37f15e4 |
children | ca7d461a53f1 |
comparison
equal
deleted
inserted
replaced
1218:00e37d22a589 | 1219:d80997bd94ce |
---|---|
1 package de.intevation.flys.importer.parsers; | 1 package de.intevation.flys.importer.parsers; |
2 | 2 |
3 import de.intevation.flys.importer.ImportHYK; | |
4 import de.intevation.flys.importer.ImportHYKEntry; | |
5 import de.intevation.flys.importer.ImportHYKFormation; | |
6 import de.intevation.flys.importer.ImportHYKFlowZone; | |
3 import de.intevation.flys.importer.ImportHYKFlowZoneType; | 7 import de.intevation.flys.importer.ImportHYKFlowZoneType; |
4 | 8 |
5 import java.io.File; | 9 import java.io.File; |
6 import java.io.IOException; | 10 import java.io.IOException; |
7 import java.io.FileInputStream; | 11 import java.io.FileInputStream; |
8 import java.io.InputStreamReader; | 12 import java.io.InputStreamReader; |
9 import java.io.LineNumberReader; | 13 import java.io.LineNumberReader; |
10 | 14 |
11 import java.util.HashMap; | 15 import java.util.HashMap; |
12 import java.util.Map; | 16 import java.util.Map; |
17 import java.util.Date; | |
18 import java.util.Calendar; | |
13 | 19 |
14 import java.math.BigDecimal; | 20 import java.math.BigDecimal; |
15 | 21 |
16 import org.apache.log4j.Logger; | 22 import org.apache.log4j.Logger; |
17 | 23 |
32 | 38 |
33 private static final String ENCODING = "ISO-8859-1"; | 39 private static final String ENCODING = "ISO-8859-1"; |
34 | 40 |
35 protected Map<String, ImportHYKFlowZoneType> flowZoneTypes; | 41 protected Map<String, ImportHYKFlowZoneType> flowZoneTypes; |
36 | 42 |
43 protected ImportHYK hyk; | |
44 | |
37 public HYKParser() { | 45 public HYKParser() { |
38 flowZoneTypes = new HashMap<String, ImportHYKFlowZoneType>(); | 46 flowZoneTypes = new HashMap<String, ImportHYKFlowZoneType>(); |
39 } | 47 } |
40 | 48 |
49 public ImportHYK getHYK() { | |
50 return hyk; | |
51 } | |
52 | |
53 private static Date yearToDate(Integer year) { | |
54 if (year == null) { | |
55 return null; | |
56 } | |
57 Calendar cal = Calendar.getInstance(); | |
58 cal.set(year, 0, 1, 12, 0, 0); | |
59 long ms = cal.getTimeInMillis(); | |
60 cal.setTimeInMillis(ms - ms%1000); | |
61 return cal.getTime(); | |
62 } | |
63 | |
41 public boolean parse(File file) { | 64 public boolean parse(File file) { |
42 | 65 |
43 boolean debug = log.isDebugEnabled(); | 66 boolean debug = log.isDebugEnabled(); |
44 | 67 |
45 log.info("Parsing HYK file '" + file + "'"); | 68 log.info("Parsing HYK file '" + file + "'"); |
46 | 69 |
47 LineNumberReader in = null; | 70 LineNumberReader in = null; |
71 | |
72 String description = | |
73 file.getParentFile().getName() + "/" + file.getName(); | |
74 | |
75 hyk = new ImportHYK(null, description); | |
48 | 76 |
49 try { | 77 try { |
50 in = | 78 in = |
51 new LineNumberReader( | 79 new LineNumberReader( |
52 new InputStreamReader( | 80 new InputStreamReader( |
69 int numZones = 0; | 97 int numZones = 0; |
70 | 98 |
71 ImportHYKFlowZoneType [] fzts = null; | 99 ImportHYKFlowZoneType [] fzts = null; |
72 BigDecimal [] coords = null; | 100 BigDecimal [] coords = null; |
73 int coordPos = 0; | 101 int coordPos = 0; |
102 | |
103 ImportHYKEntry entry = null; | |
104 ImportHYKFormation formation = null; | |
74 | 105 |
75 while ((line = in.readLine()) != null) { | 106 while ((line = in.readLine()) != null) { |
76 | 107 |
77 if (line.startsWith("*") || line.startsWith("----")) { | 108 if (line.startsWith("*") || line.startsWith("----")) { |
78 continue; | 109 continue; |
122 log.error( | 153 log.error( |
123 "parsing number of formations " + | 154 "parsing number of formations " + |
124 "or km failed in line " + in.getLineNumber()); | 155 "or km failed in line " + in.getLineNumber()); |
125 return false; | 156 return false; |
126 } | 157 } |
127 // TODO: Store HYKEntry | 158 entry = new ImportHYKEntry(hyk, km, yearToDate(year)); |
159 hyk.addEntry(entry); | |
128 | 160 |
129 state = State.LINE_2; | 161 state = State.LINE_2; |
130 break; | 162 break; |
131 | 163 |
132 case LINE_2: | 164 case LINE_2: |
144 log.error( | 176 log.error( |
145 "parsing num zones, bottom or top height " + | 177 "parsing num zones, bottom or top height " + |
146 "failed in line " + in.getLineNumber()); | 178 "failed in line " + in.getLineNumber()); |
147 return false; | 179 return false; |
148 } | 180 } |
181 formation = new ImportHYKFormation(); | |
182 formation.setEntry(entry); | |
183 | |
149 state = State.LINE_3; | 184 state = State.LINE_3; |
150 break; | 185 break; |
151 | 186 |
152 case LINE_3: | 187 case LINE_3: |
153 if (parts.length != numZones) { | 188 if (parts.length != numZones) { |
198 catch (NumberFormatException nfe) { | 233 catch (NumberFormatException nfe) { |
199 log.error("cannot parse number in line " + | 234 log.error("cannot parse number in line " + |
200 in.getLineNumber()); | 235 in.getLineNumber()); |
201 return false; | 236 return false; |
202 } | 237 } |
238 for (int i = 0; i < coords.length; ++i) { | |
239 ImportHYKFlowZone zone = new ImportHYKFlowZone( | |
240 formation, | |
241 fzts[i], | |
242 coords[i], | |
243 coords[i == coords.length-1 ? i : i+1]); | |
244 formation.addFlowZone(zone); | |
245 } | |
203 state = State.LINE_6; | 246 state = State.LINE_6; |
204 break; | 247 break; |
205 | 248 |
206 case LINE_6: | 249 case LINE_6: |
207 if (parts.length < 3) { | 250 if (parts.length < 3) { |
217 catch (NumberFormatException nfe) { | 260 catch (NumberFormatException nfe) { |
218 log.error("cannot parse number in line " + | 261 log.error("cannot parse number in line " + |
219 in.getLineNumber()); | 262 in.getLineNumber()); |
220 return false; | 263 return false; |
221 } | 264 } |
222 // TODO: now we have enough information | 265 formation.setDistanceVL(distanceVL); |
223 // to store a formation. | 266 formation.setDistanceHF(distanceHF); |
267 formation.setDistanceVR(distanceVR); | |
224 | 268 |
225 // continue with next formation. | 269 // continue with next formation. |
226 state = --numFormations > 0 // formations left? | 270 state = --numFormations > 0 // formations left? |
227 ? State.LINE_2 | 271 ? State.LINE_2 |
228 : State.LINE_1; | 272 : State.LINE_1; |
257 } | 301 } |
258 return fzt; | 302 return fzt; |
259 } | 303 } |
260 | 304 |
261 protected void reset() { | 305 protected void reset() { |
262 // TODO: reset per file data structures | 306 hyk = null; |
263 } | 307 } |
264 | 308 |
265 public void parseHYKs(File root, final Callback callback) { | 309 public void parseHYKs(File root, final Callback callback) { |
266 | 310 |
267 FileTools.walkTree(root, new FileTools.FileVisitor() { | 311 FileTools.walkTree(root, new FileTools.FileVisitor() { |