Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java @ 1216:f8b5c37f15e4
Fixes for the HYK parser
flys-backend/trunk@2342 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 17 Jul 2011 10:00:13 +0000 |
parents | 8aef353e54fb |
children | d80997bd94ce |
comparison
equal
deleted
inserted
replaced
1215:8aef353e54fb | 1216:f8b5c37f15e4 |
---|---|
13 | 13 |
14 import java.math.BigDecimal; | 14 import java.math.BigDecimal; |
15 | 15 |
16 import org.apache.log4j.Logger; | 16 import org.apache.log4j.Logger; |
17 | 17 |
18 import de.intevation.flys.utils.FileTools; | |
19 | |
18 public class HYKParser | 20 public class HYKParser |
19 { | 21 { |
20 private static Logger log = Logger.getLogger(HYKParser.class); | 22 private static Logger log = Logger.getLogger(HYKParser.class); |
21 | 23 |
24 public interface Callback { | |
25 boolean hykAccept(File file); | |
26 void hykParsed(HYKParser parser); | |
27 } // interface Callback | |
28 | |
22 public static enum State { | 29 public static enum State { |
23 LINE_1, LINE_2, LINE_3, LINE_4, LINE_5, LINE_6 | 30 LINE_1, LINE_2, LINE_3, LINE_4, LINE_5, LINE_6 |
24 }; | 31 }; |
25 | 32 |
26 private static final String ENCODING = "ISO-8859-1"; | 33 private static final String ENCODING = "ISO-8859-1"; |
30 public HYKParser() { | 37 public HYKParser() { |
31 flowZoneTypes = new HashMap<String, ImportHYKFlowZoneType>(); | 38 flowZoneTypes = new HashMap<String, ImportHYKFlowZoneType>(); |
32 } | 39 } |
33 | 40 |
34 public boolean parse(File file) { | 41 public boolean parse(File file) { |
42 | |
43 boolean debug = log.isDebugEnabled(); | |
35 | 44 |
36 log.info("Parsing HYK file '" + file + "'"); | 45 log.info("Parsing HYK file '" + file + "'"); |
37 | 46 |
38 LineNumberReader in = null; | 47 LineNumberReader in = null; |
39 | 48 |
62 ImportHYKFlowZoneType [] fzts = null; | 71 ImportHYKFlowZoneType [] fzts = null; |
63 BigDecimal [] coords = null; | 72 BigDecimal [] coords = null; |
64 int coordPos = 0; | 73 int coordPos = 0; |
65 | 74 |
66 while ((line = in.readLine()) != null) { | 75 while ((line = in.readLine()) != null) { |
67 if ((line = line.trim()).length() == 0) { | 76 |
77 if (line.startsWith("*") || line.startsWith("----")) { | |
68 continue; | 78 continue; |
69 } | 79 } |
70 if (line.startsWith("*")) { | 80 |
81 line = line.trim(); | |
82 | |
83 if (state != State.LINE_5 && line.length() == 0) { | |
71 continue; | 84 continue; |
72 } | 85 } |
86 | |
73 String [] parts = line.split("\\s+"); | 87 String [] parts = line.split("\\s+"); |
88 | |
89 if (debug) { | |
90 log.debug("'" + line + "': " + state); | |
91 } | |
74 | 92 |
75 switch (state) { | 93 switch (state) { |
76 case LINE_1: | 94 case LINE_1: |
77 if (parts.length < 2) { | 95 if (parts.length < 2) { |
78 log.error("not enough elements in line " + | 96 log.error("1: not enough elements in line " + |
79 in.getLineNumber()); | 97 in.getLineNumber()); |
80 return false; | 98 return false; |
81 } | 99 } |
82 | 100 |
83 if (parts.length == 2) { | 101 if (parts.length == 2) { |
111 state = State.LINE_2; | 129 state = State.LINE_2; |
112 break; | 130 break; |
113 | 131 |
114 case LINE_2: | 132 case LINE_2: |
115 if (parts.length < 3) { | 133 if (parts.length < 3) { |
116 log.error("not enough elements in line " + | 134 log.error("2: not enough elements in line " + |
117 in.getLineNumber()); | 135 in.getLineNumber()); |
118 return false; | 136 return false; |
119 } | 137 } |
120 try { | 138 try { |
121 numZones = Integer.parseInt(parts[0]); | 139 numZones = Integer.parseInt(parts[0]); |
141 | 159 |
142 fzts = new ImportHYKFlowZoneType[parts.length]; | 160 fzts = new ImportHYKFlowZoneType[parts.length]; |
143 for (int i = 0; i < fzts.length; ++i) { | 161 for (int i = 0; i < fzts.length; ++i) { |
144 fzts[i] = getFlowZoneType(parts[i]); | 162 fzts[i] = getFlowZoneType(parts[i]); |
145 } | 163 } |
164 coords = new BigDecimal[numZones]; | |
146 state = State.LINE_4; | 165 state = State.LINE_4; |
147 break; | 166 break; |
148 | 167 |
149 case LINE_4: | 168 case LINE_4: |
150 try { | 169 try { |
162 state = State.LINE_5; | 181 state = State.LINE_5; |
163 break; | 182 break; |
164 | 183 |
165 case LINE_5: | 184 case LINE_5: |
166 if (parts.length + coordPos < coords.length) { | 185 if (parts.length + coordPos < coords.length) { |
167 log.error("not enough elements in line " + | 186 log.error("5: not enough elements in line " + |
168 in.getLineNumber()); | 187 in.getLineNumber()); |
169 return false; | 188 return false; |
170 } | 189 } |
171 try { | 190 try { |
172 for (int i = 0; | 191 for (int i = 0; |
184 state = State.LINE_6; | 203 state = State.LINE_6; |
185 break; | 204 break; |
186 | 205 |
187 case LINE_6: | 206 case LINE_6: |
188 if (parts.length < 3) { | 207 if (parts.length < 3) { |
189 log.error("not enough elements in line " + | 208 log.error("6: not enough elements in line " + |
190 in.getLineNumber()); | 209 in.getLineNumber()); |
191 return false; | 210 return false; |
192 } | 211 } |
193 try { | 212 try { |
194 distanceVL = new BigDecimal(parts[0]); | 213 distanceVL = new BigDecimal(parts[0]); |
230 | 249 |
231 protected ImportHYKFlowZoneType getFlowZoneType(String name) { | 250 protected ImportHYKFlowZoneType getFlowZoneType(String name) { |
232 name = name.toUpperCase(); | 251 name = name.toUpperCase(); |
233 ImportHYKFlowZoneType fzt = flowZoneTypes.get(name); | 252 ImportHYKFlowZoneType fzt = flowZoneTypes.get(name); |
234 if (fzt == null) { | 253 if (fzt == null) { |
254 log.info("New flow zone type: " + name); | |
235 fzt = new ImportHYKFlowZoneType(name); | 255 fzt = new ImportHYKFlowZoneType(name); |
236 flowZoneTypes.put(name, fzt); | 256 flowZoneTypes.put(name, fzt); |
237 } | 257 } |
238 return fzt; | 258 return fzt; |
239 } | 259 } |
260 | |
261 protected void reset() { | |
262 // TODO: reset per file data structures | |
263 } | |
264 | |
265 public void parseHYKs(File root, final Callback callback) { | |
266 | |
267 FileTools.walkTree(root, new FileTools.FileVisitor() { | |
268 @Override | |
269 public boolean visit(File file) { | |
270 if (file.isFile() && file.canRead() | |
271 && file.getName().toLowerCase().endsWith(".hyk") | |
272 && (callback == null || callback.hykAccept(file))) { | |
273 reset(); | |
274 boolean success = parse(file); | |
275 log.info("parsing " + (success ? "succeeded" : "failed")); | |
276 if (success && callback != null) { | |
277 callback.hykParsed(HYKParser.this); | |
278 } | |
279 } | |
280 return true; | |
281 } | |
282 }); | |
283 } | |
284 | |
285 public static void main(String [] args) { | |
286 | |
287 HYKParser parser = new HYKParser(); | |
288 | |
289 for (String arg: args) { | |
290 parser.parseHYKs(new File(arg), null); | |
291 } | |
292 } | |
240 } | 293 } |
241 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 294 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |