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_ValidatorMessage.h>
14 #include <Config_SelectionFilterMessage.h>
15 #include <Config_PropManager.h>
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
25 #pragma warning(disable : 4996) // for getenv
32 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
35 std::string prefix = "";
36 Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
38 prefix = aProp->value();
40 * Get path to *.xml files (typically ./bin/../plugins/)
42 * the problem: application may be launched using python executable,
43 * to use environment variable (at least for the current moment)
46 char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
48 prefix = std::string(anEnv);
56 myDocumentPath = prefix + theXmlFileName;
57 std::ifstream aTestFile(myDocumentPath);
58 if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
62 Config_XMLReader::~Config_XMLReader()
67 void Config_XMLReader::readAll()
69 xmlNodePtr aRoot = findRoot();
70 readRecursively(aRoot);
73 void Config_XMLReader::processNode(xmlNodePtr theNode)
75 if (isNode(theNode, NODE_SOURCE, NULL)) {
76 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
77 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
78 readRecursively(aSourceReader.findRoot());
80 std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
82 } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
83 processValidator(theNode);
84 } else if (isNode(theNode, NODE_SELFILTER, NULL)) {
85 processSelectionFilter(theNode);
89 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
94 xmlNodePtr Config_XMLReader::findRoot()
96 if (myXmlDoc == NULL) {
97 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
99 if (myXmlDoc == NULL) {
101 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
102 << " is not parsed successfully." << std::endl;
106 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
109 std::cout << "Config_XMLReader::import: " << "Error: empty document";
115 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
119 xmlNodePtr aNode = theParent->xmlChildrenNode;
120 for (; aNode; aNode = aNode->next) {
121 //Still no text processing in features...
122 if (!isElementNode(aNode)) {
126 if (processChildren(aNode)) {
127 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::processValidator(xmlNodePtr theNode)
149 Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
150 Events_Loop* aEvLoop = Events_Loop::loop();
151 std::shared_ptr<Config_ValidatorMessage>
152 aMessage(new Config_ValidatorMessage(aValidatoEvent, this));
153 std::string aValidatorId;
154 std::list<std::string> aParameters;
155 getParametersInfo(theNode, aValidatorId, aParameters);
156 aMessage->setValidatorId(aValidatorId);
157 aMessage->setValidatorParameters(aParameters);
158 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
159 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
160 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
162 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
163 aMessage->setFeatureId(myCurrentFeature);
165 aEvLoop->send(aMessage);
168 void Config_XMLReader::processSelectionFilter(xmlNodePtr theNode)
170 Events_ID aFilterEvent = Events_Loop::eventByName(EVENT_SELFILTER_LOADED);
171 Events_Loop* aEvLoop = Events_Loop::loop();
172 std::shared_ptr<Config_SelectionFilterMessage> aMessage =
173 std::make_shared<Config_SelectionFilterMessage>(aFilterEvent, this);
174 std::string aSelectionFilterId;
175 std::list<std::string> aParameters;
176 getParametersInfo(theNode, aSelectionFilterId, aParameters);
177 aMessage->setSelectionFilterId(aSelectionFilterId);
178 aMessage->setFilterParameters(aParameters);
180 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
181 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
182 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
184 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
185 aMessage->setFeatureId(myCurrentFeature);
187 aEvLoop->send(aMessage);