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