ConstantsParser.cc 2.45 KB
/*
 * 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 */