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 char* anEnv = getenv("PLUGINS_CONFIG_FILE");
46 prefix = std::string(anEnv);
54 myDocumentPath = prefix + theXmlFileName;
55 std::ifstream aTestFile(myDocumentPath);
56 if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
60 Config_XMLReader::~Config_XMLReader()
65 void Config_XMLReader::readAll()
67 xmlNodePtr aRoot = findRoot();
68 readRecursively(aRoot);
71 void Config_XMLReader::processNode(xmlNodePtr theNode)
73 if (isNode(theNode, NODE_SOURCE, NULL)) {
74 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
75 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
76 readRecursively(aSourceReader.findRoot());
78 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
83 void Config_XMLReader::cleanup(xmlNodePtr)
88 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
93 xmlNodePtr Config_XMLReader::findRoot()
95 if (myXmlDoc == NULL) {
96 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
98 if (myXmlDoc == NULL) {
100 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
101 << " is not parsed successfully." << std::endl;
105 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
108 std::cout << "Config_XMLReader::import: " << "Error: empty document";
114 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
118 xmlNodePtr aNode = theParent->xmlChildrenNode;
119 for (; aNode; aNode = aNode->next) {
120 //Still no text processing in features...
121 if (!isElementNode(aNode)) {
125 if (processChildren(aNode)) {
126 readRecursively(aNode);
132 xmlNodePtr Config_XMLReader::node(void* theNode)
134 return static_cast<xmlNodePtr>(theNode);
137 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
139 std::string result = "";
140 char* aPropChars = (char*) theNode->name;
141 if (!aPropChars || aPropChars[0] == 0)
143 result = std::string(aPropChars);
147 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
149 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
150 std::string aValue = getProperty(theNode, theAttribute);
151 if (doClean || !aValue.empty()) {
152 myCachedAttributes[aKey] = aValue;
156 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
158 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
161 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
163 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
164 std::string result = "";
165 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
166 result = myCachedAttributes[aKey];
171 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
173 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
176 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
178 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
180 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
181 if( anEntry != myCachedAttributes.end()) {
182 myCachedAttributes.erase(anEntry);