MissionParser.cc 3.36 KB
/*
 * MissionParser.cc
 *
 *  Created on: Oct 6, 2014
 *      Author: m.mazel
 */

#include "NodeCfg.hh"
#include "AMDA_exception.hh"
#include "MissionParser.hh"
#include "InfoLogger.hh"

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace Info {

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Mission name node
 */
class MissionNameNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  MissionInfo* pMissionInfo =  pContext.get<MissionInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pMissionInfo->setName((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Mission description node
 */
class MissionDescriptionNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  MissionInfo* pMissionInfo =  pContext.get<MissionInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pMissionInfo->setDescription((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Mission URL node
 */
class MissionUrlNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  MissionInfo* pMissionInfo =  pContext.get<MissionInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pMissionInfo->setUrl((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Mission info node
 */
class MissionInfoNode : public NodeGrpCfg {
public:

	MissionInfoNode () : NodeGrpCfg() {
		getChildList()["name"] = NodeCfgSPtr(new MissionNameNode);
		getChildList()["description"] = NodeCfgSPtr(new MissionDescriptionNode);
		getChildList()["url"] = NodeCfgSPtr(new MissionUrlNode);
	}

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

		MissionInfo* pMissionInfo =  pContext.get<MissionInfo*>();

		// read mission id
		xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "id");
		if (value) {
			pMissionInfo->setId ((const char*) value);
			xmlFree(value);
		}

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

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Mission info parser
 */
MissionParser::MissionParser (const char* pXSDFile) : XMLConfigurator(pXSDFile, true)
{

	// MissionInfo root node
	getXmlConfiguratorMap()["mission"] = RootNodeCfgSPtr(new MissionInfoNode ());
}

boost::shared_ptr<MissionInfo> MissionParser::parse (const std::string& missionFile) {
	LOG4CXX_INFO(gLogger, "MissionParser::parse parsing " << missionFile);

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

	// Check schema validity and parse xml file
	try {
		XMLConfigurator::proceed(missionFile.c_str(), ctx);
	}
	catch (...) {
		LOG4CXX_INFO(gLogger, "MissionParser::parse error while parsing file " << missionFile);
		// Return a null ptr to MissionInfo
		return MissionInfoSPtr();
	}
	return missionInfo;
}

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