Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java @ 1215:8aef353e54fb
Initial version of the HYK parser. Not ready, yet.
flys-backend/trunk@2341 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 15 Jul 2011 16:57:13 +0000 |
parents | |
children | f8b5c37f15e4 |
line wrap: on
line source
package de.intevation.flys.importer.parsers; import de.intevation.flys.importer.ImportHYKFlowZoneType; import java.io.File; import java.io.IOException; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.HashMap; import java.util.Map; import java.math.BigDecimal; import org.apache.log4j.Logger; public class HYKParser { private static Logger log = Logger.getLogger(HYKParser.class); public static enum State { LINE_1, LINE_2, LINE_3, LINE_4, LINE_5, LINE_6 }; private static final String ENCODING = "ISO-8859-1"; protected Map<String, ImportHYKFlowZoneType> flowZoneTypes; public HYKParser() { flowZoneTypes = new HashMap<String, ImportHYKFlowZoneType>(); } public boolean parse(File file) { log.info("Parsing HYK file '" + file + "'"); LineNumberReader in = null; try { in = new LineNumberReader( new InputStreamReader( new FileInputStream(file), ENCODING)); String line; State state = State.LINE_1; int numFormations = 0; BigDecimal km = null; BigDecimal top = null; BigDecimal bottom = null; BigDecimal distanceVL = null; BigDecimal distanceHF = null; BigDecimal distanceVR = null; Integer year = null; int numZones = 0; ImportHYKFlowZoneType [] fzts = null; BigDecimal [] coords = null; int coordPos = 0; while ((line = in.readLine()) != null) { if ((line = line.trim()).length() == 0) { continue; } if (line.startsWith("*")) { continue; } String [] parts = line.split("\\s+"); switch (state) { case LINE_1: if (parts.length < 2) { log.error("not enough elements in line " + in.getLineNumber()); return false; } if (parts.length == 2) { // no year given year = null; } else { try { year = Integer.valueOf(parts[1]); } catch (NumberFormatException nfe) { log.error( "year is not an integer in line " + in.getLineNumber()); return false; } } try { km = new BigDecimal(parts[0]); numFormations = Integer.parseInt( parts[parts.length > 2 ? 2 : 1]); } catch (NumberFormatException nfe) { log.error( "parsing number of formations " + "or km failed in line " + in.getLineNumber()); return false; } // TODO: Store HYKEntry state = State.LINE_2; break; case LINE_2: if (parts.length < 3) { log.error("not enough elements in line " + in.getLineNumber()); return false; } try { numZones = Integer.parseInt(parts[0]); bottom = new BigDecimal(parts[1]); top = new BigDecimal(parts[2]); } catch (NumberFormatException nfe) { log.error( "parsing num zones, bottom or top height " + "failed in line " + in.getLineNumber()); return false; } state = State.LINE_3; break; case LINE_3: if (parts.length != numZones) { log.error( "number of flow zones mismatches " + "in line " + in.getLineNumber()); return false; } fzts = new ImportHYKFlowZoneType[parts.length]; for (int i = 0; i < fzts.length; ++i) { fzts[i] = getFlowZoneType(parts[i]); } state = State.LINE_4; break; case LINE_4: try { int N = Math.min(parts.length, coords.length); for (coordPos = 0; coordPos < N; ++coordPos) { coords[coordPos] = new BigDecimal(parts[coordPos]); } } catch (NumberFormatException nfe) { log.error("cannot parse number in line " + in.getLineNumber()); return false; } state = State.LINE_5; break; case LINE_5: if (parts.length + coordPos < coords.length) { log.error("not enough elements in line " + in.getLineNumber()); return false; } try { for (int i = 0; i < parts.length && coordPos < coords.length; ++i, ++coordPos ) { coords[coordPos] = new BigDecimal(parts[i]); } } catch (NumberFormatException nfe) { log.error("cannot parse number in line " + in.getLineNumber()); return false; } state = State.LINE_6; break; case LINE_6: if (parts.length < 3) { log.error("not enough elements in line " + in.getLineNumber()); return false; } try { distanceVL = new BigDecimal(parts[0]); distanceHF = new BigDecimal(parts[1]); distanceVL = new BigDecimal(parts[2]); } catch (NumberFormatException nfe) { log.error("cannot parse number in line " + in.getLineNumber()); return false; } // TODO: now we have enough information // to store a formation. // continue with next formation. state = --numFormations > 0 // formations left? ? State.LINE_2 : State.LINE_1; break; } } } catch (IOException ioe) { log.error(ioe); return false; } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { log.error(ioe); } } } return true; } protected ImportHYKFlowZoneType getFlowZoneType(String name) { name = name.toUpperCase(); ImportHYKFlowZoneType fzt = flowZoneTypes.get(name); if (fzt == null) { fzt = new ImportHYKFlowZoneType(name); flowZoneTypes.put(name, fzt); } return fzt; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :