Salome HOME
Merge branch 'BR_internationalization'
[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_CHECK_GROUP,\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   bool isPagedContainer = isNode(theNode, WDG_TOOLBOX, WDG_TOOLBOX_BOX,\r
66                                           WDG_GROUP, WDG_CHECK_GROUP,\r
67                                           WDG_SWITCH, WDG_SWITCH_CASE,  NULL);\r
68   return !isLogical && !isPagedContainer;\r
69 }\r
70 \r
71 bool isWidgetNode(xmlNodePtr theNode)\r
72 {\r
73   if(!isElementNode(theNode))\r
74     return false;\r
75   // it's parent is "feature" or "source" or a page ("box", "case")\r
76   if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, WDG_GROUP,\r
77                          WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))\r
78     return false;\r
79 \r
80   //it should not be a "source" or a "validator" node\r
81   return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
82 }\r
83 \r
84 // widget api?\r
85 bool isCaseNode(xmlNodePtr theNode)\r
86 {\r
87   if(!isElementNode(theNode))\r
88     return false;\r
89 \r
90   return isNode(theNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);\r
91 }\r
92 \r
93 bool hasChild(xmlNodePtr theNode)\r
94 {\r
95   xmlNodePtr aNode = theNode->children;\r
96   for (; aNode; aNode = aNode->next) {\r
97     if (isElementNode(theNode)) {\r
98       return true;\r
99     }\r
100   }\r
101   return false;\r
102 }\r
103 \r
104 bool hasParent(xmlNodePtr theNode)\r
105 {\r
106   xmlNodePtr aNode = theNode->parent;\r
107   if (!aNode) {\r
108     return false;\r
109   }\r
110   for (; aNode; aNode = aNode->next) {\r
111     if (isElementNode(theNode)) {\r
112       return true;\r
113     }\r
114   }\r
115   return false;\r
116 }\r
117 \r
118 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)\r
119 {\r
120   if (!hasParent(theNode)) {\r
121     return false; // have no parents at all\r
122   }\r
123   xmlNodePtr aNode = theNode->parent;\r
124   const xmlChar* aName = aNode->name;\r
125   if (!aName || !isElementNode(aNode)) {\r
126     return false;\r
127   }\r
128   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
129     return true;\r
130   }\r
131   va_list args;  // define argument list variable\r
132   va_start(args, theNodeName);  // init list; point to last defined argument\r
133   while (true) {\r
134     char *anArg = va_arg (args, char*);  // get next argument\r
135     if (anArg == NULL)\r
136       break;\r
137     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
138       va_end(args);  // cleanup the system stack\r
139       return true;\r
140     }\r
141   }\r
142   va_end(args);  // cleanup the system stack\r
143   return false;\r
144 }\r
145 \r
146 bool hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)\r
147 {\r
148   if (!hasParent(theNode)) {\r
149     return false; // have no parents at all\r
150   }\r
151   xmlNodePtr aNode = theNode->parent;\r
152   const xmlChar* aName = aNode->name;\r
153   if (!aName || !isElementNode(aNode)) {\r
154     return false;\r
155   }\r
156   for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {\r
157     if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))\r
158       return true;\r
159   }\r
160   return hasParentRecursive(aNode, theNodeNames);\r
161 }\r
162 \r
163 bool hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)\r
164 {\r
165   std::vector<const char*> aNodeNames;\r
166   va_list args;  // define argument list variable\r
167   va_start(args, theNodeName);  // init list; point to last defined argument\r
168   aNodeNames.push_back(theNodeName);\r
169   while (true) {\r
170     char *anArg = va_arg (args, char*);  // get next argument\r
171     if (anArg == NULL)\r
172       break;\r
173     aNodeNames.push_back(anArg);\r
174   }\r
175   va_end(args);  // cleanup the system stack\r
176   return hasParentRecursive(theNode, aNodeNames);\r
177 }\r
178 \r
179 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,\r
180                       std::list<std::string>& outValidatorParameters)\r
181 {\r
182   //Property id:\r
183   char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);\r
184   if (!anIdProp || anIdProp[0] == 0) {\r
185     return false;\r
186   }\r
187   outPropertyId = std::string(anIdProp);\r
188 \r
189   //Property parameters:\r
190   char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);\r
191   if (aParamProp && aParamProp[0] != 0) {\r
192     std::string aPropString = std::string(aParamProp);\r
193     std::stringstream aPropStringStream(aPropString);\r
194     char COMMA_DELIM = ',';\r
195     std::string aParameter;\r
196     while (std::getline(aPropStringStream, aParameter, ',')) {\r
197       outValidatorParameters.push_back(aParameter);\r
198     }\r
199   }\r
200   return true;\r
201 }\r
202 \r
203 std::string library(const std::string& theLibName)\r
204 {\r
205   if(theLibName.empty())\r
206     return std::string();\r
207   std::string aLibName = theLibName;\r
208 #ifndef WIN32\r
209   static std::string aLibExt( ".so" );\r
210   if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {\r
211     aLibName = "lib" + aLibName;\r
212   }\r
213 #else\r
214   static std::string aLibExt(".dll");\r
215 #endif\r
216   std::string anExt = aLibName.substr(aLibName.size() - 4);\r
217   if (anExt != aLibExt)\r
218     aLibName += aLibExt;\r
219 \r
220   return aLibName;\r
221 }\r
222 \r
223 bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }\r
224 \r
225 std::string getProperty(xmlNodePtr theNode, const char* thePropName)\r
226 {\r
227   std::string result = "";\r
228   xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);\r
229   if (!aPropChars || aPropChars[0] == 0)\r
230     return result;\r
231   result = std::string((char*)aPropChars);\r
232   xmlFree(aPropChars);\r
233 \r
234   std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);\r
235   result.erase(new_end, result.end()); \r
236 \r
237   return result;\r
238 }\r
239 \r
240 std::string getContent(xmlNodePtr theNode)\r
241 {\r
242   std::string result = "";\r
243   xmlChar* aContent = xmlNodeGetContent(theNode);\r
244   if (!aContent || aContent[0] == 0)\r
245     return result;\r
246   result = std::string((char*)aContent);\r
247   xmlFree(aContent);\r
248   return result;\r
249 }\r
250 \r
251 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)\r
252 {\r
253   return normalize(getProperty(theNode, thePropName));\r
254 }\r
255 \r
256 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)\r
257 {\r
258   std::string prop = normalize(getProperty(theNode, theAttributeName));\r
259   bool result = theDefault;\r
260   if (prop == "true" || prop == "1") {\r
261     result = true;\r
262   } else if (prop == "false" || prop == "0") {\r
263     result = false;\r
264   }\r
265   return result;\r
266 }\r
267 \r
268 CONFIG_EXPORT std::string normalize(const char* theString)\r
269 {\r
270   if (!theString)\r
271     return std::string();\r
272   return normalize(std::string(theString));\r
273 }\r
274 \r
275 CONFIG_EXPORT std::string normalize(const std::string& theString)\r
276 {\r
277   std::string result = theString;\r
278   std::transform(result.begin(), result.end(), result.begin(), ::tolower);\r
279   return result;\r
280 }\r