Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
index 1a6d1c7ce989770fc0433bb75f44c041b1faa1df..c533e3e3e92b69565f2a5494ee1bbdb9aeaef5d6 100644 (file)
@@ -5,30 +5,26 @@
  *      Author: sbh
  */
 
+#include <Config_Keywords.h>
+#include <Config_Common.h>
 #include <Config_ModuleReader.h>
 #include <Config_FeatureReader.h>
+#include <Events_Error.h>
 
-#include <libxml\parser.h>
-#include <libxml\tree.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
 
-#ifdef _DEBUG
+//Necessary for cerr
 #include <iostream>
-#endif
-
-//Hardcoded xml entities
-// * Nodes
-const static char* NODE_PLUGIN = "plugin";
-const static char* NODE_PLUGINS = "plugins";
-
-// * Properties
-const static char* PLUGINS_MODULE = "module";
-const static char* PLUGIN_CONFIG = "configuration";
-const static char* PLUGIN_LIBRARY = "library";
 
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
 
-Config_ModuleReader::Config_ModuleReader()
-    : Config_XMLReader("plugins.xml"),
-      m_isAutoImport(false)
+Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
+    : Config_XMLReader("plugins.xml"), myEventGenerated(theEventGenerated)
 {
 }
 
@@ -36,6 +32,11 @@ Config_ModuleReader::~Config_ModuleReader()
 {
 }
 
+const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
+{
+  return myFeaturesInFiles;
+}
+
 /*
  * Get module name from plugins.xml
  * (property "module")
@@ -51,11 +52,14 @@ std::string Config_ModuleReader::getModuleName()
  */
 void Config_ModuleReader::processNode(xmlNodePtr theNode)
 {
-  if(isNode(theNode, NODE_PLUGIN, NULL)) {
-    std::string aPluginName = getProperty(theNode, PLUGIN_CONFIG);
-    if(m_isAutoImport)
-      importPlugin(aPluginName);
-    m_pluginsList.push_back(aPluginName);
+  if (isNode(theNode, NODE_PLUGIN, NULL)) {
+    std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
+    std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
+    std::list<std::string> aFeatures = importPlugin(aPluginLibrary, aPluginConf);
+    std::list<std::string>::iterator it = aFeatures.begin();
+    for(; it != aFeatures.end(); it++) {
+      myFeaturesInFiles[*it] = aPluginConf;
+    }
   }
 }
 
@@ -64,18 +68,38 @@ bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
   return isNode(theNode, NODE_PLUGINS, NULL);
 }
 
-void Config_ModuleReader::importPlugin(const std::string& thePluginName)
+std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
+                                                         const std::string& thePluginFile)
 {
-  Config_FeatureReader aReader(thePluginName);
+  if (thePluginFile.empty()) { //probably a third party library
+    loadLibrary(thePluginLibrary);
+    return std::list<std::string>();
+  }
+
+  Config_FeatureReader aReader = Config_FeatureReader(thePluginFile, thePluginLibrary,
+                                                      myEventGenerated);
   aReader.readAll();
+  return aReader.features();
 }
 
-void Config_ModuleReader::setAutoImport(bool enabled)
+void Config_ModuleReader::loadLibrary(const std::string theLibName)
 {
-  m_isAutoImport = enabled;
-}
+  std::string aFileName = library(theLibName);
+  if (aFileName.empty())
+    return;
 
-const std::list<std::string>& Config_ModuleReader::pluginsList() const
-{
-  return m_pluginsList;
+#ifdef WIN32
+  HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
+  if (!aModLib) {
+    std::string errorMsg = "Failed to load " + aFileName;
+    std::cerr << errorMsg << std::endl;
+    Events_Error::send(errorMsg);
+  }
+#else
+  void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
+  if ( !aModLib ) {
+    std::cerr << "Failed to load " << aFileName.c_str() << std::endl;
+  }
+#endif
 }
+