Blame view

src/TimeTableCatalog/VOTableWriter.cc 7.64 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * VOTableWriter.cc
 *
 *  Created on: 7 août 2013
 *      Author: CS
 */

#include "VOTableData.hh"
#include "VOTableWriter.hh"
#include "TimeTableCatalogUtil.hh"

namespace TimeTableCatalog {

#if defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)

#define MY_ENCODING "UTF-8"

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

VOTableWriter::VOTableWriter(const std::string& pPath,
		const std::string& pName) :
		XMLWriter(pPath, pName) {

}

VOTableWriter::~VOTableWriter() {
}

// ---------------------- PUBLIC ----------------------
std::string VOTableWriter::write(const TimeTable& pTT) {
	return XMLWriter::write(pTT, MY_ENCODING);
}

/**
 * Creates an instance of this.
 */
std::unique_ptr<AbstractWriter> VOTableWriter::createInstance(
		const std::string& pPath, const std::string& pName) {
	return std::unique_ptr < AbstractWriter
			> (new VOTableWriter(pPath, pName));
}

/**
 * Gets the tt file expected extension, starting with .
 */
const std::string VOTableWriter::getExtension() const {
	return ".xml";
}

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

void VOTableWriter::writeContent(const TimeTable& pTT,
		xmlTextWriterPtr& pWriter) {
	// -- create root element
	openTag(pTT, pWriter, VOTableData::VOTABLE_TAG);
	// add version attribute
	addAttribute(pTT, pWriter, VOTableData::VOTABLE_VERSION_ATTRIB, VOTableData::VOTABLE_VERSION_VALUE);
	// add xmlns:xsi attribute
	addAttribute(pTT, pWriter, VOTableData::VOTABLE_XMLNS_XSI_ATTRIB, VOTableData::VOTABLE_XMLNS_XSI_VALUE);
	// add xmlns attribute
	addAttribute(pTT, pWriter, VOTableData::VOTABLE_XMLNS_ATTRIB, VOTableData::VOTABLE_XMLNS_VALUE);
	// add xmlns:stc attribute
	addAttribute(pTT, pWriter, VOTableData::VOTABLE_XMLNS_STC_ATTRIB, VOTableData::VOTABLE_XMLNS_STC_VALUE);

	// -- write metadata as name, creation date, description etc.
	writeMetadata(pTT, pWriter);

	// -- write intervals
	writeVOTable(pTT, pWriter);

	// --  close root element
	closeTag(pTT, pWriter, VOTableData::VOTABLE_TAG);
}

void VOTableWriter::writeMetadata(const TimeTable& pTT,
		xmlTextWriterPtr& pWriter) {
	// -- write name tag
	std::ostringstream allMetadata;
	allMetadata << VOTableData::NAME_TAG << pTT._name << ";" << std::endl;
	allMetadata << VOTableData::HISTORIC_TAG << pTT._history << ";"
			<< std::endl;
	allMetadata << VOTableData::CREATION_DATE_TAG;
	writeISOTime(pTT._creationDate, pTT._timeFormat, allMetadata);
	allMetadata << ";" << std::endl;
	writeElement(pTT, pWriter, VOTableData::DESCRIPTION_TAG, allMetadata.str());
}

void VOTableWriter::writeField(const TimeTable& pTT,
		xmlTextWriterPtr& pWriter,
		const std::string &name,
		const std::string &ID,
		const std::string &ucd,
		const std::string &datatype,
		const std::string &xtype,
		const std::string &utype,
		const std::string &unit,
		const std::string &description,
		int arraysize
		) {

	// -- create FIELD element
	openTag(pTT, pWriter, VOTableData::FIELD_TAG);

	// add Fields attributes
	addAttribute(pTT, pWriter, VOTableData::FIELD_NAME_ATTRIB, name);
	addAttribute(pTT, pWriter, VOTableData::FIELD_ID_ATTRIB, ID);
	addAttribute(pTT, pWriter, VOTableData::FIELD_UCD_ATTRIB, ucd);
	addAttribute(pTT, pWriter, VOTableData::FIELD_DATATYPE_ATTRIB, datatype);
	if (xtype.size() != 0)
		addAttribute(pTT, pWriter, VOTableData::FIELD_XTYPE_ATTRIB, xtype);
	addAttribute(pTT, pWriter, VOTableData::FIELD_UTYPE_ATTRIB, utype);
	if (unit.size() != 0)
		addAttribute(pTT, pWriter, VOTableData::FIELD_UNIT_ATTRIB, unit);
	if (arraysize != 1) {
de7354f6   Benjamin Renard   Fix arraysize in ...
115
116
117
118
119
120
121
		std::string arraySizeString;
		if (arraysize == -1) {
			arraySizeString = "*";
		}
		else {
			arraySizeString = std::to_string(arraysize);
		}
fbe3c2bb   Benjamin Renard   First commit
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
		addAttribute(pTT, pWriter, VOTableData::FIELD_ARRAYSIZE_ATTRIB, arraySizeString);
	}

	if (description.empty() == false) {
		writeElement(pTT, pWriter, VOTableData::DESCRIPTION_TAG, description);
	}

	// --  close FIELD element
	closeTag(pTT, pWriter, VOTableData::FIELD_TAG);
}

void VOTableWriter::writeVOTable(const TimeTable& pTT,
		xmlTextWriterPtr& pWriter) {
	// -- open tag RESOURCE
	openTag(pTT, pWriter, VOTableData::RESOURCE_TAG);
	// write tt description
	if (!pTT._description.empty()) {
		std::string description = pTT._description.at(0);
		for (size_t i = 1; i < pTT._description.size(); ++i) {
			description += "\n" + pTT._description.at(i);
		}
		writeElement(pTT, pWriter, VOTableData::DESCRIPTION_TAG, description);
	} else {
		writeElement(pTT, pWriter, VOTableData::DESCRIPTION_TAG, std::string());
	}
	// -- open tag TABLE
	openTag(pTT, pWriter, VOTableData::TABLE_TAG);

	// --------------- FIELD(S) -----------------------------
de7354f6   Benjamin Renard   Fix arraysize in ...
151
152
	writeField (pTT, pWriter, "startTime", "startTime", "time.epoch", "char", "dateTime", "", "", "", -1);
	writeField (pTT, pWriter, "stopTime", "stopTime", "time.epoch", "char", "dateTime", "", "", "", -1);
fbe3c2bb   Benjamin Renard   First commit
153
154
155
156
157
158

	// Write parameter description if defined (Catalog ONLY)
	ParameterDescriptionList pdl = pTT.getParameterDescritptions();

	for (auto parameterDescription : pdl) {
		ParameterDescription::ParameterType internalType = parameterDescription.getType();
68c29629   Benjamin Renard   Fix in TT/Catalog...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		std::string type = getTypeAsString(internalType);
		std::string ucd  = parameterDescription.getUcd();
		std::string xtype = "";
		int size = parameterDescription.getSizeAsInt();
		if (type.compare("char") == 0) {
			switch (internalType) {
				case ParameterDescription::ParameterType::Date:
					ucd = "time.epoch";
					xtype = "dateTime";
					size = -1;
					break;
				case ParameterDescription::ParameterType::Unknown:
				case ParameterDescription::ParameterType::String:
					size = -1;
					break;
			}
fbe3c2bb   Benjamin Renard   First commit
175
176
177
178
179
		}

		writeField (pTT, pWriter,
				parameterDescription.getName(),
				parameterDescription.getId(),
68c29629   Benjamin Renard   Fix in TT/Catalog...
180
181
182
				ucd,
				type,
				xtype,	// XTYPE
fbe3c2bb   Benjamin Renard   First commit
183
184
185
				parameterDescription.getUtype(),
				parameterDescription.getUnit(),
				parameterDescription.getDescription(),
68c29629   Benjamin Renard   Fix in TT/Catalog...
186
				size);
fbe3c2bb   Benjamin Renard   First commit
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	}

	// --------------- INTERVALS (+ DATA) -------------------

	// -- open tag DATA
	openTag(pTT, pWriter, VOTableData::DATA_TAG);
	// -- open tag TABLE_DATA
	openTag(pTT, pWriter, VOTableData::TABLEDATA_TAG);

	for (TimeInterval interval : pTT.getIntervals()) {
		openTag(pTT, pWriter, VOTableData::TR_TAG);

		// -- write "TD" tag for start date
		std::ostringstream osstart;
5b387233   Vincent Cephirins   RM7926 - Evol - C...
201
		writeTimeData(interval._startTime, pTT._extTimeFormat, osstart);
fbe3c2bb   Benjamin Renard   First commit
202
203
204
205
		writeElement(pTT, pWriter, VOTableData::TD_TAG, osstart.str());

		// -- write "TD" tag for stop date
		std::ostringstream osstop;
5b387233   Vincent Cephirins   RM7926 - Evol - C...
206
		writeTimeData(interval._stopTime, pTT._extTimeFormat, osstop);
fbe3c2bb   Benjamin Renard   First commit
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
		writeElement(pTT, pWriter, VOTableData::TD_TAG, osstop.str());

		// Write parameter values if defined (Catalog ONLY)
		for (auto parameterDescription : pdl) {
			std::vector<std::string> dataValues = interval.getParameterData (parameterDescription.getId());
			std::string paramData;

			for (auto dataValue : dataValues) {
				if (paramData.empty()) {
					paramData += dataValue;
				} else {
					paramData +=  VOTableData::SEPARATOR + dataValue;
				}
			}
			writeElement(pTT, pWriter, VOTableData::TD_TAG, paramData);
		}

		closeTag(pTT, pWriter, VOTableData::TR_TAG);
	}

	closeTag(pTT, pWriter, VOTableData::TABLEDATA_TAG);
	closeTag(pTT, pWriter, VOTableData::DATA_TAG);
	closeTag(pTT, pWriter, VOTableData::TABLE_TAG);
	closeTag(pTT, pWriter, VOTableData::RESOURCE_TAG);
}

68c29629   Benjamin Renard   Fix in TT/Catalog...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
std::string VOTableWriter::getTypeAsString(ParameterDescription::ParameterType type) {
	switch (type) {
		case ParameterDescription::ParameterType::Unknown:
			return "char";
		case ParameterDescription::ParameterType::Double:
			return "double";
		case ParameterDescription::ParameterType::Date:
			return "char";
		case ParameterDescription::ParameterType::String:
			return "char";
		case ParameterDescription::ParameterType::Integer:
			return "int";
	}
	return "string";
}

fbe3c2bb   Benjamin Renard   First commit
249
250
251
#endif

} /* namespace TimeTableCatalog */