]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_WidgetReader.cpp
Salome HOME
Processing of 'selection_filter' xml nodes added.
[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,
58                           NODE_SELFILTER,
59                           NODE_SOURCE, NULL);
60 }
61
62 void Config_WidgetReader::resolveSourceNodes(xmlNodePtr theNode)
63 {
64   xmlNodePtr aNode = xmlFirstElementChild(theNode);
65   std::list<xmlNodePtr> aSourceNodes;
66   while (aNode != NULL) {
67     if (isNode(aNode, NODE_SOURCE, NULL)) {
68       Config_XMLReader aSourceReader = Config_XMLReader(getProperty(aNode, SOURCE_FILE));
69       xmlNodePtr aSourceRoot = aSourceReader.findRoot();
70       if (!aSourceRoot) {
71         continue;
72       }
73       xmlNodePtr aSourceNode = xmlFirstElementChild(aSourceRoot);
74       xmlNodePtr aTargetNode = xmlDocCopyNodeList(aNode->doc, aSourceNode);
75       while (aTargetNode != NULL) {
76         xmlNodePtr aNextNode = xmlNextElementSibling(aTargetNode);
77         xmlAddPrevSibling(aNode, aTargetNode);
78         aTargetNode = aNextNode;
79       }
80       aSourceNodes.push_back(aNode);
81     }
82     aNode = xmlNextElementSibling(aNode);
83   }
84   //Remove "SOURCE" node.
85   std::list<xmlNodePtr>::iterator it = aSourceNodes.begin();
86   for (; it != aSourceNodes.end(); it++) {
87     xmlUnlinkNode(*it);
88     xmlFreeNode(*it);
89   }
90 }
91
92 std::string Config_WidgetReader::dumpNode(xmlNodePtr theNode)
93 {
94   std::string result = "";
95   if (!hasChild(theNode)) {
96     return result;
97   }
98   //Replace all "source" nodes with content;
99   resolveSourceNodes(theNode);
100   xmlBufferPtr buffer = xmlBufferCreate();
101   int size = xmlNodeDump(buffer, theNode->doc, theNode, 0, 0);
102   result = std::string((char*) (buffer->content));
103   xmlBufferFree(buffer);
104   return result;
105 }