Salome HOME
Merge branch 'BR_PYTHON_PLUGIN' of newgeom:newgeom.git into Dev_0.6.1
[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 /*
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.
71  */
72 void Config_XMLReader::readAll()
73 {
74   xmlNodePtr aRoot = findRoot();
75   readRecursively(aRoot);
76 }
77
78 /*
79  * Allows to customize reader's behavior for a node. Virtual.
80  * The default impl does nothing. (In debug mode prints
81  * some info)
82  */
83 void Config_XMLReader::processNode(xmlNodePtr theNode)
84 {
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());
89 #ifdef _DEBUG
90     std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
91 #endif
92   } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
93     processValidator(theNode);
94   } else if (isNode(theNode, NODE_SELFILTER, NULL)) {
95     processSelectionFilter(theNode);
96   }
97 }
98
99 /*
100  * Defines which nodes should be processed recursively. Virtual.
101  * The default impl is to read all nodes.
102  */
103 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
104 {
105   return true;
106 }
107
108 /*
109  *
110  */
111 xmlNodePtr Config_XMLReader::findRoot()
112 {
113   if (myXmlDoc == NULL) {
114     myXmlDoc = xmlParseFile(myDocumentPath.c_str());
115   }
116   if (myXmlDoc == NULL) {
117 #ifdef _DEBUG
118     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
119     << " is not parsed successfully." << std::endl;
120 #endif
121     return NULL;
122   }
123   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
124 #ifdef _DEBUG
125   if(aRoot == NULL) {
126     std::cout << "Config_XMLReader::import: " << "Error: empty document";
127   }
128 #endif
129   return aRoot;
130 }
131
132 /*
133  * Calls processNode() for each child (for some - recursively)
134  * of the given node.
135  * \sa ReadAll()
136  */
137 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
138 {
139   if (!theParent)
140     return;
141   xmlNodePtr aNode = theParent->xmlChildrenNode;
142   for (; aNode; aNode = aNode->next) {
143     //Still no text processing in features...
144     if (!isElementNode(aNode)) {
145       continue;
146     }
147     processNode(aNode);
148     if (processChildren(aNode)) {
149       readRecursively(aNode);
150     }
151   }
152 }
153
154 /*
155  * void* -> xmlNodePtr
156  */
157 xmlNodePtr Config_XMLReader::node(void* theNode)
158 {
159   return static_cast<xmlNodePtr>(theNode);
160 }
161
162 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
163 {
164   std::string result = "";
165   char* aPropChars = (char*) theNode->name;
166   if (!aPropChars || aPropChars[0] == 0)
167     return result;
168   result = std::string(aPropChars);
169   return result;
170 }
171
172 void Config_XMLReader::processValidator(xmlNodePtr theNode)
173 {
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));
186   } else {
187     aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
188     aMessage->setFeatureId(myCurrentFeature);
189   }
190   aEvLoop->send(aMessage);
191 }
192
193 void Config_XMLReader::processSelectionFilter(xmlNodePtr theNode)
194 {
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);
204
205   xmlNodePtr aFeatureOrWdgNode = theNode->parent;
206   if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
207     aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
208   } else {
209     aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
210     aMessage->setFeatureId(myCurrentFeature);
211   }
212   aEvLoop->send(aMessage);
213 }