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@4193: ingo@4193: import java.text.ParseException; ingo@4193: import java.util.ArrayList; ingo@4193: import java.util.Date; ingo@4193: import java.util.List; ingo@4193: ingo@4193: import org.apache.log4j.Logger; teichmann@5829: import org.dive4elements.river.importer.ImportMeasurementStation; teichmann@5829: import org.dive4elements.river.importer.ImportRange; teichmann@5829: import org.dive4elements.river.importer.ImportTimeInterval; mschaefer@8989: import org.dive4elements.river.importer.common.AbstractParser; mschaefer@8989: import org.dive4elements.river.model.MeasurementStation; ingo@4193: ingo@4193: ingo@4193: public class MeasurementStationsParser extends LineParser { ingo@4193: ingo@4193: public static class MeasurementStationParserException extends Exception { ingo@4193: ingo@4193: private static final long serialVersionUID = 1L; ingo@4193: mschaefer@8989: public MeasurementStationParserException(final String msg) { ingo@4193: super(msg); ingo@4193: } ingo@4193: } ingo@4193: tom@8412: public static final int MIN_COLUMNS = 9; tom@8412: tom@8412: public static final int MAX_COMMENT_LENGTH = 512; ingo@4193: ingo@4193: private static final Logger log = Logger mschaefer@8989: .getLogger(MeasurementStationsParser.class); ingo@4193: ingo@4193: private List measurementStations; ingo@4193: private ImportMeasurementStation current; ingo@4193: ingo@4193: @Override ingo@4193: protected void reset() { mschaefer@8989: this.measurementStations = new ArrayList<>(); ingo@4193: } ingo@4193: ingo@4193: @Override ingo@4193: protected void finish() { ingo@4193: } ingo@4193: ingo@4193: @Override mschaefer@8989: protected void handleLine(final int lineNum, final String line) { ingo@4193: if (line == null || line.startsWith(START_META_CHAR)) { ingo@4193: log.info("skip meta information at line " + lineNum); ingo@4193: return; ingo@4193: } ingo@4193: ingo@4193: try { mschaefer@8989: this.current = new ImportMeasurementStation(); tom@8412: handleDataLine(lineNum, line); mschaefer@8989: this.measurementStations.add(this.current); ingo@4193: } mschaefer@8989: catch (final MeasurementStationParserException e) { ingo@4193: log.warn("Problem in line " + lineNum + ": " + e.getMessage()); ingo@4193: } ingo@4193: } ingo@4193: ingo@4193: public List getMeasurementStations() { mschaefer@8989: return this.measurementStations; ingo@4193: } ingo@4193: mschaefer@8989: protected void handleDataLine(final int lineNum, final String line) mschaefer@8989: throws MeasurementStationParserException { mschaefer@8989: final String[] cols = line.split(SEPERATOR_CHAR); ingo@4193: ingo@4193: if (cols == null || cols.length < MIN_COLUMNS) { mschaefer@8989: final int num = cols != null ? cols.length : 0; ingo@4193: throw new MeasurementStationParserException("Not enough columns: " mschaefer@8989: + num); ingo@4193: } ingo@4193: mschaefer@8989: this.current.name = getName(cols, lineNum); mschaefer@8989: this.current.range = getRange(cols, lineNum); mschaefer@8989: this.current.measurementType = getMeasurementType(cols, lineNum); mschaefer@8989: this.current.riverside = getRiverside(cols, lineNum); mschaefer@8989: this.current.gauge = getGauge(cols, lineNum); mschaefer@8989: this.current.observationTimerange = getObservationTimerange(cols, lineNum); mschaefer@8989: this.current.operator = getOperator(cols, lineNum); mschaefer@8989: this.current.comment = getComment(cols, lineNum); ingo@4193: } ingo@4193: mschaefer@8989: protected String getName(final String[] cols, final int lineNum) mschaefer@8989: throws MeasurementStationParserException { ingo@4193: if (cols[0] == null || cols[0].length() == 0) { tom@8412: throw new MeasurementStationParserException("invalid name in line " mschaefer@8989: + lineNum); ingo@4193: } ingo@4193: ingo@4193: return cols[0]; ingo@4193: } ingo@4193: mschaefer@8989: protected ImportRange getRange(final String[] cols, final int lineNum) { mschaefer@8989: final String from = cols[1]; mschaefer@8989: final String to = cols[4]; tom@8412: if (from == null || from.length() == 0) { tom@8412: log.error("No station found in line" + lineNum); rrenkert@5451: return null; ingo@4193: } ingo@4193: ingo@4193: try { tom@8412: if (to == null || to.length() == 0) { tom@8412: log.warn("No end km found in line " + lineNum); mschaefer@8989: return new ImportRange(AbstractParser.parseDecimal(from)); tom@8412: } tom@8412: tom@8412: try { mschaefer@8989: return new ImportRange(AbstractParser.parseDecimal(from), AbstractParser.parseDecimal(to)); tom@8412: } mschaefer@8989: catch (final NumberFormatException e) { tom@8412: log.warn("Unparseable end km in line " + lineNum + mschaefer@8989: ". Error: " + e.getMessage()); mschaefer@8989: return new ImportRange(AbstractParser.parseDecimal(from)); tom@8412: } tom@8412: ingo@4193: } mschaefer@8989: catch (final NumberFormatException e) { tom@8412: log.error("Unparseable station in line " + lineNum + tom@8412: ". Error: " + e.getMessage()); rrenkert@5451: return null; ingo@4193: } ingo@4193: } ingo@4193: mschaefer@8989: protected String getMeasurementType(final String[] cols, final int lineNum) mschaefer@8989: throws MeasurementStationParserException { mschaefer@8989: final String mtype = cols[2].trim(); tom@8665: if (!(MeasurementStation.MEASUREMENT_TYPE_BEDLOAD.equals(mtype) || tom@8665: MeasurementStation.MEASUREMENT_TYPE_SUSP.equals(mtype))) { ingo@4193: throw new MeasurementStationParserException( mschaefer@8989: "invalid measurement type in line " + lineNum); ingo@4193: } ingo@4193: tom@8642: return mtype; ingo@4193: } ingo@4193: mschaefer@8989: protected String getRiverside(final String[] cols, final int lineNum) { mschaefer@8989: final String col = cols[3]; tom@8412: if (col == null || col.length() == 0) { tom@8412: log.warn("No river side given in line " + lineNum); tom@8412: } tom@8412: return col; ingo@4193: } ingo@4193: mschaefer@8989: protected String getGauge(final String[] cols, final int lineNum) { mschaefer@8989: final String col = cols[5]; tom@8412: if (col == null || col.length() == 0) { tom@8412: log.warn("Invalid gauge found in line " + lineNum); ingo@4193: } tom@8412: return col; ingo@4193: } ingo@4193: tom@8412: protected ImportTimeInterval getObservationTimerange( mschaefer@8989: final String[] cols, mschaefer@8989: final int lineNum mschaefer@8989: ) { mschaefer@8989: final String col = cols[7]; tom@8412: if (col == null || col.length() == 0) { tom@8412: log.warn("Observation time invalid in line " + lineNum); tom@8412: return null; ingo@4193: } ingo@4193: ingo@4193: try { mschaefer@8989: final Date date = getDate(col); ingo@4193: ingo@4193: if (date != null) { ingo@4193: return new ImportTimeInterval(date); ingo@4193: } tom@8412: log.warn("Observation time invalid in line " + lineNum); ingo@4193: } mschaefer@8989: catch (final ParseException pe) { tom@8412: log.warn("Unparseable observation time '" + col + mschaefer@8989: "' in line " + lineNum); ingo@4193: } rrenkert@5451: return null; ingo@4193: } ingo@4193: mschaefer@8989: protected String getOperator(final String[] cols, final int lineNum) { mschaefer@8989: final String col = cols[8]; tom@8412: if (col == null || col.length() == 0) { tom@8412: log.warn("No operator given in line " + lineNum); tom@8412: } tom@8412: return col; ingo@4193: } ingo@4193: mschaefer@8989: protected String getComment(final String[] cols, final int lineNum) { tom@8412: if (cols.length > MIN_COLUMNS) { mschaefer@8989: final String col = cols[9]; tom@8412: if (col.length() > MAX_COMMENT_LENGTH) { tom@8412: log.warn("Comment in line " + lineNum + mschaefer@8989: " longer than allowed " + MAX_COMMENT_LENGTH + mschaefer@8989: " characters. Truncated."); tom@8412: return col.substring(0, MAX_COMMENT_LENGTH); tom@8412: } tom@8412: return col; tom@8412: } tom@8412: return null; ingo@4193: } ingo@4193: }