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);
141 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
143 std::string result = "";
144 char* aPropChars = (char*) theNode->name;
145 if (!aPropChars || aPropChars[0] == 0)
147 result = std::string(aPropChars);
152 * Returns named property for a given node as std::string.
154 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* theName)
156 std::string result = "";
157 char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST theName);
158 if (!aPropChars || aPropChars[0] == 0)
160 result = std::string(aPropChars);
164 void Config_XMLReader::processValidator(xmlNodePtr theNode)
166 Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
167 Events_Loop* aEvLoop = Events_Loop::loop();
168 boost::shared_ptr<Config_ValidatorMessage>
169 aMessage(new Config_ValidatorMessage(aValidatoEvent, this));
170 std::string aValidatorId;
171 std::list<std::string> aValidatorParameters;
172 getValidatorInfo(theNode, aValidatorId, aValidatorParameters);
173 aMessage->setValidatorId(aValidatorId);
174 aMessage->setValidatorParameters(aValidatorParameters);
175 xmlNodePtr aFeatureOrWdgNode = theNode->parent;
176 if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
177 aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
179 aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
180 aMessage->setFeatureId(myCurrentFeature);
182 aEvLoop->send(aMessage);