ParamTableNode.cc 8.05 KB
/**
 * ParamTableNode.cc
 *
 *  Created on: 10 oct. 2014
 *      Author: AKKA
 */

#include <sstream>
#include <algorithm>
#include <boost/shared_ptr.hpp>

#include "ParamTableNode.hh"

#include "Config.hh"
#include "Parameter.hh"
#include "ParamTable.hh"
#include "ParamInfo.hh"
#include "Constant.hh"

using namespace log4cxx;
using namespace AMDA::Parameters;
using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace Info {

/*
 * @brief Bounds table node
 */
class BoundsTable: public AMDA::XMLConfigurator::NodeCfg {
public:
	void proceed( xmlNodePtr pNode, const AMDA::Parameters::CfgContext& context) {
		LOG4CXX_DEBUG(gLogger, "BoundsTable::proceed");

		ParamInfo* pParamInfo =  context.get<ParamInfo*>();

		std::string paramId =  pParamInfo->getId();

		//Get name
		xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
		std::string name;
		if (value != NULL)
		{
			name = (char *)value;
			xmlFree(value);
		}

		//Get units
		value = xmlGetProp(pNode, (const xmlChar *) "units");
		std::string units;
		if (value != NULL)
		{
			units = (char *)value;
			xmlFree(value);
		}

		//Get dim
		value = xmlGetProp(pNode, (const xmlChar *) "dim");
		int dim = 0;
		if (value != NULL)
		{
			dim = atoi((char *)value);
			xmlFree(value);
		}

		xmlChar* lboundsName  = NULL;
		try {
			if (!(lboundsName = xmlGetProp(pNode, (const xmlChar *) "boundsName"))) {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@boundsName")
			}

			LOG4CXX_DEBUG(gLogger, "BoundsTable::proceed - Bounds name : " << lboundsName);

			boost::shared_ptr<ParamTable> table(new ParamBoundsTable(
					paramId.c_str(), (const char*)lboundsName));

			table->setName(name);
			table->setUnits(units);

			pParamInfo->addTable(dim, table);
		} catch (...) {
			if (lboundsName) {
				xmlFree(lboundsName);
			}
			throw;
		}
		if (lboundsName) {
			xmlFree(lboundsName);
		}
	}
};

/*
 * @brief Min / Max table node
 */
class MinMaxTable: public AMDA::XMLConfigurator::NodeCfg {
public:
	void proceed( xmlNodePtr pNode, const AMDA::Parameters::CfgContext& context) {
		LOG4CXX_DEBUG(gLogger, "MinMaxTable::proceed")

		ParamInfo* pParamInfo =  context.get<ParamInfo*>();

		std::string paramId =  pParamInfo->getId();

		//Get name
		xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
		std::string name;
		if (value != NULL)
		{
			name = (char *)value;
			xmlFree(value);
		}

		//Get units
		value = xmlGetProp(pNode, (const xmlChar *) "units");
		std::string units;
		if (value != NULL)
		{
			units = (char *)value;
			xmlFree(value);
		}

		//Get dim
		value = xmlGetProp(pNode, (const xmlChar *) "dim");
		int dim = 0;
		if (value != NULL)
		{
			dim = atoi((char *)value);
			xmlFree(value);
		}

		xmlChar* lminTableName = NULL;
		xmlChar* lmaxTableName = NULL;

		try {
			if (!(lminTableName = xmlGetProp(pNode, (const xmlChar *) "minName"))) {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@minName")
			}

			if (!(lmaxTableName = xmlGetProp(pNode, (const xmlChar *) "maxName"))) {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@maxName")
			}

			LOG4CXX_DEBUG(gLogger, "MinMaxTable::proceed - Min Table name : " << lminTableName << " - Max Table name : " << lmaxTableName);

			boost::shared_ptr<ParamTable> table(new ParamMinMaxTable(
					paramId.c_str(), (const char*)lminTableName, (const char*)lmaxTableName));

			table->setName(name);
			table->setUnits(units);

			pParamInfo->addTable(dim,table);

		} catch (...) {
			if (lminTableName)
				xmlFree(lminTableName);
			if (lmaxTableName)
				xmlFree(lmaxTableName);
			throw;
		}
		if (lminTableName)
			xmlFree(lminTableName);
		if (lmaxTableName)
			xmlFree(lmaxTableName);
	}
};

/*
 * @brief Center table node
 */
class CenterTable: public AMDA::XMLConfigurator::NodeCfg {
public:
	void proceed( xmlNodePtr pNode, const AMDA::Parameters::CfgContext& context) {
		LOG4CXX_DEBUG(gLogger, "CenterTable::proceed");

		ParamInfo* pParamInfo =  context.get<ParamInfo*>();

		std::string paramId =  pParamInfo->getId();

		//Get name
		xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
		std::string name;
		if (value != NULL)
		{
			name = (char *)value;
			xmlFree(value);
		}

		//Get units
		value = xmlGetProp(pNode, (const xmlChar *) "units");
		std::string units;
		if (value != NULL)
		{
			units = (char *)value;
			xmlFree(value);
		}

		//Get dim
		value = xmlGetProp(pNode, (const xmlChar *) "dim");
		int dim = 0;
		if (value != NULL)
		{
			dim = atoi((char *)value);
			xmlFree(value);
		}

		xmlChar* lcenterName  = NULL;
		xmlChar* lsize = NULL;
		try {
			if (!(lcenterName = xmlGetProp(pNode, (const xmlChar *) "centerName"))) {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@centerName")
			}

			LOG4CXX_DEBUG(gLogger, "BoundsTable::proceed - Center name : " << lcenterName);

			if (!(lsize = xmlGetProp(pNode, (const xmlChar *) "size"))) {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@size")
			}

			boost::shared_ptr<ParamTable> table(new ParamCenterTable(
					paramId.c_str(), (const char*)lcenterName, atof((const char*)lsize)));

			table->setName(name);
			table->setUnits(units);

			pParamInfo->addTable(dim, table);
		} catch (...) {
			if (lcenterName) {
				xmlFree(lcenterName);
			}
			if (lsize) {
				xmlFree(lsize);
			}
			throw;
		}
		if (lcenterName) {
			xmlFree(lcenterName);
		}
		if (lsize) {
			xmlFree(lsize);
		}
	}
};

/*
 * @brief Center table node with band width
 */
class CenterWidthTable: public AMDA::XMLConfigurator::NodeCfg {
public:
	void proceed( xmlNodePtr pNode, const AMDA::Parameters::CfgContext& context) {
		LOG4CXX_DEBUG(gLogger, "CenterWidthTable::proceed");

		ParamInfo* pParamInfo =  context.get<ParamInfo*>();

		std::string paramId =  pParamInfo->getId();

		//Get name
		xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
		std::string name;
		if (value != NULL)
		{
			name = (char *)value;
			xmlFree(value);
		}

		//Get units
		value = xmlGetProp(pNode, (const xmlChar *) "units");
		std::string units;
		if (value != NULL)
		{
			units = (char *)value;
			xmlFree(value);
		}

		//Get dim
		value = xmlGetProp(pNode, (const xmlChar *) "dim");
		int dim = 0;
		if (value != NULL)
		{
			dim = atoi((char *)value);
			xmlFree(value);
		}

		xmlChar* lcenterName  = NULL;
		xmlChar* lwidthName   = NULL;
		try {
			if (!(lcenterName = xmlGetProp(pNode, (const xmlChar *) "centerName"))) {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@centerName")
			}

			LOG4CXX_DEBUG(gLogger, "CenterWidthTable::proceed - Center name : " << lcenterName);

			if (!(lwidthName = xmlGetProp(pNode, (const xmlChar *) "widthName"))) {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@centerName")
			}

			LOG4CXX_DEBUG(gLogger, "CenterWidthTable::proceed - Width name : " << lwidthName);

			boost::shared_ptr<ParamTable> table(new ParamCenterWidthTable(
					paramId.c_str(), (const char*)lcenterName, (const char*)lwidthName));

			table->setName(name);
			table->setUnits(units);

			pParamInfo->addTable(dim, table);
		} catch (...) {
			if (lcenterName) {
				xmlFree(lcenterName);
			}
			if (lwidthName) {
				xmlFree(lwidthName);
			}
			throw;
		}
		if (lcenterName) {
			xmlFree(lcenterName);
		}
		if (lwidthName) {
			xmlFree(lwidthName);
		}
	}
};

/*
 * @brief table node
 */
ParamTableNode::ParamTableNode() : AMDA::XMLConfigurator::NodeGrpCfg()
{
	getChildList()["boundsTable"]=NodeCfgSPtr(new BoundsTable);
	getChildList()["minMaxTable"]=NodeCfgSPtr(new MinMaxTable);
	getChildList()["centerTable"]=NodeCfgSPtr(new CenterTable);
	getChildList()["centerWidthTable"]=NodeCfgSPtr(new CenterWidthTable);
}

ParamTableNode::~ParamTableNode()
{
}

void ParamTableNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext)
{
	LOG4CXX_DEBUG(gLogger, "ParamTableNode::proceed");

	AMDA::Parameters::CfgContext lContext(pContext);
	ParamInfo* pParamInfo =  pContext.get<ParamInfo*>();
	lContext.push<ParamInfo *>(pParamInfo);

	NodeGrpCfg::proceed(pNode, lContext);
}

}/* namespace Info */
} /* namespace AMDA */