VirtualInstrumentBaseParser.cc 4.96 KB
/**
 * VirtualInstrumentBaseParser.cc
 *
 *  Created on: 21 nov. 2014
 *      Author: AKKA
 */

#include "VirtualInstrumentBaseParser.hh"

#include "LocalFileInterfaceConfig.hh"

// XML  include
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xmlstring.h>
#include <libxml/xmlschemas.h>

#include "NodeCfg.hh"
#include "Constant.hh"


using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace LocalFileInterface {

class LocalFileNode: public AMDA::XMLConfigurator::NodeCfg {
public:
	/**
	 * @brief read local file node
	 */
	void proceed(xmlNodePtr pNode,
			const AMDA::Parameters::CfgContext& context) {
		LOG4CXX_DEBUG(gLogger, "LocalFileNode::proceed")
		VirtualInstrument* lVI = context.get<VirtualInstrument*>();

		//name
		xmlChar* lName = xmlGetProp(pNode, (const xmlChar *) "name");
		if (lName == NULL)
		{
			ERROR_EXCEPTION(
					ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "name")
		}

		//start
		xmlChar* lStart = xmlGetProp(pNode, (const xmlChar *) "start");
		if (lStart == NULL)
		{
			xmlFree(lName);
			ERROR_EXCEPTION(
					ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "start")
		}

		//stop
		xmlChar* lStop = xmlGetProp(pNode, (const xmlChar *) "stop");
		if (lStop == NULL)
		{
			xmlFree(lName);
			xmlFree(lStart);
			ERROR_EXCEPTION(
					ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "stop")
		}

		lVI->addFileDefinition((const char *)lName,atof((const char*)lStart),atof((const char*)lStop));

		xmlFree(lName);
		xmlFree(lStart);
		xmlFree(lStop);
	}
};

class LocalVINode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	LocalVINode() : AMDA::XMLConfigurator::NodeGrpCfg() {
		getChildList()["file"]=NodeCfgSPtr(new LocalFileNode());
	}

	/**
	 * @brief read LocalBaseNode
	 */
	void proceed(xmlNodePtr pNode,	const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_DEBUG(gLogger, "LocalVINode::proceed")
		VirtualInstrument* lVI = pContext.get<VirtualInstrument*>();

		//id
		xmlChar* lVIId = NULL;
		if (!(lVIId = xmlGetProp(pNode, (const xmlChar *) "id")))
		{
			ERROR_EXCEPTION(
					ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "id")
		}

		std::string loadedVIId = (const char *)lVIId;

		xmlFree(lVIId);

		if (lVI->getVIId() != loadedVIId)
			return;

		//files format
		xmlChar* lformat = NULL;
		if (!(lformat = xmlGetProp(pNode, (const xmlChar *) "format")))
		{
			ERROR_EXCEPTION(
				ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "format")
		}

		std::string loadedVIFormat = (const char *)lformat;

		xmlFree(lformat);

		if (loadedVIFormat == "ASCII")
			lVI->setFilesFormat(VIFileFormat::FORMAT_ASCII);
		else if (loadedVIFormat == "CDF")
			lVI->setFilesFormat(VIFileFormat::FORMAT_CDF);
		else if (loadedVIFormat == "VOT")
			lVI->setFilesFormat(VIFileFormat::FORMAT_VOT);
		else if (loadedVIFormat == "NC")
			lVI->setFilesFormat(VIFileFormat::FORMAT_NETCDF);
		else
		{
			ERROR_EXCEPTION("Unknown format " << loadedVIFormat)
		}

		//start
		xmlChar* lStart = xmlGetProp(pNode, (const xmlChar *) "start");
		if (lStart == NULL)
		{
			ERROR_EXCEPTION(
					ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "start")
		}

		lVI->setGlobalStartTime(atof((const char*)lStart));
		xmlFree(lStart);

		//stop
		xmlChar* lStop = xmlGetProp(pNode, (const xmlChar *) "stop");
		if (lStop == NULL)
		{
			ERROR_EXCEPTION(
					ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "stop")
		}

		lVI->setGlobalStopTime(atof((const char*)lStop));
		xmlFree(lStop);

		AMDA::Parameters::CfgContext lContext;
		lContext.push<VirtualInstrument *>(lVI);
		NodeGrpCfg::proceed(pNode, lContext);
	}
};

class LocalBaseNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	LocalBaseNode() : AMDA::XMLConfigurator::NodeGrpCfg() {
		getChildList()["vi"]=NodeCfgSPtr(new LocalVINode());
	}

	/**
	 * @brief read LocalBaseNode
	 */
	void proceed(xmlNodePtr pNode,	const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_DEBUG(gLogger, "LocalBaseNode::proceed")
		VirtualInstrument* lVI = pContext.get<VirtualInstrument*>();

		AMDA::Parameters::CfgContext lContext;
		lContext.push<VirtualInstrument *>(lVI);
		NodeGrpCfg::proceed(pNode, lContext);
	}
};

VirtualInstrumentBaseParser::VirtualInstrumentBaseParser(const char* pXSDFile, const char* pBaseFileName) :
		XMLConfigurator(pXSDFile, false), _baseFileName(pBaseFileName)
{
	LOG4CXX_DEBUG(gLogger, "VirtualInstrumentBaseParser Constructor")
	getXmlConfiguratorMap()["base"] = RootNodeCfgSPtr(new LocalBaseNode);
}

VirtualInstrumentBaseParser::~VirtualInstrumentBaseParser()
{
}

void VirtualInstrumentBaseParser::parse(VirtualInstrumentSPtr lResult)
{
	AMDA::Parameters::CfgContext ctx;
	ctx.push<VirtualInstrument *>(lResult.get());

	// Check schema validity and parse xml file
	try {
		XMLConfigurator::proceed(_baseFileName.c_str(), ctx, false);
	}
	catch (...) {
		LOG4CXX_INFO(gLogger, "VirtualInstrumentBaseParser::parse error while parsing file " << _baseFileName);
		return;
	}
}

} /* namespace  LocalFileInterface*/
} /* namespace AMDA */