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>
18 #pragma warning(disable : 4996) // for getenv
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 theNode)
66 if (isNode(theNode, NODE_SOURCE, NULL)) {
67 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
68 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
69 readRecursively(aSourceReader.findRoot());
71 std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
73 } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
74 processValidator(theNode);
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 if (myXmlDoc == NULL) {
93 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
95 if (myXmlDoc == NULL) {
97 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
98 << " is not parsed successfully." << std::endl;
102 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
105 std::cout << "Config_XMLReader::import: " << "Error: empty document";
112 * Calls processNode() for each child (for some - recursively)
116 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
120 xmlNodePtr aNode = theParent->xmlChildrenNode;
121 for (; aNode; aNode = aNode->next) {
122 //Still no text processing in features...
123 if (!isElementNode(aNode)) {
127 if (processChildren(aNode)) {
128 readRecursively(aNode);
134 * void* -> xmlNodePtr
136 xmlNodePtr Config_XMLReader::node(void* theNode)
138 return static_cast<xmlNodePtr>(theNode);
142 * Returns named property for a given node as std::string.
144 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* theName)
146 std::string result = "";
147 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST theName);
148 if (!aPropChars || aPropChars[0] == 0)
150 result = std::string(aPropChars);
154 void Config_XMLReader::processValidator(xmlNodePtr theNode)
156 Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
157 Events_Loop* aEvLoop = Events_Loop::loop();
158 Config_ValidatorMessage aMessage(aValidatoEvent, this);
159 std::string aValidatorId;
160 std::list<std::string> aValidatorParameters;
161 getValidatorInfo(theNode, aValidatorId, aValidatorParameters);
162 aMessage.setValidatorId(aValidatorId);
163 aMessage.setValidatorParameters(aValidatorParameters);
164 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
165 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
166 aMessage.setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
168 aMessage.setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
169 aMessage.setFeatureId(myCurrentFeature);
171 aEvLoop->send(aMessage);