Blame view

src/XMLRequest/XMLRequestParser.cc 12.2 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
/*
 * 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>
193183ca   Benjamin Renard   Give the possibil...
31
#include <boost/lexical_cast.hpp>
fbe3c2bb   Benjamin Renard   First commit
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

// 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*>();
fbe3c2bb   Benjamin Renard   First commit
47
48

		try {
c6a67968   Benjamin Renard   Fix some violatio...
49
50
51
52
53
54
55
			xmlChar* lParamName = xmlGetProp(pNode, (const xmlChar *) "id");
			std::string paramNameStr;
			if (lParamName) {
				paramNameStr = std::string((char*)lParamName);
				xmlFree(lParamName);
			}
			else {
fbe3c2bb   Benjamin Renard   First commit
56
57
				ERROR_EXCEPTION( ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << PARAMNAME)
			}
c6a67968   Benjamin Renard   Fix some violatio...
58
			lParameterManager->createParameter(paramNameStr.c_str());
fbe3c2bb   Benjamin Renard   First commit
59
60
		} catch (...) {
			//This parameter is ignored, continue
fbe3c2bb   Benjamin Renard   First commit
61
62
//			throw;
		}
fbe3c2bb   Benjamin Renard   First commit
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
  }
};

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*>();

897858c8   Benjamin Renard   Support multi TT ...
111
112
		std::string emptyStr;
		lParameterManager->addInputInterval(_startTime, _startTime + _timeInt, 0, emptyStr, emptyStr, 0);
fbe3c2bb   Benjamin Renard   First commit
113
114
115
116
117
118
119
120
121
122
	}

	double _startTime;
	double _timeInt;
};

class TimeTableNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
897858c8   Benjamin Renard   Support multi TT ...
123
124
	  //TT Id => TT path
	  xmlChar* lTTId = NULL;
fbe3c2bb   Benjamin Renard   First commit
125
126

	  ParameterManager* lParameterManager =  pContext.get<ParameterManager*>();
897858c8   Benjamin Renard   Support multi TT ...
127
	  if (!(lTTId = xmlGetProp(pNode, (const xmlChar *) "id"))) {
fbe3c2bb   Benjamin Renard   First commit
128
129
130
		  ERROR_EXCEPTION(
				 AMDA::XMLConfigurator::ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << AMDA::XMLConfigurator::TIMETABLENAME)
	  }
897858c8   Benjamin Renard   Support multi TT ...
131
	  std::string lStrTTPath((const char*)lTTId);
fbe3c2bb   Benjamin Renard   First commit
132

897858c8   Benjamin Renard   Support multi TT ...
133
134
	  if(lTTId){
		  xmlFree(lTTId);
fbe3c2bb   Benjamin Renard   First commit
135
136
	  }

897858c8   Benjamin Renard   Support multi TT ...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
	  //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);
	  }
fbe3c2bb   Benjamin Renard   First commit
152
153
154

	  // check local file exists
	  // if distant file does not exist an error will be thrown in the next if
897858c8   Benjamin Renard   Support multi TT ...
155
156
157
	  if((lStrTTPath.find("http://")!=std::string::npos
			  && lStrTTPath.find("https://")!=std::string::npos)
			  && !boost::filesystem::exists(lStrTTPath)){
fbe3c2bb   Benjamin Renard   First commit
158
		  ERROR_EXCEPTION(
897858c8   Benjamin Renard   Support multi TT ...
159
				 AMDA::XMLConfigurator::ERROR_FILE_NOT_EXIST << lStrTTPath);
fbe3c2bb   Benjamin Renard   First commit
160
161
162
	  }

	  // Read file
897858c8   Benjamin Renard   Support multi TT ...
163
	  std::string lReaderType = TimeTableCatalogFactory::getInstance().getReaderType(lStrTTPath);
fbe3c2bb   Benjamin Renard   First commit
164
165
	  if(lReaderType.empty()){
		  ERROR_EXCEPTION(
897858c8   Benjamin Renard   Support multi TT ...
166
				 "Unknown Timetable : " << lStrTTPath);
fbe3c2bb   Benjamin Renard   First commit
167
168
169

	  }
	  TimeTableCatalog::TimeTable inputTT;
897858c8   Benjamin Renard   Support multi TT ...
170
	  inputTT.read(lStrTTPath, lReaderType);
fbe3c2bb   Benjamin Renard   First commit
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

	  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(
897858c8   Benjamin Renard   Support multi TT ...
186
			  				 "Cannot find index : " << index << " in TimeTable : " << lStrTTPath);
fbe3c2bb   Benjamin Renard   First commit
187
188
189
190
191
		  }

		  lParameterManager->addInputInterval(
				  lTimeIntervalList[index]._startTime,
				  lTimeIntervalList[index]._stopTime,
897858c8   Benjamin Renard   Support multi TT ...
192
193
				  lTimeIntervalList[index]._index,
				  lStrTTPath, lStrTTName, inputTT.getIntervalNumber());
fbe3c2bb   Benjamin Renard   First commit
194
195
196
197
198
	  }
	  else
	  {
		  //Add all intervals
		  for(std::vector<TimeInterval>::const_iterator it = lTimeIntervalList.begin(); it != lTimeIntervalList.end(); ++it) {
897858c8   Benjamin Renard   Support multi TT ...
199
			  lParameterManager->addInputInterval(it->_startTime, it->_stopTime, it->_index, lStrTTPath, lStrTTName, inputTT.getIntervalNumber());
fbe3c2bb   Benjamin Renard   First commit
200
201
202
203
204
205
206
207
208
		  }
	  }
  }
};

class CatalogNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
897858c8   Benjamin Renard   Support multi TT ...
209
210
	  //Catalog Id => catalog path
	  xmlChar* lCatalogId = NULL;
fbe3c2bb   Benjamin Renard   First commit
211
212

	  ParameterManager* lParameterManager =  pContext.get<ParameterManager*>();
897858c8   Benjamin Renard   Support multi TT ...
213
	  if (!(lCatalogId = xmlGetProp(pNode, (const xmlChar *) "id"))) {
fbe3c2bb   Benjamin Renard   First commit
214
215
216
		  ERROR_EXCEPTION(
				 AMDA::XMLConfigurator::ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << AMDA::XMLConfigurator::TIMETABLENAME)
	  }
897858c8   Benjamin Renard   Support multi TT ...
217
	  std::string lStrCatalogPath((const char*)lCatalogId);
fbe3c2bb   Benjamin Renard   First commit
218

897858c8   Benjamin Renard   Support multi TT ...
219
220
	  if(lCatalogId){
		  xmlFree(lCatalogId);
fbe3c2bb   Benjamin Renard   First commit
221
222
	  }

897858c8   Benjamin Renard   Support multi TT ...
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
	  //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);
	  }
fbe3c2bb   Benjamin Renard   First commit
238
239
240

	  // check local file exists
	  // if distant file does not exist an error will be thrown in the next if
897858c8   Benjamin Renard   Support multi TT ...
241
242
243
	  if((lStrCatalogPath.find("http://")!=std::string::npos
			  && lStrCatalogPath.find("https://")!=std::string::npos)
			  && !boost::filesystem::exists(lStrCatalogPath)){
fbe3c2bb   Benjamin Renard   First commit
244
		  ERROR_EXCEPTION(
897858c8   Benjamin Renard   Support multi TT ...
245
				 AMDA::XMLConfigurator::ERROR_FILE_NOT_EXIST << lStrCatalogPath);
fbe3c2bb   Benjamin Renard   First commit
246
247
248
	  }

	  // Read file
897858c8   Benjamin Renard   Support multi TT ...
249
	  std::string lReaderType = TimeTableCatalogFactory::getInstance().getReaderType(lStrCatalogPath);
fbe3c2bb   Benjamin Renard   First commit
250
251
	  if(lReaderType.empty()){
		  ERROR_EXCEPTION(
897858c8   Benjamin Renard   Support multi TT ...
252
				 "Unknown Catalog : " << lStrCatalogPath);
fbe3c2bb   Benjamin Renard   First commit
253
254
255

	  }
	  TimeTableCatalog::Catalog inputCatalog;
897858c8   Benjamin Renard   Support multi TT ...
256
	  inputCatalog.read(lStrCatalogPath, lReaderType);
fbe3c2bb   Benjamin Renard   First commit
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

	  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(
897858c8   Benjamin Renard   Support multi TT ...
272
			  				 "Cannot find index : " << index << " in Catalog : " << lStrCatalogPath);
fbe3c2bb   Benjamin Renard   First commit
273
274
275
276
277
		  }

		  lParameterManager->addInputInterval(
				  lTimeIntervalList[index]._startTime,
				  lTimeIntervalList[index]._stopTime,
897858c8   Benjamin Renard   Support multi TT ...
278
279
280
281
				  lTimeIntervalList[index]._index,
				  lStrCatalogPath,
				  lStrCatalogName,
				  inputCatalog.getIntervalNumber());
fbe3c2bb   Benjamin Renard   First commit
282
283
284
285
286
287
288
289
290
291
292

		  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)
		  {
897858c8   Benjamin Renard   Support multi TT ...
293
			  lParameterManager->addInputInterval(it->_startTime, it->_stopTime, it->_index, lStrCatalogPath, lStrCatalogName, inputCatalog.getIntervalNumber());
fbe3c2bb   Benjamin Renard   First commit
294
295
296
297
298
299
300
			  for (auto &desc : inputCatalog.getParameterDescriptions())
				  lParameterManager->addInputIntervalDataList(it->_index, desc.getId(), lTimeIntervalList[it->_index].getParameterData(desc.getId()));
		  }
	  }
  }
};

193183ca   Benjamin Renard   Give the possibil...
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
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);
  }
};

fbe3c2bb   Benjamin Renard   First commit
338
339
340
341
342
343
344
345
346
347
348
349
350
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>
193183ca   Benjamin Renard   Give the possibil...
351
352
353
	  <paramsTimeRestrictions>
	    <param id="imf" restriction="1200441599"/>
	  </paramsTimeRestrictions>
fbe3c2bb   Benjamin Renard   First commit
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
	  <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);
		}
	}
	{
193183ca   Benjamin Renard   Give the possibil...
396
397
398
399
400
401
		// Params time restrictions node
		NodeGrpCfg* lParamsTimeRestrictionsNode = new NodeGrpCfg();
		lRequestNode->getChildList()["paramsTimeRestrictions"] = NodeCfgSPtr(lParamsTimeRestrictionsNode);
		lParamsTimeRestrictionsNode->getChildList()["param"] = NodeCfgSPtr(new ParamTimeRestrictionNode);
	}
	{
fbe3c2bb   Benjamin Renard   First commit
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
		// 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 {
48d596b2   brenard   Do not log an err...
418
		XMLConfigurator::proceed(requestFile.c_str(), ctx, false);
fbe3c2bb   Benjamin Renard   First commit
419
420
421
422
423
424
425
426
427
	} catch (AMDA::AMDA_exception & e) {
		e << AMDA::errno_code(AMDA_INFORMATION_REQUEST_ERR);
		throw;
	}

}

} /* namespace XMLRequest */
} /* namespace AMDA */