teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer.parsers; ingo@2806: ingo@2808: import java.math.BigDecimal; ingo@2808: ingo@2808: import java.text.ParseException; ingo@2808: ingo@2806: import org.apache.log4j.Logger; ingo@2806: teichmann@5829: import org.dive4elements.river.importer.ImportBedHeight; teichmann@5829: import org.dive4elements.river.importer.ImportBedHeightSingle; teichmann@5829: import org.dive4elements.river.importer.ImportBedHeightSingleValue; ingo@2806: ingo@2806: ingo@2811: public class BedHeightSingleParser extends BedHeightParser { ingo@2806: ingo@2806: private static final Logger log = ingo@2806: Logger.getLogger(BedHeightSingleParser.class); ingo@2806: ingo@2806: ingo@2808: ingo@2811: @Override ingo@2811: protected ImportBedHeight newImportBedHeight(String description) { ingo@2811: return new ImportBedHeightSingle(description); ingo@2808: } ingo@2808: ingo@2808: ingo@2808: felix@3955: /** felix@3955: * Create ImportBedHeightSingleValue from a line of csv file, add felix@3955: * it to the ImportBedHeight. felix@3955: */ ingo@2811: @Override ingo@2811: protected void handleDataLine(ImportBedHeight obj, String line) { tom@7354: String[] values = line.split(SEPERATOR_CHAR, -1); ingo@2808: tom@7354: if (values == null) { tom@7354: log.warn("BSP: Error while parsing data line: '" + line + "'"); ingo@2808: return; ingo@2808: } ingo@2808: tom@6203: Double km; felix@3955: felix@3955: try { tom@6203: km = new Double(nf.parse(values[0]).doubleValue()); tom@6273: tom@6273: Double key = Double.valueOf(km); tom@6273: tom@6273: if (kmExists.contains(key)) { tom@6273: log.warn("duplicate station '" + km + "': -> ignored"); tom@6273: return; tom@6273: } tom@6273: tom@6273: kmExists.add(key); tom@6273: } tom@6273: felix@3955: catch (ParseException e) { felix@3955: // We expect a lot of ";;;;;;" lines. felix@3955: //log.warn("BSP: Error while parsing km of data row.", e); felix@3955: return; felix@3955: } felix@3955: felix@3955: // Handle gaps like "10,0;;;;;". felix@3955: if (values.length == 1) { tom@6047: // Do not import line without useful data felix@3955: return; felix@3955: } felix@3955: felix@3955: // Because we cannot enforce consistency of values with complete data felix@3955: // via null constraints in the database (as there are "gap" values), felix@3955: // do this checks manually. tom@6298: // if (values[3].length() == 0 || values[4].length() == 0 tom@6298: // || values[5].length() == 0) { felix@3955: //log.warn("BSP: Error while parsing data row (manual null constraint violated)."); tom@6298: // return; tom@6298: //} felix@3955: tom@6298: Double height = null; tom@6298: if (values[1].length() > 0) { tom@6298: try { tom@6298: height = new Double(nf.parse(values[1]).doubleValue()); tom@6298: } tom@6298: catch (ParseException e) { tom@6298: log.warn("BSP: unparseable height " + values[1]); tom@6298: } tom@6298: } ingo@2808: tom@6298: Double uncertainty = null; tom@6298: if (values[2].length() > 0) { tom@6298: try { tom@6298: uncertainty = new Double(nf.parse(values[2]).doubleValue()); tom@6298: } tom@6298: catch (ParseException e) { tom@6298: log.warn("BSP: unparseable uncertainty value " + values[2]); tom@6298: } tom@6298: } tom@6298: tom@6298: Double dataGap = null; tom@6298: if (values[3].length() > 0) { tom@6298: try { tom@6298: dataGap = new Double(nf.parse(values[3]).doubleValue()); tom@6298: } tom@6298: catch (ParseException e) { tom@6298: log.warn("BSP: unparseable data gap " + values[3]); tom@6298: } tom@6298: } tom@6298: tom@6298: Double soundingWidth = null; tom@6298: if (values[4].length() > 0) { tom@6298: try { tom@6298: soundingWidth = new Double(nf.parse(values[4]).doubleValue()); tom@6298: } tom@6298: catch (ParseException e) { tom@6298: log.warn("BSP: unparseable sounding width " + values[4]); tom@6298: } tom@6298: } tom@6298: tom@6298: Double width = null; tom@6298: if (values[5].length() > 0) { tom@6298: try { tom@6298: width = new Double(nf.parse(values[5]).doubleValue()); tom@6298: } tom@6298: catch (ParseException e) { tom@6298: log.warn("BSP: unparseable width " + values[5]); tom@6298: } tom@6298: } tom@6298: tom@6298: // try { tom@6298: ImportBedHeightSingleValue value = new ImportBedHeightSingleValue( tom@6298: (ImportBedHeightSingle) obj, tom@6298: km, tom@6298: height,//new Double(nf.parse(values[1]).doubleValue()), tom@6298: uncertainty, tom@6298: dataGap, tom@6298: soundingWidth, tom@6298: // parseBigDecimal(values[4], line), tom@6298: width tom@6298: ); tom@6298: tom@6298: obj.addValue(value); tom@6298: // } tom@6298: // catch (ParseException e) { tom@6298: // log.warn("BSP: unparseable value in data row.", e); tom@6298: //} ingo@2808: } teichmann@5992: tom@6298: // private BigDecimal parseBigDecimal(String value, String line) { tom@6298: // BigDecimal result = null; tom@6298: // try { tom@6298: // Double dValue = Double.valueOf(value.replace(",", ".")); tom@6298: // result = new BigDecimal(dValue.doubleValue()); tom@6298: // } tom@6298: // catch (NumberFormatException nfe) { tom@6298: // log.warn( tom@6298: // "Could not parse " + tom@6298: // value + tom@6298: // " in bed heigt single row: " tom@6298: // + line); tom@6298: // } tom@6298: // return result; tom@6298: // } ingo@2806: } ingo@2806: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :