Salome HOME
Merge remote-tracking branch 'remotes/origin/SketchSolver_Linux'
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
1 /*
2  * Config_ModuleReader.cpp
3  *
4  *  Created on: Mar 20, 2014
5  *      Author: sbh
6  */
7
8 #include <Config_Keywords.h>
9 #include <Config_Common.h>
10 #include <Config_ModuleReader.h>
11 #include <Config_FeatureReader.h>
12 #include <Events_Error.h>
13
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16
17 //Necessary for cerr
18 #include <iostream>
19
20 #ifdef WIN32
21 #include <windows.h>
22 #else
23 #include <dlfcn.h>
24 #endif
25
26 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
27     : Config_XMLReader("plugins.xml"), myEventGenerated(theEventGenerated)
28 {
29 }
30
31 Config_ModuleReader::~Config_ModuleReader()
32 {
33 }
34
35 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
36 {
37   return myFeaturesInFiles;
38 }
39
40 /*
41  * Get module name from plugins.xml
42  * (property "module")
43  */
44 std::string Config_ModuleReader::getModuleName()
45 {
46   xmlNodePtr aRoot = findRoot();
47   return getProperty(aRoot, PLUGINS_MODULE);
48 }
49
50 /*
51  *
52  */
53 void Config_ModuleReader::processNode(xmlNodePtr theNode)
54 {
55   if (isNode(theNode, NODE_PLUGIN, NULL)) {
56     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
57     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
58     std::list<std::string> aFeatures = importPlugin(aPluginLibrary, aPluginConf);
59     std::list<std::string>::iterator it = aFeatures.begin();
60     for(; it != aFeatures.end(); it++) {
61       myFeaturesInFiles[*it] = aPluginConf;
62     }
63   }
64 }
65
66 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
67 {
68   return isNode(theNode, NODE_PLUGINS, NULL);
69 }
70
71 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
72                                                          const std::string& thePluginFile)
73 {
74   if (thePluginFile.empty()) { //probably a third party library
75     loadLibrary(thePluginLibrary);
76     return std::list<std::string>();
77   }
78
79   Config_FeatureReader aReader = Config_FeatureReader(thePluginFile, thePluginLibrary,
80                                                       myEventGenerated);
81   aReader.readAll();
82   return aReader.features();
83 }
84
85 void Config_ModuleReader::loadLibrary(const std::string theLibName)
86 {
87 #ifdef _DEBUG
88   std::cout << "Config_ModuleReader::loading library... "  << theLibName.c_str() << std::endl;
89 #endif
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) {
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 ) {
104     std::cerr << "Failed to load " << aFileName.c_str() << std::endl;
105   }
106 #endif
107 }
108