ConstantsParser.cc
2.45 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
/*
* ConstantsParser.cc
*
* Created on: Mar 18, 2019
* Author: AKKA
*/
#include "NodeCfg.hh"
#include "AMDA_exception.hh"
#include "ConstantsParser.hh"
#include "ParserLogger.hh"
#include <boost/lexical_cast.hpp>
using namespace AMDA::XMLConfigurator;
namespace AMDA {
namespace parser {
///////////////////////////////////////////////////////////////////////////////
/*
* @brief constant id node
*/
class ConstantNode : public NodeCfg
{
public:
void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
std::string val;
if ((pNode->children != NULL) && (pNode->children->content != NULL)) {
val = std::string((char*)pNode->children->content);
}
//Get name
xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
std::string name;
if (value != NULL)
{
name = "@";
name += (char *)value;
xmlFree(value);
}
if (!name.empty()) {
std::map<std::string, std::string> * pConstantsMap = pContext.get<std::map<std::string, std::string> *>();
(*pConstantsMap)[name] = val;
}
}
};
///////////////////////////////////////////////////////////////////////////////
/*
* @brief constants node
*/
class ConstantsNode : public NodeGrpCfg {
public:
ConstantsNode () : NodeGrpCfg() {
getChildList()["constant"] = NodeCfgSPtr(new ConstantNode);
}
void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
LOG4CXX_INFO(gLogger, "ConstantsNode::proceed");
// Proceed nodes
NodeGrpCfg::proceed(pNode, pContext);
}
};
///////////////////////////////////////////////////////////////////////////////
/*
* @brief constants node parser
*/
ConstantsParser::ConstantsParser (const char* pXSDFile) : XMLConfigurator(pXSDFile,true)
{
// constants root node
getXmlConfiguratorMap()["constants"] = RootNodeCfgSPtr(new ConstantsNode ());
}
std::map<std::string, std::string> ConstantsParser::parse (const std::string& constantsFile) {
LOG4CXX_INFO(gLogger, "ConstantsParser::parse parsing " << constantsFile);
AMDA::Parameters::CfgContext ctx;
std::map<std::string, std::string> constantsMap;
ctx.push<std::map<std::string, std::string> *>(&constantsMap);
// Check schema validity and parse xml file
try {
if (!XMLConfigurator::proceed(constantsFile.c_str(), ctx, true)) {
return constantsMap;
}
}
catch (...) {
LOG4CXX_INFO(gLogger, "ConstantsParser::parse error while parsing file " << constantsFile);
return constantsMap;
}
return constantsMap;
}
} /* namespace parser */
} /* namespace AMDA */