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 std::string prefix = "";
30 * Get path to *.xml files (typically ./bin/../plugins/)
32 * the problem: application may be launched using python executable,
33 * to use environment variable (at least for the current moment)
35 char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
37 prefix = std::string(anEnv) + "/";
40 myDocumentPath = prefix + theXmlFileName;
43 Config_XMLReader::~Config_XMLReader()
49 * Read all nodes in associated xml file,
50 * recursively if processChildren(xmlNode) is true for the xmlNode.
51 * For each read node the processNode will be called.
53 void Config_XMLReader::readAll()
55 xmlNodePtr aRoot = findRoot();
56 readRecursively(aRoot);
60 * Allows to customize reader's behavior for a node. Virtual.
61 * The default impl does nothing. (In debug mode prints
64 void Config_XMLReader::processNode(xmlNodePtr aNode)
67 std::cout << "Config_XMLReader::processNode: "
68 << aNode->name << " content: "
69 << aNode->content << std::endl;
74 * Defines which nodes should be processed recursively. Virtual.
75 * The default impl is to read all nodes.
77 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
85 xmlNodePtr Config_XMLReader::findRoot()
87 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
88 if (myXmlDoc == NULL) {
90 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
91 << " is not parsed successfully." << std::endl;
96 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
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)
114 xmlNodePtr aNode = theParent->xmlChildrenNode;
115 for(; aNode; aNode = aNode->next) {
117 if (processChildren(aNode)) {
118 readRecursively(aNode);
124 * void* -> xmlNodePtr
126 xmlNodePtr Config_XMLReader::node(void* theNode)
128 return static_cast<xmlNodePtr>(theNode);
132 * Returns named property for a given node as std::string.
134 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name)
136 std::string result = "";
137 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name);
138 if (!aPropChars || aPropChars[0] == 0)
140 result = std::string(aPropChars);