4 * Created on: Mar 14, 2014
8 #include <Config_XMLReader.h>
10 #include <Event_Loop.hxx>
12 #include <libxml\parser.h>
13 #include <libxml\tree.h>
15 //For GetModuleFileNameW
23 static bool IsNode(xmlNodePtr theNode, const char* theNodeName)
25 return theNode->type == XML_ELEMENT_NODE
26 && !xmlStrcmp(theNode->name, (const xmlChar *) theNodeName);
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";
36 Config_XMLReader::Config_XMLReader(const std::string& theXmlFile)
38 setDocumentPath(theXmlFile);
41 Config_XMLReader::~Config_XMLReader()
45 std::string Config_XMLReader::documentPath() const
47 return m_DocumentPath;
50 void Config_XMLReader::setDocumentPath(std::string documentPath)
54 HMODULE hModule = GetModuleHandleW(NULL);
55 WCHAR wchar_path[MAX_PATH];
56 GetModuleFileNameW(hModule, wchar_path, MAX_PATH);
57 char char_path[MAX_PATH];
59 WideCharToMultiByte(CP_ACP, 0, wchar_path, -1, char_path, MAX_PATH, &DefChar, NULL);
60 prefix = std::string(char_path);
62 unsigned found = prefix.rfind("bin");
63 if(found != std::string::npos)
64 prefix.replace(found, prefix.length(), "plugins\\");
66 //TODO(sbh): Find full path to binary on linux
67 prefix = "../plugins/";
69 m_DocumentPath = prefix + documentPath;
72 void Config_XMLReader::readAll()
78 * TODO: make virtual as beforeImport
80 bool Config_XMLReader::import()
84 aDoc = xmlParseFile(m_DocumentPath.c_str());
87 std::cout << "Config_XMLReader::import: " << "Document " << m_DocumentPath
88 << " is not parsed successfully." << std::endl;
92 xmlNodePtr aRoot = xmlDocGetRootElement(aDoc);
95 std::cout << "Config_XMLReader::import: " << "Error: empty document";
100 for(aWbSec = aRoot->xmlChildrenNode; aWbSec; aWbSec = aWbSec->next) { // searching for higher level element "workbench"
101 if(IsNode(aWbSec, "workbench")) {
102 result = importWorkbench(aWbSec);
105 std::cout << "Config_XMLReader::import: "
106 << "Found strange section, should be workbench" << std::endl;
115 * TODO(sbh): make virtual as doImport
117 bool Config_XMLReader::importWorkbench(void* theRoot)
119 xmlNodePtr aGroupNode = (static_cast<xmlNodePtr>(theRoot))->xmlChildrenNode;
120 Event_Loop* aEvLoop = Event_Loop::Loop();
123 std::cout << "Config_XMLReader::importWorkbench: "
124 << "No event loop registered" << std::endl;
128 for(; aGroupNode; aGroupNode = aGroupNode->next) { // searching for record
129 if(!IsNode(aGroupNode, "group"))
131 std::string aGroupName = getProperty(aGroupNode, FEATURE_GROUP_NAME);
132 if(aGroupName.empty())
134 xmlNodePtr aFtNode = aGroupNode->xmlChildrenNode;
135 for(; aFtNode; aFtNode = aFtNode->next) {
136 if(!IsNode(aFtNode, "feature"))
139 Config_FeatureMessage aMessage(aEvLoop->EventByName("Feature"), this);
140 fillFeature(aFtNode, aMessage);
141 aMessage.m_group = aGroupName;
142 aEvLoop->Send(aMessage);
148 void Config_XMLReader::fillFeature(void *theRoot,
149 Config_FeatureMessage& outFeatureMessage)
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);
158 std::string Config_XMLReader::getProperty(void *theRoot, const char* name)
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)
165 result = std::string(aPropChars);