Salome HOME
The obligatory and concealment attribute's properties now registers in model by speci...
[modules/shaper.git] / src / Config / Config_Common.cpp
1 /*\r
2  * Config_Common.cpp\r
3  *\r
4  *  Created on: Apr 17, 2014\r
5  *      Author: sbh\r
6  */\r
7 \r
8 #include "Config_Common.h"\r
9 #include <Config_Keywords.h>\r
10 \r
11 #include <libxml/parser.h>\r
12 #include <libxml/tree.h>\r
13 \r
14 #include <sstream> // for stringstream\r
15 \r
16 #include <string>\r
17 #include <algorithm> // for std::transform\r\r
18 bool isElementNode(xmlNodePtr theNode)\r
19 {\r
20   return theNode->type == XML_ELEMENT_NODE;\r
21 }\r
22 \r
23 bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)\r
24 {\r
25   bool result = false;\r
26   const xmlChar* aName = theNode->name;\r
27   if (!aName || !isElementNode(theNode)) {\r
28     return false;\r
29   }\r
30   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {\r
31     return true;\r
32   }\r
33   va_list args;  // define argument list variable\r
34   va_start(args, theNodeName);  // init list; point to last defined argument\r
35   while (true) {\r
36     char *anArg = va_arg (args, char*);  // get next argument\r
37     if (anArg == NULL)\r
38       break;\r
39     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {\r
40       va_end(args);  // cleanup the system stack\r
41       return true;\r
42     }\r
43   }\r
44   va_end(args);  // cleanup the system stack\r
45   return false;\r
46 }\r
47 \r
48 bool isWidgetNode(xmlNodePtr theNode)\r
49 {\r
50   if(!isElementNode(theNode))\r
51     return false;\r
52   // it's parent is "feature" or "source"\r
53   xmlNodePtr aParentNode = theNode->parent;\r
54   if(!isNode(aParentNode, NODE_FEATURE, NODE_SOURCE, NULL))\r
55     return false;\r
56 \r
57   //it should not be a "source" or a "validator" node\r
58   return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NULL);\r
59 }\r
60 \r
61 bool hasChild(xmlNodePtr theNode)\r
62 {\r
63   xmlNodePtr aNode = theNode->children;\r
64   for (; aNode; aNode = aNode->next) {\r
65     if (isElementNode(theNode)) {\r
66       return true;\r
67     }\r
68   }\r
69   return false;\r
70 }\r
71 \r
72 bool getValidatorInfo(xmlNodePtr theNode, std::string& outValidatorId,\r
73                       std::list<std::string>& outValidatorParameters)\r
74 {\r
75   //Validator id:\r
76   char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);\r
77   if (!anIdProp || anIdProp[0] == 0) {\r
78     return false;\r
79   }\r
80   outValidatorId = std::string(anIdProp);\r
81 \r
82   //Validator parameters:\r
83   char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST VALIDATOR_PARAMETERS);\r
84   if (aParamProp && aParamProp[0] != 0) {\r
85     std::string aPropString = std::string(aParamProp);\r
86     std::stringstream aPropStringStream(aPropString);\r
87     char COMMA_DELIM = ',';\r
88     std::string aValidatorParameter;\r
89     while (std::getline(aPropStringStream, aValidatorParameter, ',')) {\r
90       outValidatorParameters.push_back(aValidatorParameter);\r
91     }\r
92   }\r
93   return true;\r
94 }\r
95 \r
96 std::string library(const std::string& theLibName)\r
97 {\r
98   std::string aLibName = theLibName;\r
99 #ifndef WIN32\r
100   static std::string aLibExt( ".so" );\r
101   if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {\r
102     aLibName = "lib" + aLibName;\r
103   }\r
104 #else\r
105   static std::string aLibExt(".dll");\r
106 #endif\r
107   std::string anExt = aLibName.substr(aLibName.size() - 4);\r
108   if (anExt != aLibExt)\r
109     aLibName += aLibExt;\r
110 \r
111   return aLibName;\r
112 }\r
113 \r
114 std::string getProperty(xmlNodePtr theNode, const char* thePropName)\r
115 {\r
116   std::string result = "";\r
117   char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST thePropName);\r
118   if (!aPropChars || aPropChars[0] == 0)\r
119     return result;\r
120   result = std::string(aPropChars);\r
121   return result;\r
122 }\r
123 \r
124 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)\r
125 {\r
126   std::string prop = getProperty(theNode, theAttributeName);\r
127   std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);\r
128   bool result = theDefault;\r
129   if (prop == "true" || prop == "1") {\r
130     result = true;\r
131   } else if (prop == "false" || prop == "0") {\r
132     result = false;\r
133   }\r
134   return result;\r
135 }\r