Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java @ 1197:ce3dacc6ea92
PRFParser: extract km from lines. TODO: extract data.
flys-backend/trunk@2297 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 07 Jul 2011 09:29:31 +0000 |
parents | 46127af605ba |
children | 661a9304f2f5 |
comparison
equal
deleted
inserted
replaced
1196:46127af605ba | 1197:ce3dacc6ea92 |
---|---|
49 firstFractionPlaces = Integer.parseInt(m.group(4)); | 49 firstFractionPlaces = Integer.parseInt(m.group(4)); |
50 secondIntegerPlaces = Integer.parseInt(m.group(5)); | 50 secondIntegerPlaces = Integer.parseInt(m.group(5)); |
51 secondFractionPlaces = Integer.parseInt(m.group(6)); | 51 secondFractionPlaces = Integer.parseInt(m.group(6)); |
52 } | 52 } |
53 | 53 |
54 public void extractData(String line, Map<Double, Double> dest) { | 54 public boolean extractData(String line, Map<Double, Double> dest) |
55 throws NumberFormatException | |
56 { | |
55 //TODO: Implement me! | 57 //TODO: Implement me! |
58 return true; | |
56 } | 59 } |
57 } // class DataFormat | 60 } // class DataFormat |
58 | 61 |
59 public static class KMFormat { | 62 public static class KMFormat { |
60 protected int deleteChars; | 63 protected int deleteChars; |
61 protected int integerPlaces; | 64 protected int integerPlaces; |
62 protected int fractionPlaces; | 65 protected int fractionPlaces; |
63 | 66 |
67 protected double scale; | |
68 protected double shift; | |
69 | |
64 public KMFormat() { | 70 public KMFormat() { |
65 } | 71 } |
66 | 72 |
67 public KMFormat(Matcher m) { | 73 public KMFormat(Matcher m) { |
68 deleteChars = Integer.parseInt(m.group(1)); | 74 deleteChars = Integer.parseInt(m.group(1)); |
69 integerPlaces = Integer.parseInt(m.group(2)); | 75 integerPlaces = Integer.parseInt(m.group(2)); |
70 fractionPlaces = Integer.parseInt(m.group(3)); | 76 fractionPlaces = Integer.parseInt(m.group(3)); |
71 } | 77 |
72 | 78 shift = Math.pow(10, fractionPlaces); |
73 public double extractKm(String line) { | 79 scale = 1d/shift; |
74 // TODO: Implement me! | 80 } |
75 return 0d; | 81 |
82 public double extractKm(String line) throws NumberFormatException { | |
83 | |
84 if (line.length() <= deleteChars) { | |
85 throw new NumberFormatException("line too short"); | |
86 } | |
87 | |
88 String kmS = | |
89 line.substring(deleteChars, deleteChars+integerPlaces); | |
90 | |
91 double km = Double.parseDouble(kmS.trim()); | |
92 | |
93 return fractionPlaces > 0 | |
94 ? ((int)((scale*km)*shift))/shift | |
95 : km; | |
76 } | 96 } |
77 } // class KMFormat | 97 } // class KMFormat |
78 | 98 |
79 protected Map<Double, Map<Double, Double>> data; | 99 protected Map<Double, Map<Double, Double>> data; |
80 | 100 |
114 return false; | 134 return false; |
115 } | 135 } |
116 | 136 |
117 DataFormat dataFormat = new DataFormat(m); | 137 DataFormat dataFormat = new DataFormat(m); |
118 | 138 |
119 if ((line = in.readLine()) == null) { | 139 if ((line = in.readLine()) == null |
140 || (line = line.trim()).length() == 0) { | |
120 log.warn("premature EOF. Expected integer in line 2"); | 141 log.warn("premature EOF. Expected integer in line 2"); |
121 return false; | 142 return false; |
122 } | 143 } |
123 | 144 |
124 try { | 145 try { |
145 log.warn( | 166 log.warn( |
146 "line 4 does not look like a PRF km extraction pattern."); | 167 "line 4 does not look like a PRF km extraction pattern."); |
147 return false; | 168 return false; |
148 } | 169 } |
149 | 170 |
171 KMFormat kmFormat = new KMFormat(m); | |
172 | |
173 if ((line = in.readLine()) == null | |
174 || (line = line.trim()).length() == 0) { | |
175 log.warn("premature EOF. Expected skip row count."); | |
176 return false; | |
177 } | |
178 | |
179 int lineSkipCount; | |
180 try { | |
181 if ((lineSkipCount = Integer.parseInt(line)) < 0) { | |
182 throw new IllegalArgumentException(lineSkipCount + " < 0"); | |
183 } | |
184 } | |
185 catch (NumberFormatException nfe) { | |
186 log.warn( | |
187 "line 5 is not an positive integer."); | |
188 return false; | |
189 } | |
190 | |
191 int skip = lineSkipCount; | |
192 | |
150 while ((line = in.readLine()) != null) { | 193 while ((line = in.readLine()) != null) { |
151 if (line.length() == 0) { | 194 if (skip > 0) { |
195 --skip; | |
152 continue; | 196 continue; |
153 } | 197 } |
154 // TODO: Do data and km extraction | 198 double km; |
199 try { | |
200 km = kmFormat.extractKm(line); | |
201 } | |
202 catch (NumberFormatException iae) { | |
203 log.warn("cannot extract km in line + " + in.getLineNumber()); | |
204 return false; | |
205 } | |
206 | |
207 Double station = Double.valueOf(km); | |
208 | |
209 Map<Double, Double> kmData = data.get(station); | |
210 | |
211 if (kmData == null) { | |
212 log.debug("found new km: " + station); | |
213 kmData = new TreeMap<Double, Double>(); | |
214 data.put(station, kmData); | |
215 } | |
216 | |
217 try { | |
218 if (!dataFormat.extractData(line, kmData)) { | |
219 skip = lineSkipCount; | |
220 } | |
221 } | |
222 catch (NumberFormatException nfe) { | |
223 log.warn("cannot extract data from line " + in.getLineNumber()); | |
224 return false; | |
225 } | |
155 } | 226 } |
156 } | 227 } |
157 catch (IOException ioe) { | 228 catch (IOException ioe) { |
158 log.error(ioe); | 229 log.error(ioe); |
159 return false; | 230 return false; |