PluginsParser.cc 4.03 KB
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   PluginsParser.cc
 * Author: hacene
 * 
 * Created on March 17, 2020, 10:16 AM
 */

#include "PluginsParser.hh"
#include "NodeCfg.hh"
#include <boost/lexical_cast.hpp>


using namespace AMDA::XMLConfigurator;

namespace AMDA {
    namespace Parameters {

        class PluginNode : public NodeCfg {

            void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
                if ((pNode->children != NULL) && (pNode->children->content != NULL)) {
                    AMDA::Parameters::PluginObject *pluginObject = pContext.get<AMDA::Parameters::PluginObject *>();
                    if (pluginObject != NULL) {
                        pluginObject->setPluginPath(std::string((char*) pNode->children->content));
                    } else {
                        // throw exception 
                    }
                }
            }
        };

        class RequestProcessNode : public NodeGrpCfg {
        public:

            RequestProcessNode() : NodeGrpCfg() {
                getChildList()["plugin"] = NodeCfgSPtr(new PluginNode);
            }

            void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
                xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
                std::string name;
                if (value != NULL) {
                    name = (char *) value;
                    xmlFree(value);
                }
                value = xmlGetProp(pNode, (const xmlChar *) "type");
                std::string type;
                if (value != NULL) {
                    type = (char *) value;
                    xmlFree(value);
                }
                if (!name.empty() && !type.empty()) {
                    AMDA::Parameters::CfgContext ctx;
                    PluginObject pluginObject;
                    ctx.push<PluginObject*>(&pluginObject);
                    pluginObject.setRequestProcess(name);
                    pluginObject.setType(type);

                    NodeGrpCfg::proceed(pNode, ctx);
                    AMDA::Parameters::AMDA_ProcessPluginMaps *processPluginMaps = pContext.get<AMDA::Parameters::AMDA_ProcessPluginMaps *>();
                    (*processPluginMaps)[type][name] = pluginObject.getPluginPath();
                }
            }

        };
        ///////////////////////////////////////////////////////////////////////////////

        /*
         * @brief plugins node
         */
        class PluginsNode : public NodeGrpCfg {
        public:

            PluginsNode() : NodeGrpCfg() {
                getChildList()["requestProcess"] = NodeCfgSPtr(new RequestProcessNode);
            }

            void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {


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

        ///////////////////////////////////////////////////////////////////////////////

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

            // functions root node
            getXmlConfiguratorMap()["plugins"] = RootNodeCfgSPtr(new PluginsNode());
        }

        AMDA::Parameters::AMDA_ProcessPluginMaps PluginsParser::parse(const std::string& pluginsFile) {
            AMDA::Parameters::CfgContext ctx;
            AMDA::Parameters::AMDA_ProcessPluginMaps processPluginMaps;
            ctx.push<AMDA::Parameters::AMDA_ProcessPluginMaps *> (&processPluginMaps);
            // Check schema validity and parse xml file
            try {
                if (!XMLConfigurator::proceed(pluginsFile.c_str(), ctx, true)) {
                    return processPluginMaps;
                }
            } catch (...) {
                throw;
            }
            return processPluginMaps;
        }
    } // end AMDA
} //end parameters