Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java @ 4447:a5993b69439b
Add processor to be able to generate curves for WKms values
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Wed, 07 Nov 2012 15:52:25 +0100 |
parents | f63b39799d2d |
children | 13596605e81f |
line wrap: on
line source
package de.intevation.flys.importer.parsers; import java.io.File; import java.io.IOException; 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 de.intevation.flys.importer.ImportSQRelation; import de.intevation.flys.importer.ImportSQRelationValue; import de.intevation.flys.importer.ImportTimeInterval; public class SQRelationParser extends LineParser { private static final Logger log = Logger.getLogger(SQRelationParser.class); private static final Pattern TIMERANGE_REGEX = Pattern.compile(".*Zeitraum.*\\s(\\w*)-(\\w*).*"); private static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); private List<ImportSQRelation> relations; private ImportSQRelation current; private String currentDescription; public SQRelationParser() { relations = new ArrayList<ImportSQRelation>(); } public List<ImportSQRelation> getSQRelations() { return relations; } @Override public void parse(File file) throws IOException { this.currentDescription = file.getName(); super.parse(file); } @Override protected void reset() { current = new ImportSQRelation(); } @Override protected void finish() { if (current != null) { current.setDescription(currentDescription); relations.add(current); } } @Override protected void handleLine(int lineNum, String line) { if (line.startsWith(START_META_CHAR)) { handleMetaLine(stripMetaLine(line)); } else { handleDataLine(line); } } protected void handleMetaLine(String line) { Matcher m = TIMERANGE_REGEX.matcher(line); if (m.matches()) { String lo = m.group(1); String hi = m.group(2); log.debug("Found timerange " + lo + " - " + hi); try { int low = nf.parse(lo).intValue(); int high = nf.parse(hi).intValue(); current.setTimeInterval(new ImportTimeInterval( getDateFromYear(low), getDateFromYear(high) )); } catch (ParseException nfe) { log.warn("Cannot parse time range.", nfe); } } } protected void handleDataLine(String line) { String[] cols = line.split(SEPERATOR_CHAR); if (cols.length < 8) { log.warn("skip invalid data line: '" + line + "'"); return; } try { current.addValue(new ImportSQRelationValue( cols[1], cols[2], cols[4], nf.parse(cols[3]).doubleValue(), nf.parse(cols[6]).doubleValue(), nf.parse(cols[7]).doubleValue() )); } catch (ParseException pe) { log.warn("Error while parsing sq relation row: '" + line + "'", pe); } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :