XMLRequestParser.cc
12.5 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* XMLRequestParser.cc
*
* Created on: Nov 23, 2012
* Author: f.casimir
*/
//Common module include
#include "DicError.hh"
// DD_Client_r_lib module includes
#include "TimeUtil.hh"
//Parameters module include
#include "ServicesServer.hh"
//XMLConfigurator module include
#include "NodeCfg.hh"
#include "Constant.hh"
//XMLRequest module include
#include "Config.hh"
#include "XMLRequestParser.hh"
#include "TimeInterval.hh"
#include "TimeTableCatalogFactory.hh"
#include "TimeTable.hh"
#include "Catalog.hh"
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
// Using namespace
using namespace log4cxx;
using namespace AMDA::Parameters;
using namespace AMDA::XMLConfigurator;
using namespace TimeTableCatalog;
namespace AMDA {
namespace XMLRequest {
class ParamNode : public NodeCfg
{
public:
void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
ParameterManager* lParameterManager = pContext.get<ParameterManager*>();
try {
xmlChar* lParamName = xmlGetProp(pNode, (const xmlChar *) "id");
std::string paramNameStr;
if (lParamName) {
paramNameStr = std::string((char*)lParamName);
xmlFree(lParamName);
}
else {
ERROR_EXCEPTION( ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << PARAMNAME)
}
lParameterManager->createParameter(paramNameStr.c_str());
} catch (...) {
//This parameter is ignored, continue
// throw;
}
}
};
class IntervalNode : public NodeGrpCfg {
public:
class StartTimeNode : public NodeCfg {
public:
void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
IntervalNode* lIntervalNode = pContext.get<IntervalNode*>();
double lStartTime = DD_Time2Double((const char*)pNode->children->content);
lIntervalNode->_startTime = lStartTime;
}
};
class TimeIntervalNode : public NodeCfg
{
public:
void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
IntervalNode* lIntervalNode = pContext.get<IntervalNode*>();
double lTimeInt = DD_Time2Double((const char*)pNode->children->content);
lIntervalNode->_timeInt = lTimeInt;
}
};
IntervalNode () : NodeGrpCfg(), _startTime(0), _timeInt(0) {
getChildList()["startTime"] = NodeCfgSPtr(new StartTimeNode);
getChildList()["timeInterval"] = NodeCfgSPtr(new TimeIntervalNode);
}
void proceed(xmlNodePtr pNode,
const AMDA::Parameters::CfgContext& pContext) {
LOG4CXX_DEBUG(gLogger, "IntervalNode::proceed");
// Create new context
CfgContext lContext;
lContext.push<IntervalNode*>(this);
// Go to next node
NodeGrpCfg::proceed(pNode, lContext);
// Get manager of the new output parameter for this node
ParameterManager* lParameterManager = pContext.get<ParameterManager*>();
std::string emptyStr;
lParameterManager->addInputInterval(_startTime, _startTime + _timeInt, 0, emptyStr, emptyStr, 0);
}
double _startTime;
double _timeInt;
};
class TimeTableNode : public NodeCfg
{
public:
void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
//TT Id => TT path
xmlChar* lTTId = NULL;
ParameterManager* lParameterManager = pContext.get<ParameterManager*>();
if (!(lTTId = xmlGetProp(pNode, (const xmlChar *) "id"))) {
ERROR_EXCEPTION(
AMDA::XMLConfigurator::ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << AMDA::XMLConfigurator::TIMETABLENAME)
}
std::string lStrTTPath((const char*)lTTId);
if(lTTId){
xmlFree(lTTId);
}
//Time Table name
std::string lStrTTName;
xmlChar* lTTName = NULL;
if (!(lTTName = xmlGetProp(pNode, (const xmlChar *) "name")))
{
boost::filesystem::path ttPath(lStrTTPath);
lStrTTName = ttPath.filename().string();
// name of the tt without parent path and extension
lStrTTName = lStrTTName.substr(0, lStrTTName.size() - ttPath.extension().string().size());
}
else
{
lStrTTName = (const char*)lTTName;
xmlFree(lTTName);
}
// check local file exists
// if distant file does not exist an error will be thrown in the next if
if((lStrTTPath.find("http://")!=std::string::npos
&& lStrTTPath.find("https://")!=std::string::npos)
&& !boost::filesystem::exists(lStrTTPath)){
ERROR_EXCEPTION(
AMDA::XMLConfigurator::ERROR_FILE_NOT_EXIST << lStrTTPath);
}
// Read file
std::string lReaderType = TimeTableCatalogFactory::getInstance().getReaderType(lStrTTPath);
if(lReaderType.empty()){
ERROR_EXCEPTION(
"Unknown Timetable : " << lStrTTPath);
}
TimeTableCatalog::TimeTable inputTT;
inputTT.read(lStrTTPath, lReaderType);
const std::vector<TimeInterval> lTimeIntervalList = inputTT.getIntervals();
// Get interval index if exists
xmlChar* lTTIndex = xmlGetProp(pNode, (const xmlChar *) "index");
if (lTTIndex)
{
//Add only interval designate by index
int tmpIndex = atoi( (const char*)lTTIndex );
size_t index;
if(tmpIndex == -1)
index = lTimeIntervalList.size() -1;
else
index = (size_t)tmpIndex;
xmlFree(lTTIndex);
if (index >= lTimeIntervalList.size())
{
ERROR_EXCEPTION(
"Cannot find index : " << index << " in TimeTable : " << lStrTTPath);
}
lParameterManager->addInputInterval(
lTimeIntervalList[index]._startTime,
lTimeIntervalList[index]._stopTime,
lTimeIntervalList[index]._index,
lStrTTPath, lStrTTName, inputTT.getIntervalNumber());
}
else
{
//Add all intervals
for(std::vector<TimeInterval>::const_iterator it = lTimeIntervalList.begin(); it != lTimeIntervalList.end(); ++it) {
lParameterManager->addInputInterval(it->_startTime, it->_stopTime, it->_index, lStrTTPath, lStrTTName, inputTT.getIntervalNumber());
}
}
}
};
class CatalogNode : public NodeCfg
{
public:
void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
//Catalog Id => catalog path
xmlChar* lCatalogId = NULL;
ParameterManager* lParameterManager = pContext.get<ParameterManager*>();
if (!(lCatalogId = xmlGetProp(pNode, (const xmlChar *) "id"))) {
ERROR_EXCEPTION(
AMDA::XMLConfigurator::ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << AMDA::XMLConfigurator::TIMETABLENAME)
}
std::string lStrCatalogPath((const char*)lCatalogId);
if(lCatalogId){
xmlFree(lCatalogId);
}
//Catalog name
std::string lStrCatalogName;
xmlChar* lCatalogName = NULL;
if (!(lCatalogName = xmlGetProp(pNode, (const xmlChar *) "name")))
{
boost::filesystem::path ttPath(lStrCatalogPath);
lStrCatalogName = ttPath.filename().string();
// name of the tt without parent path and extension
lStrCatalogName = lStrCatalogName.substr(0, lStrCatalogName.size() - ttPath.extension().string().size());
}
else
{
lStrCatalogName = (const char*)lCatalogName;
xmlFree(lCatalogName);
}
// check local file exists
// if distant file does not exist an error will be thrown in the next if
if((lStrCatalogPath.find("http://")!=std::string::npos
&& lStrCatalogPath.find("https://")!=std::string::npos)
&& !boost::filesystem::exists(lStrCatalogPath)){
ERROR_EXCEPTION(
AMDA::XMLConfigurator::ERROR_FILE_NOT_EXIST << lStrCatalogPath);
}
// Read file
std::string lReaderType = TimeTableCatalogFactory::getInstance().getReaderType(lStrCatalogPath);
if(lReaderType.empty()){
ERROR_EXCEPTION(
"Unknown Catalog : " << lStrCatalogPath);
}
TimeTableCatalog::Catalog inputCatalog;
inputCatalog.read(lStrCatalogPath, lReaderType);
std::vector<TimeInterval> lTimeIntervalList = inputCatalog.getIntervals();
// Get interval index if exists
xmlChar* lCatalogIndex = xmlGetProp(pNode, (const xmlChar *) "index");
if (lCatalogIndex)
{
//Add only interval designate by index
int tmpIndex = atoi( (const char*)lCatalogIndex );
size_t index;
if(tmpIndex == -1)
index = lTimeIntervalList.size() -1;
else
index = (size_t)tmpIndex;
xmlFree(lCatalogIndex);
if (index >= lTimeIntervalList.size())
{
ERROR_EXCEPTION(
"Cannot find index : " << index << " in Catalog : " << lStrCatalogPath);
}
lParameterManager->addInputInterval(
lTimeIntervalList[index]._startTime,
lTimeIntervalList[index]._stopTime,
lTimeIntervalList[index]._index,
lStrCatalogPath,
lStrCatalogName,
inputCatalog.getIntervalNumber());
for (auto &desc : inputCatalog.getParameterDescriptions())
{
lParameterManager->addInputIntervalDataList(index, desc.getId(), lTimeIntervalList[index].getParameterData(desc.getId()));
}
}
else
{
//Add all intervals
for(std::vector<TimeInterval>::const_iterator it = lTimeIntervalList.begin(); it != lTimeIntervalList.end(); ++it)
{
lParameterManager->addInputInterval(it->_startTime, it->_stopTime, it->_index, lStrCatalogPath, lStrCatalogName, inputCatalog.getIntervalNumber());
for (auto &desc : inputCatalog.getParameterDescriptions())
lParameterManager->addInputIntervalDataList(it->_index, desc.getId(), lTimeIntervalList[it->_index].getParameterData(desc.getId()));
}
}
}
};
class ParamTimeRestrictionNode : public NodeCfg
{
public:
void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
ParameterManager* lParameterManager = pContext.get<ParameterManager*>();
xmlChar* lParamName = xmlGetProp(pNode, (const xmlChar *) "id");
std::string paramNameStr;
if (lParamName) {
paramNameStr = std::string((char*)lParamName);
xmlFree(lParamName);
}
else {
ERROR_EXCEPTION( ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@id")
}
xmlChar* lRestrictionTime = xmlGetProp(pNode, (const xmlChar *) "restriction");
double restrictionTime = NAN;
if (lRestrictionTime) {
std::string restrictionTimeStr = std::string((char*)lRestrictionTime);
xmlFree(lRestrictionTime);
try
{
restrictionTime = boost::lexical_cast<double>(restrictionTimeStr);
}
catch (boost::bad_lexical_cast &)
{
}
}
else {
ERROR_EXCEPTION( ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@restriction")
}
lParameterManager->addParamTimeRestriction(paramNameStr, restrictionTime);
}
};
XMLRequestParser::XMLRequestParser(const char* pXSDFile) : XMLConfigurator(pXSDFile,false) {
/*
<request>
<params>
<param id='imf'/>
<param id='dst'/>
</params>
<times>
<interval>
<startTime>2008000000000000</startTime>
<timeInterval>0000000100000000</timeInterval>
<interval>
</times>
<paramsTimeRestrictions>
<param id="imf" restriction="1200441599"/>
</paramsTimeRestrictions>
<outputs>
<download>
<timeFormat>ISO</timeFormat>
<param id='imf'/>
</download>
<download>
<timeFormat>ISO</timeFormat>
<param id='dst'/>
</download>
</outputs>
</request>
*/
// Request root node
NodeGrpCfg* lRequestNode = new NodeGrpCfg();
getXmlConfiguratorMap()["request"] = RootNodeCfgSPtr(lRequestNode);
{
// Params group node
NodeGrpCfg* lParamsNode = new NodeGrpCfg();
lRequestNode->getChildList()["params"] = NodeCfgSPtr(lParamsNode);
lParamsNode->getChildList()["param"] = NodeCfgSPtr(new ParamNode);
}
{
// Times group node
NodeGrpCfg* lTimeNode = new NodeGrpCfg();
lRequestNode->getChildList()["times"] = NodeCfgSPtr(lTimeNode);
{
// Interval group node
NodeGrpCfg* lIntervalNode = new IntervalNode();
lTimeNode->getChildList()["interval"] = NodeCfgSPtr(lIntervalNode);
}
{
// Timetable group node
lTimeNode->getChildList()["timetable"] = NodeCfgSPtr(
new TimeTableNode);
}
{
// Catalog group node
lTimeNode->getChildList()["catalog"] = NodeCfgSPtr(
new CatalogNode);
}
}
{
// Params time restrictions node
NodeGrpCfg* lParamsTimeRestrictionsNode = new NodeGrpCfg();
lRequestNode->getChildList()["paramsTimeRestrictions"] = NodeCfgSPtr(lParamsTimeRestrictionsNode);
lParamsTimeRestrictionsNode->getChildList()["param"] = NodeCfgSPtr(new ParamTimeRestrictionNode);
}
{
// Output group node
NodeGrpCfg* lOutputNode = new NodeGrpCfg();
lRequestNode->getChildList()["outputs"] = NodeCfgSPtr(lOutputNode);
}
}
XMLRequestParser::~XMLRequestParser() {
}
void XMLRequestParser::operator()( AMDA::Parameters::ParameterManager& lParameterManager, const std::string& requestFile)
{
CfgContext ctx;
ctx.push<ParameterManager*>(&lParameterManager);
ctx.push<ServicesServer*>(ServicesServer::getInstance());
try {
XMLConfigurator::proceed(requestFile.c_str(), ctx, false);
} catch (AMDA::AMDA_exception & e) {
e << AMDA::errno_code(AMDA_INFORMATION_REQUEST_ERR);
throw;
}
}
} /* namespace XMLRequest */
} /* namespace AMDA */