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
30 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
33 std::string prefix = "";
34 Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
36 prefix = aProp->value();
38 * Get path to *.xml files (typically ./bin/../plugins/)
40 * the problem: application may be launched using python executable,
41 * to use environment variable (at least for the current moment)
44 prefix = pluginConfigFile();
51 myDocumentPath = prefix + theXmlFileName;
52 std::ifstream aTestFile(myDocumentPath);
53 if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
57 Config_XMLReader::~Config_XMLReader()
62 std::string Config_XMLReader::pluginConfigFile()
65 char* anEnv = getenv("SHAPER_ROOT_DIR");
67 aValue = std::string(anEnv);
69 anEnv = getenv("OPENPARTS_ROOT_DIR");
71 aValue = std::string(anEnv);
82 void Config_XMLReader::readAll()
84 xmlNodePtr aRoot = findRoot();
85 readRecursively(aRoot);
88 void Config_XMLReader::processNode(xmlNodePtr theNode)
90 if (isNode(theNode, NODE_SOURCE, NULL)) {
91 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
92 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
93 readRecursively(aSourceReader.findRoot());
95 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
100 void Config_XMLReader::cleanup(xmlNodePtr)
105 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
110 xmlNodePtr Config_XMLReader::findRoot()
112 if (myXmlDoc == NULL) {
113 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
115 if (myXmlDoc == NULL) {
117 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
118 << " is not parsed successfully." << std::endl;
122 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
125 std::cout << "Config_XMLReader::import: " << "Error: empty document";
131 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
135 xmlNodePtr aNode = theParent->xmlChildrenNode;
136 for (; aNode; aNode = aNode->next) {
137 //Still no text processing in features...
138 if (!isElementNode(aNode)) {
142 if (processChildren(aNode)) {
143 readRecursively(aNode);
149 xmlNodePtr Config_XMLReader::node(void* theNode)
151 return static_cast<xmlNodePtr>(theNode);
154 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
156 std::string result = "";
157 char* aPropChars = (char*) theNode->name;
158 if (!aPropChars || aPropChars[0] == 0)
160 result = std::string(aPropChars);
164 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
166 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
167 std::string aValue = getProperty(theNode, theAttribute);
168 if (doClean || !aValue.empty()) {
169 myCachedAttributes[aKey] = aValue;
173 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
175 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
178 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
180 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
181 std::string result = "";
182 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
183 result = myCachedAttributes[aKey];
188 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
190 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
193 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
195 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
197 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
198 if( anEntry != myCachedAttributes.end()) {
199 myCachedAttributes.erase(anEntry);