4 * Created on: Mar 14, 2014
8 #include <Config_XMLReader.h>
10 #include <Event_Loop.hxx>
11 #include <libxml\parser.h>
12 #include <libxml\tree.h>
15 //For GetModuleFileNameW
23 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
26 //Get path to *.xml files (typically ./bin/../plugins/)
28 HMODULE hModule = GetModuleHandleW(NULL);
29 WCHAR wchar_path[MAX_PATH];
30 GetModuleFileNameW(hModule, wchar_path, MAX_PATH);
31 char char_path[MAX_PATH];
33 WideCharToMultiByte(CP_ACP, 0, wchar_path, -1, char_path, MAX_PATH, &DefChar, NULL);
34 prefix = std::string(char_path);
36 unsigned found = prefix.rfind("bin");
37 if (found != std::string::npos)
38 prefix.replace(found, prefix.length(), "plugins\\");
40 //TODO(sbh): Find full path to binary on linux
41 prefix = "../plugins/";
44 myDocumentPath = prefix + theXmlFileName;
47 Config_XMLReader::~Config_XMLReader()
52 * Read all nodes (recursively if processChildren() is true
53 * for a node). For each read node the processNode will be
56 void Config_XMLReader::readAll()
58 xmlNodePtr aRoot = findRoot();
59 readRecursively(aRoot);
63 * Allows to customize reader's behavior for a node. Virtual.
64 * The default implementation does nothing. (In debug mode prints
67 void Config_XMLReader::processNode(xmlNodePtr aNode)
70 std::cout << "Config_XMLReader::processNode: "
71 << aNode->name << " content: "
72 << aNode->content << std::endl;
77 * Defines which nodes should be processed recursively. Virtual.
78 * The default implementation to read all nodes.
80 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
85 xmlNodePtr Config_XMLReader::findRoot()
88 aDoc = xmlParseFile(myDocumentPath.c_str());
91 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
92 << " is not parsed successfully." << std::endl;
96 xmlNodePtr aRoot = xmlDocGetRootElement(aDoc);
99 std::cout << "Config_XMLReader::import: " << "Error: empty document";
106 * Calls processNode() for each child (for some - recursively)
110 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
112 static Event_ID aFeatureEvent = Event_Loop::EventByName("Feature");
116 xmlNodePtr aNode = theParent->xmlChildrenNode;
117 for(; aNode; aNode = aNode->next) {
119 if (processChildren(aNode)) {
120 readRecursively(aNode);
121 Config_FeatureMessage aMessage(aFeatureEvent, this);
127 * void* -> xmlNodePtr
129 xmlNodePtr Config_XMLReader::node(void* theNode)
131 return static_cast<xmlNodePtr>(theNode);
135 * Returns named property for a given node as std::string.
137 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name)
139 std::string result = "";
140 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name);
141 if (!aPropChars || aPropChars[0] == 0)
143 result = std::string(aPropChars);
148 * Returns true if theNode is XML node with a given name.
150 bool Config_XMLReader::isNode(xmlNodePtr theNode, const char* theNodeName, ...)
153 const xmlChar* aName = theNode->name;
154 if (!aName || theNode->type != XML_ELEMENT_NODE)
157 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName))
160 va_list args; // define argument list variable
161 va_start(args, theNodeName); // init list; point to last defined argument
163 char *anArg = va_arg (args, char *); // get next argument
166 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
167 va_end(args); // cleanup the system stack
171 va_end(args); // cleanup the system stack