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.math.BigDecimal; 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; ingo@4193: teichmann@5829: import org.dive4elements.river.importer.ImportMeasurementStation; teichmann@5829: import org.dive4elements.river.importer.ImportRange; teichmann@5829: import org.dive4elements.river.importer.ImportTimeInterval; 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: ingo@4193: public MeasurementStationParserException(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 ingo@4193: .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() { ingo@4193: this.measurementStations = new ArrayList(); ingo@4193: } ingo@4193: ingo@4193: @Override ingo@4193: protected void finish() { ingo@4193: } ingo@4193: ingo@4193: @Override ingo@4193: protected void handleLine(int lineNum, 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 { ingo@4193: current = new ImportMeasurementStation(); tom@8412: handleDataLine(lineNum, line); ingo@4193: measurementStations.add(current); ingo@4193: } ingo@4193: catch (MeasurementStationParserException e) { ingo@4193: log.warn("Problem in line " + lineNum + ": " + e.getMessage()); ingo@4193: } ingo@4193: } ingo@4193: ingo@4193: public List getMeasurementStations() { ingo@4193: return measurementStations; ingo@4193: } ingo@4193: tom@8412: protected void handleDataLine(int lineNum, String line) ingo@4193: throws MeasurementStationParserException { ingo@4193: String[] cols = line.split(SEPERATOR_CHAR); ingo@4193: ingo@4193: if (cols == null || cols.length < MIN_COLUMNS) { ingo@4193: int num = cols != null ? cols.length : 0; ingo@4193: throw new MeasurementStationParserException("Not enough columns: " ingo@4193: + num); ingo@4193: } ingo@4193: tom@8412: current.name = getName(cols, lineNum); tom@8412: current.range = getRange(cols, lineNum); tom@8412: current.measurementType = getMeasurementType(cols, lineNum); tom@8412: current.riverside = getRiverside(cols, lineNum); tom@8412: current.gauge = getGauge(cols, lineNum); tom@8412: current.observationTimerange = getObservationTimerange(cols, lineNum); tom@8412: current.operator = getOperator(cols, lineNum); tom@8412: current.comment = getComment(cols, lineNum); ingo@4193: } ingo@4193: tom@8412: protected String getName(String[] cols, int lineNum) ingo@4193: throws MeasurementStationParserException { ingo@4193: if (cols[0] == null || cols[0].length() == 0) { tom@8412: throw new MeasurementStationParserException("invalid name in line " tom@8412: + lineNum); ingo@4193: } ingo@4193: ingo@4193: return cols[0]; ingo@4193: } ingo@4193: tom@8412: protected ImportRange getRange(String[] cols, int lineNum) { tom@8412: String from = cols[1]; tom@8412: 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: double lower = getDouble(from); ingo@4193: tom@8412: if (to == null || to.length() == 0) { tom@8412: log.warn("No end km found in line " + lineNum); tom@8412: return new ImportRange(new BigDecimal(lower)); tom@8412: } tom@8412: tom@8412: try { tom@8412: double upper = getDouble(to); tom@8412: tom@8412: return new ImportRange(new BigDecimal(lower), tom@8412: new BigDecimal(upper)); tom@8412: } tom@8412: catch (ParseException e) { tom@8412: log.warn("Unparseable end km in line " + lineNum + tom@8412: ". Error: " + e.getMessage()); tom@8412: return new ImportRange(new BigDecimal(lower)); tom@8412: } tom@8412: ingo@4193: } ingo@4193: catch (ParseException 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: tom@8412: protected String getMeasurementType(String[] cols, int lineNum) ingo@4193: throws MeasurementStationParserException { ingo@4193: if (cols[2] == null || cols[2].length() == 0) { ingo@4193: throw new MeasurementStationParserException( tom@8412: "invalid measurement type in line " + lineNum); ingo@4193: } ingo@4193: ingo@4193: return cols[2]; ingo@4193: } ingo@4193: tom@8412: protected String getRiverside(String[] cols, int lineNum) { tom@8412: 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: tom@8412: protected String getGauge(String[] cols, int lineNum) { tom@8412: 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( tom@8412: String[] cols, tom@8412: int lineNum tom@8412: ) { tom@8412: 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 { tom@8412: 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: } ingo@4193: catch (ParseException pe) { tom@8412: log.warn("Unparseable observation time '" + col + tom@8412: "' in line " + lineNum); ingo@4193: } rrenkert@5451: return null; ingo@4193: } ingo@4193: tom@8412: protected String getOperator(String[] cols, int lineNum) { tom@8412: 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: tom@8412: protected String getComment(String[] cols, int lineNum) { tom@8412: if (cols.length > MIN_COLUMNS) { tom@8412: String col = cols[9]; tom@8412: if (col.length() > MAX_COMMENT_LENGTH) { tom@8412: log.warn("Comment in line " + lineNum + tom@8412: " longer than allowed " + MAX_COMMENT_LENGTH + tom@8412: " 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: }