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