4 * Created on: Mar 14, 2014
8 #include <Config_XMLReader.h>
9 #include <Config_Keywords.h>
10 #include <Config_Common.h>
12 #include <Events_Loop.h>
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
18 //For GetModuleFileNameW
27 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
30 std::string prefix = "";
32 * Get path to *.xml files (typically ./bin/../plugins/)
34 * the problem: application may be launched using python executable,
35 * to use environment variable (at least for the current moment)
37 char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
39 prefix = std::string(anEnv) + "/";
42 myDocumentPath = prefix + theXmlFileName;
45 Config_XMLReader::~Config_XMLReader()
51 * Read all nodes in associated xml file,
52 * recursively if processChildren(xmlNode) is true for the xmlNode.
53 * For each read node the processNode will be called.
55 void Config_XMLReader::readAll()
57 xmlNodePtr aRoot = findRoot();
58 readRecursively(aRoot);
62 * Allows to customize reader's behavior for a node. Virtual.
63 * The default impl does nothing. (In debug mode prints
66 void Config_XMLReader::processNode(xmlNodePtr theNode)
68 if (isNode(theNode, NODE_SOURCE, NULL)) {
69 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
70 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
71 readRecursively(aSourceReader.findRoot());
73 std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
79 * Defines which nodes should be processed recursively. Virtual.
80 * The default impl is to read all nodes.
82 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
90 xmlNodePtr Config_XMLReader::findRoot()
92 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
93 if (myXmlDoc == NULL) {
95 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
96 << " is not parsed successfully." << std::endl;
100 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
103 std::cout << "Config_XMLReader::import: " << "Error: empty document";
110 * Calls processNode() for each child (for some - recursively)
114 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
118 xmlNodePtr aNode = theParent->xmlChildrenNode;
119 for(; aNode; aNode = aNode->next) {
121 if (processChildren(aNode)) {
122 readRecursively(aNode);
128 * void* -> xmlNodePtr
130 xmlNodePtr Config_XMLReader::node(void* theNode)
132 return static_cast<xmlNodePtr>(theNode);
136 * Returns named property for a given node as std::string.
138 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name)
140 std::string result = "";
141 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name);
142 if (!aPropChars || aPropChars[0] == 0)
144 result = std::string(aPropChars);