Mercurial > dive4elements > river
view backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.java @ 8991:c43d8c1a4455
Parse via decimalformat instead of bigdecimal constructor (to cope with strings with chars after the actual number)
author | mschaefer |
---|---|
date | Tue, 10 Apr 2018 16:43:32 +0200 |
parents | 2693bfaf503d |
children |
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde * Software engineering by Intevation GmbH * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.importer.parsers; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.text.NumberFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.dive4elements.river.importer.ImportDepth; import org.dive4elements.river.importer.ImportSedimentDensity; import org.dive4elements.river.importer.ImportSedimentDensityValue; import org.dive4elements.river.importer.common.AbstractParser; public class SedimentDensityParser extends LineParser { private static final Logger log = Logger.getLogger(SedimentDensityParser.class); public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); public static final Pattern META_DEPTH = Pattern.compile("^Tiefe: (\\d++)-(\\d++).*"); public static final Pattern META_YEAR = Pattern.compile("^Jahr: (\\d{4}).*"); protected List<ImportSedimentDensity> sedimentDensities; protected ImportSedimentDensity current; protected String currentDescription; protected String yearString; public SedimentDensityParser() { this.sedimentDensities = new ArrayList<>(); } @Override public void parse(final File file) throws IOException { this.currentDescription = file.getName(); super.parse(file); } @Override protected void reset() { this.current = new ImportSedimentDensity(this.currentDescription); } @Override protected void finish() { if (this.current != null) { this.sedimentDensities.add(this.current); } } @Override protected void handleLine(final int lineNum, final String line) { if (line.startsWith(START_META_CHAR)) { handleMetaLine(stripMetaLine(line)); } else { handleDataLine(line); } } protected void handleMetaLine(final String line) { if (handleMetaDepth(line)) { return; } if (handleMetaYear(line)) { return; } log.warn("Unknown meta line: '" + line + "'"); } protected boolean handleMetaDepth(final String line) { final Matcher m = META_DEPTH.matcher(line); if (m.matches()) { final String lo = m.group(1); final String up = m.group(2); log.info("Found sediment density depth: " + lo + " - " + up + " cm"); try { final ImportDepth depth = new ImportDepth( AbstractParser.parseDecimal(lo), AbstractParser.parseDecimal(up)); this.current.setDepth(depth); return true; } catch (final ParseException pe) { log.warn("Unparseable numbers in: '" + line + "'"); } } else { log.debug("Meta line doesn't contain depth information: " + line); } return false; } protected boolean handleMetaYear(final String line) { final Matcher m = META_YEAR.matcher(line); if (m.matches()) { this.yearString = m.group(1); log.info("Found sediment density year: " + this.yearString); return true; } log.debug("Meta line doesn't contain year: " + line); return false; } protected void handleDataLine(final String line) { final String[] vals = line.split(SEPERATOR_CHAR); if (vals == null || vals.length < 3) { log.warn("skip invalid data line: '" + line + "'"); return; } BigDecimal km = null; BigDecimal shoreOffset = null; BigDecimal density = null; try { km = AbstractParser.parseDecimal(vals[0]); density = AbstractParser.parseDecimal(vals[2]); if (!vals[1].isEmpty()) { shoreOffset = AbstractParser.parseDecimal(vals[1]); } } catch (final ParseException pe) { log.warn("Unparseable numbers in '" + line + "'"); } if (km == null || density == null) { log.warn("SDP: No km nor density given. Skip line"); return; } BigDecimal year = null; if (this.yearString != null) { try { year = AbstractParser.parseDecimal(this.yearString); } catch (final ParseException pe) { log.warn("Unparseable year string"); } } this.current.addValue(new ImportSedimentDensityValue( km, shoreOffset, density, year, this.currentDescription)); } public List<ImportSedimentDensity> getSedimentDensities() { return this.sedimentDensities; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :