Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/MeasurementStationsParser.java @ 4768:99f9e371371b
Move distance calculation to Coordinate class. Use inheritance instead of composition in Anchor class. Made Anchor class static. Use epsilon equal comparision when checking for same station: Boy, do you ever learn that sharp equal comparison of doubles is not a clever idea!?
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 04 Jan 2013 15:35:51 +0100 |
parents | f63b39799d2d |
children | 278d8759c92b |
line wrap: on
line source
package de.intevation.flys.importer.parsers; import java.math.BigDecimal; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; import de.intevation.flys.importer.ImportMeasurementStation; import de.intevation.flys.importer.ImportRange; import de.intevation.flys.importer.ImportTimeInterval; public class MeasurementStationsParser extends LineParser { public static class MeasurementStationParserException extends Exception { private static final long serialVersionUID = 1L; public MeasurementStationParserException(String msg) { super(msg); } } public static final int MIN_COLUMNS = 10; private static final Logger log = Logger .getLogger(MeasurementStationsParser.class); private List<ImportMeasurementStation> measurementStations; private ImportMeasurementStation current; @Override protected void reset() { this.measurementStations = new ArrayList<ImportMeasurementStation>(); } @Override protected void finish() { } @Override protected void handleLine(int lineNum, String line) { if (line == null || line.startsWith(START_META_CHAR)) { log.info("skip meta information at line " + lineNum); return; } try { current = new ImportMeasurementStation(); handleDataLine(line); measurementStations.add(current); } catch (MeasurementStationParserException e) { log.warn("Problem in line " + lineNum + ": " + e.getMessage()); } } public List<ImportMeasurementStation> getMeasurementStations() { return measurementStations; } protected void handleDataLine(String line) throws MeasurementStationParserException { String[] cols = line.split(SEPERATOR_CHAR); if (cols == null || cols.length < MIN_COLUMNS) { int num = cols != null ? cols.length : 0; throw new MeasurementStationParserException("Not enough columns: " + num); } current.name = getName(cols); current.station = getStation(cols); current.range = getRange(cols); current.measurementType = getMeasurementType(cols); current.riverside = getRiverside(cols); current.gauge = getGauge(cols); current.observationTimerange = getObservationTimerange(cols); current.operator = getOperator(cols); current.comment = getComment(cols); log.debug("Found new measurement station '" + current.name + "' at km " + current.station); } protected String getName(String[] cols) throws MeasurementStationParserException { if (cols[0] == null || cols[0].length() == 0) { throw new MeasurementStationParserException("invalid name '" + cols[0] + "'"); } return cols[0]; } protected double getStation(String[] cols) throws MeasurementStationParserException { if (cols[1] == null || cols[1].length() == 0) { throw new MeasurementStationParserException("invalid station '" + cols[1] + "'"); } try { return getDouble(cols[1]); } catch (ParseException e) { throw new MeasurementStationParserException( "unable to parse station: " + e.getMessage()); } } protected ImportRange getRange(String[] cols) throws MeasurementStationParserException { if (cols[4] == null || cols[4].length() == 0) { throw new MeasurementStationParserException("invalid lower range '" + cols[4] + "'"); } if (cols[5] == null || cols[5].length() == 0) { throw new MeasurementStationParserException("invalid lower range '" + cols[5] + "'"); } try { double lower = getDouble(cols[4]); double upper = getDouble(cols[5]); return new ImportRange(new BigDecimal(lower), new BigDecimal(upper)); } catch (ParseException e) { throw new MeasurementStationParserException( "unable to parse range: " + e.getMessage()); } } protected String getMeasurementType(String[] cols) throws MeasurementStationParserException { if (cols[2] == null || cols[2].length() == 0) { throw new MeasurementStationParserException( "invalid measurement type '" + cols[2] + "'"); } return cols[2]; } protected String getRiverside(String[] cols) { return cols[3]; } protected String getGauge(String[] cols) throws MeasurementStationParserException { if (cols[6] == null || cols[6].length() == 0) { throw new MeasurementStationParserException("invalid gauge '" + cols[6] + "'"); } return cols[6]; } protected ImportTimeInterval getObservationTimerange(String[] cols) throws MeasurementStationParserException { if (cols[8] == null || cols[8].length() == 0) { throw new MeasurementStationParserException( "invalid observation time '" + cols[8] + "'"); } try { Date date = getDate(cols[8]); if (date != null) { return new ImportTimeInterval(date); } throw new MeasurementStationParserException( "invalid observation time '" + cols[8] + "'"); } catch (ParseException pe) { throw new MeasurementStationParserException(pe.getMessage()); } } protected String getOperator(String[] cols) { return cols[9]; } protected String getComment(String[] cols) { return cols.length > 10 ? cols[10] : null; } }