]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_XMLReader.cpp
Salome HOME
Implimentation of the Config XML Reader
[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
12 #include <libxml\parser.h>
13 #include <libxml\tree.h>
14 #ifdef WIN32
15 //For GetModuleFileNameW
16 #include <windows.h>
17 #endif
18
19 #ifdef _DEBUG
20 #include <iostream>
21 #endif
22
23 static bool IsNode(xmlNodePtr theNode, const char* theNodeName)
24 {
25   return theNode->type == XML_ELEMENT_NODE
26       && !xmlStrcmp(theNode->name, (const xmlChar *) theNodeName);
27 }
28
29 const static char* FEATURE_ID = "id";
30 const static char* FEATURE_TEXT = "text";
31 const static char* FEATURE_TOOLTIP = "tooltip";
32 const static char* FEATURE_ICON = "icon";
33 const static char* FEATURE_KEYSEQUENCE = "keysequence";
34 const static char* FEATURE_GROUP_NAME = "name";
35
36 Config_XMLReader::Config_XMLReader(const std::string& theXmlFile)
37 {
38   setDocumentPath(theXmlFile);
39 }
40
41 Config_XMLReader::~Config_XMLReader()
42 {
43 }
44
45 std::string Config_XMLReader::documentPath() const
46 {
47   return m_DocumentPath;
48 }
49
50 void Config_XMLReader::setDocumentPath(std::string documentPath)
51 {
52   std::string prefix;
53 #ifdef WIN32
54   HMODULE hModule = GetModuleHandleW(NULL);
55   WCHAR wchar_path[MAX_PATH];
56   GetModuleFileNameW(hModule, wchar_path, MAX_PATH);
57   char char_path[MAX_PATH];
58   char DefChar = ' ';
59   WideCharToMultiByte(CP_ACP, 0, wchar_path, -1, char_path, MAX_PATH, &DefChar, NULL);
60   prefix = std::string(char_path);
61   //chop "bin\XGUI.exe"
62   unsigned found = prefix.rfind("bin");
63   if(found != std::string::npos)
64     prefix.replace(found, prefix.length(), "plugins\\");
65 #else
66   //TODO(sbh): Find full path to binary on linux
67   prefix = "../plugins/";
68 #endif
69   m_DocumentPath = prefix + documentPath;
70 }
71
72 void Config_XMLReader::readAll()
73 {
74   import();
75 }
76
77 /*
78  * TODO: make virtual as beforeImport
79  */
80 bool Config_XMLReader::import()
81 {
82   bool result = false;
83   xmlDocPtr aDoc;
84   aDoc = xmlParseFile(m_DocumentPath.c_str());
85   if(aDoc == NULL) {
86     #ifdef _DEBUG
87     std::cout << "Config_XMLReader::import: " << "Document " << m_DocumentPath
88               << " is not parsed successfully." << std::endl;
89     #endif
90     return result;
91   }
92   xmlNodePtr aRoot = xmlDocGetRootElement(aDoc);
93   if(aRoot == NULL) {
94     #ifdef _DEBUG
95     std::cout << "Config_XMLReader::import: " << "Error: empty document";
96     #endif
97     return result;
98   }
99   xmlNodePtr aWbSec;
100   for(aWbSec = aRoot->xmlChildrenNode; aWbSec; aWbSec = aWbSec->next) { // searching for higher level element "workbench"
101     if(IsNode(aWbSec, "workbench")) {
102       result = importWorkbench(aWbSec);
103     } else {
104       #ifdef _DEBUG
105       std::cout << "Config_XMLReader::import: "
106                 << "Found strange section, should be workbench" << std::endl;
107       #endif
108       continue;
109     }
110   }
111   return result;
112 }
113
114 /*
115  * TODO(sbh): make virtual as doImport
116  */
117 bool Config_XMLReader::importWorkbench(void* theRoot)
118 {
119   xmlNodePtr aGroupNode = (static_cast<xmlNodePtr>(theRoot))->xmlChildrenNode;
120   Event_Loop* aEvLoop = Event_Loop::Loop();
121   if(!aEvLoop) {
122     #ifdef _DEBUG
123     std::cout << "Config_XMLReader::importWorkbench: "
124               << "No event loop registered" << std::endl;
125     #endif
126     return false;
127   }
128   for(; aGroupNode; aGroupNode = aGroupNode->next) { // searching for record
129     if(!IsNode(aGroupNode, "group"))
130       continue;
131     std::string aGroupName = getProperty(aGroupNode, FEATURE_GROUP_NAME);
132     if(aGroupName.empty())
133       continue;
134     xmlNodePtr aFtNode = aGroupNode->xmlChildrenNode;
135     for(; aFtNode; aFtNode = aFtNode->next) {
136       if(!IsNode(aFtNode, "feature"))
137         continue;
138       //Create feature...
139       Config_FeatureMessage aMessage(aEvLoop->EventByName("Feature"), this);
140       fillFeature(aFtNode, aMessage);
141       aMessage.m_group = aGroupName;
142       aEvLoop->Send(aMessage);
143     }
144   }
145   return true;
146 }
147
148 void Config_XMLReader::fillFeature(void *theRoot,
149                                    Config_FeatureMessage& outFeatureMessage)
150 {
151   outFeatureMessage.m_id = getProperty(theRoot, FEATURE_ID);
152   outFeatureMessage.m_text = getProperty(theRoot, FEATURE_TEXT);
153   outFeatureMessage.m_tooltip = getProperty(theRoot, FEATURE_TOOLTIP);
154   outFeatureMessage.m_icon = getProperty(theRoot, FEATURE_ICON);
155   outFeatureMessage.m_keysequence = getProperty(theRoot, FEATURE_KEYSEQUENCE);
156 }
157
158 std::string Config_XMLReader::getProperty(void *theRoot, const char* name)
159 {
160   std::string result = "";
161   xmlNodePtr aNode = (static_cast<xmlNodePtr>(theRoot));
162   char* aPropChars = (char*) xmlGetProp(aNode, BAD_CAST name);
163   if(!aPropChars || aPropChars[0] == 0)
164     return result;
165   result = std::string(aPropChars);
166   return result;
167 }