Salome HOME
4a6590c68a20446a7300d17e4da827ca498b5730
[modules/shaper.git] / src / Config / Config_WidgetReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_WidgetReader.cpp
5  *
6  *  Created on: Apr 2, 2014
7  *      Author: sbh
8  */
9
10 #include <Config_WidgetReader.h>
11 #include <Config_Keywords.h>
12 #include <Config_Common.h>
13
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16 #include <libxml/xpath.h>
17 #include <libxml/xmlstring.h>
18
19 #include <list>
20
21 #ifdef _DEBUG
22 #include <iostream>
23 #endif
24
25 Config_WidgetReader::Config_WidgetReader(const std::string& theXmlFile)
26     : Config_XMLReader(theXmlFile)
27
28 {
29 }
30
31 Config_WidgetReader::~Config_WidgetReader()
32 {
33 }
34
35 std::string Config_WidgetReader::featureWidgetCfg(const std::string& theFeatureName)
36 {
37   return myWidgetCache[theFeatureName];
38 }
39
40 std::string Config_WidgetReader::featureDescription(const std::string& theFeatureName)
41 {
42   return myDescriptionCache[theFeatureName];
43 }
44
45 void Config_WidgetReader::processNode(xmlNodePtr theNode)
46 {
47   if (isNode(theNode, NODE_FEATURE, NULL)) {
48     std::string aFeature = getProperty(theNode, _ID);
49     myWidgetCache[aFeature] = dumpNode(theNode);
50     myDescriptionCache[aFeature] = getProperty(theNode, FEATURE_TEXT);
51   }
52   //Process SOURCE nodes.
53   Config_XMLReader::processNode(theNode);
54 }
55
56 bool Config_WidgetReader::processChildren(xmlNodePtr theNode)
57 {
58   //Read all nodes recursively, source and validator nodes have no children
59   return !isNode(theNode, NODE_VALIDATOR,
60                           NODE_SELFILTER,
61                           NODE_SOURCE, NULL);
62 }
63
64 void Config_WidgetReader::resolveSourceNodes(xmlNodePtr theNode)
65 {
66   xmlNodePtr aNode = xmlFirstElementChild(theNode);
67   std::list<xmlNodePtr> aSourceNodes;
68   while (aNode != NULL) {
69     if (isNode(aNode, NODE_SOURCE, NULL)) {
70       Config_XMLReader aSourceReader = Config_XMLReader(getProperty(aNode, SOURCE_FILE));
71       xmlNodePtr aSourceRoot = aSourceReader.findRoot();
72       if (aSourceRoot) {
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     }
83     aNode = xmlNextElementSibling(aNode);
84   }
85   //Remove "SOURCE" node.
86   std::list<xmlNodePtr>::iterator it = aSourceNodes.begin();
87   for (; it != aSourceNodes.end(); it++) {
88     xmlUnlinkNode(*it);
89     xmlFreeNode(*it);
90   }
91 }
92
93 std::string Config_WidgetReader::dumpNode(xmlNodePtr theNode)
94 {
95   std::string result = "";
96   if (!hasChild(theNode)) {
97     // feature which has the next property should be dumped itself
98     std::string anOwnPanel = getProperty(theNode, PROPERTY_PANEL_ID);
99     if (!anOwnPanel.empty()) {
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     }
105     return result;
106   }
107   //Replace all "source" nodes with content;
108   resolveSourceNodes(theNode);
109   xmlBufferPtr buffer = xmlBufferCreate();
110   int size = xmlNodeDump(buffer, theNode->doc, theNode, 0, 0);
111   result = std::string((char*) (buffer->content));
112   xmlBufferFree(buffer);
113   return result;
114 }