]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_Common.cpp
Salome HOME
Checking isInitialized for attributes is SketchPlugin and SketchSolver
[modules/shaper.git] / src / Config / Config_Common.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D\r
2 \r
3 /*\r
4  * Config_Common.cpp\r
5  *\r
6  *  Created on: Apr 17, 2014\r
7  *      Author: sbh\r
8  */\r
9 \r
10 #include "Config_Common.h"\r
11 #include <Config_Keywords.h>\r
12 \r
13 #include <libxml/parser.h>\r
14 #include <libxml/tree.h>\r
15 \r
16 #include <sstream> // for stringstream\r
17 \r
18 #include <string>\r
19 #include <algorithm> // for std::transform\r
20 #include <vector>\r
21 \r
22 bool isElementNode(xmlNodePtr theNode)\r
23 {\r
24   if (!theNode)\r
25     return false;\r
26   return theNode->type == XML_ELEMENT_NODE;\r
27 }\r
28 \r
29 bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)\r
30 {\r
31   const xmlChar* aName = theNode->name;\r
32   if (!aName || !isElementNode(theNode)) {\r
33     return false;\r
34   }\r
35   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
36     return true;\r
37   }\r
38   va_list args;  // define argument list variable\r
39   va_start(args, theNodeName);  // init list; point to last defined argument\r
40   while (true) {\r
41     char *anArg = va_arg (args, char*);  // get next argument\r
42     if (anArg == NULL)\r
43       break;\r
44     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
45       va_end(args);  // cleanup the system stack\r
46       return true;\r
47     }\r
48   }\r
49   va_end(args);  // cleanup the system stack\r
50   return false;\r
51 }\r
52 \r
53 bool isAttributeNode(xmlNodePtr theNode)\r
54 {\r
55   if(!isElementNode(theNode))\r
56     return false;\r
57   // it's parent is "feature" or "source" or page ("case" or "box")\r
58   if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, \r
59                          WDG_GROUP, WDG_OPTIONALBOX,\r
60                          WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))\r
61     return false;\r
62 \r
63   //it should not be a "source" or a "validator" node\r
64   bool isLogical = isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
65   // here must be only widgets not connected to attributes\r
66   bool isPagedContainer = isNode(theNode, WDG_TOOLBOX_BOX,\r
67                                           WDG_GROUP,\r
68                                           WDG_SWITCH_CASE,  NULL);\r
69   return !isLogical && !isPagedContainer;\r
70 }\r
71 \r
72 bool isWidgetNode(xmlNodePtr theNode)\r
73 {\r
74   if(!isElementNode(theNode))\r
75     return false;\r
76   // it's parent is "feature" or "source" or a page ("box", "case")\r
77   if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, WDG_GROUP, WDG_OPTIONALBOX,\r
78                          WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))\r
79     return false;\r
80 \r
81   //it should not be a "source" or a "validator" node\r
82   return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
83 }\r
84 \r
85 // widget api?\r
86 bool isCaseNode(xmlNodePtr theNode)\r
87 {\r
88   if(!isElementNode(theNode))\r
89     return false;\r
90 \r
91   return isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);\r
92 }\r
93 \r
94 bool hasChild(xmlNodePtr theNode)\r
95 {\r
96   xmlNodePtr aNode = theNode->children;\r
97   for (; aNode; aNode = aNode->next) {\r
98     if (isElementNode(theNode)) {\r
99       return true;\r
100     }\r
101   }\r
102   return false;\r
103 }\r
104 \r
105 bool hasParent(xmlNodePtr theNode)\r
106 {\r
107   xmlNodePtr aNode = theNode->parent;\r
108   if (!aNode) {\r
109     return false;\r
110   }\r
111   for (; aNode; aNode = aNode->next) {\r
112     if (isElementNode(theNode)) {\r
113       return true;\r
114     }\r
115   }\r
116   return false;\r
117 }\r
118 \r
119 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)\r
120 {\r
121   if (!hasParent(theNode)) {\r
122     return false; // have no parents at all\r
123   }\r
124   xmlNodePtr aNode = theNode->parent;\r
125   const xmlChar* aName = aNode->name;\r
126   if (!aName || !isElementNode(aNode)) {\r
127     return false;\r
128   }\r
129   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
130     return true;\r
131   }\r
132   va_list args;  // define argument list variable\r
133   va_start(args, theNodeName);  // init list; point to last defined argument\r
134   while (true) {\r
135     char *anArg = va_arg (args, char*);  // get next argument\r
136     if (anArg == NULL)\r
137       break;\r
138     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
139       va_end(args);  // cleanup the system stack\r
140       return true;\r
141     }\r
142   }\r
143   va_end(args);  // cleanup the system stack\r
144   return false;\r
145 }\r
146 \r
147 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)\r
148 {\r
149   if (!hasParent(theNode)) {\r
150     return 0; // have no parents at all\r
151   }\r
152   xmlNodePtr aNode = theNode->parent;\r
153   const xmlChar* aName = aNode->name;\r
154   if (!aName || !isElementNode(aNode)) {\r
155     return 0;\r
156   }\r
157   for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {\r
158     if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))\r
159       return aNode;\r
160   }\r
161   return hasParentRecursive(aNode, theNodeNames);\r
162 }\r
163 \r
164 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)\r
165 {\r
166   std::vector<const char*> aNodeNames;\r
167   va_list args;  // define argument list variable\r
168   va_start(args, theNodeName);  // init list; point to last defined argument\r
169   aNodeNames.push_back(theNodeName);\r
170   while (true) {\r
171     char *anArg = va_arg (args, char*);  // get next argument\r
172     if (anArg == NULL)\r
173       break;\r
174     aNodeNames.push_back(anArg);\r
175   }\r
176   va_end(args);  // cleanup the system stack\r
177   return hasParentRecursive(theNode, aNodeNames);\r
178 }\r
179 \r
180 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,\r
181                       std::list<std::string>& outValidatorParameters)\r
182 {\r
183   //Property id:\r
184   char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);\r
185   if (!anIdProp || anIdProp[0] == 0) {\r
186     return false;\r
187   }\r
188   outPropertyId = std::string(anIdProp);\r
189 \r
190   //Property parameters:\r
191   char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);\r
192   if (aParamProp && aParamProp[0] != 0) {\r
193     std::string aPropString = std::string(aParamProp);\r
194     std::stringstream aPropStringStream(aPropString);\r
195     char COMMA_DELIM = ',';\r
196     std::string aParameter;\r
197     while (std::getline(aPropStringStream, aParameter, ',')) {\r
198       outValidatorParameters.push_back(aParameter);\r
199     }\r
200   }\r
201   return true;\r
202 }\r
203 \r
204 std::string library(const std::string& theLibName)\r
205 {\r
206   if(theLibName.empty())\r
207     return std::string();\r
208   std::string aLibName = theLibName;\r
209 #ifndef WIN32\r
210   static std::string aLibExt( ".so" );\r
211   if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {\r
212     aLibName = "lib" + aLibName;\r
213   }\r
214 #else\r
215   static std::string aLibExt(".dll");\r
216 #endif\r
217   std::string anExt = aLibName.substr(aLibName.size() - 4);\r
218   if (anExt != aLibExt)\r
219     aLibName += aLibExt;\r
220 \r
221   return aLibName;\r
222 }\r
223 \r
224 bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }\r
225 \r
226 std::string getProperty(xmlNodePtr theNode, const char* thePropName)\r
227 {\r
228   std::string result = "";\r
229   xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);\r
230   if (!aPropChars || aPropChars[0] == 0)\r
231     return result;\r
232   result = std::string((char*)aPropChars);\r
233   xmlFree(aPropChars);\r
234 \r
235   std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);\r
236   result.erase(new_end, result.end()); \r
237 \r
238   return result;\r
239 }\r
240 \r
241 std::string getContent(xmlNodePtr theNode)\r
242 {\r
243   std::string result = "";\r
244   xmlChar* aContent = xmlNodeGetContent(theNode);\r
245   if (!aContent || aContent[0] == 0)\r
246     return result;\r
247   result = std::string((char*)aContent);\r
248   xmlFree(aContent);\r
249   return result;\r
250 }\r
251 \r
252 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)\r
253 {\r
254   return normalize(getProperty(theNode, thePropName));\r
255 }\r
256 \r
257 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)\r
258 {\r
259   std::string prop = normalize(getProperty(theNode, theAttributeName));\r
260   bool result = theDefault;\r
261   if (prop == "true" || prop == "1") {\r
262     result = true;\r
263   } else if (prop == "false" || prop == "0") {\r
264     result = false;\r
265   }\r
266   return result;\r
267 }\r
268 \r
269 CONFIG_EXPORT std::string normalize(const char* theString)\r
270 {\r
271   if (!theString)\r
272     return std::string();\r
273   return normalize(std::string(theString));\r
274 }\r
275 \r
276 CONFIG_EXPORT std::string normalize(const std::string& theString)\r
277 {\r
278   std::string result = theString;\r
279   std::transform(result.begin(), result.end(), result.begin(), ::tolower);\r
280   return result;\r
281 }\r