]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_XMLReader.cpp
Salome HOME
Fix for the issue #1321 : remove dependency on additional environment variables
[modules/shaper.git] / src / Config / Config_XMLReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_XMLReader.cpp
5  *
6  *  Created on: Mar 14, 2014
7  *      Author: sbh
8  */
9
10 #include <Config_XMLReader.h>
11 #include <Config_Keywords.h>
12 #include <Config_Common.h>
13 #include <Config_PropManager.h>
14
15 #include <Events_Loop.h>
16 #include <Events_Error.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19
20 #include <fstream>
21
22 #ifdef WIN32
23 #pragma warning(disable : 4996) // for getenv
24 #endif
25
26 #ifdef _DEBUG
27 #include <iostream>
28 #endif
29
30 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
31     : myXmlDoc(NULL)
32 {
33   std::string prefix = ""; 
34   Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
35   if (aProp)
36     prefix = aProp->value();
37   /*
38    * Get path to *.xml files (typically ./bin/../plugins/)
39
40    * the problem: application may be launched using python executable,
41    * to use environment variable (at least for the current moment)
42    */
43   if (prefix.empty())
44     prefix = pluginConfigFile();
45
46 #ifdef WIN32
47     prefix += "\\";
48 #else
49     prefix += "/";
50 #endif
51   myDocumentPath = prefix + theXmlFileName;
52   std::ifstream aTestFile(myDocumentPath);
53   if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
54   aTestFile.close();
55 }
56
57 Config_XMLReader::~Config_XMLReader()
58 {
59   xmlFreeDoc(myXmlDoc);
60 }
61
62 std::string Config_XMLReader::pluginConfigFile()
63 {
64   std::string aValue;
65   char* anEnv = getenv("SHAPER_ROOT_DIR");
66   if (anEnv) {
67     aValue = std::string(anEnv);
68   } else {
69     anEnv = getenv("OPENPARTS_ROOT_DIR");
70     if (anEnv)
71       aValue = std::string(anEnv);
72   }
73 #ifdef WIN32
74     aValue += "\\";
75 #else
76     aValue += "/";
77 #endif
78   aValue += "plugins";
79   return aValue;
80 }
81
82 void Config_XMLReader::readAll()
83 {
84   xmlNodePtr aRoot = findRoot();
85   readRecursively(aRoot);
86 }
87
88 void Config_XMLReader::processNode(xmlNodePtr theNode)
89 {
90   if (isNode(theNode, NODE_SOURCE, NULL)) {
91     std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
92     Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
93     readRecursively(aSourceReader.findRoot());
94 #ifdef _DEBUG
95     //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
96 #endif
97   }
98 }
99
100 void Config_XMLReader::cleanup(xmlNodePtr)
101 {
102   // do nothing;
103 }
104
105 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
106 {
107   return true;
108 }
109
110 xmlNodePtr Config_XMLReader::findRoot()
111 {
112   if (myXmlDoc == NULL) {
113     myXmlDoc = xmlParseFile(myDocumentPath.c_str());
114   }
115   if (myXmlDoc == NULL) {
116 #ifdef _DEBUG
117     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
118     << " is not parsed successfully." << std::endl;
119 #endif
120     return NULL;
121   }
122   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
123 #ifdef _DEBUG
124   if(aRoot == NULL) {
125     std::cout << "Config_XMLReader::import: " << "Error: empty document";
126   }
127 #endif
128   return aRoot;
129 }
130
131 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
132 {
133   if (!theParent)
134     return;
135   xmlNodePtr aNode = theParent->xmlChildrenNode;
136   for (; aNode; aNode = aNode->next) {
137     //Still no text processing in features...
138     if (!isElementNode(aNode)) {
139       continue;
140     }
141     processNode(aNode);
142     if (processChildren(aNode)) {
143       readRecursively(aNode);
144     }
145     cleanup(aNode);
146   }
147 }
148
149 xmlNodePtr Config_XMLReader::node(void* theNode)
150 {
151   return static_cast<xmlNodePtr>(theNode);
152 }
153
154 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
155 {
156   std::string result = "";
157   char* aPropChars = (char*) theNode->name;
158   if (!aPropChars || aPropChars[0] == 0)
159     return result;
160   result = std::string(aPropChars);
161   return result;
162 }
163
164 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
165 {
166   std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
167   std::string aValue = getProperty(theNode, theAttribute);
168   if (doClean || !aValue.empty()) {
169     myCachedAttributes[aKey] = aValue;
170   }
171 }
172
173 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
174 {
175   return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
176 }
177
178 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
179 {
180   std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
181   std::string result = "";
182   if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
183     result = myCachedAttributes[aKey];
184   }
185   return result;
186 }
187
188 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
189 {
190   return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
191 }
192
193 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
194 {
195   std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
196   bool result = false;
197   std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
198   if( anEntry != myCachedAttributes.end()) {
199     myCachedAttributes.erase(anEntry);
200     result = true;
201   }
202   return result;
203 }