1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
6 * Created on: Mar 14, 2014
10 #include <Config_XMLReader.h>
11 #include <Config_Keywords.h>
12 #include <Config_Common.h>
13 #include <Config_PropManager.h>
14 #include <Config_ModuleReader.h>
16 #include <Events_Loop.h>
17 #include <Events_InfoMessage.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
24 #pragma warning(disable : 4996) // for getenv
32 static const char FSEP = '\\';
34 static const char FSEP = '/';
37 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
40 std::string prefix = "";
41 Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
43 prefix = aProp->value();
45 * Get path to *.xml files (typically ./bin/../plugins/)
47 * the problem: application may be launched using python executable,
48 * to use environment variable (at least for the current moment)
51 prefix = pluginConfigFile();
53 myDocumentPath = prefix + FSEP + theXmlFileName;
54 std::ifstream aTestFile(myDocumentPath);
56 Events_InfoMessage("Config_XMLReader", "Unable to open %1").arg(myDocumentPath).send();
60 Config_XMLReader::~Config_XMLReader()
65 std::string Config_XMLReader::pluginConfigFile()
68 char* anEnv = getenv("SHAPER_ROOT_DIR");
70 aValue = std::string(anEnv) +
71 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
73 anEnv = getenv("OPENPARTS_ROOT_DIR");
75 aValue = std::string(anEnv) + FSEP + "plugins";
81 void Config_XMLReader::readAll()
83 // to load external modules dependencies (like GEOm for Connector Feature
84 Config_ModuleReader::loadScript("salome.shaper.initConfig", false);
86 xmlNodePtr aRoot = findRoot();
87 readRecursively(aRoot);
90 void Config_XMLReader::processNode(xmlNodePtr theNode)
92 if (isNode(theNode, NODE_SOURCE, NULL)) {
93 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
94 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
95 readRecursively(aSourceReader.findRoot());
97 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
102 void Config_XMLReader::cleanup(xmlNodePtr)
107 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
112 xmlNodePtr Config_XMLReader::findRoot()
114 if (myXmlDoc == NULL) {
115 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
117 if (myXmlDoc == NULL) {
119 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
120 << " is not parsed successfully." << std::endl;
124 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
127 std::cout << "Config_XMLReader::import: " << "Error: empty document";
133 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
137 xmlNodePtr aNode = theParent->xmlChildrenNode;
138 for (; aNode; aNode = aNode->next) {
139 //Still no text processing in features...
140 if (!isElementNode(aNode)) {
144 if (processChildren(aNode)) {
145 readRecursively(aNode);
151 xmlNodePtr Config_XMLReader::node(void* theNode)
153 return static_cast<xmlNodePtr>(theNode);
156 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
158 std::string result = "";
159 char* aPropChars = (char*) theNode->name;
160 if (!aPropChars || aPropChars[0] == 0)
162 result = std::string(aPropChars);
166 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
168 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
169 std::string aValue = getProperty(theNode, theAttribute);
170 if (doClean || !aValue.empty()) {
171 myCachedAttributes[aKey] = aValue;
175 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
177 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
180 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
182 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
183 std::string result = "";
184 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
185 result = myCachedAttributes[aKey];
190 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
192 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
195 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
197 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
199 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
200 if( anEntry != myCachedAttributes.end()) {
201 myCachedAttributes.erase(anEntry);
207 const char* Config_XMLReader::encoding() const
209 return (const char*) myXmlDoc->encoding;