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>
15 #include <Events_Loop.h>
16 #include <Events_InfoMessage.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
23 #pragma warning(disable : 4996) // for getenv
31 static const char FSEP = '\\';
33 static const char FSEP = '/';
36 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
39 std::string prefix = "";
40 Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
42 prefix = aProp->value();
44 * Get path to *.xml files (typically ./bin/../plugins/)
46 * the problem: application may be launched using python executable,
47 * to use environment variable (at least for the current moment)
50 prefix = pluginConfigFile();
52 myDocumentPath = prefix + FSEP + theXmlFileName;
53 std::ifstream aTestFile(myDocumentPath);
55 Events_InfoMessage("Config_XMLReader", "Unable to open %1").arg(myDocumentPath).send();
59 Config_XMLReader::~Config_XMLReader()
64 std::string Config_XMLReader::pluginConfigFile()
67 char* anEnv = getenv("SHAPER_ROOT_DIR");
69 aValue = std::string(anEnv) +
70 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
72 anEnv = getenv("OPENPARTS_ROOT_DIR");
74 aValue = std::string(anEnv) + FSEP + "plugins";
80 void Config_XMLReader::readAll()
82 xmlNodePtr aRoot = findRoot();
83 readRecursively(aRoot);
86 void Config_XMLReader::processNode(xmlNodePtr theNode)
88 if (isNode(theNode, NODE_SOURCE, NULL)) {
89 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
90 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
91 readRecursively(aSourceReader.findRoot());
93 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
98 void Config_XMLReader::cleanup(xmlNodePtr)
103 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
108 xmlNodePtr Config_XMLReader::findRoot()
110 if (myXmlDoc == NULL) {
111 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
113 if (myXmlDoc == NULL) {
115 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
116 << " is not parsed successfully." << std::endl;
120 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
123 std::cout << "Config_XMLReader::import: " << "Error: empty document";
129 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
133 xmlNodePtr aNode = theParent->xmlChildrenNode;
134 for (; aNode; aNode = aNode->next) {
135 //Still no text processing in features...
136 if (!isElementNode(aNode)) {
140 if (processChildren(aNode)) {
141 readRecursively(aNode);
147 xmlNodePtr Config_XMLReader::node(void* theNode)
149 return static_cast<xmlNodePtr>(theNode);
152 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
154 std::string result = "";
155 char* aPropChars = (char*) theNode->name;
156 if (!aPropChars || aPropChars[0] == 0)
158 result = std::string(aPropChars);
162 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
164 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
165 std::string aValue = getProperty(theNode, theAttribute);
166 if (doClean || !aValue.empty()) {
167 myCachedAttributes[aKey] = aValue;
171 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
173 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
176 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
178 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
179 std::string result = "";
180 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
181 result = myCachedAttributes[aKey];
186 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
188 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
191 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
193 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
195 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
196 if( anEntry != myCachedAttributes.end()) {
197 myCachedAttributes.erase(anEntry);
203 const char* Config_XMLReader::encoding() const
205 return (const char*) myXmlDoc->encoding;