Salome HOME
The obligatory and concealment attribute's properties now registers in model by speci...
[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 #ifdef WIN32
18 #pragma warning(disable : 4996) // for getenv
19 #endif
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 theNode)
65 {
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());
70 #ifdef _DEBUG
71     std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
72 #endif
73   } else if (isNode(theNode, NODE_VALIDATOR, NULL)) {
74     processValidator(theNode);
75   }
76 }
77
78 /*
79  * Defines which nodes should be processed recursively. Virtual.
80  * The default impl is to read all nodes.
81  */
82 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
83 {
84   return true;
85 }
86
87 /*
88  *
89  */
90 xmlNodePtr Config_XMLReader::findRoot()
91 {
92   if (myXmlDoc == NULL) {
93     myXmlDoc = xmlParseFile(myDocumentPath.c_str());
94   }
95   if (myXmlDoc == NULL) {
96 #ifdef _DEBUG
97     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
98     << " is not parsed successfully." << std::endl;
99 #endif
100     return NULL;
101   }
102   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
103 #ifdef _DEBUG
104   if(aRoot == NULL) {
105     std::cout << "Config_XMLReader::import: " << "Error: empty document";
106   }
107 #endif
108   return aRoot;
109 }
110
111 /*
112  * Calls processNode() for each child (for some - recursively)
113  * of the given node.
114  * \sa ReadAll()
115  */
116 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
117 {
118   if (!theParent)
119     return;
120   xmlNodePtr aNode = theParent->xmlChildrenNode;
121   for (; aNode; aNode = aNode->next) {
122     //Still no text processing in features...
123     if (!isElementNode(aNode)) {
124       continue;
125     }
126     processNode(aNode);
127     if (processChildren(aNode)) {
128       readRecursively(aNode);
129     }
130   }
131 }
132
133 /*
134  * void* -> xmlNodePtr
135  */
136 xmlNodePtr Config_XMLReader::node(void* theNode)
137 {
138   return static_cast<xmlNodePtr>(theNode);
139 }
140
141 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
142 {
143   std::string result = "";
144   char* aPropChars = (char*) theNode->name;
145   if (!aPropChars || aPropChars[0] == 0)
146     return result;
147   result = std::string(aPropChars);
148   return result;
149 }
150
151 void Config_XMLReader::processValidator(xmlNodePtr theNode)
152 {
153   Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
154   Events_Loop* aEvLoop = Events_Loop::loop();
155   boost::shared_ptr<Config_ValidatorMessage> 
156     aMessage(new Config_ValidatorMessage(aValidatoEvent, this));
157   std::string aValidatorId;
158   std::list<std::string> aValidatorParameters;
159   getValidatorInfo(theNode, aValidatorId, aValidatorParameters);
160   aMessage->setValidatorId(aValidatorId);
161   aMessage->setValidatorParameters(aValidatorParameters);
162   xmlNodePtr aFeatureOrWdgNode = theNode->parent;
163   if (isNode(aFeatureOrWdgNode, NODE_FEATURE, NULL)) {
164     aMessage->setFeatureId(getProperty(aFeatureOrWdgNode, _ID));
165   } else {
166     aMessage->setAttributeId(getProperty(aFeatureOrWdgNode, _ID));
167     aMessage->setFeatureId(myCurrentFeature);
168   }
169   aEvLoop->send(aMessage);
170 }