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_Error.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);
54 if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
58 Config_XMLReader::~Config_XMLReader()
63 std::string Config_XMLReader::pluginConfigFile()
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 + "plugins";
79 void Config_XMLReader::readAll()
81 xmlNodePtr aRoot = findRoot();
82 readRecursively(aRoot);
85 void Config_XMLReader::processNode(xmlNodePtr theNode)
87 if (isNode(theNode, NODE_SOURCE, NULL)) {
88 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
89 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
90 readRecursively(aSourceReader.findRoot());
92 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
97 void Config_XMLReader::cleanup(xmlNodePtr)
102 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
107 xmlNodePtr Config_XMLReader::findRoot()
109 if (myXmlDoc == NULL) {
110 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
112 if (myXmlDoc == NULL) {
114 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
115 << " is not parsed successfully." << std::endl;
119 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
122 std::cout << "Config_XMLReader::import: " << "Error: empty document";
128 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
132 xmlNodePtr aNode = theParent->xmlChildrenNode;
133 for (; aNode; aNode = aNode->next) {
134 //Still no text processing in features...
135 if (!isElementNode(aNode)) {
139 if (processChildren(aNode)) {
140 readRecursively(aNode);
146 xmlNodePtr Config_XMLReader::node(void* theNode)
148 return static_cast<xmlNodePtr>(theNode);
151 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
153 std::string result = "";
154 char* aPropChars = (char*) theNode->name;
155 if (!aPropChars || aPropChars[0] == 0)
157 result = std::string(aPropChars);
161 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
163 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
164 std::string aValue = getProperty(theNode, theAttribute);
165 if (doClean || !aValue.empty()) {
166 myCachedAttributes[aKey] = aValue;
170 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
172 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
175 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
177 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
178 std::string result = "";
179 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
180 result = myCachedAttributes[aKey];
185 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
187 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
190 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
192 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
194 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
195 if( anEntry != myCachedAttributes.end()) {
196 myCachedAttributes.erase(anEntry);