4 * Created on: Mar 14, 2014
8 #include <Config_XMLReader.h>
9 #include <Config_Keywords.h>
10 #include <Config_Common.h>
11 #include <Config_ValidatorMessage.h>
13 #include <Events_Loop.h>
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
19 //For GetModuleFileNameW
28 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
31 std::string prefix = "";
33 * Get path to *.xml files (typically ./bin/../plugins/)
35 * the problem: application may be launched using python executable,
36 * to use environment variable (at least for the current moment)
38 char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
40 prefix = std::string(anEnv) + "/";
43 myDocumentPath = prefix + theXmlFileName;
46 Config_XMLReader::~Config_XMLReader()
52 * Read all nodes in associated xml file,
53 * recursively if processChildren(xmlNode) is true for the xmlNode.
54 * For each read node the processNode will be called.
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 impl does nothing. (In debug mode prints
67 void Config_XMLReader::processNode(xmlNodePtr theNode)
69 if (isNode(theNode, NODE_SOURCE, NULL)) {
70 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
71 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
72 readRecursively(aSourceReader.findRoot());
74 std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
76 } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
77 processValidator(theNode);
82 * Defines which nodes should be processed recursively. Virtual.
83 * The default impl is to read all nodes.
85 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
93 xmlNodePtr Config_XMLReader::findRoot()
95 if (myXmlDoc == NULL) {
96 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
98 if (myXmlDoc == NULL) {
100 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
101 << " is not parsed successfully." << std::endl;
105 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
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) {
125 //Still no text processing in features...
126 if(isElementNode(aNode)) {
129 if (processChildren(aNode)) {
130 readRecursively(aNode);
136 * void* -> xmlNodePtr
138 xmlNodePtr Config_XMLReader::node(void* theNode)
140 return static_cast<xmlNodePtr>(theNode);
144 * Returns named property for a given node as std::string.
146 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* theName)
148 std::string result = "";
149 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST theName);
150 if (!aPropChars || aPropChars[0] == 0)
152 result = std::string(aPropChars);
156 void Config_XMLReader::processValidator(xmlNodePtr theNode)
158 Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
159 Events_Loop* aEvLoop = Events_Loop::loop();
160 Config_ValidatorMessage aMessage(aValidatoEvent, this);
161 std::string aValidatorId;
162 std::list<std::string> aValidatorParameters;
163 getValidatorInfo(theNode, aValidatorId, aValidatorParameters);
164 aMessage.setValidatorId(aValidatorId);
165 aMessage.setValidatorParameters(aValidatorParameters);
166 if(isNode(theNode->parent, NODE_FEATURE, NULL)) {
167 aMessage.setFeatureId(getProperty(theNode->parent, _ID));
169 xmlNodePtr aWdgNode = theNode->parent;
170 aMessage.setAttributeId(getProperty(aWdgNode, _ID));
171 aMessage.setFeatureId(getProperty(aWdgNode->parent, _ID));
173 aEvLoop->send(aMessage);