Salome HOME
Fix for the issue #1321 : remove dependency on additional environment variables
[modules/shaper.git] / src / Config / Config_XMLReader.cpp
index adce851dc03fb2da012c3e1473c44ef7d80eb65e..0f36a154867dc7635377ef82d6d840492743a6b1 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 /*
  * Config_XMLReader.cpp
  *
  */
 
 #include <Config_XMLReader.h>
+#include <Config_Keywords.h>
+#include <Config_Common.h>
+#include <Config_PropManager.h>
+
+#include <Events_Loop.h>
+#include <Events_Error.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
 
-#include <Event_Loop.hxx>
-#include <libxml\parser.h>
-#include <libxml\tree.h>
+#include <fstream>
 
 #ifdef WIN32
-//For GetModuleFileNameW
-#include <windows.h>
+#pragma warning(disable : 4996) // for getenv
 #endif
 
 #ifdef _DEBUG
 #include <iostream>
 #endif
 
-
 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
+    : myXmlDoc(NULL)
 {
-  std::string prefix;
-  //Get path to *.xml files (typically ./bin/../plugins/)
+  std::string prefix = ""; 
+  Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
+  if (aProp)
+    prefix = aProp->value();
+  /*
+   * Get path to *.xml files (typically ./bin/../plugins/)
+
+   * the problem: application may be launched using python executable,
+   * to use environment variable (at least for the current moment)
+   */
+  if (prefix.empty())
+    prefix = pluginConfigFile();
+
 #ifdef WIN32
-  HMODULE hModule = GetModuleHandleW(NULL);
-  WCHAR wchar_path[MAX_PATH];
-  GetModuleFileNameW(hModule, wchar_path, MAX_PATH);
-  char char_path[MAX_PATH];
-  char DefChar = ' ';
-  WideCharToMultiByte(CP_ACP, 0, wchar_path, -1, char_path, MAX_PATH, &DefChar, NULL);
-  prefix = std::string(char_path);
-  //chop "bin\XGUI.exe"
-  unsigned found = prefix.rfind("bin");
-  if(found != std::string::npos)
-    prefix.replace(found, prefix.length(), "plugins\\");
+    prefix += "\\";
 #else
-  //TODO(sbh): Find full path to binary on linux
-  prefix = "../plugins/";
+    prefix += "/";
 #endif
-
-  m_DocumentPath = prefix + theXmlFileName;
+  myDocumentPath = prefix + theXmlFileName;
+  std::ifstream aTestFile(myDocumentPath);
+  if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
+  aTestFile.close();
 }
 
-
 Config_XMLReader::~Config_XMLReader()
 {
+  xmlFreeDoc(myXmlDoc);
+}
+
+std::string Config_XMLReader::pluginConfigFile()
+{
+  std::string aValue;
+  char* anEnv = getenv("SHAPER_ROOT_DIR");
+  if (anEnv) {
+    aValue = std::string(anEnv);
+  } else {
+    anEnv = getenv("OPENPARTS_ROOT_DIR");
+    if (anEnv)
+      aValue = std::string(anEnv);
+  }
+#ifdef WIN32
+    aValue += "\\";
+#else
+    aValue += "/";
+#endif
+  aValue += "plugins";
+  return aValue;
 }
 
-/*
- * Read all nodes (recursively if processChildren() is true
- * for a node). For each read node the processNode will be
- * called.
- */
 void Config_XMLReader::readAll()
 {
   xmlNodePtr aRoot = findRoot();
   readRecursively(aRoot);
 }
 
-/*
- * Allows to customize reader's behavior for a node. Virtual.
- * The default implementation does nothing. (In debug mode prints
- * some info)
- */
-void Config_XMLReader::processNode(xmlNodePtr aNode)
+void Config_XMLReader::processNode(xmlNodePtr theNode)
 {
-  #ifdef _DEBUG
-    std::cout << "Config_XMLReader::processNode: "
-              << aNode->name << " content: "
-              << aNode->content << std::endl;
-  #endif
+  if (isNode(theNode, NODE_SOURCE, NULL)) {
+    std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
+    Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
+    readRecursively(aSourceReader.findRoot());
+#ifdef _DEBUG
+    //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
+#endif
+  }
+}
+
+void Config_XMLReader::cleanup(xmlNodePtr)
+{
+  // do nothing;
 }
 
