comparison backend/src/main/java/org/dive4elements/river/importer/parsers/WstParser.java @ 7481:bdb12632c5f5

WST-Parser and doc: reject files with wrong number of columns.
author Tom Gottfried <tom@intevation.de>
date Thu, 31 Oct 2013 15:25:31 +0100
parents 2a2e89c01588
children e327492ba9d0
comparison
equal deleted inserted replaced
7480:2a2e89c01588 7481:bdb12632c5f5
186 186
187 line = line.replace(',', '.'); 187 line = line.replace(',', '.');
188 188
189 // handle Q-lines 189 // handle Q-lines
190 if (line.startsWith("*\u001f")) { 190 if (line.startsWith("*\u001f")) {
191 BigDecimal [] data = 191 BigDecimal [] data = null;
192 parseLineAsDouble(line, columnCount, false, true); 192 try {
193 data = parseLineAsDouble(line, columnCount, false, true);
194 }
195 catch(IllegalArgumentException iae) {
196 log.error(iae.getMessage());
197 return;
198 }
193 199
194 if (aktAbfluesse != null) { // add Q-ranges obtained from previous lines 200 if (aktAbfluesse != null) { // add Q-ranges obtained from previous lines
195 if (kmHist1 != null && kmHist2 != null 201 if (kmHist1 != null && kmHist2 != null
196 && kmHist1.compareTo(kmHist2) < 0) { // stations descending in file 202 && kmHist1.compareTo(kmHist2) < 0) { // stations descending in file
197 BigDecimal t = minKm; minKm = maxKm; maxKm = t; 203 BigDecimal t = minKm; minKm = maxKm; maxKm = t;
327 iwc.setTimeInterval(guessDate(potentialDate)); 333 iwc.setTimeInterval(guessDate(potentialDate));
328 } 334 }
329 columnHeaderChecked = true; 335 columnHeaderChecked = true;
330 } 336 }
331 337
332 BigDecimal [] data = 338 BigDecimal [] data = null;
333 parseLineAsDouble(line, columnCount, true, false); 339 try {
340 data = parseLineAsDouble(line, columnCount, true, false);
341 }
342 catch(IllegalArgumentException iae) {
343 log.error(iae.getMessage());
344 return;
345 }
334 346
335 BigDecimal kaem = data[0]; 347 BigDecimal kaem = data[0];
336 348
337 if (!kms.add(kaem)) { 349 if (!kms.add(kaem)) {
338 log.warn( 350 log.warn(
343 } 355 }
344 356
345 // check consistence of station ordering in file 357 // check consistence of station ordering in file
346 if (kmHist2 != null && 358 if (kmHist2 != null &&
347 kmHist2.compareTo(kmHist1) != kmHist1.compareTo(kaem)) { 359 kmHist2.compareTo(kmHist1) != kmHist1.compareTo(kaem)) {
348 throw new ParseException("Stations in " + file + 360 throw new ParseException("WST: Stations in " + file +
349 " near line " + in.getLineNumber() + 361 " near line " + in.getLineNumber() +
350 " not ordered. File rejected."); 362 " not ordered. File rejected.");
351 } 363 }
352 364
353 // remember stations in two previous lines 365 // remember stations in two previous lines
527 throw new IllegalArgumentException("station too short"); 539 throw new IllegalArgumentException("station too short");
528 } 540 }
529 strings.add(line.substring(0, 8)); 541 strings.add(line.substring(0, 8));
530 } 542 }
531 543
532 int pos = 9; 544 int pos = 0;
533 for (int i = 0; i < tokenCount; ++i) { 545 for (int i = 0; i < tokenCount; ++i) {
546 pos += 9;
547 if (pos >= line.length()) {
548 throw new IllegalArgumentException(
549 "WST: number of columns is less than expected. File rejected.");
550 }
534 strings.add(line.substring(pos, 551 strings.add(line.substring(pos,
535 Math.min(pos + 8, line.length()))); 552 Math.min(pos + 8, line.length())));
536 pos += 9;
537 } 553 }
538 554
539 return strings.toArray(new String[strings.size()]); 555 return strings.toArray(new String[strings.size()]);
540 } 556 }
541 } 557 }

http://dive4elements.wald.intevation.org