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()
68 * Read all nodes in associated xml file,
69 * recursively if processChildren(xmlNode) is true for the xmlNode.
70 * For each read node the processNode will be called.
72 void Config_XMLReader::readAll()
74 xmlNodePtr aRoot = findRoot();
75 readRecursively(aRoot);
79 * Allows to customize reader's behavior for a node. Virtual.
80 * The default impl does nothing. (In debug mode prints
83 void Config_XMLReader::processNode(xmlNodePtr theNode)
85 if (isNode(theNode, NODE_SOURCE, NULL)) {
86 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
87 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
88 readRecursively(aSourceReader.findRoot());
90 std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
92 } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
93 processValidator(theNode);
94 } else if (isNode(theNode, NODE_SELFILTER, NULL)) {
95 processSelectionFilter(theNode);
100 * Defines which nodes should be processed recursively. Virtual.
101 * The default impl is to read all nodes.
103 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
111 xmlNodePtr Config_XMLReader::findRoot()
113 if (myXmlDoc == NULL) {
114 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
116 if (myXmlDoc == NULL) {
118 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
119 << " is not parsed successfully." << std::endl;
123 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
126 std::cout << "Config_XMLReader::import: " << "Error: empty document";
133 * Calls processNode() for each child (for some - recursively)
137 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
141 xmlNodePtr aNode = theParent->xmlChildrenNode;
142 for (; aNode; aNode = aNode->next) {
143 //Still no text processing in features...
144 if (!isElementNode(aNode)) {
148 if (processChildren(aNode)) {
149 readRecursively(aNode);
155 * void* -> xmlNodePtr
157 xmlNodePtr Config_XMLReader::node(void* theNode)
159 return static_cast<xmlNodePtr>(theNode);
162 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
164 std::string result = "";
165 char* aPropChars = (char*) theNode->name;
166 if (!aPropChars || aPropChars[0] == 0)
168 result = std::string(aPropChars);
172 void Config_XMLReader::processValidator(xmlNodePtr theNode)
174 Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
175 Events_Loop* aEvLoop = Events_Loop::loop();
176 std::shared_ptr<Config_ValidatorMessage>
177 aMessage(new Config_ValidatorMessage(aValidatoEvent, this));
178 std::string aValidatorId;
179 std::list<std::string> aParameters;
180 getParametersInfo(theNode, aValidatorId, aParameters);
181 aMessage->setValidatorId(aValidatorId);
182 aMessage->setValidatorParameters(aParameters);
183 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
184 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
185 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
187 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
188 aMessage->setFeatureId(myCurrentFeature);
190 aEvLoop->send(aMessage);
193 void Config_XMLReader::processSelectionFilter(xmlNodePtr theNode)
195 Events_ID aFilterEvent = Events_Loop::eventByName(EVENT_SELFILTER_LOADED);
196 Events_Loop* aEvLoop = Events_Loop::loop();
197 std::shared_ptr<Config_SelectionFilterMessage> aMessage =
198 std::make_shared<Config_SelectionFilterMessage>(aFilterEvent, this);
199 std::string aSelectionFilterId;
200 std::list<std::string> aParameters;
201 getParametersInfo(theNode, aSelectionFilterId, aParameters);
202 aMessage->setSelectionFilterId(aSelectionFilterId);
203 aMessage->setFilterParameters(aParameters);
205 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
206 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
207 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
209 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
210 aMessage->setFeatureId(myCurrentFeature);
212 aEvLoop->send(aMessage);