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>
12 #include <Config_SelectionFilterMessage.h>
13 #include <Config_PropManager.h>
15 #include <Events_Loop.h>
16 #include <Events_Error.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
23 #pragma warning(disable : 4996) // for getenv
30 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
33 std::string prefix = "";
34 Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
36 prefix = aProp->value();
38 * Get path to *.xml files (typically ./bin/../plugins/)
40 * the problem: application may be launched using python executable,
41 * to use environment variable (at least for the current moment)
44 char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
46 prefix = std::string(anEnv);
54 myDocumentPath = prefix + theXmlFileName;
55 std::ifstream aTestFile(myDocumentPath);
56 if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
60 Config_XMLReader::~Config_XMLReader()
66 * Read all nodes in associated xml file,
67 * recursively if processChildren(xmlNode) is true for the xmlNode.
68 * For each read node the processNode will be called.
70 void Config_XMLReader::readAll()
72 xmlNodePtr aRoot = findRoot();
73 readRecursively(aRoot);
77 * Allows to customize reader's behavior for a node. Virtual.
78 * The default impl does nothing. (In debug mode prints
81 void Config_XMLReader::processNode(xmlNodePtr theNode)
83 if (isNode(theNode, NODE_SOURCE, NULL)) {
84 std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
85 Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
86 readRecursively(aSourceReader.findRoot());
88 std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
90 } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
91 processValidator(theNode);
92 } else if (isNode(theNode, NODE_SELFILTER, NULL)) {
93 processSelectionFilter(theNode);
98 * Defines which nodes should be processed recursively. Virtual.
99 * The default impl is to read all nodes.
101 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
109 xmlNodePtr Config_XMLReader::findRoot()
111 if (myXmlDoc == NULL) {
112 myXmlDoc = xmlParseFile(myDocumentPath.c_str());
114 if (myXmlDoc == NULL) {
116 std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
117 << " is not parsed successfully." << std::endl;
121 xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
124 std::cout << "Config_XMLReader::import: " << "Error: empty document";
131 * Calls processNode() for each child (for some - recursively)
135 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
139 xmlNodePtr aNode = theParent->xmlChildrenNode;
140 for (; aNode; aNode = aNode->next) {
141 //Still no text processing in features...
142 if (!isElementNode(aNode)) {
146 if (processChildren(aNode)) {
147 readRecursively(aNode);
153 * void* -> xmlNodePtr
155 xmlNodePtr Config_XMLReader::node(void* theNode)
157 return static_cast<xmlNodePtr>(theNode);
160 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
162 std::string result = "";
163 char* aPropChars = (char*) theNode->name;
164 if (!aPropChars || aPropChars[0] == 0)
166 result = std::string(aPropChars);
170 void Config_XMLReader::processValidator(xmlNodePtr theNode)
172 Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
173 Events_Loop* aEvLoop = Events_Loop::loop();
174 std::shared_ptr<Config_ValidatorMessage>
175 aMessage(new Config_ValidatorMessage(aValidatoEvent, this));
176 std::string aValidatorId;
177 std::list<std::string> aValidatorParameters;
178 getValidatorInfo(theNode, aValidatorId, aValidatorParameters);
179 aMessage->setValidatorId(aValidatorId);
180 aMessage->setValidatorParameters(aValidatorParameters);
181 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
182 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
183 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
185 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
186 aMessage->setFeatureId(myCurrentFeature);
188 aEvLoop->send(aMessage);
191 void Config_XMLReader::processSelectionFilter(xmlNodePtr theNode)
193 Events_ID aFilterEvent = Events_Loop::eventByName(EVENT_SELFILTER_LOADED);
194 Events_Loop* aEvLoop = Events_Loop::loop();
195 std::shared_ptr<Config_SelectionFilterMessage> aMessage =
196 std::make_shared<Config_SelectionFilterMessage>(aFilterEvent, this);
197 std::string aSelectionFilterId = getProperty(theNode, _ID);
198 aMessage->setSelectionFilterId(aSelectionFilterId);
199 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
200 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
201 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
203 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
204 aMessage->setFeatureId(myCurrentFeature);
206 aEvLoop->send(aMessage);