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
21 bool isElementNode(xmlNodePtr theNode)
\r
25 return theNode->type == XML_ELEMENT_NODE;
\r
28 bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)
\r
30 const xmlChar* aName = theNode->name;
\r
31 if (!aName || !isElementNode(theNode)) {
\r
34 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
\r
37 va_list args; // define argument list variable
\r
38 va_start(args, theNodeName); // init list; point to last defined argument
\r
40 char *anArg = va_arg (args, char*); // get next argument
\r
43 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
\r
44 va_end(args); // cleanup the system stack
\r
48 va_end(args); // cleanup the system stack
\r
52 bool isAttributeNode(xmlNodePtr theNode)
\r
54 if(!isElementNode(theNode))
\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
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
68 bool isWidgetNode(xmlNodePtr theNode)
\r
70 if(!isElementNode(theNode))
\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
77 //it should not be a "source" or a "validator" node
\r
78 return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);
\r
82 bool isCaseNode(xmlNodePtr theNode)
\r
84 if(!isElementNode(theNode))
\r
87 return isNode(theNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);
\r
90 bool hasChild(xmlNodePtr theNode)
\r
92 xmlNodePtr aNode = theNode->children;
\r
93 for (; aNode; aNode = aNode->next) {
\r
94 if (isElementNode(theNode)) {
\r
101 bool hasParent(xmlNodePtr theNode)
\r
103 xmlNodePtr aNode = theNode->parent;
\r
107 for (; aNode; aNode = aNode->next) {
\r
108 if (isElementNode(theNode)) {
\r
115 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)
\r
117 if (!hasParent(theNode)) {
\r
118 return false; // have no parents at all
\r
120 xmlNodePtr aNode = theNode->parent;
\r
121 const xmlChar* aName = aNode->name;
\r
122 if (!aName || !isElementNode(aNode)) {
\r
125 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
\r
128 va_list args; // define argument list variable
\r
129 va_start(args, theNodeName); // init list; point to last defined argument
\r
131 char *anArg = va_arg (args, char*); // get next argument
\r
134 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
\r
135 va_end(args); // cleanup the system stack
\r
139 va_end(args); // cleanup the system stack
\r
143 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,
\r
144 std::list<std::string>& outValidatorParameters)
\r
147 char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);
\r
148 if (!anIdProp || anIdProp[0] == 0) {
\r
151 outPropertyId = std::string(anIdProp);
\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
167 std::string library(const std::string& theLibName)
\r
169 if(theLibName.empty())
\r
170 return std::string();
\r
171 std::string aLibName = theLibName;
\r
173 static std::string aLibExt( ".so" );
\r
174 if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {
\r
175 aLibName = "lib" + aLibName;
\r
178 static std::string aLibExt(".dll");
\r
180 std::string anExt = aLibName.substr(aLibName.size() - 4);
\r
181 if (anExt != aLibExt)
\r
182 aLibName += aLibExt;
\r
187 std::string getProperty(xmlNodePtr theNode, const char* thePropName)
\r
189 std::string result = "";
\r
190 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST thePropName);
\r
191 if (!aPropChars || aPropChars[0] == 0)
\r
193 result = std::string(aPropChars);
\r
197 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)
\r
199 return normalize(getProperty(theNode, thePropName));
\r
202 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)
\r
204 std::string prop = normalize(getProperty(theNode, theAttributeName));
\r
205 bool result = theDefault;
\r
206 if (prop == "true" || prop == "1") {
\r
208 } else if (prop == "false" || prop == "0") {
\r
214 CONFIG_EXPORT std::string normalize(const char* theString)
\r
217 return std::string();
\r
218 return normalize(std::string(theString));
\r
221 CONFIG_EXPORT std::string normalize(const std::string& theString)
\r
223 std::string result = theString;
\r
224 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
\r