sascha@189: package de.intevation.flys.importer; sascha@189: sascha@189: import java.io.File; sascha@189: import java.io.IOException; sascha@189: import java.io.LineNumberReader; sascha@189: import java.io.FileInputStream; sascha@189: import java.io.InputStreamReader; sascha@189: sascha@189: import java.math.BigDecimal; sascha@189: sascha@189: import java.util.regex.Pattern; sascha@189: import java.util.regex.Matcher; sascha@189: sascha@189: import java.util.HashMap; sascha@195: import java.util.ArrayList; sascha@189: sascha@189: import org.apache.log4j.Logger; sascha@189: sascha@189: public class StaFileParser sascha@189: { sascha@189: private static Logger log = Logger.getLogger(StaFileParser.class); sascha@189: sascha@189: public static final String ENCODING = "ISO-8859-1"; sascha@189: sascha@189: public static final Pattern QWTD_ = sascha@189: Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([QWTD-]).*"); sascha@189: sascha@189: public StaFileParser() { sascha@189: } sascha@189: sascha@189: public boolean parse(ImportGauge gauge) throws IOException { sascha@189: sascha@189: File file = gauge.getStaFile(); sascha@189: sascha@189: log.info("parsing STA file: " + file); sascha@189: LineNumberReader in = null; sascha@189: try { sascha@189: in = sascha@189: new LineNumberReader( sascha@189: new InputStreamReader( sascha@189: new FileInputStream(file), ENCODING)); sascha@189: sascha@189: String line = in.readLine(); sascha@189: sascha@189: if (line == null) { sascha@189: log.warn("STA file is empty."); sascha@189: return false; sascha@189: } sascha@189: sascha@189: if (line.length() < 37) { sascha@189: log.warn("first line in STA file is too short."); sascha@189: return false; sascha@189: } sascha@189: sascha@189: gauge.setName(line.substring(16, 37).trim()); sascha@189: sascha@189: String [] values = line.substring(38).trim().split("\\s+", 2); sascha@189: sascha@189: if (values.length < 2) { sascha@189: log.warn("Not enough columns for aeo and datum"); sascha@189: } sascha@189: try { sascha@189: gauge.setAeo(new BigDecimal(values[0].replace(",", "."))); sascha@189: gauge.setDatum(new BigDecimal(values[1].replace(",", "."))); sascha@189: } sascha@189: catch (NumberFormatException nfe) { sascha@189: log.warn("cannot parse aeo or datum"); sascha@189: return false; sascha@189: } sascha@189: sascha@189: line = in.readLine(); sascha@189: sascha@189: if (line == null) { sascha@189: log.warn("STA file has not enough lines"); sascha@189: return false; sascha@189: } sascha@189: sascha@189: if (line.length() < 36) { sascha@189: log.warn("second line is too short"); sascha@189: return false; sascha@189: } sascha@189: sascha@189: try { sascha@192: gauge.setStation( sascha@189: new BigDecimal(line.substring(29, 36).trim())); sascha@189: } sascha@189: catch (NumberFormatException nfe) { sascha@189: log.warn("parsing of the datum of the gauge failed"); sascha@189: return false; sascha@189: } sascha@189: sascha@189: // overread the next six lines sascha@189: for (int i = 0; i < 6; ++i) { sascha@189: if ((line = in.readLine()) == null) { sascha@189: log.warn("STA file is too short"); sascha@189: return false; sascha@189: } sascha@189: } sascha@189: sascha@189: HashMap types = sascha@189: new HashMap(); sascha@189: sascha@195: ArrayList namedMainValues = sascha@195: new ArrayList(); sascha@195: sascha@195: ArrayList mainValues = sascha@195: new ArrayList(); sascha@195: sascha@189: while ((line = in.readLine()) != null) { sascha@189: Matcher m = QWTD_.matcher(line); sascha@189: if (m.matches()) { sascha@189: BigDecimal value; sascha@189: try { sascha@189: value = new BigDecimal(m.group(2).replace(",", ".")); sascha@189: } sascha@189: catch (NumberFormatException nfe) { sascha@189: log.warn("value not parseable in line " sascha@189: + in.getLineNumber()); sascha@189: continue; sascha@189: } sascha@189: String typeString = m.group(3); sascha@189: log.debug("\t type: " + typeString); sascha@189: ImportMainValueType type = types.get(typeString); sascha@189: if (type == null) { sascha@189: type = new ImportMainValueType(typeString); sascha@189: types.put(typeString, type); sascha@189: } sascha@195: String name = m.group(1); sascha@195: ImportNamedMainValue namedMainValue = sascha@195: new ImportNamedMainValue(type, name); sascha@195: namedMainValues.add(namedMainValue); sascha@195: sascha@195: ImportMainValue mainValue = sascha@195: new ImportMainValue(gauge, namedMainValue, value); sascha@195: sascha@195: mainValues.add(mainValue); sascha@189: } sascha@189: else { sascha@189: // TODO: treat as a comment sascha@189: } sascha@189: } sascha@195: gauge.setMainValueTypes( sascha@195: new ArrayList(types.values())); sascha@195: gauge.setNamedMainValues(namedMainValues); sascha@195: gauge.setMainValues(mainValues); sascha@189: } sascha@189: finally { sascha@189: if (in != null) { sascha@189: in.close(); sascha@189: } sascha@189: } sascha@189: log.info("finished parsing STA file: " + file); sascha@189: return true; sascha@189: } sascha@189: } sascha@189: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :