Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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 \r
21 bool isElementNode(xmlNodePtr theNode)\r
22 {\r
23   if (!theNode)\r
24     return false;\r
25   return theNode->type == XML_ELEMENT_NODE;\r
26 }\r
27 \r
28 bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)\r
29 {\r
30   const xmlChar* aName = theNode->name;\r
31   if (!aName || !isElementNode(theNode)) {\r
32     return false;\r
33   }\r
34   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
35     return true;\r
36   }\r
37   va_list args;  // define argument list variable\r
38   va_start(args, theNodeName);  // init list; point to last defined argument\r
39   while (true) {\r
40     char *anArg = va_arg (args, char*);  // get next argument\r
41     if (anArg == NULL)\r
42       break;\r
43     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
44       va_end(args);  // cleanup the system stack\r
45       return true;\r
46     }\r
47   }\r
48   va_end(args);  // cleanup the system stack\r
49   return false;\r
50 }\r
51 \r
52 bool isAttributeNode(xmlNodePtr theNode)\r
53 {\r
54   if(!isElementNode(theNode))\r
55     return false;\r
56   // it's parent is "feature" or "source" or page ("case" or "box")\r
57   if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, \r
58                          WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))\r
59     return false;\r
60 \r
61   //it should not be a "source" or a "validator" node\r
62   bool isLogical = isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
63   bool isPagedContainer = isNode(theNode, WDG_TOOLBOX, WDG_TOOLBOX_BOX,\r
64                                           WDG_SWITCH, WDG_SWITCH_CASE,  NULL);\r
65   return !isLogical && !isPagedContainer;\r
66 }\r
67 \r
68 bool isWidgetNode(xmlNodePtr theNode)\r
69 {\r
70   if(!isElementNode(theNode))\r
71     return false;\r
72   // it's parent is "feature" or "source" or a page ("box", "case")\r
73   if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, \r
74                          WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))\r
75     return false;\r
76 \r
77   //it should not be a "source" or a "validator" node\r
78   return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);\r
79 }\r
80 \r
81 // widget api?\r
82 bool isCaseNode(xmlNodePtr theNode)\r
83 {\r
84   if(!isElementNode(theNode))\r
85     return false;\r
86 \r
87   return isNode(theNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);\r
88 }\r
89 \r
90 bool hasChild(xmlNodePtr theNode)\r
91 {\r
92   xmlNodePtr aNode = theNode->children;\r
93   for (; aNode; aNode = aNode->next) {\r
94     if (isElementNode(theNode)) {\r
95       return true;\r
96     }\r
97   }\r
98   return false;\r
99 }\r
100 \r
101 bool hasParent(xmlNodePtr theNode)\r
102 {\r
103   xmlNodePtr aNode = theNode->parent;\r
104   if (!aNode) {\r
105     return false;\r
106   }\r
107   for (; aNode; aNode = aNode->next) {\r
108     if (isElementNode(theNode)) {\r
109       return true;\r
110     }\r
111   }\r
112   return false;\r
113 }\r
114 \r
115 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)\r
116 {\r
117   if (!hasParent(theNode)) {\r
118     return false; // have no parents at all\r
119   }\r
120   xmlNodePtr aNode = theNode->parent;\r
121   const xmlChar* aName = aNode->name;\r
122   if (!aName || !isElementNode(aNode)) {\r
123     return false;\r
124   }\r
125   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
126     return true;\r
127   }\r
128   va_list args;  // define argument list variable\r
129   va_start(args, theNodeName);  // init list; point to last defined argument\r
130   while (true) {\r
131     char *anArg = va_arg (args, char*);  // get next argument\r
132     if (anArg == NULL)\r
133       break;\r
134     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
135       va_end(args);  // cleanup the system stack\r
136       return true;\r
137     }\r
138   }\r
139   va_end(args);  // cleanup the system stack\r
140   return false;\r
141 }\r
142 \r
143 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,\r
144                       std::list<std::string>& outValidatorParameters)\r
145 {\r
146   //Property id:\r
147   char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);\r
148   if (!anIdProp || anIdProp[0] == 0) {\r
149     return false;\r
150   }\r
151   outPropertyId = std::string(anIdProp);\r
152 \r
153   //Property parameters:\r
154   char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);\r
155   if (aParamProp && aParamProp[0] != 0) {\r
156     std::string aPropString = std::string(aParamProp);\r
157     std::stringstream aPropStringStream(aPropString);\r
158     char COMMA_DELIM = ',';\r
159     std::string aParameter;\r
160     while (std::getline(aPropStringStream, aParameter, ',')) {\r
161       outValidatorParameters.push_back(aParameter);\r
162     }\r
163   }\r
164   return true;\r
165 }\r
166 \r
167 std::string library(const std::string& theLibName)\r
168 {\r
169   if(theLibName.empty())\r
170     return std::string();\r
171   std::string aLibName = theLibName;\r
172 #ifndef WIN32\r
173   static std::string aLibExt( ".so" );\r
174   if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {\r
175     aLibName = "lib" + aLibName;\r
176   }\r
177 #else\r
178   static std::string aLibExt(".dll");\r
179 #endif\r
180   std::string anExt = aLibName.substr(aLibName.size() - 4);\r
181   if (anExt != aLibExt)\r
182     aLibName += aLibExt;\r
183 \r
184   return aLibName;\r
185 }\r
186 \r
187 std::string getProperty(xmlNodePtr theNode, const char* thePropName)\r
188 {\r
189   std::string result = "";\r
190   char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST thePropName);\r
191   if (!aPropChars || aPropChars[0] == 0)\r
192     return result;\r
193   result = std::string(aPropChars);\r
194   return result;\r
195 }\r
196 \r
197 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)\r
198 {\r
199   return normalize(getProperty(theNode, thePropName));\r
200 }\r
201 \r
202 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)\r
203 {\r
204   std::string prop = normalize(getProperty(theNode, theAttributeName));\r
205   bool result = theDefault;\r
206   if (prop == "true" || prop == "1") {\r
207     result = true;\r
208   } else if (prop == "false" || prop == "0") {\r
209     result = false;\r
210   }\r
211   return result;\r
212 }\r
213 \r
214 CONFIG_EXPORT std::string normalize(const char* theString)\r
215 {\r
216   if (!theString)\r
217     return std::string();\r
218   return normalize(std::string(theString));\r
219 }\r
220 \r
221 CONFIG_EXPORT std::string normalize(const std::string& theString)\r
222 {\r
223   std::string result = theString;\r
224   std::transform(result.begin(), result.end(), result.begin(), ::tolower);\r
225   return result;\r
226 }\r