-/*
- * Defines which nodes should be processed recursively. Virtual.
- * The default implementation to read all nodes.
- */
 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
 {
   return true;
@@ -86,69 +109,95 @@ bool Config_XMLReader::processChildren(xmlNodePtr aNode)
 
 xmlNodePtr Config_XMLReader::findRoot()
 {
-  xmlDocPtr aDoc;
-  aDoc = xmlParseFile(m_DocumentPath.c_str());
-  if(aDoc == NULL) {
-    #ifdef _DEBUG
-    std::cout << "Config_XMLReader::import: " << "Document " << m_DocumentPath
-              << " is not parsed successfully." << std::endl;
-    #endif
+  if (myXmlDoc == NULL) {
+    myXmlDoc = xmlParseFile(myDocumentPath.c_str());
+  }
+  if (myXmlDoc == NULL) {
+#ifdef _DEBUG
+    std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
+    << " is not parsed successfully." << std::endl;
+#endif
     return NULL;
   }
-  xmlNodePtr aRoot = xmlDocGetRootElement(aDoc);
-  #ifdef _DEBUG
+  xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
+#ifdef _DEBUG
   if(aRoot == NULL) {
     std::cout << "Config_XMLReader::import: " << "Error: empty document";
   }
-  #endif
+#endif
   return aRoot;
 }
 
-/*
- * Calls processNode() for each child (for some - recursively)
- * of the given node.
- * \sa ReadAll()
- */
 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
 {
-  if(!theParent)
+  if (!theParent)
     return;
   xmlNodePtr aNode = theParent->xmlChildrenNode;
-  for(; aNode; aNode = aNode->next) {
+  for (; aNode; aNode = aNode->next) {
+    //Still no text processing in features...
+    if (!isElementNode(aNode)) {
+      continue;
+    }
     processNode(aNode);
-    if(processChildren(aNode)) {
+    if (processChildren(aNode)) {
       readRecursively(aNode);
     }
+    cleanup(aNode);
   }
 }
 
-/*
- * void* -> xmlNodePtr
- */
 xmlNodePtr Config_XMLReader::node(void* theNode)
 {
   return static_cast<xmlNodePtr>(theNode);
 }
 
-/*
- * Returns named property for a given node as std::string.
- */
-std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name)
+std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
 {
   std::string result = "";
-  char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name);
-  if(!aPropChars || aPropChars[0] == 0)
+  char* aPropChars = (char*) theNode->name;
+  if (!aPropChars || aPropChars[0] == 0)
     return result;
   result = std::string(aPropChars);
   return result;
 }
 
-/*
- * Returns true if theNode is XML node with a given name.
- */
-bool Config_XMLReader::isNode(xmlNodePtr theNode, const char* theNodeName)
+void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
+{
+  std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
+  std::string aValue = getProperty(theNode, theAttribute);
+  if (doClean || !aValue.empty()) {
+    myCachedAttributes[aKey] = aValue;
+  }
+}
+
+std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
 {
-  const char* emptyLine = "";
-  return theNode->type == XML_ELEMENT_NODE
-      && !xmlStrcmp(theNode->name, (const xmlChar *) theNodeName);
+  return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
+}
+
+std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
+{
+  std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
+  std::string result = "";
+  if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
+    result = myCachedAttributes[aKey];
+  }
+  return result;
+}
+
+bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
+{
+  return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
+}
+
+bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
+{
+  std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
+  bool result = false;
+  std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
+  if( anEntry != myCachedAttributes.end()) {
+    myCachedAttributes.erase(anEntry);
+    result = true;
+  }
+  return result;
 }