XMLRequestParser.cc
10.8 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
/*
* 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>
// 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*>();
xmlChar* lParamName = NULL;
try {
if (!(lParamName = xmlGetProp(pNode, (const xmlChar *) "id"))) {
ERROR_EXCEPTION( ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << PARAMNAME)
}
lParameterManager->createParameter(std::string((char*)lParamName));
} catch (...) {
//This parameter is ignored, continue
// if (lParamName) {
// xmlFree(lParamName);
// }
// throw;
}
if (lParamName) {
xmlFree(lParamName);
}
}
};
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
size_t index = atoi( (const char*)lTTIndex );
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
size_t index = atoi( (const char*)lCatalogIndex );
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()));
}
}
}
};
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>
<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);
}
}
{
// 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);
} catch (AMDA::AMDA_exception & e) {
e << AMDA::errno_code(AMDA_INFORMATION_REQUEST_ERR);
throw;
}
}
} /* namespace XMLRequest */
} /* namespace AMDA */