ResultWriter.cpp
9.62 KB
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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "ResultWriter.h"
#include "../Common/Toolbox.h"
#include "../TimeManager/TimeManager.h"
using namespace TREPS::Common;
using namespace TREPS::TimeManager;
namespace TREPS
{
namespace RequestManager
{
ResultWriterClass::ResultWriterClass(void):XMLManagerClass()
{
}
bool ResultWriterClass::init(void)
{
return this->create("treps");
}
void ResultWriterClass::setError(const char *msg)
{
Node * resNode = this->getResultNode();
if (resNode == NULL)
return;
this->addAttributeToNode("success","false",resNode);
this->addStrArg("error",msg);
}
void ResultWriterClass::setSuccess(void)
{
Node * resNode = this->getResultNode();
if (resNode == NULL)
return;
this->addAttributeToNode("success","true",resNode);
}
Node * ResultWriterClass::createArgNode(const char *tag_name)
{
Node * resNode = this->getResultNode();
if (resNode == NULL)
return NULL;
Node *newNode = this->addChildToNode(tag_name,resNode);
return newNode;
}
Node * ResultWriterClass::addStrArg(const char *tag_name, const char *value)
{
Node *newNode = this->createArgNode(tag_name);
if (newNode == NULL)
return NULL;
this->setNodeContent(value, newNode);
return newNode;
}
Node * ResultWriterClass::addIntArg(const char *tag_name, int value)
{
Node *newNode = this->createArgNode(tag_name);
if (newNode == NULL)
return NULL;
this->setNodeContent(intToStr(value).c_str(), newNode);
this->addAttributeToNode("type", "int", newNode);
return newNode;
}
Node * ResultWriterClass::addFileInfoArg(const char *tag_name, t_FileInfo info)
{
Node *newNode = this->createArgNode(tag_name);
if (newNode == NULL)
return NULL;
//add type
this->addAttributeToNode("type", "fileinfo", newNode);
//add detected frames node
Node *framesNode = this->addChildToNode("frames",newNode);
if (framesNode == NULL)
return NULL;
for (t_StringList::iterator it=info.frames.begin(); it != info.frames.end(); ++it)
{
//add frame node
Node *frameNode = this->addChildToNode("frame",framesNode);
//add frame id
this->addAttributeToNode("id",(*it).c_str(),frameNode);
}
//add number of records
Node *recordsNode = this->addChildToNode("records",newNode);
if (recordsNode == NULL)
return NULL;
this->addAttributeToNode("total", intToStr(info.nbRecords).c_str(), recordsNode);
//add fields node
Node *fieldsNode = this->addChildToNode("fields",newNode);
if (fieldsNode == NULL)
return NULL;
this->addAttributeToNode("total", intToStr(info.fields.size()).c_str(), fieldsNode);
//get time manager instance
TimeManagerClass *timeMgr = TimeManagerClass::getInstance();
for (t_FieldList::iterator it=info.fields.begin(); it != info.fields.end(); ++it)
{
t_Field crtField = (*it);
//add field node
Node *fieldNode = this->addChildToNode("field",fieldsNode);
if (fieldNode == NULL)
return NULL;
//add field id
this->addAttributeToNode("id",crtField.id.c_str(),fieldNode);
//add type
this->addAttributeToNode("type",fieldTypeToStr(crtField.type).c_str(),fieldNode);
//add timeformat if type == FT_TIME
if (crtField.type == FT_TIME)
{
string timeId = timeMgr->getTimeIdFromFormatAndPattern(crtField.timeformat, crtField.timepattern.c_str());
this->addAttributeToNode("timeformat",timeId.c_str(),fieldNode);
// add time pattern
this->addAttributeToNode("timepattern",crtField.timepattern.c_str(),fieldNode);
}
//add field name
Node *nameNode = this->addChildToNode("name",fieldNode);
if (nameNode == NULL)
return NULL;
this->setNodeContent(crtField.name.c_str(), nameNode);
//add dimensions
Node *dimsNode = this->addChildToNode("dimensions",fieldNode);
if (dimsNode == NULL)
return NULL;
for (t_DimensionList::iterator dim=crtField.dims.begin(); dim != crtField.dims.end(); ++dim)
{
Node *dimNode = this->addChildToNode("dimension",dimsNode);
if (dimNode == NULL)
return NULL;
this->addAttributeToNode("id", (*dim).id.c_str(), dimNode);
this->addAttributeToNode("size", intToStr((*dim).size).c_str(), dimNode);
}
//add field nbRecords
recordsNode = this->addChildToNode("records",fieldNode);
if (recordsNode == NULL)
return NULL;
this->addAttributeToNode("total",intToStr(crtField.nbRecords).c_str(),recordsNode);
//add field attributes
Node *attributesNode = this->addAttributesNode(fieldNode, &crtField.attributes);
if (attributesNode == NULL)
return NULL;
//add field ucd
Node *ucdNode = this->addChildToNode("ucd",fieldNode);
if (ucdNode == NULL)
return NULL;
this->setNodeContent(crtField.ucd.c_str(), ucdNode);
//add field unit
Node *unitNode = this->addChildToNode("unit",fieldNode);
if (unitNode == NULL)
return NULL;
this->setNodeContent(crtField.unit.c_str(), unitNode);
}
//add vectors node
Node *vectorsNode = this->addVectorsNode(newNode, &info.vectors);
if (vectorsNode == NULL)
return NULL;
//add file attributes
Node *attributesNode = this->addAttributesNode(newNode, &info.attributes);
if (attributesNode == NULL)
return NULL;
return newNode;
}
Node * ResultWriterClass::addDataRecordListArg(const char *tag_name, DataRecordListClass *data)
{
Node *newNode = this->createArgNode(tag_name);
if (newNode == NULL)
return NULL;
this->addAttributeToNode("type", "data", newNode);
DataRecordClass *crtRecord = data->getFirstRecord();
while (crtRecord != NULL)
{
Node *recordNode = this->addChildToNode("rec",newNode);
map<string, string> valList = crtRecord->getValuesMap();
for (map<string,string>::iterator it2 = valList.begin(); it2 != valList.end(); ++it2)
{
string id = (it2->first);
string val = (it2->second);
Node *valNode = this->addChildToNode("val",recordNode);
this->addAttributeToNode("id",id.c_str(),valNode);
this->setNodeContent(val.c_str(),valNode);
}
crtRecord = crtRecord->getNextRecord();
}
return newNode;
}
Node * ResultWriterClass::addTransformationInfoArg(const char *tag_name, t_TransformationInfo info)
{
Node *newNode = this->createArgNode(tag_name);
if (newNode == NULL)
return NULL;
//argument type
this->addAttributeToNode("type", "runinfo", newNode);
//frames node
Node *framesNode = this->addChildToNode("frames",newNode);
if (framesNode == NULL)
return NULL;
this->addAttributeToNode("src", info.srcFrame.c_str(), framesNode);
this->addAttributeToNode("dst", info.dstFrame.c_str(), framesNode);
//time node
Node *timeNode = this->addChildToNode("time",newNode);
if (timeNode == NULL)
return NULL;
this->addAttributeToNode("fieldId", info.timeFieldId.c_str(), timeNode);
this->addAttributeToNode("formatId", info.timeFormatId.c_str(), timeNode);
this->addAttributeToNode("pattern", info.timePattern.c_str(), timeNode);
//source vectors node
Node *vectorsNode = this->addVectorsNode(newNode, &info.srcVectors);
if (vectorsNode == NULL)
return NULL;
return newNode;
}
Node * ResultWriterClass::getResultNode(void)
{
Node *resNode = this->getChildFromRoot("result");
if (!resNode)
resNode = this->addChildToRoot("result");
return resNode;
}
Node * ResultWriterClass::addVectorsNode(Node *parent, t_VectorList *vectors)
{
//add vectors node
Node *vectorsNode = this->addChildToNode("vectors", parent);
if (vectorsNode == NULL)
return NULL;
this->addAttributeToNode("total", intToStr(vectors->size()).c_str(), vectorsNode);
for (t_VectorList::iterator it=vectors->begin(); it != vectors->end(); ++it)
{
t_Vector crtVector = (*it);
//add vector node
Node *vectorNode = this->addChildToNode("vector",vectorsNode);
if (vectorNode == NULL)
return NULL;
//add vector info
this->addAttributeToNode("id",crtVector.id.c_str(),vectorNode);
this->addAttributeToNode("comp_1",crtVector.fieldId[0].c_str(),vectorNode);
this->addAttributeToNode("comp_2",crtVector.fieldId[1].c_str(),vectorNode);
this->addAttributeToNode("comp_3",crtVector.fieldId[2].c_str(),vectorNode);
//add nbRecords
Node *recordsNode = this->addChildToNode("records",vectorNode);
if (recordsNode == NULL)
return NULL;
this->addAttributeToNode("total",intToStr(crtVector.nbRecords).c_str(),recordsNode);
}
return vectorsNode;
}
Node *ResultWriterClass::addAttributesNode(Node *parent, map<string,string> *attributes)
{
Node *attributesNode = this->addChildToNode("attributes",parent);
if (attributesNode == NULL)
return NULL;
for (map<string,string>::iterator att=attributes->begin(); att != attributes->end(); ++att)
{
string attName = (att->first);
string attVal = (att->second);
Node *attributeNode = this->addChildToNode("attribute",attributesNode);
if (attributeNode == NULL)
return NULL;
this->addAttributeToNode("name",attName.c_str(),attributeNode);
this->setNodeContent(attVal.c_str(),attributeNode);
}
return attributesNode;
}
}
}