Mercurial > dive4elements > river
view backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.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.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.ImportMorphWidth; import org.dive4elements.river.importer.ImportMorphWidthValue; import org.dive4elements.river.importer.ImportUnit; import org.dive4elements.river.importer.common.AbstractParser; public class MorphologicalWidthParser extends LineParser { private static final Logger log = Logger.getLogger(MorphologicalWidthParser.class); public static final NumberFormat nf = NumberFormat.getInstance( DEFAULT_LOCALE); public static final Pattern META_UNIT = Pattern.compile("^Einheit: \\[(.*)\\].*"); protected List<ImportMorphWidth> morphWidths; protected ImportMorphWidth current; public MorphologicalWidthParser() { this.morphWidths = new ArrayList<>(); } @Override protected void reset() { this.current = new ImportMorphWidth(); } @Override protected void finish() { if (this.current != null) { this.morphWidths.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 (handleMetaUnit(line)) { return; } else { log.warn("MWP: Unknown meta line: '" + line + "'"); } } protected boolean handleMetaUnit(final String line) { final Matcher m = META_UNIT.matcher(line); if (m.matches()) { final String unit = m.group(1); this.current.setUnit(new ImportUnit(unit)); return true; } return false; } protected void handleDataLine(final String line) { final String[] vals = line.split(SEPERATOR_CHAR); if (vals == null || vals.length < 2) { log.warn("MWP: skip invalid data line: '" + line + "'"); return; } try { final BigDecimal km = AbstractParser.parseDecimal(vals[0]); final BigDecimal width = AbstractParser.parseDecimal(vals[1]); final String desc = vals.length > 2 ? vals[2] : null; this.current.addValue(new ImportMorphWidthValue( km, width, desc )); } catch (final ParseException pe) { log.warn("MWP: unparseable number in data row: " + line); } } public List<ImportMorphWidth> getMorphologicalWidths() { return this.morphWidths; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :