1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
\r
6 * Created on: Apr 17, 2014
\r
10 #include "Config_Common.h"
\r
11 #include <Config_Keywords.h>
\r
13 #include <libxml/parser.h>
\r
14 #include <libxml/tree.h>
\r
16 #include <sstream> // for stringstream
\r
19 #include <algorithm> // for std::transform
\r
22 bool isElementNode(xmlNodePtr theNode)
\r
26 return theNode->type == XML_ELEMENT_NODE;
\r
29 bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)
\r
31 const xmlChar* aName = theNode->name;
\r
32 if (!aName || !isElementNode(theNode)) {
\r
35 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
\r
38 va_list args; // define argument list variable
\r
39 va_start(args, theNodeName); // init list; point to last defined argument
\r
41 char *anArg = va_arg (args, char*); // get next argument
\r
44 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
\r
45 va_end(args); // cleanup the system stack
\r
49 va_end(args); // cleanup the system stack
\r
53 bool isAttributeNode(xmlNodePtr theNode)
\r
55 if(!isElementNode(theNode))
\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
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
71 bool isWidgetNode(xmlNodePtr theNode)
\r
73 if(!isElementNode(theNode))
\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
80 //it should not be a "source" or a "validator" node
\r
81 return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);
\r
85 bool isCaseNode(xmlNodePtr theNode)
\r
87 if(!isElementNode(theNode))
\r
90 return isNode(theNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);
\r
93 bool hasChild(xmlNodePtr theNode)
\r
95 xmlNodePtr aNode = theNode->children;
\r
96 for (; aNode; aNode = aNode->next) {
\r
97 if (isElementNode(theNode)) {
\r
104 bool hasParent(xmlNodePtr theNode)
\r
106 xmlNodePtr aNode = theNode->parent;
\r
110 for (; aNode; aNode = aNode->next) {
\r
111 if (isElementNode(theNode)) {
\r
118 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)
\r
120 if (!hasParent(theNode)) {
\r
121 return false; // have no parents at all
\r
123 xmlNodePtr aNode = theNode->parent;
\r
124 const xmlChar* aName = aNode->name;
\r
125 if (!aName || !isElementNode(aNode)) {
\r
128 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
\r
131 va_list args; // define argument list variable
\r
132 va_start(args, theNodeName); // init list; point to last defined argument
\r
134 char *anArg = va_arg (args, char*); // get next argument
\r
137 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
\r
138 va_end(args); // cleanup the system stack
\r
142 va_end(args); // cleanup the system stack
\r
146 bool hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)
\r
148 if (!hasParent(theNode)) {
\r
149 return false; // have no parents at all
\r
151 xmlNodePtr aNode = theNode->parent;
\r
152 const xmlChar* aName = aNode->name;
\r
153 if (!aName || !isElementNode(aNode)) {
\r
156 for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {
\r
157 if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))
\r
160 return hasParentRecursive(aNode, theNodeNames);
\r
163 bool hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)
\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
170 char *anArg = va_arg (args, char*); // get next argument
\r
173 aNodeNames.push_back(anArg);
\r
175 va_end(args); // cleanup the system stack
\r
176 return hasParentRecursive(theNode, aNodeNames);
\r
179 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,
\r
180 std::list<std::string>& outValidatorParameters)
\r
183 char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);
\r
184 if (!anIdProp || anIdProp[0] == 0) {
\r
187 outPropertyId = std::string(anIdProp);
\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
203 std::string library(const std::string& theLibName)
\r
205 if(theLibName.empty())
\r
206 return std::string();
\r
207 std::string aLibName = theLibName;
\r
209 static std::string aLibExt( ".so" );
\r
210 if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {
\r
211 aLibName = "lib" + aLibName;
\r
214 static std::string aLibExt(".dll");
\r
216 std::string anExt = aLibName.substr(aLibName.size() - 4);
\r
217 if (anExt != aLibExt)
\r
218 aLibName += aLibExt;
\r
223 std::string getProperty(xmlNodePtr theNode, const char* thePropName)
\r
225 std::string result = "";
\r
226 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST thePropName);
\r
227 if (!aPropChars || aPropChars[0] == 0)
\r
229 result = std::string(aPropChars);
\r
233 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)
\r
235 return normalize(getProperty(theNode, thePropName));
\r
238 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)
\r
240 std::string prop = normalize(getProperty(theNode, theAttributeName));
\r
241 bool result = theDefault;
\r
242 if (prop == "true" || prop == "1") {
\r
244 } else if (prop == "false" || prop == "0") {
\r
250 CONFIG_EXPORT std::string normalize(const char* theString)
\r
253 return std::string();
\r
254 return normalize(std::string(theString));
\r
257 CONFIG_EXPORT std::string normalize(const std::string& theString)
\r
259 std::string result = theString;
\r
260 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
\r