# HG changeset patch # User Sascha L. Teichmann # Date 1310030971 0 # Node ID ce3dacc6ea92019cfb1004cfc19afb46b6adcb22 # Parent 46127af605ba191ba45046ddd69077b1e9771f42 PRFParser: extract km from lines. TODO: extract data. flys-backend/trunk@2297 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 46127af605ba -r ce3dacc6ea92 flys-backend/ChangeLog --- a/flys-backend/ChangeLog Wed Jul 06 17:54:49 2011 +0000 +++ b/flys-backend/ChangeLog Thu Jul 07 09:29:31 2011 +0000 @@ -1,3 +1,8 @@ +2011-07-07 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/importer/PRFParser.java: + Extract km from lines. TODO: extract data. + 2011-07-06 Sascha L. Teichmann * src/main/java/de/intevation/flys/importer/PRFParser.java: New. diff -r 46127af605ba -r ce3dacc6ea92 flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java Wed Jul 06 17:54:49 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java Thu Jul 07 09:29:31 2011 +0000 @@ -51,8 +51,11 @@ secondFractionPlaces = Integer.parseInt(m.group(6)); } - public void extractData(String line, Map dest) { + public boolean extractData(String line, Map dest) + throws NumberFormatException + { //TODO: Implement me! + return true; } } // class DataFormat @@ -61,6 +64,9 @@ protected int integerPlaces; protected int fractionPlaces; + protected double scale; + protected double shift; + public KMFormat() { } @@ -68,11 +74,25 @@ deleteChars = Integer.parseInt(m.group(1)); integerPlaces = Integer.parseInt(m.group(2)); fractionPlaces = Integer.parseInt(m.group(3)); + + shift = Math.pow(10, fractionPlaces); + scale = 1d/shift; } - public double extractKm(String line) { - // TODO: Implement me! - return 0d; + public double extractKm(String line) throws NumberFormatException { + + if (line.length() <= deleteChars) { + throw new NumberFormatException("line too short"); + } + + String kmS = + line.substring(deleteChars, deleteChars+integerPlaces); + + double km = Double.parseDouble(kmS.trim()); + + return fractionPlaces > 0 + ? ((int)((scale*km)*shift))/shift + : km; } } // class KMFormat @@ -116,7 +136,8 @@ DataFormat dataFormat = new DataFormat(m); - if ((line = in.readLine()) == null) { + if ((line = in.readLine()) == null + || (line = line.trim()).length() == 0) { log.warn("premature EOF. Expected integer in line 2"); return false; } @@ -147,11 +168,61 @@ return false; } + KMFormat kmFormat = new KMFormat(m); + + if ((line = in.readLine()) == null + || (line = line.trim()).length() == 0) { + log.warn("premature EOF. Expected skip row count."); + return false; + } + + int lineSkipCount; + try { + if ((lineSkipCount = Integer.parseInt(line)) < 0) { + throw new IllegalArgumentException(lineSkipCount + " < 0"); + } + } + catch (NumberFormatException nfe) { + log.warn( + "line 5 is not an positive integer."); + return false; + } + + int skip = lineSkipCount; + while ((line = in.readLine()) != null) { - if (line.length() == 0) { + if (skip > 0) { + --skip; continue; } - // TODO: Do data and km extraction + double km; + try { + km = kmFormat.extractKm(line); + } + catch (NumberFormatException iae) { + log.warn("cannot extract km in line + " + in.getLineNumber()); + return false; + } + + Double station = Double.valueOf(km); + + Map kmData = data.get(station); + + if (kmData == null) { + log.debug("found new km: " + station); + kmData = new TreeMap(); + data.put(station, kmData); + } + + try { + if (!dataFormat.extractData(line, kmData)) { + skip = lineSkipCount; + } + } + catch (NumberFormatException nfe) { + log.warn("cannot extract data from line " + in.getLineNumber()); + return false; + } } } catch (IOException ioe) {