4 * Created on: Mar 14, 2014
8 #include <Config_XMLReader.h>
10 #include <Event_Loop.h>
11 #include <libxml\parser.h>
12 #include <libxml\tree.h>
16 //For GetModuleFileNameW
25 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
28 /* the problem: application may be launched using python execuable, to use environment variable
29 (at least for the current moment)
30 //Get path to *.xml files (typically ./bin/../plugins/)
32 HMODULE hModule = GetModuleHandleW(NULL);
33 WCHAR wchar_path[MAX_PATH];
34 GetModuleFileNameW(hModule, wchar_path, MAX_PATH);
35 char char_path[MAX_PATH];
37 WideCharToMultiByte(CP_ACP, 0, wchar_path, -1, char_path, MAX_PATH, &DefChar, NULL);
38 prefix = std::string(char_path);
40 unsigned found = prefix.rfind("bin");
41 if (found != std::string::npos)
42 prefix.replace(found, prefix.length(), "plugins\\");
44 //TODO(sbh): Find full path to binary on linux
45 prefix = "../plugins/";
48 char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
50 prefix = std::string(anEnv) + "/";
53 myDocumentPath = prefix + theXmlFileName;
56 Config_XMLReader::~Config_XMLReader()
61 * Read all nodes (recursively if processChildren() is true
62 * for a node). For each read node the processNode will be
65 void Config_XMLReader::readAll()
67 xmlNodePtr aRoot = findRoot();
68 readRecursively(aRoot);
72 * Allows to customize reader's behavior for a node. Virtual.
73 * The default implementation does nothing. (In debug mode prints
76 void Config_XMLReader::processNode(xmlNodePtr aNode)
79 std::cout << "Config_XMLReader::processNode: "
80 << aNode->name << " content: "
81 << aNode->content << std::endl;
86 * Defines which nodes should be processed recursively. Virtual.
87 * The default implementation to read all nodes.
89 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
94 xmlNodePtr Config_XMLReader::findRoot()
97 aDoc = xmlParseFile(myDocumentPath.c_str());
100 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
101 << " is not parsed successfully." << std::endl;
105 xmlNodePtr aRoot = xmlDocGetRootElement(aDoc);
108 std::cout << "Config_XMLReader::import: " << "Error: empty document";
115 * Calls processNode() for each child (for some - recursively)
119 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
123 xmlNodePtr aNode = theParent->xmlChildrenNode;
124 for(; aNode; aNode = aNode->next) {
126 if (processChildren(aNode)) {
127 readRecursively(aNode);
133 * void* -> xmlNodePtr
135 xmlNodePtr Config_XMLReader::node(void* theNode)
137 return static_cast<xmlNodePtr>(theNode);
141 * Returns named property for a given node as std::string.
143 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name)
145 std::string result = "";
146 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name);
147 if (!aPropChars || aPropChars[0] == 0)
149 result = std::string(aPropChars);
154 * Returns true if theNode is XML node with a given name.
156 bool Config_XMLReader::isNode(xmlNodePtr theNode, const char* theNodeName, ...)
159 const xmlChar* aName = theNode->name;
160 if (!aName || theNode->type != XML_ELEMENT_NODE)
163 if (!xmlStrcmp(aName, (const xmlChar *) theNodeName))
166 va_list args; // define argument list variable
167 va_start(args, theNodeName); // init list; point to last defined argument
169 char *anArg = va_arg (args, char *); // get next argument
172 if (!xmlStrcmp(aName, (const xmlChar *) anArg)) {
173 va_end(args); // cleanup the system stack
177 va_end(args); // cleanup the system stack