Blame view

src/expressionParser/ConstantsParser.cc 2.45 KB
7e9d20f7   Benjamin Renard   Add parser to loa...
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
/*
 * 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) {
e58d7be4   Benjamin Renard   Parser: Add const...
28
	std::string val;
7e9d20f7   Benjamin Renard   Add parser to loa...
29
	if ((pNode->children != NULL) && (pNode->children->content != NULL)) {
e58d7be4   Benjamin Renard   Parser: Add const...
30
		val = std::string((char*)pNode->children->content);
7e9d20f7   Benjamin Renard   Add parser to loa...
31
32
33
34
35
36
37
	}

	//Get name
	xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
	std::string name;
	if (value != NULL)
	{
e58d7be4   Benjamin Renard   Parser: Add const...
38
39
		name = "@";
		name += (char *)value;
7e9d20f7   Benjamin Renard   Add parser to loa...
40
41
42
43
		xmlFree(value);
	}

	if (!name.empty()) {
e58d7be4   Benjamin Renard   Parser: Add const...
44
		std::map<std::string, std::string> * pConstantsMap =  pContext.get<std::map<std::string, std::string> *>();
7e9d20f7   Benjamin Renard   Add parser to loa...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		(*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");

7e9d20f7   Benjamin Renard   Add parser to loa...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		// 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 ());
}

e58d7be4   Benjamin Renard   Parser: Add const...
80
std::map<std::string, std::string> ConstantsParser::parse (const std::string& constantsFile) {
7e9d20f7   Benjamin Renard   Add parser to loa...
81
82
83
	LOG4CXX_INFO(gLogger, "ConstantsParser::parse parsing " << constantsFile);

	AMDA::Parameters::CfgContext ctx;
e58d7be4   Benjamin Renard   Parser: Add const...
84
85
	std::map<std::string, std::string> constantsMap;
	ctx.push<std::map<std::string, std::string> *>(&constantsMap);
7e9d20f7   Benjamin Renard   Add parser to loa...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

	// 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 */