Salome HOME
Sources formated according to the codeing standards
[modules/shaper.git] / src / Config / Config_XMLReader.cpp
1 /*
2  * Config_XMLReader.cpp
3  *
4  *  Created on: Mar 14, 2014
5  *      Author: sbh
6  */
7
8 #include <Config_XMLReader.h>
9 #include <Config_Keywords.h>
10 #include <Config_Common.h>
11 #include <Config_ValidatorMessage.h>
12
13 #include <Events_Loop.h>
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16
17 /*
18  #ifdef WIN32
19  //For GetModuleFileNameW
20  #include <windows.h>
21  #endif
22  */
23
24 #ifdef _DEBUG
25 #include <iostream>
26 #endif
27
28 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
29     : myXmlDoc(NULL)
30 {
31   std::string prefix = "";
32   /*
33    * Get path to *.xml files (typically ./bin/../plugins/)
34
35    * the problem: application may be launched using python executable,
36    * to use environment variable (at least for the current moment)
37    */
38   char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
39   if (anEnv) {
40     prefix = std::string(anEnv) + "/";
41   }
42
43   myDocumentPath = prefix + theXmlFileName;
44 }
45
46 Config_XMLReader::~Config_XMLReader()
47 {
48   xmlFreeDoc(myXmlDoc);
49 }
50
51 /*
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.
55  */
56 void Config_XMLReader::readAll()
57 {
58   xmlNodePtr aRoot = findRoot();
59   readRecursively(aRoot);
60 }
61
62 /*
63  * Allows to customize reader's behavior for a node. Virtual.
64  * The default impl does nothing. (In debug mode prints
65  * some info)
66  */
67 void Config_XMLReader::processNode(xmlNodePtr theNode)
68 {
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());
73 #ifdef _DEBUG
74     std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
75 #endif
76   } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
77     processValidator(theNode);
78   }
79 }
80
81 /*
82  * Defines which nodes should be processed recursively. Virtual.
83  * The default impl is to read all nodes.
84  */
85 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
86 {
87   return true;
88 }
89
90 /*
91  *
92  */
93 xmlNodePtr Config_XMLReader::findRoot()
94 {
95   if (myXmlDoc == NULL) {
96     myXmlDoc = xmlParseFile(myDocumentPath.c_str());
97   }
98   if (myXmlDoc == NULL) {
99 #ifdef _DEBUG
100     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
101     << " is not parsed successfully." << std::endl;
102 #endif
103     return NULL;
104   }
105   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
106 #ifdef _DEBUG
107   if(aRoot == NULL) {
108     std::cout << "Config_XMLReader::import: " << "Error: empty document";
109   }
110 #endif
111   return aRoot;
112 }
113
114 /*
115  * Calls processNode() for each child (for some - recursively)
116  * of the given node.
117  * \sa ReadAll()
118  */
119 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
120 {
121   if (!theParent)
122     return;
123   xmlNodePtr aNode = theParent->xmlChildrenNode;
124   for (; aNode; aNode = aNode->next) {
125     //Still no text processing in features...
126     if (!isElementNode(aNode)) {
127       continue;
128     }
129     processNode(aNode);
130     if (processChildren(aNode)) {
131       readRecursively(aNode);
132     }
133   }
134 }
135
136 /*
137  * void* -> xmlNodePtr
138  */
139 xmlNodePtr Config_XMLReader::node(void* theNode)
140 {
141   return static_cast<xmlNodePtr>(theNode);
142 }
143
144 /*
145  * Returns named property for a given node as std::string.
146  */
147 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* theName)
148 {
149   std::string result = "";
150   char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST theName);
151   if (!aPropChars || aPropChars[0] == 0)
152     return result;
153   result = std::string(aPropChars);
154   return result;
155 }
156
157 void Config_XMLReader::processValidator(xmlNodePtr theNode)
158 {
159   Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
160   Events_Loop* aEvLoop = Events_Loop::loop();
161   Config_ValidatorMessage aMessage(aValidatoEvent, this);
162   std::string aValidatorId;
163   std::list<std::string> aValidatorParameters;
164   getValidatorInfo(theNode, aValidatorId, aValidatorParameters);
165   aMessage.setValidatorId(aValidatorId);
166   aMessage.setValidatorParameters(aValidatorParameters);
167   xmlNodePtr aFeatureOrWdgNode = theNode->parent;
168   if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
169     aMessage.setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
170   } else {
171     aMessage.setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
172     aMessage.setFeatureId(myCurrentFeature);
173   }
174   aEvLoop->send(aMessage);
175 }