Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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 void Config_XMLReader::cleanup(xmlNodePtr)
90 {
91   // do nothing;
92 }
93
94 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
95 {
96   return true;
97 }
98
99 xmlNodePtr Config_XMLReader::findRoot()
100 {
101   if (myXmlDoc == NULL) {
102     myXmlDoc = xmlParseFile(myDocumentPath.c_str());
103   }
104   if (myXmlDoc == NULL) {
105 #ifdef _DEBUG
106     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
107     << " is not parsed successfully." << std::endl;
108 #endif
109     return NULL;
110   }
111   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
112 #ifdef _DEBUG
113   if(aRoot == NULL) {
114     std::cout << "Config_XMLReader::import: " << "Error: empty document";
115   }
116 #endif
117   return aRoot;
118 }
119
120 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
121 {
122   if (!theParent)
123     return;
124   xmlNodePtr aNode = theParent->xmlChildrenNode;
125   for (; aNode; aNode = aNode->next) {
126     //Still no text processing in features...
127     if (!isElementNode(aNode)) {
128       continue;
129     }
130     processNode(aNode);
131     if (processChildren(aNode)) {
132       readRecursively(aNode);
133     }
134     cleanup(aNode);
135   }
136 }
137
138 xmlNodePtr Config_XMLReader::node(void* theNode)
139 {
140   return static_cast<xmlNodePtr>(theNode);
141 }
142
143 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
144 {
145   std::string result = "";
146   char* aPropChars = (char*) theNode->name;
147   if (!aPropChars || aPropChars[0] == 0)
148     return result;
149   result = std::string(aPropChars);
150   return result;
151 }
152
153 void Config_XMLReader::processValidator(xmlNodePtr theNode)
154 {
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));
167   } else {
168     aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
169     aMessage->setFeatureId(myCurrentFeature);
170   }
171   aEvLoop->send(aMessage);
172 }
173
174 void Config_XMLReader::processSelectionFilter(xmlNodePtr theNode)
175 {
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);
185
186   xmlNodePtr aFeatureOrWdgNode = theNode->parent;
187   if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
188     aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
189   } else {
190     aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
191     aMessage->setFeatureId(myCurrentFeature);
192   }
193   aEvLoop->send(aMessage);
194 }