Salome HOME
Features and plugins loading mechanisms
[modules/shaper.git] / src / Config / Config_XMLReader.cpp
1 /*
2  * Config_XMLReader.cpp
3  *
4  *  Created on: Mar 14, 2014
5  *      Author: sbh
6  */
7
8 #include <Config_XMLReader.h>
9
10 #include <Event_Loop.h>
11 #include <libxml\parser.h>
12 #include <libxml\tree.h>
13
14 /*
15 #ifdef WIN32
16 //For GetModuleFileNameW
17 #include <windows.h>
18 #endif
19 */
20
21 #ifdef _DEBUG
22 #include <iostream>
23 #endif
24
25 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
26 {
27   std::string prefix;
28   /* the problem: application may be launched using python execuable, to use environment variable
29                   (at least for the current moment)
30   //Get path to *.xml files (typically ./bin/../plugins/)
31 #ifdef WIN32
32   HMODULE hModule = GetModuleHandleW(NULL);
33   WCHAR wchar_path[MAX_PATH];
34   GetModuleFileNameW(hModule, wchar_path, MAX_PATH);
35   char char_path[MAX_PATH];
36   char DefChar = ' ';
37   WideCharToMultiByte(CP_ACP, 0, wchar_path, -1, char_path, MAX_PATH, &DefChar, NULL);
38   prefix = std::string(char_path);
39   //chop "bin\XGUI.exe"
40   unsigned found = prefix.rfind("bin");
41   if (found != std::string::npos)
42     prefix.replace(found, prefix.length(), "plugins\\");
43 #else
44   //TODO(sbh): Find full path to binary on linux
45   prefix = "../plugins/";
46 #endif
47   */
48   char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
49   if (anEnv) {
50     prefix = std::string(anEnv) + "/";
51   }
52
53   myDocumentPath = prefix + theXmlFileName;
54 }
55
56 Config_XMLReader::~Config_XMLReader()
57 {
58 }
59
60 /*
61  * Read all nodes (recursively if processChildren() is true
62  * for a node). For each read node the processNode will be
63  * called.
64  */
65 void Config_XMLReader::readAll()
66 {
67   xmlNodePtr aRoot = findRoot();
68   readRecursively(aRoot);
69 }
70
71 /*
72  * Allows to customize reader's behavior for a node. Virtual.
73  * The default implementation does nothing. (In debug mode prints
74  * some info)
75  */
76 void Config_XMLReader::processNode(xmlNodePtr aNode)
77 {
78 #ifdef _DEBUG
79   std::cout << "Config_XMLReader::processNode: "
80   << aNode->name << " content: "
81   << aNode->content << std::endl;
82 #endif
83 }
84
85 /*
86  * Defines which nodes should be processed recursively. Virtual.
87  * The default implementation to read all nodes.
88  */
89 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
90 {
91   return true;
92 }
93
94 xmlNodePtr Config_XMLReader::findRoot()
95 {
96   xmlDocPtr aDoc;
97   aDoc = xmlParseFile(myDocumentPath.c_str());
98   if (aDoc == NULL) {
99 #ifdef _DEBUG
100     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
101     << " is not parsed successfully." << std::endl;
102 #endif
103     return NULL;
104   }
105   xmlNodePtr aRoot = xmlDocGetRootElement(aDoc);
106 #ifdef _DEBUG
107   if(aRoot == NULL) {
108     std::cout << "Config_XMLReader::import: " << "Error: empty document";
109   }
110 #endif
111   return aRoot;
112 }
113
114 /*
115  * Calls processNode() for each child (for some - recursively)
116  * of the given node.
117  * \sa ReadAll()
118  */
119 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
120 {
121   if (!theParent)
122     return;
123   xmlNodePtr aNode = theParent->xmlChildrenNode;
124   for(; aNode; aNode = aNode->next) {
125     processNode(aNode);
126     if (processChildren(aNode)) {
127       readRecursively(aNode);
128     }
129   }
130 }
131
132 /*
133  * void* -> xmlNodePtr
134  */
135 xmlNodePtr Config_XMLReader::node(void* theNode)
136 {
137   return static_cast<xmlNodePtr>(theNode);
138 }
139
140 /*
141  * Returns named property for a given node as std::string.
142  */
143 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name)
144 {
145   std::string result = "";
146   char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name);
147   if (!aPropChars || aPropChars[0] == 0)
148     return result;
149   result = std::string(aPropChars);
150   return result;
151 }
152
153 /*
154  * Returns true if theNode is XML node with a given name.
155  */
156 bool Config_XMLReader::isNode(xmlNodePtr theNode, const char* theNodeName, ...)
157 {
158   bool result = false;
159   const xmlChar* aName = theNode->name;
160   if (!aName || theNode->type != XML_ELEMENT_NODE)
161     return false;
162
163   if (!xmlStrcmp(aName, (const xmlChar *) theNodeName))
164     return true;
165
166   va_list args; // define argument list variable
167   va_start(args, theNodeName); // init list; point to last defined argument
168   while(true) {
169     char *anArg = va_arg (args, char *); // get next argument
170     if (anArg == NULL)
171       break;
172     if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
173       va_end(args); // cleanup the system stack
174       return true;
175     }
176   }
177   va_end(args); // cleanup the system stack
178   return false;
179 }