Blame view

src/XMLRequest/XMLRequestParser.cc 10.8 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
/*
 * 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*>();
fbe3c2bb   Benjamin Renard   First commit
46
47

		try {
c6a67968   Benjamin Renard   Fix some violatio...
48
49
50
51
52
53
54
			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
55
56
				ERROR_EXCEPTION( ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << PARAMNAME)
			}
c6a67968   Benjamin Renard   Fix some violatio...
57
			lParameterManager->createParameter(paramNameStr.c_str());
fbe3c2bb   Benjamin Renard   First commit
58
59
		} catch (...) {
			//This parameter is ignored, continue
fbe3c2bb   Benjamin Renard   First commit
60
61
//			throw;
		}
fbe3c2bb   Benjamin Renard   First commit
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
  }
};

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 ...
110
111
		std::string emptyStr;
		lParameterManager->addInputInterval(_startTime, _startTime + _timeInt, 0, emptyStr, emptyStr, 0);
fbe3c2bb   Benjamin Renard   First commit
112
113
114
115
116
117
118
119
120
121
	}

	double _startTime;
	double _timeInt;
};

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

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

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

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

	  // 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 ...
154
155
156
	  if((lStrTTPath.find("http://")!=std::string::npos
			  && lStrTTPath.find("https://")!=std::string::npos)
			  && !boost::filesystem::exists(lStrTTPath)){
fbe3c2bb   Benjamin Renard   First commit
157
		  ERROR_EXCEPTION(
897858c8   Benjamin Renard   Support multi TT ...
158
				 AMDA::XMLConfigurator::ERROR_FILE_NOT_EXIST << lStrTTPath);
fbe3c2bb   Benjamin Renard   First commit
159
160
161
	  }

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

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

	  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 ...
185
			  				 "Cannot find index : " << index << " in TimeTable : " << lStrTTPath);
fbe3c2bb   Benjamin Renard   First commit
186
187
188
189
190
		  }

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

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

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

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

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

	  // 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 ...
240
241
242
	  if((lStrCatalogPath.find("http://")!=std::string::npos
			  && lStrCatalogPath.find("https://")!=std::string::npos)
			  && !boost::filesystem::exists(lStrCatalogPath)){
fbe3c2bb   Benjamin Renard   First commit
243
		  ERROR_EXCEPTION(
897858c8   Benjamin Renard   Support multi TT ...
244
				 AMDA::XMLConfigurator::ERROR_FILE_NOT_EXIST << lStrCatalogPath);
fbe3c2bb   Benjamin Renard   First commit
245
246
247
	  }

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

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

	  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 ...
271
			  				 "Cannot find index : " << index << " in Catalog : " << lStrCatalogPath);
fbe3c2bb   Benjamin Renard   First commit
272
273
274
275
276
		  }

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

		  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 ...
292
			  lParameterManager->addInputInterval(it->_startTime, it->_stopTime, it->_index, lStrCatalogPath, lStrCatalogName, inputCatalog.getIntervalNumber());
fbe3c2bb   Benjamin Renard   First commit
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
			  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 {
48d596b2   brenard   Do not log an err...
371
		XMLConfigurator::proceed(requestFile.c_str(), ctx, false);
fbe3c2bb   Benjamin Renard   First commit
372
373
374
375
376
377
378
379
380
	} catch (AMDA::AMDA_exception & e) {
		e << AMDA::errno_code(AMDA_INFORMATION_REQUEST_ERR);
		throw;
	}

}

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