1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Config_Common.h"
22 #include <Config_Keywords.h>
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
27 #include <sstream> // for stringstream
30 #include <algorithm> // for std::transform
33 bool isElementNode(xmlNodePtr theNode)
37 return theNode->type == XML_ELEMENT_NODE;
40 bool isNode(xmlNodePtr theNode, const char* theNodeName, ...)
42 const xmlChar* aName = theNode->name;
43 if (!aName || !isElementNode(theNode)) {
46 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
49 va_list args; // define argument list variable
50 va_start(args, theNodeName); // init list; point to last defined argument
52 char *anArg = va_arg (args, char*); // get next argument
55 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
56 va_end(args); // cleanup the system stack
60 va_end(args); // cleanup the system stack
64 bool isAttributeNode(xmlNodePtr theNode)
66 if(!isElementNode(theNode))
68 // it's parent is "feature" or "source" or page ("case" or "box")
69 if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE,
70 WDG_GROUP, WDG_OPTIONALBOX,
71 WDG_TOOLBOX_BOX, WDG_RADIOBOX_ITEM,
72 WDG_SWITCH_CASE, NULL))
75 //it should not be a "source" or a "validator" node
76 bool isLogical = isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NULL);
77 // here must be only widgets not connected to attributes
78 bool isPagedContainer = isNode(theNode, WDG_TOOLBOX_BOX,
80 WDG_SWITCH_CASE, NULL);
81 return !isLogical && !isPagedContainer;
84 bool isWidgetNode(xmlNodePtr theNode)
86 if(!isElementNode(theNode))
88 // it's parent is "feature" or "source" or a page ("box", "case")
89 if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, WDG_GROUP, WDG_OPTIONALBOX,
90 WDG_TOOLBOX_BOX, WDG_RADIOBOX_ITEM, WDG_SWITCH_CASE, NULL))
93 //it should not be a "source" or a "validator" node
94 return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NULL);
98 bool isCaseNode(xmlNodePtr theNode)
100 if(!isElementNode(theNode))
103 return isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX,
104 WDG_RADIOBOX_ITEM, NULL);
107 bool hasChild(xmlNodePtr theNode)
109 xmlNodePtr aNode = theNode->children;
110 for (; aNode; aNode = aNode->next) {
111 if (isElementNode(theNode)) {
118 bool hasParent(xmlNodePtr theNode)
120 xmlNodePtr aNode = theNode->parent;
124 for (; aNode; aNode = aNode->next) {
125 if (isElementNode(theNode)) {
132 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)
134 if (!hasParent(theNode)) {
135 return false; // have no parents at all
137 xmlNodePtr aNode = theNode->parent;
138 const xmlChar* aName = aNode->name;
139 if (!aName || !isElementNode(aNode)) {
142 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
145 va_list args; // define argument list variable
146 va_start(args, theNodeName); // init list; point to last defined argument
148 char *anArg = va_arg (args, char*); // get next argument
151 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
152 va_end(args); // cleanup the system stack
156 va_end(args); // cleanup the system stack
160 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)
162 if (!hasParent(theNode)) {
163 return 0; // have no parents at all
165 xmlNodePtr aNode = theNode->parent;
166 const xmlChar* aName = aNode->name;
167 if (!aName || !isElementNode(aNode)) {
170 for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {
171 if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))
174 return hasParentRecursive(aNode, theNodeNames);
177 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)
179 std::vector<const char*> aNodeNames;
180 va_list args; // define argument list variable
181 va_start(args, theNodeName); // init list; point to last defined argument
182 aNodeNames.push_back(theNodeName);
184 char *anArg = va_arg (args, char*); // get next argument
187 aNodeNames.push_back(anArg);
189 va_end(args); // cleanup the system stack
190 return hasParentRecursive(theNode, aNodeNames);
193 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,
194 std::list<std::string>& outValidatorParameters)
197 char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);
198 if (!anIdProp || anIdProp[0] == 0) {
201 outPropertyId = std::string(anIdProp);
203 //Property parameters:
204 char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);
205 if (aParamProp && aParamProp[0] != 0) {
206 std::string aPropString = std::string(aParamProp);
207 std::stringstream aPropStringStream(aPropString);
208 char COMMA_DELIM = ',';
209 std::string aParameter;
210 while (std::getline(aPropStringStream, aParameter, ',')) {
211 outValidatorParameters.push_back(aParameter);
217 std::string library(const std::string& theLibName)
219 if(theLibName.empty())
220 return std::string();
221 std::string aLibName = theLibName;
223 static std::string aLibExt( ".so" );
224 if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {
225 aLibName = "lib" + aLibName;
228 static std::string aLibExt(".dll");
230 std::string anExt = aLibName.substr(aLibName.size() - 4);
231 if (anExt != aLibExt)
237 bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
239 std::string getProperty(xmlNodePtr theNode, const char* thePropName)
241 std::string result = "";
242 xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);
243 if (!aPropChars || aPropChars[0] == 0)
245 result = std::string((char*)aPropChars);
248 std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);
249 result.erase(new_end, result.end());
254 std::string getContent(xmlNodePtr theNode)
256 std::string result = "";
257 xmlChar* aContent = xmlNodeGetContent(theNode);
258 if (!aContent || aContent[0] == 0)
260 result = std::string((char*)aContent);
265 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)
267 return normalize(getProperty(theNode, thePropName));
270 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)
272 std::string prop = normalize(getProperty(theNode, theAttributeName));
273 bool result = theDefault;
274 if (prop == "true" || prop == "1") {
276 } else if (prop == "false" || prop == "0") {
282 CONFIG_EXPORT std::string normalize(const char* theString)
285 return std::string();
286 return normalize(std::string(theString));
289 CONFIG_EXPORT std::string normalize(const std::string& theString)
291 std::string result = theString;
292 std::transform(result.begin(), result.end(), result.begin(), ::tolower);