Salome HOME
7268c3d2b33c094bc0e678dc389df4fa8840c78f
[modules/shaper.git] / src / Config / Config_WidgetReader.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Config_WidgetReader.h>
21 #include <Config_Keywords.h>
22 #include <Config_Common.h>
23
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <libxml/xpath.h>
27 #include <libxml/xmlstring.h>
28
29 #include <list>
30
31 #ifdef _DEBUG
32 #include <iostream>
33 #endif
34
35 Config_WidgetReader::Config_WidgetReader(const std::string& theXmlFile)
36     : Config_XMLReader(theXmlFile)
37
38 {
39 }
40
41 Config_WidgetReader::~Config_WidgetReader()
42 {
43 }
44
45 std::string Config_WidgetReader::featureWidgetCfg(const std::string& theFeatureName)
46 {
47   return myWidgetCache[theFeatureName];
48 }
49
50 std::string Config_WidgetReader::featureDescription(const std::string& theFeatureName)
51 {
52   return myDescriptionCache[theFeatureName];
53 }
54
55 void Config_WidgetReader::processNode(xmlNodePtr theNode)
56 {
57   if (isNode(theNode, NODE_FEATURE, NULL)) {
58     std::string aFeature = getProperty(theNode, _ID);
59     myWidgetCache[aFeature] = dumpNode(theNode);
60     myDescriptionCache[aFeature] = getProperty(theNode, FEATURE_TEXT);
61   }
62   //Process SOURCE nodes.
63   Config_XMLReader::processNode(theNode);
64 }
65
66 bool Config_WidgetReader::processChildren(xmlNodePtr theNode)
67 {
68   //Read all nodes recursively, source and validator nodes have no children
69   return !isNode(theNode, NODE_VALIDATOR,
70                           NODE_SOURCE, NULL);
71 }
72
73 void Config_WidgetReader::resolveSourceNodes(xmlNodePtr theNode)
74 {
75   xmlNodePtr aNode = xmlFirstElementChild(theNode);
76   std::list<xmlNodePtr> aSourceNodes;
77   while (aNode != NULL) {
78     if (isNode(aNode, NODE_SOURCE, NULL)) {
79       Config_XMLReader aSourceReader = Config_XMLReader(getProperty(aNode, SOURCE_FILE));
80       xmlNodePtr aSourceRoot = aSourceReader.findRoot();
81       if (aSourceRoot) {
82         xmlNodePtr aSourceNode = xmlFirstElementChild(aSourceRoot);
83         xmlNodePtr aTargetNode = xmlDocCopyNodeList(aNode->doc, aSourceNode);
84         while (aTargetNode != NULL) {
85           xmlNodePtr aNextNode = xmlNextElementSibling(aTargetNode);
86           xmlAddPrevSibling(aNode, aTargetNode);
87           aTargetNode = aNextNode;
88         }
89         aSourceNodes.push_back(aNode);
90       }
91     }
92     aNode = xmlNextElementSibling(aNode);
93   }
94   //Remove "SOURCE" node.
95   std::list<xmlNodePtr>::iterator it = aSourceNodes.begin();
96   for (; it != aSourceNodes.end(); it++) {
97     xmlUnlinkNode(*it);
98     xmlFreeNode(*it);
99   }
100 }
101
102 std::string Config_WidgetReader::dumpNode(xmlNodePtr theNode)
103 {
104   std::string result = "";
105   if (!hasChild(theNode)) {
106     // feature which has the next property should be dumped itself
107     std::string anOwnPanel = getProperty(theNode, PROPERTY_PANEL_ID);
108     if (!anOwnPanel.empty()) {
109       xmlBufferPtr buffer = xmlBufferCreate();
110 #ifdef _DEBUG
111       int size =
112 #endif
113         xmlNodeDump(buffer, theNode->doc, theNode, 0, 0);
114       result = std::string((char*) (buffer->content));
115       xmlBufferFree(buffer);
116     }
117     return result;
118   }
119   //Replace all "source" nodes with content;
120   resolveSourceNodes(theNode);
121   xmlBufferPtr buffer = xmlBufferCreate();
122 #ifdef _DEBUG
123   int size =
124 #endif
125     xmlNodeDump(buffer, theNode->doc, theNode, 0, 0);
126   result = std::string((char*) (buffer->content));
127   xmlBufferFree(buffer);
128   return result;
129 }