Salome HOME
Sources formated according to the codeing standards
[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"),
28       myEventGenerated(theEventGenerated)
29 {
30 }
31
32 Config_ModuleReader::~Config_ModuleReader()
33 {
34 }
35
36 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
37 {
38   return myFeaturesInFiles;
39 }
40
41 /*
42  * Get module name from plugins.xml
43  * (property "module")
44  */
45 std::string Config_ModuleReader::getModuleName()
46 {
47   xmlNodePtr aRoot = findRoot();
48   return getProperty(aRoot, PLUGINS_MODULE);
49 }
50
51 /*
52  *
53  */
54 void Config_ModuleReader::processNode(xmlNodePtr theNode)
55 {
56   if (isNode(theNode, NODE_PLUGIN, NULL)) {
57     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
58     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
59     std::list<std::string> aFeatures = importPlugin(aPluginLibrary, aPluginConf);
60     std::list<std::string>::iterator it = aFeatures.begin();
61     for (; it != aFeatures.end(); it++) {
62       myFeaturesInFiles[*it] = aPluginConf;
63     }
64   }
65 }
66
67 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
68 {
69   return isNode(theNode, NODE_PLUGINS, NULL);
70 }
71
72 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
73                                                          const std::string& thePluginFile)
74 {
75   if (thePluginFile.empty()) {  //probably a third party library
76     loadLibrary(thePluginLibrary);
77     return std::list<std::string>();
78   }
79
80   Config_FeatureReader aReader = Config_FeatureReader(thePluginFile, thePluginLibrary,
81                                                       myEventGenerated);
82   aReader.readAll();
83   return aReader.features();
84 }
85
86 void Config_ModuleReader::loadLibrary(const std::string theLibName)
87 {
88   std::string aFileName = library(theLibName);
89   if (aFileName.empty())
90     return;
91
92 #ifdef WIN32
93   HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
94   if (!aModLib && theLibName != "DFBrowser") {  // don't shor error for internal debugging tool
95     std::string errorMsg = "Failed to load " + aFileName;
96     std::cerr << errorMsg << std::endl;
97     Events_Error::send(errorMsg);
98   }
99 #else
100   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
101   if ( !aModLib && theLibName != "DFBrowser") {  // don't shor error for internal debugging tool
102     std::cerr << "Failed to load " << aFileName.c_str() << std::endl;
103   }
104 #endif
105 }
106