DataSetParser.cc 7.19 KB
/*
 * DataSetParser.cc
 *
 *  Created on: Oct 6, 2014
 *      Author: m.mazel
 */

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

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace Info {

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

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

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset source node
 */
class DataSetSourceNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setSource((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset global start node
 */
class DataSetGlobalStartNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setGlobalStart((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset global stop node
 */
class DataSetGlobalStopNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setGlobalStop((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset min sampling node
 */
class DataSetMinSamplingNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setMinSampling(atoi ((const char*)pNode->children->content));
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset max sampling node
 */
class DataSetMaxSamplingNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setMaxSampling(atoi ((const char*)pNode->children->content));
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset caveats node
 */
class DataSetCaveatsNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setCaveats((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset acknowledgement node
 */
class DataSetAcknowledgementNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setAcknowledgement((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset instrument id node
 */
class DataSetInstrumentIdNode : public NodeCfg
{
public:
  void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
	  DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();
	  if ((pNode->children != NULL) && (pNode->children->content != NULL))
		  pDataSetInfo->setInstrumentId((const char*)pNode->children->content);
  }
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset info node
 */
class DataSetInfoNode : public NodeGrpCfg {
public:

	DataSetInfoNode () : NodeGrpCfg() {
		getChildList()["name"] = NodeCfgSPtr(new DataSetNameNode);
		getChildList()["description"] = NodeCfgSPtr(new DataSetDescriptionNode);
		getChildList()["source"] = NodeCfgSPtr(new DataSetSourceNode);
		getChildList()["global_start"] = NodeCfgSPtr(new DataSetGlobalStartNode);
		getChildList()["global_stop"] = NodeCfgSPtr(new DataSetGlobalStopNode);
		getChildList()["min_sampling"] = NodeCfgSPtr(new DataSetMinSamplingNode);
		getChildList()["max_sampling"] = NodeCfgSPtr(new DataSetMaxSamplingNode);
		getChildList()["caveats"] = NodeCfgSPtr(new DataSetCaveatsNode);
		getChildList()["acknowledgement"] = NodeCfgSPtr(new DataSetAcknowledgementNode);
		getChildList()["instrument_id"] = NodeCfgSPtr(new DataSetInstrumentIdNode);
	}

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

		DataSetInfo* pDataSetInfo =  pContext.get<DataSetInfo*>();

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

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

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief Dataset node parser
 */
DataSetParser::DataSetParser (const char* pXSDFile) : XMLConfigurator(pXSDFile,true)
{

	// DatasetInfo root node
	getXmlConfiguratorMap()["dataset"] = RootNodeCfgSPtr(new DataSetInfoNode ());
}

boost::shared_ptr<DataSetInfo> DataSetParser::parse (const std::string& dataSetFile) {
	LOG4CXX_INFO(gLogger, "DataSetParser::parse parsing " << dataSetFile);

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

	// Check schema validity and parse xml file
	try {
		if (!XMLConfigurator::proceed(dataSetFile.c_str(), ctx, true)) {
			return DataSetInfoSPtr();
		}
	}
	catch (...) {
		LOG4CXX_INFO(gLogger, "DataSetParser::parse error while parsing file " << dataSetFile);
		// Return a null ptr to DataSetInfo
		return DataSetInfoSPtr();
	}
	return dataSetInfo;
}

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