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_SWITCH_CASE, NULL))
74 //it should not be a "source" or a "validator" node
75 bool isLogical = isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NULL);
76 // here must be only widgets not connected to attributes
77 bool isPagedContainer = isNode(theNode, WDG_TOOLBOX_BOX,
79 WDG_SWITCH_CASE, NULL);
80 return !isLogical && !isPagedContainer;
83 bool isWidgetNode(xmlNodePtr theNode)
85 if(!isElementNode(theNode))
87 // it's parent is "feature" or "source" or a page ("box", "case")
88 if(!hasParent(theNode, NODE_FEATURE, NODE_SOURCE, WDG_GROUP, WDG_OPTIONALBOX,
89 WDG_TOOLBOX_BOX, WDG_SWITCH_CASE, NULL))
92 //it should not be a "source" or a "validator" node
93 return !isNode(theNode, NODE_SOURCE, NODE_VALIDATOR, NULL);
97 bool isCaseNode(xmlNodePtr theNode)
99 if(!isElementNode(theNode))
102 return isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL);
105 bool hasChild(xmlNodePtr theNode)
107 xmlNodePtr aNode = theNode->children;
108 for (; aNode; aNode = aNode->next) {
109 if (isElementNode(theNode)) {
116 bool hasParent(xmlNodePtr theNode)
118 xmlNodePtr aNode = theNode->parent;
122 for (; aNode; aNode = aNode->next) {
123 if (isElementNode(theNode)) {
130 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)
132 if (!hasParent(theNode)) {
133 return false; // have no parents at all
135 xmlNodePtr aNode = theNode->parent;
136 const xmlChar* aName = aNode->name;
137 if (!aName || !isElementNode(aNode)) {
140 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
143 va_list args; // define argument list variable
144 va_start(args, theNodeName); // init list; point to last defined argument
146 char *anArg = va_arg (args, char*); // get next argument
149 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
150 va_end(args); // cleanup the system stack
154 va_end(args); // cleanup the system stack
158 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)
160 if (!hasParent(theNode)) {
161 return 0; // have no parents at all
163 xmlNodePtr aNode = theNode->parent;
164 const xmlChar* aName = aNode->name;
165 if (!aName || !isElementNode(aNode)) {
168 for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {
169 if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))
172 return hasParentRecursive(aNode, theNodeNames);
175 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)
177 std::vector<const char*> aNodeNames;
178 va_list args; // define argument list variable
179 va_start(args, theNodeName); // init list; point to last defined argument
180 aNodeNames.push_back(theNodeName);
182 char *anArg = va_arg (args, char*); // get next argument
185 aNodeNames.push_back(anArg);
187 va_end(args); // cleanup the system stack
188 return hasParentRecursive(theNode, aNodeNames);
191 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,
192 std::list<std::string>& outValidatorParameters)
195 char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);
196 if (!anIdProp || anIdProp[0] == 0) {
199 outPropertyId = std::string(anIdProp);
201 //Property parameters:
202 char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);
203 if (aParamProp && aParamProp[0] != 0) {
204 std::string aPropString = std::string(aParamProp);
205 std::stringstream aPropStringStream(aPropString);
206 char COMMA_DELIM = ',';
207 std::string aParameter;
208 while (std::getline(aPropStringStream, aParameter, ',')) {
209 outValidatorParameters.push_back(aParameter);
215 std::string library(const std::string& theLibName)
217 if(theLibName.empty())
218 return std::string();
219 std::string aLibName = theLibName;
221 static std::string aLibExt( ".so" );
222 if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {
223 aLibName = "lib" + aLibName;
226 static std::string aLibExt(".dll");
228 std::string anExt = aLibName.substr(aLibName.size() - 4);
229 if (anExt != aLibExt)
235 bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
237 std::string getProperty(xmlNodePtr theNode, const char* thePropName)
239 std::string result = "";
240 xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);
241 if (!aPropChars || aPropChars[0] == 0)
243 result = std::string((char*)aPropChars);
246 std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);
247 result.erase(new_end, result.end());
252 std::string getContent(xmlNodePtr theNode)
254 std::string result = "";
255 xmlChar* aContent = xmlNodeGetContent(theNode);
256 if (!aContent || aContent[0] == 0)
258 result = std::string((char*)aContent);
263 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)
265 return normalize(getProperty(theNode, thePropName));
268 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)
270 std::string prop = normalize(getProperty(theNode, theAttributeName));
271 bool result = theDefault;
272 if (prop == "true" || prop == "1") {
274 } else if (prop == "false" || prop == "0") {
280 CONFIG_EXPORT std::string normalize(const char* theString)
283 return std::string();
284 return normalize(std::string(theString));
287 CONFIG_EXPORT std::string normalize(const std::string& theString)
289 std::string result = theString;
290 std::transform(result.begin(), result.end(), result.begin(), ::tolower);