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);
99 bool isCaseNode(xmlNodePtr theNode)
101 if(!isElementNode(theNode))
104 return isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX,
105 WDG_RADIOBOX_ITEM, NULL);
109 bool hasChild(xmlNodePtr theNode)
111 xmlNodePtr aNode = theNode->children;
112 for (; aNode; aNode = aNode->next) {
113 if (isElementNode(theNode)) {
120 bool hasParent(xmlNodePtr theNode)
122 xmlNodePtr aNode = theNode->parent;
126 for (; aNode; aNode = aNode->next) {
127 if (isElementNode(theNode)) {
134 bool hasParent(xmlNodePtr theNode, const char* theNodeName, ...)
136 if (!hasParent(theNode)) {
137 return false; // have no parents at all
139 xmlNodePtr aNode = theNode->parent;
140 const xmlChar* aName = aNode->name;
141 if (!aName || !isElementNode(aNode)) {
144 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName)) {
147 va_list args; // define argument list variable
148 va_start(args, theNodeName); // init list; point to last defined argument
150 char *anArg = va_arg (args, char*); // get next argument
153 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
154 va_end(args); // cleanup the system stack
158 va_end(args); // cleanup the system stack
162 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const std::vector<const char*>& theNodeNames)
164 if (!hasParent(theNode)) {
165 return 0; // have no parents at all
167 xmlNodePtr aNode = theNode->parent;
168 const xmlChar* aName = aNode->name;
169 if (!aName || !isElementNode(aNode)) {
172 for (size_t anIndex = 0; anIndex < theNodeNames.size(); ++anIndex) {
173 if (!xmlStrcmp(aName, (const xmlChar *) theNodeNames[anIndex]))
176 return hasParentRecursive(aNode, theNodeNames);
179 xmlNodePtr hasParentRecursive(xmlNodePtr theNode, const char* theNodeName, ...)
181 std::vector<const char*> aNodeNames;
182 va_list args; // define argument list variable
183 va_start(args, theNodeName); // init list; point to last defined argument
184 aNodeNames.push_back(theNodeName);
186 char *anArg = va_arg (args, char*); // get next argument
189 aNodeNames.push_back(anArg);
191 va_end(args); // cleanup the system stack
192 return hasParentRecursive(theNode, aNodeNames);
195 bool getParametersInfo(xmlNodePtr theNode, std::string& outPropertyId,
196 std::list<std::string>& outValidatorParameters)
199 char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID);
200 if (!anIdProp || anIdProp[0] == 0) {
203 outPropertyId = std::string(anIdProp);
205 //Property parameters:
206 char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST _PARAMETERS);
207 if (aParamProp && aParamProp[0] != 0) {
208 std::string aPropString = std::string(aParamProp);
209 std::stringstream aPropStringStream(aPropString);
210 char COMMA_DELIM = ',';
211 std::string aParameter;
212 while (std::getline(aPropStringStream, aParameter, ',')) {
213 outValidatorParameters.push_back(aParameter);
219 std::string library(const std::string& theLibName)
221 if(theLibName.empty())
222 return std::string();
223 std::string aLibName = theLibName;
225 static std::string aLibExt( ".so" );
226 if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib") {
227 aLibName = "lib" + aLibName;
230 static std::string aLibExt(".dll");
232 std::string anExt = aLibName.substr(aLibName.size() - 4);
233 if (anExt != aLibExt)
239 bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
241 std::string getProperty(xmlNodePtr theNode, const char* thePropName)
243 std::string result = "";
244 xmlChar* aPropChars = xmlGetProp(theNode, BAD_CAST thePropName);
245 if (!aPropChars || aPropChars[0] == 0)
247 result = std::string((char*)aPropChars);
250 std::string::iterator new_end = std::unique(result.begin(), result.end(), BothAreSpaces);
251 result.erase(new_end, result.end());
257 std::string getContent(xmlNodePtr theNode)
259 std::string result = "";
260 xmlChar* aContent = xmlNodeGetContent(theNode);
261 if (!aContent || aContent[0] == 0)
263 result = std::string((char*)aContent);
269 std::string getNormalizedProperty(xmlNodePtr theNode, const char* thePropName)
271 return normalize(getProperty(theNode, thePropName));
274 bool getBooleanAttribute(xmlNodePtr theNode, const char* theAttributeName, bool theDefault)
276 std::string prop = normalize(getProperty(theNode, theAttributeName));
277 bool result = theDefault;
278 if (prop == "true" || prop == "1") {
280 } else if (prop == "false" || prop == "0") {
286 CONFIG_EXPORT std::string normalize(const std::string& theString)
288 std::string result = theString;
289 std::transform(result.begin(), result.end(), result.begin(), ::tolower);