Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / Config / Config_WidgetReader.cpp
1 /*
2  * Config_WidgetReader.cpp
3  *
4  *  Created on: Apr 2, 2014
5  *      Author: sbh
6  */
7
8 #include <Config_WidgetReader.h>
9 #include <Config_Keywords.h>
10 #include <Config_Common.h>
11
12 #include <libxml/parser.h>
13 #include <libxml/tree.h>
14 #include <libxml/xpath.h>
15 #include <libxml/xmlstring.h>
16
17 #include <list>
18
19 #ifdef _DEBUG
20 #include <iostream>
21 #endif
22
23 Config_WidgetReader::Config_WidgetReader(const std::string& theXmlFile)
24     : Config_XMLReader(theXmlFile)
25
26 {
27 }
28
29 Config_WidgetReader::~Config_WidgetReader()
30 {
31 }
32
33 std::string Config_WidgetReader::featureWidgetCfg(const std::string& theFeatureName)
34 {
35   return myWidgetCache[theFeatureName];
36 }
37
38 std::string Config_WidgetReader::featureDescription(const std::string& theFeatureName)
39 {
40   return myDescriptionCache[theFeatureName];
41 }
42
43 void Config_WidgetReader::processNode(xmlNodePtr theNode)
44 {
45   if (isNode(theNode, NODE_FEATURE, NULL)) {
46     myCurrentFeature = getProperty(theNode, _ID);
47     myWidgetCache[myCurrentFeature] = dumpNode(theNode);
48     myDescriptionCache[myCurrentFeature] = getProperty(theNode, FEATURE_TEXT);
49   }
50   //Process SOURCE nodes.
51   Config_XMLReader::processNode(theNode);
52 }
53
54 bool Config_WidgetReader::processChildren(xmlNodePtr theNode)
55 {
56   //Read all nodes recursively, source and validator nodes have no children
57   return !isNode(theNode, NODE_VALIDATOR, NODE_SOURCE, NULL);
58 }
59
60 void Config_WidgetReader::resolveSourceNodes(xmlNodePtr theNode)
61 {
62   xmlNodePtr aNode = xmlFirstElementChild(theNode);
63   std::list<xmlNodePtr> aSourceNodes;
64   while (aNode != NULL) {
65     if (isNode(aNode, NODE_SOURCE, NULL)) {
66       Config_XMLReader aSourceReader = Config_XMLReader(getProperty(aNode, SOURCE_FILE));
67       xmlNodePtr aSourceRoot = aSourceReader.findRoot();
68       if (!aSourceRoot) {
69         continue;
70       }
71       xmlNodePtr aSourceNode = xmlFirstElementChild(aSourceRoot);
72       xmlNodePtr aTargetNode = xmlDocCopyNodeList(aNode->doc, aSourceNode);
73       while (aTargetNode != NULL) {
74         xmlNodePtr aNextNode = xmlNextElementSibling(aTargetNode);
75         xmlAddPrevSibling(aNode, aTargetNode);
76         aTargetNode = aNextNode;
77       }
78       aSourceNodes.push_back(aNode);
79     }
80     aNode = xmlNextElementSibling(aNode);
81   }
82   //Remove "SOURCE" node.
83   std::list<xmlNodePtr>::iterator it = aSourceNodes.begin();
84   for (; it != aSourceNodes.end(); it++) {
85     xmlUnlinkNode(*it);
86     xmlFreeNode(*it);
87   }
88 }
89
90 std::string Config_WidgetReader::dumpNode(xmlNodePtr theNode)
91 {
92   std::string result = "";
93   if (!hasChild(theNode)) {
94     return result;
95   }
96   //Replace all "source" nodes with content;
97   resolveSourceNodes(theNode);
98   xmlBufferPtr buffer = xmlBufferCreate();
99   int size = xmlNodeDump(buffer, theNode->doc, theNode, 0, 0);
100   result = std::string((char*) (buffer->content));
101   xmlBufferFree(buffer);
102   return result;
103 }