Salome HOME
Templates for implementation of Geom classes receiving
[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
10 #include <Event_Loop.h>
11 #include <libxml/parser.h>
12 #include <libxml/tree.h>
13
14 /*
15 #ifdef WIN32
16 //For GetModuleFileNameW
17 #include <windows.h>
18 #endif
19 */
20
21 #ifdef _DEBUG
22 #include <iostream>
23 #endif
24
25 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
26     : myXmlDoc(NULL)
27 {
28   std::string prefix = "";
29   /*
30    * Get path to *.xml files (typically ./bin/../plugins/)
31
32    * the problem: application may be launched using python executable,
33    * to use environment variable (at least for the current moment)
34    */
35   char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
36   if (anEnv) {
37     prefix = std::string(anEnv) + "/";
38   }
39
40   myDocumentPath = prefix + theXmlFileName;
41 }
42
43 Config_XMLReader::~Config_XMLReader()
44 {
45   xmlFreeDoc(myXmlDoc);
46 }
47
48 /*
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.
52  */
53 void Config_XMLReader::readAll()
54 {
55   xmlNodePtr aRoot = findRoot();
56   readRecursively(aRoot);
57 }
58
59 /*
60  * Allows to customize reader's behavior for a node. Virtual.
61  * The default impl does nothing. (In debug mode prints
62  * some info)
63  */
64 void Config_XMLReader::processNode(xmlNodePtr aNode)
65 {
66 #ifdef _DEBUG
67   std::cout << "Config_XMLReader::processNode: "
68   << aNode->name << " content: "
69   << aNode->content << std::endl;
70 #endif
71 }
72
73 /*
74  * Defines which nodes should be processed recursively. Virtual.
75  * The default impl is to read all nodes.
76  */
77 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
78 {
79   return true;
80 }
81
82 /*
83  *
84  */
85 xmlNodePtr Config_XMLReader::findRoot()
86 {
87   myXmlDoc = xmlParseFile(myDocumentPath.c_str());
88   if (myXmlDoc == NULL) {
89 #ifdef _DEBUG
90     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
91     << " is not parsed successfully." << std::endl;
92 #endif
93     return NULL;
94   }
95
96   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
97 #ifdef _DEBUG
98   if(aRoot == NULL) {
99     std::cout << "Config_XMLReader::import: " << "Error: empty document";
100   }
101 #endif
102   return aRoot;
103 }
104
105 /*
106  * Calls processNode() for each child (for some - recursively)
107  * of the given node.
108  * \sa ReadAll()
109  */
110 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
111 {
112   if (!theParent)
113     return;
114   xmlNodePtr aNode = theParent->xmlChildrenNode;
115   for(; aNode; aNode = aNode->next) {
116     processNode(aNode);
117     if (processChildren(aNode)) {
118       readRecursively(aNode);
119     }
120   }
121 }
122
123 /*
124  * void* -> xmlNodePtr
125  */
126 xmlNodePtr Config_XMLReader::node(void* theNode)
127 {
128   return static_cast<xmlNodePtr>(theNode);
129 }
130
131 /*
132  * Returns named property for a given node as std::string.
133  */
134 std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name)
135 {
136   std::string result = "";
137   char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name);
138   if (!aPropChars || aPropChars[0] == 0)
139     return result;
140   result = std::string(aPropChars);
141   return result;
142 }
143