TemplateParamsParser.cc 6.98 KB
/*
 * 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("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 */