sascha@184: package de.intevation.flys.importer; sascha@184: sascha@184: import java.io.File; sascha@184: sascha@184: import java.util.List; sascha@184: import java.util.ArrayList; sascha@184: sascha@184: import java.io.IOException; sascha@184: import java.io.LineNumberReader; sascha@184: import java.io.FileInputStream; sascha@184: import java.io.InputStreamReader; sascha@184: sascha@189: import java.math.BigDecimal; sascha@189: sascha@184: import org.apache.log4j.Logger; sascha@184: sascha@184: import de.intevation.flys.utils.FileTools; sascha@184: sascha@184: public class PegelGltParser sascha@184: { sascha@186: private static Logger log = Logger.getLogger(PegelGltParser.class); sascha@184: sascha@184: public static final String ENCODING = "ISO-8859-1"; sascha@184: sascha@184: public static final String KM = "km:"; sascha@184: sascha@184: protected List gauges; sascha@184: sascha@184: public PegelGltParser() { sascha@184: gauges = new ArrayList(); sascha@184: } sascha@184: sascha@184: public List getGauges() { sascha@184: return gauges; sascha@184: } sascha@184: sascha@184: public void parse(File file) throws IOException { sascha@184: sascha@184: File parent = file.getParentFile(); sascha@184: sascha@184: log.info("parsing GLT file '" + file + "'"); sascha@184: LineNumberReader in = null; sascha@184: try { sascha@184: in = sascha@184: new LineNumberReader( sascha@184: new InputStreamReader( sascha@184: new FileInputStream(file), ENCODING)); sascha@184: sascha@184: String line = null; sascha@184: while ((line = in.readLine()) != null) { sascha@184: if ((line = line.trim()).length() == 0) { sascha@184: continue; sascha@184: } sascha@184: sascha@184: int kmPos = line.indexOf(KM); sascha@184: if (kmPos < 0) { sascha@184: log.warn("no gauge found in line " + in.getLineNumber()); sascha@184: continue; sascha@184: } sascha@184: sascha@184: String gaugeName = line.substring(0, kmPos).trim(); sascha@184: log.info("Found gauge '" + gaugeName + "'"); sascha@184: sascha@184: line = line.substring(kmPos + KM.length()).trim(); sascha@184: sascha@184: String [] parts = line.split("\\s+"); sascha@184: if (parts.length < 4) { sascha@184: log.warn("line " + in.getLineNumber() sascha@184: + " has not enough columns"); sascha@184: continue; sascha@184: } sascha@184: sascha@189: BigDecimal from = new BigDecimal(parts[0].replace(",", ".")); sascha@189: BigDecimal to = new BigDecimal(parts[1].replace(",", ".")); sascha@189: if (to.compareTo(from) > 0) { sascha@189: BigDecimal t = from; from = to; to = t; sascha@189: } sascha@185: ImportRange range = new ImportRange(from, to); sascha@184: File staFile = FileTools.repair(new File(parent, parts[2])); sascha@184: File atFile = FileTools.repair(new File(parent, parts[3])); sascha@184: sascha@184: if (log.isDebugEnabled()) { sascha@184: log.debug("\tfrom: " + from); sascha@184: log.debug("\tto: " + to); sascha@184: log.debug("\tsta: " + staFile); sascha@184: log.debug("\tat: " + atFile); sascha@184: } sascha@184: sascha@185: gauges.add(new ImportGauge(range, staFile, atFile)); sascha@184: } sascha@184: } sascha@184: finally { sascha@184: if (in != null) { sascha@184: in.close(); sascha@184: } sascha@184: } sascha@184: } sascha@184: } sascha@184: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :