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 void Config_XMLReader::cleanup(xmlNodePtr)
94 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
99 xmlNodePtr Config_XMLReader::findRoot()
101 if (myXmlDoc == NULL) {
102 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
104 if (myXmlDoc == NULL) {
106 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
107 << " is not parsed successfully." << std::endl;
111 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
114 std::cout << "Config_XMLReader::import: " << "Error: empty document";
120 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
124 xmlNodePtr aNode = theParent->xmlChildrenNode;
125 for (; aNode; aNode = aNode->next) {
126 //Still no text processing in features...
127 if (!isElementNode(aNode)) {
131 if (processChildren(aNode)) {
132 readRecursively(aNode);
138 xmlNodePtr Config_XMLReader::node(void* theNode)
140 return static_cast<xmlNodePtr>(theNode);
143 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
145 std::string result = "";
146 char* aPropChars = (char*) theNode->name;
147 if (!aPropChars || aPropChars[0] == 0)
149 result = std::string(aPropChars);
153 void Config_XMLReader::processValidator(xmlNodePtr theNode)
155 Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
156 Events_Loop* aEvLoop = Events_Loop::loop();
157 std::shared_ptr<Config_ValidatorMessage>
158 aMessage(new Config_ValidatorMessage(aValidatoEvent, this));
159 std::string aValidatorId;
160 std::list<std::string> aParameters;
161 getParametersInfo(theNode, aValidatorId, aParameters);
162 aMessage->setValidatorId(aValidatorId);
163 aMessage->setValidatorParameters(aParameters);
164 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
165 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
166 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
168 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
169 aMessage->setFeatureId(myCurrentFeature);
171 aEvLoop->send(aMessage);
174 void Config_XMLReader::processSelectionFilter(xmlNodePtr theNode)
176 Events_ID aFilterEvent = Events_Loop::eventByName(EVENT_SELFILTER_LOADED);
177 Events_Loop* aEvLoop = Events_Loop::loop();
178 std::shared_ptr<Config_SelectionFilterMessage> aMessage =
179 std::make_shared<Config_SelectionFilterMessage>(aFilterEvent, this);
180 std::string aSelectionFilterId;
181 std::list<std::string> aParameters;
182 getParametersInfo(theNode, aSelectionFilterId, aParameters);
183 aMessage->setSelectionFilterId(aSelectionFilterId);
184 aMessage->setFilterParameters(aParameters);
186 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
187 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
188 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
190 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
191 aMessage->setFeatureId(myCurrentFeature);
193 aEvLoop->send(aMessage);