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("PLUGINS_CONFIG_FILE");
67 aValue = std::string(anEnv);
72 void Config_XMLReader::readAll()
74 xmlNodePtr aRoot = findRoot();
75 readRecursively(aRoot);
78 void Config_XMLReader::processNode(xmlNodePtr theNode)
80 if (isNode(theNode, NODE_SOURCE, NULL)) {
81 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
82 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
83 readRecursively(aSourceReader.findRoot());
85 //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
90 void Config_XMLReader::cleanup(xmlNodePtr)
95 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
100 xmlNodePtr Config_XMLReader::findRoot()
102 if (myXmlDoc == NULL) {
103 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
105 if (myXmlDoc == NULL) {
107 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
108 << " is not parsed successfully." << std::endl;
112 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
115 std::cout << "Config_XMLReader::import: " << "Error: empty document";
121 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
125 xmlNodePtr aNode = theParent->xmlChildrenNode;
126 for (; aNode; aNode = aNode->next) {
127 //Still no text processing in features...
128 if (!isElementNode(aNode)) {
132 if (processChildren(aNode)) {
133 readRecursively(aNode);
139 xmlNodePtr Config_XMLReader::node(void* theNode)
141 return static_cast<xmlNodePtr>(theNode);
144 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
146 std::string result = "";
147 char* aPropChars = (char*) theNode->name;
148 if (!aPropChars || aPropChars[0] == 0)
150 result = std::string(aPropChars);
154 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute, bool doClean)
156 std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
157 std::string aValue = getProperty(theNode, theAttribute);
158 if (doClean || !aValue.empty()) {
159 myCachedAttributes[aKey] = aValue;
163 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
165 return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
168 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
170 std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
171 std::string result = "";
172 if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
173 result = myCachedAttributes[aKey];
178 bool Config_XMLReader::cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute)
180 return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
183 bool Config_XMLReader::cleanupAttribute(const char* theNodeName, const char* theNodeAttribute)
185 std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
187 std::map<std::string, std::string>::iterator anEntry = myCachedAttributes.find(aKey);
188 if( anEntry != myCachedAttributes.end()) {
189 myCachedAttributes.erase(anEntry);