Salome HOME
Documentation for config module updated
[modules/shaper.git] / src / Config / Config_XMLReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_XMLReader.cpp
5  *
6  *  Created on: Mar 14, 2014
7  *      Author: sbh
8  */
9
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>
16
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
21
22 #include <fstream>
23
24 #ifdef WIN32
25 #pragma warning(disable : 4996) // for getenv
26 #endif
27
28 #ifdef _DEBUG
29 #include <iostream>
30 #endif
31
32 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
33     : myXmlDoc(NULL)
34 {
35   std::string prefix = ""; 
36   Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
37   if (aProp)
38     prefix = aProp->value();
39   /*
40    * Get path to *.xml files (typically ./bin/../plugins/)
41
42    * the problem: application may be launched using python executable,
43    * to use environment variable (at least for the current moment)
44    */
45   if (prefix.empty()) {
46     char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
47     if (anEnv) {
48       prefix = std::string(anEnv);
49     }
50   }
51 #ifdef WIN32
52     prefix += "\\";
53 #else
54     prefix += "/";
55 #endif
56   myDocumentPath = prefix + theXmlFileName;
57   std::ifstream aTestFile(myDocumentPath);
58   if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
59   aTestFile.close();
60 }
61
62 Config_XMLReader::~Config_XMLReader()
63 {
64   xmlFreeDoc(myXmlDoc);
65 }
66
67 void Config_XMLReader::readAll()
68 {
69   xmlNodePtr aRoot = findRoot();
70   readRecursively(aRoot);
71 }
72
73 void Config_XMLReader::processNode(xmlNodePtr theNode)
74 {
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());
79 #ifdef _DEBUG
80     std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
81 #endif
82   } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
83     processValidator(theNode);
84   } else if (isNode(theNode, NODE_SELFILTER, NULL)) {
85     processSelectionFilter(theNode);
86   }
87 }
88
89 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
90 {
91   return true;
92 }
93
94 xmlNodePtr Config_XMLReader::findRoot()
95 {
96   if (myXmlDoc == NULL) {
97     myXmlDoc = xmlParseFile(myDocumentPath.c_str());
98   }
99   if (myXmlDoc == NULL) {
100 #ifdef _DEBUG
101     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
102     << " is not parsed successfully." << std::endl;
103 #endif
104     return NULL;
105   }
106   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
107 #ifdef _DEBUG
108   if(aRoot == NULL) {
109     std::cout << "Config_XMLReader::import: " << "Error: empty document";
110   }
111 #endif
112   return aRoot;
113 }
114
115 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
116 {
117   if (!theParent)
118     return;
119   xmlNodePtr aNode = theParent->xmlChildrenNode;
120   for (; aNode; aNode = aNode->next) {
121     //Still no text processing in features...
122     if (!isElementNode(aNode)) {
123       continue;
124     }
125     processNode(aNode);
126     if (processChildren(aNode)) {
127       readRecursively(aNode);
128     }
129   }
130 }
131
132 xmlNodePtr Config_XMLReader::node(void* theNode)
133 {
134   return static_cast<xmlNodePtr>(theNode);
135 }
136
137 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
138 {
139   std::string result = "";
140   char* aPropChars = (char*) theNode->name;
141   if (!aPropChars || aPropChars[0] == 0)
142     return result;
143   result = std::string(aPropChars);
144   return result;
145 }
146
147 void Config_XMLReader::processValidator(xmlNodePtr theNode)
148 {
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));
161   } else {
162     aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
163     aMessage->setFeatureId(myCurrentFeature);
164   }
165   aEvLoop->send(aMessage);
166 }
167
168 void Config_XMLReader::processSelectionFilter(xmlNodePtr theNode)
169 {
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);
179
180   xmlNodePtr aFeatureOrWdgNode = theNode->parent;
181   if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
182     aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
183   } else {
184     aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
185     aMessage->setFeatureId(myCurrentFeature);
186   }
187   aEvLoop->send(aMessage);
188 }