InstrumentParser.cc 5.75 KB
/*
 * InstrumentParser.cc
 *
 *  Created on: Oct 6, 2014
 *      Author: m.mazel
 */

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

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace Info {

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

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

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Instrument url node
 */
class InstrumentUrlNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  InstrumentInfo* pInstrumentInfo =  pContext.get<InstrumentInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pInstrumentInfo->setUrl((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Instrument measurement node
 */
class InstrumentMeasurementNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  InstrumentInfo* pInstrumentInfo =  pContext.get<InstrumentInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pInstrumentInfo->setMeasurement((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Instrument PI node
 */
class InstrumentPiNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  InstrumentInfo* pInstrumentInfo =  pContext.get<InstrumentInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pInstrumentInfo->setPi((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Intrument type node
 */
class InstrumentInstrumentTypeNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  InstrumentInfo* pInstrumentInfo =  pContext.get<InstrumentInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pInstrumentInfo->setInstrumentType((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Instrument mission id node
 */
class InstrumentMissionIdNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  InstrumentInfo* pInstrumentInfo =  pContext.get<InstrumentInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pInstrumentInfo->setMissionId((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Instrument node
 */
class InstrumentInfoNode : public NodeGrpCfg {
public:

	InstrumentInfoNode () : NodeGrpCfg() {
		getChildList()["name"] = NodeCfgSPtr(new InstrumentNameNode);
		getChildList()["description"] = NodeCfgSPtr(new InstrumentDescriptionNode);
		getChildList()["url"] = NodeCfgSPtr(new InstrumentUrlNode);
		getChildList()["measurement_type"] = NodeCfgSPtr(new InstrumentMeasurementNode);
		getChildList()["pi"] = NodeCfgSPtr(new InstrumentPiNode);
		getChildList()["instrument_type"] = NodeCfgSPtr(new InstrumentInstrumentTypeNode);
		getChildList()["mission_id"] = NodeCfgSPtr(new InstrumentMissionIdNode);
	}

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

		InstrumentInfo* pInstrumentInfo =  pContext.get<InstrumentInfo*>();

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

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

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Parser of instrument info
 */
InstrumentParser::InstrumentParser (const char* pXSDFile) : XMLConfigurator(pXSDFile,true)
{

	// InstrumentInfo root node
	getXmlConfiguratorMap()["instrument"] = RootNodeCfgSPtr(new InstrumentInfoNode ());
}

boost::shared_ptr<InstrumentInfo> InstrumentParser::parse (const std::string& instrumentFile) {
	LOG4CXX_INFO(gLogger, "InstrumentParser::parse parsing " << instrumentFile);

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

	// Check schema validity and parse xml file
	try {
		if (!XMLConfigurator::proceed(instrumentFile.c_str(), ctx, true)) {
			return InstrumentInfoSPtr();
		}
	}
	catch (...) {
		LOG4CXX_INFO(gLogger, "InstrumentParser::parse error while parsing file " << instrumentFile);
		// Return a null ptr to InstrumentInfo
		return InstrumentInfoSPtr();
	}
	return instrumentInfo;
}

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