TemplateParamsParser.cc
7.08 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
/*
* TemplateParamsParser.cc
*
* Created on: May 10, 2019
* Author: AKKA
*/
#include "NodeCfg.hh"
#include "AMDA_exception.hh"
#include "TemplateParamsParser.hh"
#include "ParserLogger.hh"
#include <boost/lexical_cast.hpp>
using namespace AMDA::XMLConfigurator;
namespace AMDA {
namespace parser {
///////////////////////////////////////////////////////////////////////////////
/*
* @brief item node
*/
class ParamTemplateArgItemNode : public NodeCfg
{
public:
void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
ArgumentItem item;
//Get key
xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "key");
std::string key;
if (value != NULL)
{
key = (char *)value;
xmlFree(value);
}
item.setKey(key);
//Get name
value = xmlGetProp(pNode, (const xmlChar *) "name");
std::string name;
if (value != NULL)
{
name = (char *)value;
xmlFree(value);
}
item.setName(name);
//Add item in argument
TemplateAgrument *pArg = pContext.get<TemplateAgrument *>();
pArg->addItem(item);
}
};
///////////////////////////////////////////////////////////////////////////////
/*
* @brief argument node
*/
class ParamTemplateArgNode : public NodeGrpCfg
{
public:
ParamTemplateArgNode () : NodeGrpCfg() {
getChildList()["item"] = NodeCfgSPtr(new ParamTemplateArgItemNode);
}
void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
TemplateAgrument arg;
//Get key
xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "key");
std::string key;
if (value != NULL)
{
key = (char *)value;
xmlFree(value);
}
arg.setKey(key);
//Get name
value = xmlGetProp(pNode, (const xmlChar *) "name");
std::string name;
if (value != NULL)
{
name = (char *)value;
xmlFree(value);
}
arg.setName(name);
//Get type
value = xmlGetProp(pNode, (const xmlChar *) "type");
TemplateAgrument::ArgumentType type = TemplateAgrument::ArgumentType::AT_STRING;
if (value != NULL)
{
std::string typeStr;
typeStr = (char *)value;
if (typeStr.compare("string") == 0) {
type = TemplateAgrument::ArgumentType::AT_STRING;
}
else if (typeStr.compare("int") == 0) {
type = TemplateAgrument::ArgumentType::AT_INT;
}
else if (typeStr.compare("float") == 0) {
type = TemplateAgrument::ArgumentType::AT_FLOAT;
}
else if (typeStr.compare("date") == 0) {
type = TemplateAgrument::ArgumentType::AT_DATE;
}
else if (typeStr.compare("bool") == 0) {
type = TemplateAgrument::ArgumentType::AT_BOOL;
}
else if (typeStr.compare("list") == 0) {
type = TemplateAgrument::ArgumentType::AT_LIST;
}
else if (typeStr.compare("generated-list") == 0) {
type = TemplateAgrument::ArgumentType::AT_GENERATED_LIST;
}
xmlFree(value);
}
arg.setType(type);
//Get default
value = xmlGetProp(pNode, (const xmlChar *) "default");
std::string def;
if (value != NULL)
{
def = (char *)value;
xmlFree(value);
}
arg.setDefault(def);
if (type == TemplateAgrument::ArgumentType::AT_GENERATED_LIST) {
//Generate items
std::string nameTpl;
int minKey = 0;
int maxKey = 0;
//Get nametpl
value = xmlGetProp(pNode, (const xmlChar *) "nametpl");
if (value != NULL)
{
nameTpl = (char *)value;
xmlFree(value);
}
//Get minkey
value = xmlGetProp(pNode, (const xmlChar *) "minkey");
if (value != NULL)
{
minKey = boost::lexical_cast<int>(std::string((char*)value));
xmlFree(value);
}
//Get maxkey
value = xmlGetProp(pNode, (const xmlChar *) "maxkey");
if (value != NULL)
{
maxKey = boost::lexical_cast<int>(std::string((char*)value));
xmlFree(value);
}
arg.generateAutoItems(nameTpl, minKey, maxKey);
}
else {
//Load items
AMDA::Parameters::CfgContext ctx;
ctx.push<TemplateAgrument *>(&arg);
NodeGrpCfg::proceed(pNode, ctx);
}
//Add argument in template param
TemplateParam *pTemplateParam = pContext.get<TemplateParam *>();
pTemplateParam->addArgument(arg);
}
};
///////////////////////////////////////////////////////////////////////////////
/*
* @brief arguments node
*/
class ParamTemplateArgsNode : public NodeGrpCfg
{
public:
ParamTemplateArgsNode () : NodeGrpCfg() {
getChildList()["argument"] = NodeCfgSPtr(new ParamTemplateArgNode);
}
void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
// Proceed nodes
NodeGrpCfg::proceed(pNode, pContext);
}
};
///////////////////////////////////////////////////////////////////////////////
/*
* @brief paramTemplate node
*/
class ParamTemplateNode : public NodeGrpCfg
{
public:
ParamTemplateNode () : NodeGrpCfg() {
getChildList()["arguments"] = NodeCfgSPtr(new ParamTemplateArgsNode);
}
void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
//Get paramId
xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "paramId");
std::string paramId;
if (value != NULL)
{
paramId = (char *)value;
xmlFree(value);
}
//Get fileName
value = xmlGetProp(pNode, (const xmlChar *) "fileName");
std::string fileName;
if (value != NULL)
{
fileName = (char *)value;
xmlFree(value);
}
if (!paramId.empty() && !fileName.empty()) {
AMDA::Parameters::CfgContext ctx;
TemplateParam templateParam;
ctx.push<TemplateParam *>(&templateParam);
templateParam.setParamId(paramId);
templateParam.setFileName(fileName);
// Proceed nodes
NodeGrpCfg::proceed(pNode, ctx);
//Add template param in list
TemplateParamsList *pTemplateParamsList = pContext.get<TemplateParamsList *>();
pTemplateParamsList->push_back(templateParam);
}
}
};
///////////////////////////////////////////////////////////////////////////////
/*
* @brief paramTemplateList node
*/
class ParamTemplateListNode : public NodeGrpCfg {
public:
ParamTemplateListNode () : NodeGrpCfg() {
getChildList()["paramTemplate"] = NodeCfgSPtr(new ParamTemplateNode);
}
void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
LOG4CXX_INFO(gLogger, "ParamTemplateListNode::proceed");
// Proceed nodes
NodeGrpCfg::proceed(pNode, pContext);
}
};
///////////////////////////////////////////////////////////////////////////////
/*
* @brief template params node parser
*/
TemplateParamsParser::TemplateParamsParser (const char* pXSDFile) : XMLConfigurator(pXSDFile,true)
{
// paramTemplateList root node
getXmlConfiguratorMap()["paramTemplateList"] = RootNodeCfgSPtr(new ParamTemplateListNode ());
}
TemplateParamsList TemplateParamsParser::parse (const std::string& templateParamsFile) {
LOG4CXX_INFO(gLogger, "TemplateParamsParser::parse parsing " << templateParamsFile);
AMDA::Parameters::CfgContext ctx;
TemplateParamsList templateParamsList;
ctx.push<TemplateParamsList *>(&templateParamsList);
// Check schema validity and parse xml file
try {
if (!XMLConfigurator::proceed(templateParamsFile.c_str(), ctx, true)) {
return templateParamsList;
}
}
catch (...) {
LOG4CXX_INFO(gLogger, "TemplateParamsParser::parse error while parsing file " << templateParamsFile);
return templateParamsList;
}
return templateParamsList;
}
} /* namespace parser */
} /* namespace AMDA */