Blame view

src/TimeTableCatalog/VOTableWriter.cc 7.02 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
115
116
117
118
119
120
121
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
/*
 * 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) {
		std::string arraySizeString = std::to_string(arraysize);
		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) -----------------------------
	writeField (pTT, pWriter, "startTime", "startTime", "time.epoch", "char", "dateTime", "", "", "", 1);
	writeField (pTT, pWriter, "stopTime", "stopTime", "time.epoch", "char", "dateTime", "", "", "", 1);

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

	for (auto parameterDescription : pdl) {
		ParameterDescription::ParameterType internalType = parameterDescription.getType();
		std::string voType;
		if (internalType == ParameterDescription::ParameterType::Unknown) {
			voType = "char";
		} else if (internalType == ParameterDescription::ParameterType::Integer) {
			voType = "int";
		} else if (internalType == ParameterDescription::ParameterType::Double) {
			voType = "double";
		} else if (internalType == ParameterDescription::ParameterType::String) {
			voType = "char";
		}

		writeField (pTT, pWriter,
				parameterDescription.getName(),
				parameterDescription.getId(),
				parameterDescription.getUcd(),
				voType,
				"",	// XTYPE
				parameterDescription.getUtype(),
				parameterDescription.getUnit(),
				parameterDescription.getDescription(),
				parameterDescription.getSizeAsInt());
	}

	// --------------- 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;
		writeISOTime(interval._startTime, pTT._timeFormat, osstart);
		writeElement(pTT, pWriter, VOTableData::TD_TAG, osstart.str());

		// -- write "TD" tag for stop date
		std::ostringstream osstop;
		writeISOTime(interval._stopTime, pTT._timeFormat, osstop);
		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);
}

#endif

} /* namespace TimeTableCatalog */