Blame view

src/TimeTableCatalog/VOTableReader.cc 8.17 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * VOTableReader.cc
 *
 *  Created on: 7 août 2013
 *      Author: CS
 */

#include "VOTableData.hh"
#include "VOTableReader.hh"
#include "TimeTableCatalogUtil.hh"
#include "Catalog.hh"
#include <iostream>
#include <string.h>

68c29629   Benjamin Renard   Fix in TT/Catalog...
15
16
#include <boost/algorithm/string.hpp>

fbe3c2bb   Benjamin Renard   First commit
17
18
19
20
21
22
namespace TimeTableCatalog {

const std::string VOTableReader::FORMAT = "VO";

VOTableReader::VOTableReader(const std::string& pPath) :
		XMLReader(pPath) {
68c29629   Benjamin Renard   Fix in TT/Catalog...
23
	_tmpIntervalstartdate = -1;
fbe3c2bb   Benjamin Renard   First commit
24

68c29629   Benjamin Renard   Fix in TT/Catalog...
25
26
27
28
	_readingVOTable     = false;
	_readingResource    = false;
	_readingTable       = false;
	_readingField       = false;
fbe3c2bb   Benjamin Renard   First commit
29
	_readingDescription = false;
68c29629   Benjamin Renard   Fix in TT/Catalog...
30
31
32
33
34
	_readingTr          = false;
	_readingTd          = false;

	_tdCount    = 0;
	_fieldCount = 0;
fbe3c2bb   Benjamin Renard   First commit
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
}

VOTableReader::~VOTableReader() {
}

// ---------------- PUBLIC -------------------

std::unique_ptr<AbstractReader> VOTableReader::createInstance(
		const std::string& pPath) {
	return std::unique_ptr < AbstractReader > (new VOTableReader(pPath));
}

std::string VOTableReader::getFirstNode() {
	return VOTableData::VOTABLE_TAG;
}

// ----------------- PRIVATE --------------------------

/**
 *
 * @pTT : the timetable to load
 * @reader: the xmlReader
 *
 * Fills the given TimeTable with the xml file content
 */
void VOTableReader::processNode(TimeTable& pTT, xmlTextReaderPtr reader, int &crtIndex) {
	const xmlChar *name;

	// -- read tag
	name = xmlTextReaderConstName(reader);
	if (name == NULL)
		name = BAD_CAST "--";

	// get tag name to easily handle it
	std::string tagName = reinterpret_cast<const char*>(name);

68c29629   Benjamin Renard   Fix in TT/Catalog...
71
72
73
74
75
76
77
78
79
80
81
	if (tagName == VOTableData::VOTABLE_TAG) {
		_readingVOTable = (xmlTextReaderNodeType(reader) == 1);
	}
	else if (tagName == VOTableData::RESOURCE_TAG) {
		_readingResource = (xmlTextReaderNodeType(reader) == 1);
	}
	else if (tagName == VOTableData::TABLE_TAG) {
		_readingTable = (xmlTextReaderNodeType(reader) == 1);
	}
	else if (tagName == VOTableData::DESCRIPTION_TAG) {
		_readingDescription = (xmlTextReaderNodeType(reader) == 1);
fbe3c2bb   Benjamin Renard   First commit
82
83
	}
	else if (tagName == VOTableData::FIELD_TAG) {
68c29629   Benjamin Renard   Fix in TT/Catalog...
84
		_readingField = (xmlTextReaderNodeType(reader) == 1);
fbe3c2bb   Benjamin Renard   First commit
85
86
		if (_readingField) {
			readField (pTT, reader);
68c29629   Benjamin Renard   Fix in TT/Catalog...
87
			++_fieldCount;
fbe3c2bb   Benjamin Renard   First commit
88
89
90
		}
	}
	else if (tagName == VOTableData::TR_TAG) {
68c29629   Benjamin Renard   Fix in TT/Catalog...
91
92
		_readingTr = (xmlTextReaderNodeType(reader) == 1);
		if (!_readingTr) {
fbe3c2bb   Benjamin Renard   First commit
93
			_tdCount = 0;
68c29629   Benjamin Renard   Fix in TT/Catalog...
94
		}
fbe3c2bb   Benjamin Renard   First commit
95
96
	}
	else if (tagName == VOTableData::TD_TAG) {
68c29629   Benjamin Renard   Fix in TT/Catalog...
97
		_readingTd = (xmlTextReaderNodeType(reader) == 1);
fbe3c2bb   Benjamin Renard   First commit
98
	}
68c29629   Benjamin Renard   Fix in TT/Catalog...
99
100
	else if ((tagName == "#text") && (xmlTextReaderNodeType(reader) == 3)) {
		if (_readingDescription) {
fbe3c2bb   Benjamin Renard   First commit
101
102
			readDescription (pTT, reader);
		}
68c29629   Benjamin Renard   Fix in TT/Catalog...
103
		else if (_readingTr == true) {
fbe3c2bb   Benjamin Renard   First commit
104
105
106
107
108
109
110
111
112
113
114
115
116
			readInterval (pTT, reader, crtIndex);
			++crtIndex;
			_tdCount++;
		}
	}
}

void VOTableReader::readDescription(TimeTable& pTT, xmlTextReaderPtr reader) {
	// -- read tag text (may be NULL)
	const xmlChar *value = xmlTextReaderConstValue(reader);

	std::string tagValue = reinterpret_cast<const char*>(value);

68c29629   Benjamin Renard   Fix in TT/Catalog...
117
	if (_readingVOTable && !_readingResource) {
fbe3c2bb   Benjamin Renard   First commit
118
		readMetadata(tagValue, pTT);
68c29629   Benjamin Renard   Fix in TT/Catalog...
119
	} else if (_readingResource && !_readingField) {
fbe3c2bb   Benjamin Renard   First commit
120
121
122
123
		if (value != NULL)
			split(tagValue, '\n', pTT._description);
		else
			pTT._description.clear();
68c29629   Benjamin Renard   Fix in TT/Catalog...
124
125
	} else if (_readingField && (_fieldCount > 2)) {
		// Two first fields are used for start time and stop time
fbe3c2bb   Benjamin Renard   First commit
126
127
128
129
130
131
132
133
134
135
136
		ParameterDescription *pd = pTT.getLastParameterDescritption();
		pd->setDescription(tagValue);
	}
}

void VOTableReader::readField(TimeTable& pTT, xmlTextReaderPtr reader) {
	const xmlChar *attrName;

	attrName = xmlTextReaderGetAttribute (reader, (const xmlChar*)VOTableData::FIELD_NAME_ATTRIB.c_str());

	// Jump startTime & stopTime tag
68c29629   Benjamin Renard   Fix in TT/Catalog...
137
	if (_fieldCount >= 2){
fbe3c2bb   Benjamin Renard   First commit
138
139
140
141
142
143
144
145
146
		const xmlChar *attrId, *attrSize, *attrType, *attrUnit, *attrUcd, *attrUtype;

		attrId	 		= xmlTextReaderGetAttribute (reader, (const xmlChar*)VOTableData::FIELD_ID_ATTRIB.c_str());
		attrSize 		= xmlTextReaderGetAttribute (reader, (const xmlChar*)VOTableData::FIELD_ARRAYSIZE_ATTRIB.c_str());
		attrType 		= xmlTextReaderGetAttribute (reader, (const xmlChar*)VOTableData::FIELD_DATATYPE_ATTRIB.c_str());
		attrUnit		= xmlTextReaderGetAttribute (reader, (const xmlChar*)VOTableData::FIELD_UNIT_ATTRIB.c_str());
		attrUcd			= xmlTextReaderGetAttribute (reader, (const xmlChar*)VOTableData::FIELD_UCD_ATTRIB.c_str());
		attrUtype		= xmlTextReaderGetAttribute (reader, (const xmlChar*)VOTableData::FIELD_UTYPE_ATTRIB.c_str());

fbe3c2bb   Benjamin Renard   First commit
147
		std::string voType = std::string((const char *)attrType);
68c29629   Benjamin Renard   Fix in TT/Catalog...
148
		ParameterDescription::ParameterType internalType = getTypeFromString(voType);
fbe3c2bb   Benjamin Renard   First commit
149
150
151
152
153

		std::string internalSize("1");
		if (((char *) attrSize) != NULL) {
			internalSize = std::string((const char *)attrSize);
		}
68c29629   Benjamin Renard   Fix in TT/Catalog...
154
155
156
		if (internalSize.compare("*") == 0 || internalType == ParameterDescription::ParameterType::String) {
			internalSize = "1";
		}
fbe3c2bb   Benjamin Renard   First commit
157

5b91d159   Benjamin Renard   Fix catalog reade...
158
159
160
161
162
163
		std::string attrIdStr = (attrId != NULL) ? std::string((const char *)attrId) : "";
		std::string attrNameStr = (attrName != NULL) ? std::string((const char *)attrName) : "";
		std::string attrUnitStr = (attrUnit != NULL) ? std::string((const char *)attrUnit) : "";
		std::string attrUcdStr = (attrUcd != NULL) ? std::string((const char *)attrUcd) : "";
		std::string attrUtypeStr = (attrUtype != NULL) ? std::string((const char *)attrUtype) : "";

fbe3c2bb   Benjamin Renard   First commit
164
		((Catalog *) &pTT)->addParameterDescription(ParameterDescription(
5b91d159   Benjamin Renard   Fix catalog reade...
165
166
				attrIdStr,
				attrNameStr,
fbe3c2bb   Benjamin Renard   First commit
167
168
				internalSize,
				internalType,
5b91d159   Benjamin Renard   Fix catalog reade...
169
				attrUnitStr,
fbe3c2bb   Benjamin Renard   First commit
170
				"",		// Read later
0b2d2cab   Erdogan Furkan   Kernel part for c...
171
				"",		// Read later
5b91d159   Benjamin Renard   Fix catalog reade...
172
173
				attrUcdStr,
				attrUtypeStr));
fbe3c2bb   Benjamin Renard   First commit
174
175
176
177
178
179
180
181
182

	}
}

void VOTableReader::readInterval (TimeTable& pTT, xmlTextReaderPtr reader, int pcrtIndex) {
	// -- read tag text (may be NULL)
	const xmlChar *value = xmlTextReaderConstValue(reader);

	std::string tagValue = reinterpret_cast<const char*>(value);
fbe3c2bb   Benjamin Renard   First commit
183
184
185
186
187
188
189
	// first TD is startdate
	if (_tdCount == 0) {
		// read start date of interval
		_tmpIntervalstartdate = readISOTime(tagValue);

		// read time format, the first date of the first interval
		if (pTT._timeFormat == TimeTable::TIME_FORMAT::UNKNOWN) {
818d2ea6   Benjamin Renard   Support new time ...
190
191
			int size = 0;
			pTT._timeFormat = getTimeFormat(tagValue, size);
fbe3c2bb   Benjamin Renard   First commit
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
		}
	} else if (_tdCount == 1) {
		// start date is set, that means we are waiting for stop date
		pTT.addInterval( TimeInterval(_tmpIntervalstartdate, readISOTime(tagValue), pcrtIndex));
	} else {
		TimeInterval * pLastInterval = pTT.getLastInterval();
		// If interval list is not empty, add parameter data to the last interval
		if (pLastInterval != NULL) {
			ParameterDescriptionList pdl = pTT.getParameterDescritptions();

			std::vector<std::string> paramList;
			split (tagValue, VOTableData::SEPARATOR.c_str()[0], paramList);

			int nbParamDataInterval = pLastInterval->getParameterDataCount();
			std::string paramKey = pdl [nbParamDataInterval].getId();
			pLastInterval->addParameterData(paramKey, paramList);
		}
	}
}

void VOTableReader::readMetadata(const std::string& pDescription,
		TimeTable& pTimeTable) {
	// try to find string like "Prop:xxxx" and set _prop attribute to "xxxx"
	std::vector<std::string> lines;
	split(pDescription, ';', lines);

	for (std::string line : lines) {

		// -- here try to find "Name:"
		if (contains(line, VOTableData::NAME_TAG)) {
			extractvalue(line, pTimeTable._name);
		}
		// -- here try to find "Historic:"
		else if (contains(line, VOTableData::HISTORIC_TAG)) {
			extractvalue(line, pTimeTable._history);
		}
		// -- here try to find "CreationDate:"
		// and try to set it as unix time
		// if impossible, add the line to the description
		else if (contains(line, VOTableData::CREATION_DATE_TAG)) {
			// convert creation date string to char*
			std::string creationDate;
			extractvalue(line, creationDate);
			// try to read date
			try {
				pTimeTable._creationDate = TimeTableCatalog::readISOTime(creationDate);
			} catch (...) {
				LOG4CXX_WARN(_logger,
						"Unable to parse creation date in " + getPath());
			}
		} else {
			// ignore lines
		}
	}
}

68c29629   Benjamin Renard   Fix in TT/Catalog...
248
249
250
251
252
253
254
255
256
257
258
259
260
ParameterDescription::ParameterType VOTableReader::getTypeFromString(std::string type) {
	boost::algorithm::trim(type);
	boost::algorithm::to_lower(type);

	if ((type.compare("boolean") == 0) || (type.compare("short") == 0) || (type.compare("int") == 0) || (type.compare("long") == 0)) {
		return ParameterDescription::ParameterType::Integer;
	}
	else if ((type.compare("float") == 0) || (type.compare("double") == 0)) {
		return ParameterDescription::ParameterType::Double;
	}
	return ParameterDescription::ParameterType::String;
}

fbe3c2bb   Benjamin Renard   First commit
261
} /* namespace TimeTableCatalog */