/* ================================================================ * Delft FEWS * ================================================================ * * Project Info: http://www.wldelft.nl/soft/fews/index.html * Project Lead: Karel Heynert (karel.heynert@wldelft.nl) * * (C) Copyright 2003, by WL | Delft Hydraulics * P.O. Box 177 * 2600 MH Delft * The Netherlands * http://www.wldelft.nl * * DELFT-FEWS is a sophisticated collection of modules designed * for building a FEWS customised to the specific requirements * of individual agencies. An open modelling approach allows users * to add their own modules in an efficient way. * * ---------------------------------------------------------------- * ShdTimeSeriesParser.java * ---------------------------------------------------------------- * (C) Copyright 2003, by WL | Delft Hydraulics * * Original Author: Jitka Tacoma */ package nl.wldelft.timeseriesparsers; import nl.wldelft.util.TextUtils; import nl.wldelft.util.io.LineReader; import nl.wldelft.util.io.TextParser; import nl.wldelft.util.timeseries.DefaultTimeSeriesHeader; import nl.wldelft.util.timeseries.TimeSeriesContentHandler; import java.io.IOException; public class ShdTimeSeriesParser implements TextParser { @Override public void parse(LineReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws IOException { reader.setCommentLinePrefix('#'); contentHandler.addMissingValue(""); DefaultTimeSeriesHeader header = new DefaultTimeSeriesHeader(); String[] buffer = new String[3]; int i = 1; for (String line; (line = reader.readLine()) != null; i++) { line = line.trim(); String key = line.substring(0, 5); if (key.equalsIgnoreCase("%ASCI") || key.equalsIgnoreCase("ASCII")) { //New data block found, so initialize reading of the new block i = 1; header.clear(); } else if (key.substring(0, 3).equalsIgnoreCase("%HB") || key.substring(0, 3).equalsIgnoreCase(";HB")) { //read location and parameter ID String[] str = TextUtils.split(line, '-'); if (str.length >= 3) { header.setLocationId(str[1]); header.setParameterId(str[2]); } } else if (i == 3 && (key.charAt(0) == '%') || (key.charAt(0) == ';')) { //read unit from the header line 3 ( if any specified ) TextUtils.split(line, '-', '\"', buffer); header.setUnit(buffer[2]); } else if ((!(key.charAt(0) == '%') && !(key.charAt(0) == ';')) && header.getLocationId() != null && header.getParameterId() != null) { //All rows that does not start with %, ; or ASCII are data rows contentHandler.setTimeSeriesHeader(header); if (!contentHandler.isCurrentTimeSeriesHeaderForAllTimesRejected()) { TextUtils.split(line, '\t', buffer); String time = buffer[1]; String timePattern = time.length() <= "HH:mm".length() ? "HH:mm" : "HH:mm:ss"; contentHandler.setTime(contentHandler.getDefaultTimeZone(), "dd.MM.yyyy", buffer[0], timePattern, time); contentHandler.setValue('.', buffer[2]); contentHandler.applyCurrentFields(); } } } } }