Mercurial > dive4elements > river
changeset 5019:c872249ec9a9
Added BundesWasserStrassenParser.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Tue, 19 Feb 2013 12:01:49 +0100 |
parents | e939098ee272 |
children | 443c173f043a |
files | flys-backend/src/main/java/de/intevation/flys/importer/parsers/BundesWasserStrassenParser.java |
diffstat | 1 files changed, 85 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BundesWasserStrassenParser.java Tue Feb 19 12:01:49 2013 +0100 @@ -0,0 +1,85 @@ +package de.intevation.flys.importer.parsers; + +import java.math.BigDecimal; + +import java.text.NumberFormat; +import java.text.ParseException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; + +import de.intevation.flys.importer.ImportMorphWidth; +import de.intevation.flys.importer.ImportMorphWidthValue; +import de.intevation.flys.importer.ImportUnit; + +/** Parse CSV file that contains official numbers for rivers. */ +public class BundesWasserStrassenParser extends LineParser { + + /** Private logger. */ + private static final Logger logger = + Logger.getLogger(BundesWasserStrassenParser.class); + + /** Map from rivernames to Official numbers. */ + private HashMap<String,Long> numberMap; + + + public BundesWasserStrassenParser() { + numberMap = new HashMap<String,Long>(); + } + + + /** No need to reset. */ + @Override + protected void reset() { + } + + + /** No action needed on eof. */ + @Override + protected void finish() { + } + + + /** Handle a line of the bwastr-id file. */ + @Override + protected void handleLine(int lineNum, String line) { + String[] vals = line.split(","); + if (vals.length != 2) { + logger.warn("Invalid bwastr-id line:\n" + line); + return; + } + try{ + String name = unwrap(vals[0].toLowerCase()); + String numberStr = unwrap(vals[1]); + Long number = Long.valueOf(numberStr); + numberMap.put(name, number); + } + catch (NumberFormatException e) { + logger.warn("Invalid number in bwastr-id line:\n" + line); + } + } + + + /** Get river->official number mapping. */ + public HashMap<String,Long> getMap() { + return numberMap; + } + + + /** Remove leading and trailing quotes. */ + protected String unwrap(String input) { + if (input.startsWith("\"")) { + input = input.substring(1); + } + if (input.endsWith("\"")) { + input = input.substring(0, input.length() - 1); + } + return input; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :