Salome HOME
Issue #273: Add copyright string
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_ModuleReader.cpp
5  *
6  *  Created on: Mar 20, 2014
7  *      Author: sbh
8  */
9
10 #include <Config_Keywords.h>
11 #include <Config_Common.h>
12 #include <Config_ModuleReader.h>
13 #include <Config_FeatureReader.h>
14 #include <Events_Error.h>
15
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
18
19 //Necessary for cerr
20 #include <iostream>
21
22 #ifdef WIN32
23 #include <windows.h>
24 #else
25 #include <dlfcn.h>
26 #endif
27
28 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
29     : Config_XMLReader("plugins.xml"),
30       myEventGenerated(theEventGenerated)
31 {
32 }
33
34 Config_ModuleReader::~Config_ModuleReader()
35 {
36 }
37
38 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
39 {
40   return myFeaturesInFiles;
41 }
42
43 /*
44  * Get module name from plugins.xml
45  * (property "module")
46  */
47 std::string Config_ModuleReader::getModuleName()
48 {
49   xmlNodePtr aRoot = findRoot();
50   return getProperty(aRoot, PLUGINS_MODULE);
51 }
52
53 /*
54  *
55  */
56 void Config_ModuleReader::processNode(xmlNodePtr theNode)
57 {
58   if (isNode(theNode, NODE_PLUGIN, NULL)) {
59     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
60     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
61     std::list<std::string> aFeatures = importPlugin(aPluginLibrary, aPluginConf);
62     std::list<std::string>::iterator it = aFeatures.begin();
63     for (; it != aFeatures.end(); it++) {
64       myFeaturesInFiles[*it] = aPluginConf;
65     }
66   }
67 }
68
69 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
70 {
71   return isNode(theNode, NODE_PLUGINS, NULL);
72 }
73
74 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
75                                                          const std::string& thePluginFile)
76 {
77   if (thePluginFile.empty()) {  //probably a third party library
78     loadLibrary(thePluginLibrary);
79     return std::list<std::string>();
80   }
81
82   Config_FeatureReader aReader = Config_FeatureReader(thePluginFile, thePluginLibrary,
83                                                       myEventGenerated);
84   aReader.readAll();
85   return aReader.features();
86 }
87
88 void Config_ModuleReader::loadLibrary(const std::string theLibName)
89 {
90   std::string aFileName = library(theLibName);
91   if (aFileName.empty())
92     return;
93
94 #ifdef WIN32
95   HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
96   if (!aModLib && theLibName != "DFBrowser") {  // don't shor error for internal debugging tool
97     std::string errorMsg = "Failed to load " + aFileName;
98     std::cerr << errorMsg << std::endl;
99     Events_Error::send(errorMsg);
100   }
101 #else
102   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
103   if ( !aModLib && theLibName != "DFBrowser") {  // don't shor error for internal debugging tool
104     std::cerr << "Failed to load " << aFileName.c_str() << std::endl;
105   }
106 #endif
107 }
108