1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Config_XMLReader.h>
21 #include <Config_Keywords.h>
22 #include <Config_Common.h>
23 #include <Config_PropManager.h>
24 #include <Config_ModuleReader.h>
26 #include <Events_Loop.h>
27 #include <Events_InfoMessage.h>
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
35 #pragma warning(disable : 4996) // for getenv
43 static const char FSEP = '\\';
45 static const char FSEP = '/';
48 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
49 : myXmlDoc(NULL), myRootFileName(theXmlFileName)
51 myDocumentPath = findConfigFile(theXmlFileName);
52 if (myDocumentPath.empty()) {
53 Events_InfoMessage("Config_XMLReader", "Unable to open %1").arg(theXmlFileName).send();
57 Config_XMLReader::~Config_XMLReader()
63 std::string Config_XMLReader::resourcesConfigFile()
66 char* anEnv = getenv("SHAPER_ROOT_DIR");
68 aValue = std::string(anEnv) +
69 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
71 anEnv = getenv("OPENPARTS_ROOT_DIR");
73 aValue = std::string(anEnv) + FSEP + "resources";
79 std::string Config_XMLReader::pluginConfigFile()
82 char* anEnv = getenv("SHAPER_ROOT_DIR");
84 aValue = std::string(anEnv) +
85 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
87 anEnv = getenv("OPENPARTS_ROOT_DIR");
89 aValue = std::string(anEnv) + FSEP + "plugins";
96 std::string Config_XMLReader::findConfigFile(const std::string theFileName, const int theFindIndex)
99 for(int aSolution = 0; aSolution < 12; aSolution++) {
100 std::string aFileName;
101 if (aSolution == 0) {
102 Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
105 aFileName = aProp->value();
107 std::ostringstream anEnvName;
109 anEnvName<<"SHAPER_ROOT_DIR";
110 else if (aSolution == 2)
111 anEnvName<<"OPENPARTS_ROOT_DIR";
113 anEnvName<<"OPENPARTS_PLUGINS_DIR";
115 char* anEnv = getenv(anEnvName.str().c_str());
118 if (aSolution > 2) { // there may be several paths separated by ";" symbol
120 std::string anEnvPart = anEnv;
121 size_t aPosStart = 0, aPosEnd;
122 for(int aSubNum = 0; aSubNum < aSolution - 3; aSubNum++) {
124 aPosStart = anEnvPart.find(';', aPosStart);
125 if (aPosStart == std::string::npos)
128 if (aPosStart == std::string::npos)
132 aPosEnd = anEnvPart.find(';', aPosStart);
133 aFileName = anEnvPart.substr(aPosStart,
134 aPosEnd == std::string::npos ? aPosEnd : aPosEnd - aPosStart) + FSEP;
137 aFileName = std::string(anEnv) + FSEP;
140 aFileName += std::string("share") + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
141 else if (aSolution == 2)
142 aFileName += "plugins";
145 aFileName += FSEP + theFileName;
146 std::ifstream aTestFile(aFileName);
148 if (aResultIndex == theFindIndex)
151 if (aSolution == 1) // don't allow SHAPER and OpenParts paths treated simultaneously
155 return ""; // no files found
158 void Config_XMLReader::readAll()
160 // to load external modules dependencies (like GEOM for Connector Feature)
161 Config_ModuleReader::loadScript("salome.shaper.initConfig", false);
163 for(int aSolution = 0; true; aSolution++) {
164 std::string aFoundFile = findConfigFile(myRootFileName, aSolution);
165 if (aFoundFile.empty()) {
166 break; // no more solutions
169 if (myXmlDoc != NULL) { // clear the previous XML document - now the new one will be opened
170 xmlFreeDoc(myXmlDoc);
173 xmlNodePtr aRoot = findRoot(aFoundFile);
174 readRecursively(aRoot);
178 void Config_XMLReader::processNode(xmlNodePtr theNode)
180 if (isNode(theNode, NODE_SOURCE, NULL)) {
181 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
182 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
183 readRecursively(aSourceReader.findRoot());
185 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
190 void Config_XMLReader::cleanup(xmlNodePtr)
196 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
202 xmlNodePtr Config_XMLReader::findRoot(const std::string theDocumentPath)
204 std::string aDocPath = theDocumentPath.empty() ? myDocumentPath : theDocumentPath;
205 if (myXmlDoc == NULL) {
206 myXmlDoc = xmlParseFile(aDocPath.c_str());
208 if (myXmlDoc == NULL) {
210 std::cout << "Config_XMLReader::import: " << "Document " << aDocPath
211 << " is not parsed successfully." << std::endl;
215 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
218 std::cout << "Config_XMLReader::import: " << "Error: empty document";
224 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
228 xmlNodePtr aNode = theParent->xmlChildrenNode;
229 for (; aNode; aNode = aNode->next) {
230 //Still no text processing in features...
231 if (!isElementNode(aNode)) {
235 if (processChildren(aNode)) {
236 readRecursively(aNode);
243 xmlNodePtr Config_XMLReader::node(void* theNode)
245 return static_cast<xmlNodePtr>(theNode);
249 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
251 std::string result = "";
252 char* aPropChars = (char*) theNode->name;
253 if (!aPropChars || aPropChars[0] == 0)
255 result = std::string(aPropChars);
259 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
261 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
262 std::string aValue = getProperty(theNode, theAttribute);
263 if (doClean || !aValue.empty()) {
264 myCachedAttributes[aKey] = aValue;
269 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
271 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
275 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
277 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
278 std::string result = "";
279 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
280 result = myCachedAttributes[aKey];
285 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
287 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
290 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
292 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
294 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
295 if( anEntry != myCachedAttributes.end()) {
296 myCachedAttributes.erase(anEntry);
303 const char* Config_XMLReader::encoding() const
305 return (const char*) myXmlDoc->encoding;