SpiceKernelConfigParser.cc 3.66 KB
/*
 * SpiceKernelConfigParser.cc
 *
 *  Created on: Jul 8, 2016
 *      Author: AKKA IS
 */

#include "NodeCfg.hh"
#include "AMDA_exception.hh"
#include "SpiceKernelConfigParser.hh"
#include "SpiceKernelLogger.hh"

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace SpiceKernel {

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief SpiceKernel data file node
 */
class DataFileNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  SpiceKernelConfig* pSpiceKernelConfig =  pContext.get<SpiceKernelConfig*>();

	  std::string path;
	  std::string bodyId;

	  // read path
	  xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "path");
	  if (value) {
		path = (const char*) value;
	  	xmlFree(value);
	  }
	  else {
		  LOG4CXX_ERROR(gLogger, "DataFileNode::proceed - Cannot load data file path");
	  }

	  // read bodyId
	  value = xmlGetProp(pNode, (const xmlChar *) "bodyId");
	  if (value) {
		  bodyId = (const char*) value;
		  	xmlFree(value);
	  }
	  else {
		  LOG4CXX_ERROR(gLogger, "DataFileNode::proceed - Cannot load data file related body id");
	  }

	  if (!path.empty() && !bodyId.empty())
		  pSpiceKernelConfig->addDataFileForBodyId(atoi(bodyId.c_str()), path.c_str());
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief SpiceKernel data files node
 */
class DataFilesNode : public NodeGrpCfg
{
public:
	DataFilesNode () : NodeGrpCfg() {
		getChildList()["datafile"] = NodeCfgSPtr(new DataFileNode());
	}


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

		SpiceKernelConfig* pSpiceKernelConfig =  pContext.get<SpiceKernelConfig*>();

		// read rootPath
		xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "rootPath");
		if (value) {
			pSpiceKernelConfig->setRootPath ((const char*) value);
			xmlFree(value);
		}

		// Proceed nodes
		NodeGrpCfg::proceed(pNode, pContext);
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief SpiceKernel config node
 */
class SpiceKernelConfigNode : public NodeGrpCfg {
public:

	SpiceKernelConfigNode () : NodeGrpCfg() {
		getChildList()["datafiles"] = NodeCfgSPtr(new DataFilesNode());
	}

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

		SpiceKernelConfig* pSpiceKernelConfig =  pContext.get<SpiceKernelConfig*>();

		// Proceed nodes
		NodeGrpCfg::proceed(pNode, pContext);
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset node parser
 */
SpiceKernelConfigParser::SpiceKernelConfigParser (const char* pXSDFile) : XMLConfigurator(pXSDFile,false)
{
	// SpiceKernel config root node
	getXmlConfiguratorMap()["spicekernelconfig"] = RootNodeCfgSPtr(new SpiceKernelConfigNode());
}

boost::shared_ptr<SpiceKernelConfig> SpiceKernelConfigParser::parse (const std::string& configFile) {
	LOG4CXX_INFO(gLogger, "SpiceKernelConfigParser::parse parsing " << configFile);

	AMDA::Parameters::CfgContext ctx;
	boost::shared_ptr<SpiceKernelConfig> spiceKernelConfig (new SpiceKernelConfig ());
	ctx.push<SpiceKernelConfig *>(spiceKernelConfig.get());

	// Check schema validity and parse xml file
	try {
		XMLConfigurator::proceed(configFile.c_str(), ctx);
	}
	catch (...) {
		LOG4CXX_INFO(gLogger, "SpiceKernelConfigParser::parse error while parsing file " << configFile);
		// Return a null ptr to DataSetInfo
		return boost::shared_ptr<SpiceKernelConfig>();
	}
	return spiceKernelConfig;
}

} /* namespace SpiceKernel */
} /* namespace AMDA */