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_OPTIONALBOX,
\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 // here must be only widgets not connected to attributes
\r
66 bool isPagedContainer = isNode(theNode, WDG_TOOLBOX_BOX,
\r
68 WDG_SWITCH_CASE, NULL);
\r
69 return !isLogical && !isPagedContainer;
\r
72 bool isWidgetNode(xmlNodePtr theNode)
\r
74 if(!isElementNode(theNode))
\r
76 // it's parent is "feature" or "source" or a page ("box", "case")
\r
77 if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, WDG_GROUP, WDG_OPTIONALBOX,
\r
78 WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))
\r
81 //it should not be a "source" or a "validator" node
\r
82 return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NODE_SELFILTER, NULL);
\r
86 bool isCaseNode(xmlNodePtr theNode)
\r
88 if(!isElementNode(theNode))
\r
91 return isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);
\r
94 bool hasChild(xmlNodePtr theNode)
\r
96 xmlNodePtr aNode = theNode->children;
\r
97 for (; aNode; aNode = aNode->next) {
\r
98 if (isElementNode(theNode)) {
\r
105 bool hasParent(xmlNodePtr theNode)
\r
107 xmlNodePtr aNode = theNode->parent;
\r
111 for (; aNode; aNode = aNode->next) {
\r
112 if (isElementNode(theNode)) {
\r
119 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)
\r
121 if (!hasParent(theNode)) {
\r
122 return false; // have no parents at all
\r
124 xmlNodePtr aNode = theNode->parent;
\r
125 const xmlChar* aName = aNode->name;
\r
126 if (!aName || !isElementNode(aNode)) {
\r
129 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
\r
132 va_list args; // define argument list variable
\r
133 va_start(args, theNodeName); // init list; point to last defined argument
\r
135 char *anArg = va_arg (args, char*); // get next argument
\r
138 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
\r
139 va_end(args); // cleanup the system stack
\r
143 va_end(args); // cleanup the system stack
\r
147 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)
\r
149 if (!hasParent(theNode)) {
\r
150 return 0; // have no parents at all
\r
152 xmlNodePtr aNode = theNode->parent;
\r
153 const xmlChar* aName = aNode->name;
\r
154 if (!aName || !isElementNode(aNode)) {
\r
157 for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {
\r
158 if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))
\r
161 return hasParentRecursive(aNode, theNodeNames);
\r
164 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)
\r
166 std::vector<const char*> aNodeNames;
\r
167 va_list args; // define argument list variable
\r
168 va_start(args, theNodeName); // init list; point to last defined argument
\r
169 aNodeNames.push_back(theNodeName);
\r
171 char *anArg = va_arg (args, char*); // get next argument
\r
174 aNodeNames.push_back(anArg);
\r
176 va_end(args); // cleanup the system stack
\r
177 return hasParentRecursive(theNode, aNodeNames);
\r
180 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,
\r
181 std::list<std::string>& outValidatorParameters)
\r
184 char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);
\r
185 if (!anIdProp || anIdProp[0] == 0) {
\r
188 outPropertyId = std::string(anIdProp);
\r
190 //Property parameters:
\r
191 char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);
\r
192 if (aParamProp && aParamProp[0] != 0) {
\r
193 std::string aPropString = std::string(aParamProp);
\r
194 std::stringstream aPropStringStream(aPropString);
\r
195 char COMMA_DELIM = ',';
\r
196 std::string aParameter;
\r
197 while (std::getline(aPropStringStream, aParameter, ',')) {
\r
198 outValidatorParameters.push_back(aParameter);
\r
204 std::string library(const std::string& theLibName)
\r
206 if(theLibName.empty())
\r
207 return std::string();
\r
208 std::string aLibName = theLibName;
\r
210 static std::string aLibExt( ".so" );
\r
211 if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {
\r
212 aLibName = "lib" + aLibName;
\r
215 static std::string aLibExt(".dll");
\r
217 std::string anExt = aLibName.substr(aLibName.size() - 4);
\r
218 if (anExt != aLibExt)
\r
219 aLibName += aLibExt;
\r
224 bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
\r
226 std::string getProperty(xmlNodePtr theNode, const char* thePropName)
\r
228 std::string result = "";
\r
229 xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);
\r
230 if (!aPropChars || aPropChars[0] == 0)
\r
232 result = std::string((char*)aPropChars);
\r
233 xmlFree(aPropChars);
\r
235 std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);
\r
236 result.erase(new_end, result.end());
\r
241 std::string getContent(xmlNodePtr theNode)
\r
243 std::string result = "";
\r
244 xmlChar* aContent = xmlNodeGetContent(theNode);
\r
245 if (!aContent || aContent[0] == 0)
\r
247 result = std::string((char*)aContent);
\r
252 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)
\r
254 return normalize(getProperty(theNode, thePropName));
\r
257 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)
\r
259 std::string prop = normalize(getProperty(theNode, theAttributeName));
\r
260 bool result = theDefault;
\r
261 if (prop == "true" || prop == "1") {
\r
263 } else if (prop == "false" || prop == "0") {
\r
269 CONFIG_EXPORT std::string normalize(const char* theString)
\r
272 return std::string();
\r
273 return normalize(std::string(theString));
\r
276 CONFIG_EXPORT std::string normalize(const std::string& theString)
\r
278 std::string result = theString;
\r
279 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
\r