Blame view

src/XMLParameterConfigurator/XMLParameterConfigurator.cc 4.49 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * XMLParameterConfigurator.cc
 *
 *  Created on: 17 oct. 2012
 *      Author: AKKA IS
 */

#include <iostream>
#include <sstream>
#include <string>

#include <boost/algorithm/string.hpp>

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
14
15
using namespace std;
using namespace boost;
fbe3c2bb   Benjamin Renard   First commit
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include "log4cxx/logger.h"

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

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

#include "ParamNode.hh"
#include "XMLParameterConfigurator.hh"
#include "Config.hh"
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
31
#include "ServicesServer.hh"
fbe3c2bb   Benjamin Renard   First commit
32
33
34
35
36
37

using namespace log4cxx;
using namespace AMDA::XMLConfigurator;

// Global variable

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
namespace AMDA {
    namespace XMLParameterConfigurator {
        log4cxx::LoggerPtr gLogger = log4cxx::Logger::getLogger("AMDA-Kernel.XMLParameterConfigurator");

        XMLParameterConfigurator::XMLParameterConfigurator(const char* pXSDFile) : XMLConfigurator(pXSDFile, true) {
            LOG4CXX_DEBUG(gLogger, "XMLParameterConfigurator Constructor")
            getXmlConfiguratorMap()["param"] = RootNodeCfgSPtr(new ParamNode);
        }

        XMLParameterConfigurator::~XMLParameterConfigurator() {
        }

        bool XMLParameterConfigurator::proceed(const char* filename,
                const AMDA::Parameters::CfgContext& pCtx, bool optionalFile) {
                                    // load plugins 
                        AMDA::Parameters::ServicesServer *servicesServer = AMDA::Parameters::ServicesServer::getInstance();
                        servicesServer->loadPluginByType("paramGet");
            xmlDocPtr lDoc = NULL;
            xmlNodePtr lRoot = NULL;
            std::string configurationFileName = this->getParamPath() + "/" + filename
                    + ".xml";
            LOG4CXX_DEBUG(gLogger, "Filename to parse: " << configurationFileName)
            try {
                if (access(configurationFileName.c_str(), F_OK) != 0) {
                    if (optionalFile) {
                        LOG4CXX_WARN(gLogger, "Cannot find file: " << configurationFileName)
                        return false;
                    }
                    ERROR_EXCEPTION(ERROR_FILE_NOT_EXIST << configurationFileName);
                }

                if (!(lDoc = xmlParseFile(configurationFileName.c_str()))) {
                    ERROR_EXCEPTION(ERROR_BAD_XML_FORMAT);
                }

                //  const char* lParamerterXSD = "DataBaseParameters/xsd/parameter.xsd";
                //  if ( ! is_valid( lDoc, lParamerterXSD)) {
                //      ERROR_EXCEPTION( ERROR_DOC_NOT_VALID << " XSD file: " << lParamerterXSD);
                //    }

                if (!(lRoot = xmlDocGetRootElement(lDoc))) {
                    ERROR_EXCEPTION(ERROR_EMPTY_DOCUMENT);
                }
                AMDA::Parameters::CfgContext lContext(pCtx);
                lContext.push<std::string *>(&configurationFileName);

                // XML_XML_ID case
                xmlNs* lNsXML = NULL;
                xmlAttrPtr cur = lRoot->properties;
                while (cur && !lNsXML) {
                    if ((xmlStrEqual(cur->name, (const xmlChar *) "id") == 1) && cur->ns
                            && (xmlStrEqual(cur->ns->prefix, (const xmlChar *) "xml")
                            == 1)) {
                        lNsXML = cur->ns;
                        cur->ns = NULL;
                    } else {
                        cur = cur->next;
                    }
                }
                if (!lNsXML) {
                    ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << lRoot->name << "@" << XML_XML_ID)
                }

                if (!NodeCfg::getIsValid(lRoot, _XSDFile)) {
                    cur->ns = lNsXML;
                    ERROR_EXCEPTION(ERROR_DOC_NOT_VALID << " XSD file: " << _XSDFile);
                }
                cur->ns = lNsXML;

                RootNodeCfgMap::iterator it = getXmlConfiguratorMap().find(
                        std::string((const char*) lRoot->name));
                if (it != getXmlConfiguratorMap().end()) {
                    it->second->proceed(lRoot, lContext);
                } else {
                    ERROR_EXCEPTION(ERROR_ROOT_NODE_NOT_SUPPORTED << lRoot->name);
                }
            } catch (...) {
                if (lDoc) {
                    xmlFreeDoc(lDoc);
                }
                throw;
            }
            if (lDoc) {
                xmlFreeDoc(lDoc);
            }
            return true;
        }

    } /* namespace  XMLParameterConfigurator*/
fbe3c2bb   Benjamin Renard   First commit
127
} /* namespace AMDA */