/* ================================================================
* Delft FEWS
* ================================================================
*
* Project Info: http://www.wldelft.nl/soft/fews/index.html
* Project Lead: Karel Heynert (karel.heynert@wldelft.nl)
*
* (C) Copyright 2008, by Deltares (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.
*
* ----------------------------------------------------------------
* HydamsXMLTimeSeriesParser.java
* ----------------------------------------------------------------
* (C) Copyright 2008, by Deltares
*
* Original Author: Saskia Vermeer
* Contributor(s):
*
* Changes:
* --------
* 04 jan 2011 Version 1 ();
*/
package nl.wldelft.timeseriesparsers;
import nl.wldelft.util.FileUtils;
import nl.wldelft.util.TextUtils;
import nl.wldelft.util.io.XmlParser;
import nl.wldelft.util.timeseries.DefaultTimeSeriesHeader;
import nl.wldelft.util.timeseries.TimeSeriesContentHandler;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
/*
* TimeSeries reader for HyDaMS xml files.
*
* Each file contains a single location/parameter combination, the values of which are determined by the filename:
* _.xml
*
*
* Example
*
*
*
*
*
*/
public class HydamsXMLTimeSeriesParser implements XmlParser {
private TimeSeriesContentHandler contentHandler = null;
private String virtualFileName = null;
private XMLStreamReader reader = null;
private DefaultTimeSeriesHeader header = null;
private static final String MISSING_VALUE = "RWLuecke";
@Override
public void parse(XMLStreamReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws Exception {
this.reader = reader;
this.virtualFileName = virtualFileName;
this.contentHandler = contentHandler;
//FEWS-5400: (Simone Patzke) Entries with values RWLuecke should be skipped instead of set to missingValue
//this.contentHandler.addMissingValue(MISSING_VALUE);
parseHeaderFromFileName();
reader.require(XMLStreamConstants.START_DOCUMENT, null, null);
reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "TSD");
reader.nextTag();
while (!reader.isEndElement()) {
parseDefTag();
if (header != null) {
this.contentHandler.createTimeSeriesHeaderAlias(0, header);
}
parseData();
}
reader.require(XMLStreamConstants.END_ELEMENT, null, "TSD");
reader.next();
reader.require(XMLStreamConstants.END_DOCUMENT, null, null);
}
/**
* Parse the attributes of the DEF element
*
* @throws XMLStreamException
*/
private void parseDefTag() throws XMLStreamException {
if (!reader.isStartElement()) return;
if (!TextUtils.equals(reader.getLocalName(), "DEF")) return;
String unit = reader.getAttributeValue(null, "EINHEIT");
header.setUnit(unit);
reader.nextTag();
}
/**
* Parse the data within the DATA tag
* @throws XMLStreamException
*/
private void parseData() throws XMLStreamException {
reader.nextTag();
if (!reader.isStartElement()) return;
if (!TextUtils.equals(reader.getLocalName(), "DATA")) return;
reader.next();
while (!reader.isEndElement()) {
readTimeSeries(reader.getText());
if (reader.hasNext()) reader.next();
}
reader.nextTag();
}
/**
* Parse the data found in the text
, which must have the following format:
* date-value pairs are separated by newLine
* between date and value there are one or more spaces
* @param text String containing the data to be parsed to the current timeSeriesHeader
*/
private void readTimeSeries(String text) {
String[] result = text.split("\\n");
for (int i=0; i_.xml, but found: " + virtualFileName);
}
// split on underscore - first item is the externalLocationId, second is the externalParameter
header.setLocationId(fileNameParts[0]);
header.setParameterId(fileNameParts[1]);
}
}