1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Config_XMLReader.h>
22 #include <Config_Keywords.h>
23 #include <Config_Common.h>
24 #include <Config_PropManager.h>
25 #include <Config_ModuleReader.h>
27 #include <Events_Loop.h>
28 #include <Events_InfoMessage.h>
29 #include <libxml/parser.h>
30 #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)
51 std::string prefix = "";
52 Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
54 prefix = aProp->value();
56 * Get path to *.xml files (typically ./bin/../plugins/)
58 * the problem: application may be launched using python executable,
59 * to use environment variable (at least for the current moment)
62 prefix = pluginConfigFile();
64 myDocumentPath = prefix + FSEP + theXmlFileName;
65 std::ifstream aTestFile(myDocumentPath);
67 Events_InfoMessage("Config_XMLReader", "Unable to open %1").arg(myDocumentPath).send();
71 Config_XMLReader::~Config_XMLReader()
76 std::string Config_XMLReader::pluginConfigFile()
79 char* anEnv = getenv("SHAPER_ROOT_DIR");
81 aValue = std::string(anEnv) +
82 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
84 anEnv = getenv("OPENPARTS_ROOT_DIR");
86 aValue = std::string(anEnv) + FSEP + "plugins";
92 void Config_XMLReader::readAll()
94 // to load external modules dependencies (like GEOm for Connector Feature
95 Config_ModuleReader::loadScript("salome.shaper.initConfig", false);
97 xmlNodePtr aRoot = findRoot();
98 readRecursively(aRoot);
101 void Config_XMLReader::processNode(xmlNodePtr theNode)
103 if (isNode(theNode, NODE_SOURCE, NULL)) {
104 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
105 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
106 readRecursively(aSourceReader.findRoot());
108 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
113 void Config_XMLReader::cleanup(xmlNodePtr)
118 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
123 xmlNodePtr Config_XMLReader::findRoot()
125 if (myXmlDoc == NULL) {
126 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
128 if (myXmlDoc == NULL) {
130 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
131 << " is not parsed successfully." << std::endl;
135 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
138 std::cout << "Config_XMLReader::import: " << "Error: empty document";
144 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
148 xmlNodePtr aNode = theParent->xmlChildrenNode;
149 for (; aNode; aNode = aNode->next) {
150 //Still no text processing in features...
151 if (!isElementNode(aNode)) {
155 if (processChildren(aNode)) {
156 readRecursively(aNode);
162 xmlNodePtr Config_XMLReader::node(void* theNode)
164 return static_cast<xmlNodePtr>(theNode);
167 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
169 std::string result = "";
170 char* aPropChars = (char*) theNode->name;
171 if (!aPropChars || aPropChars[0] == 0)
173 result = std::string(aPropChars);
177 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
179 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
180 std::string aValue = getProperty(theNode, theAttribute);
181 if (doClean || !aValue.empty()) {
182 myCachedAttributes[aKey] = aValue;
186 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
188 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
191 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
193 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
194 std::string result = "";
195 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
196 result = myCachedAttributes[aKey];
201 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
203 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
206 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
208 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
210 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
211 if( anEntry != myCachedAttributes.end()) {
212 myCachedAttributes.erase(anEntry);
218 const char* Config_XMLReader::encoding() const
220 return (const char*) myXmlDoc->encoding;