Salome HOME
10eb506e46e667c677c897b458906f824ed997ee
[modules/shaper.git] / src / Config / Config_WidgetReader.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Config_WidgetReader.h>
22 #include <Config_Keywords.h>
23 #include <Config_Common.h>
24
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
27 #include <libxml/xpath.h>
28 #include <libxml/xmlstring.h>
29
30 #include <list>
31
32 #ifdef _DEBUG
33 #include <iostream>
34 #endif
35
36 Config_WidgetReader::Config_WidgetReader(const std::string& theXmlFile)
37     : Config_XMLReader(theXmlFile)
38
39 {
40 }
41
42 Config_WidgetReader::~Config_WidgetReader()
43 {
44 }
45
46 std::string Config_WidgetReader::featureWidgetCfg(const std::string& theFeatureName)
47 {
48   return myWidgetCache[theFeatureName];
49 }
50
51 std::string Config_WidgetReader::featureDescription(const std::string& theFeatureName)
52 {
53   return myDescriptionCache[theFeatureName];
54 }
55
56 void Config_WidgetReader::processNode(xmlNodePtr theNode)
57 {
58   if (isNode(theNode, NODE_FEATURE, NULL)) {
59     std::string aFeature = getProperty(theNode, _ID);
60     myWidgetCache[aFeature] = dumpNode(theNode);
61     myDescriptionCache[aFeature] = getProperty(theNode, FEATURE_TEXT);
62   }
63   //Process SOURCE nodes.
64   Config_XMLReader::processNode(theNode);
65 }
66
67 bool Config_WidgetReader::processChildren(xmlNodePtr theNode)
68 {
69   //Read all nodes recursively, source and validator nodes have no children
70   return !isNode(theNode, NODE_VALIDATOR,
71                           NODE_SOURCE, NULL);
72 }
73
74 void Config_WidgetReader::resolveSourceNodes(xmlNodePtr theNode)
75 {
76   xmlNodePtr aNode = xmlFirstElementChild(theNode);
77   std::list<xmlNodePtr> aSourceNodes;
78   while (aNode != NULL) {
79     if (isNode(aNode, NODE_SOURCE, NULL)) {
80       Config_XMLReader aSourceReader = Config_XMLReader(getProperty(aNode, SOURCE_FILE));
81       xmlNodePtr aSourceRoot = aSourceReader.findRoot();
82       if (aSourceRoot) {
83         xmlNodePtr aSourceNode = xmlFirstElementChild(aSourceRoot);
84         xmlNodePtr aTargetNode = xmlDocCopyNodeList(aNode->doc, aSourceNode);
85         while (aTargetNode != NULL) {
86           xmlNodePtr aNextNode = xmlNextElementSibling(aTargetNode);
87           xmlAddPrevSibling(aNode, aTargetNode);
88           aTargetNode = aNextNode;
89         }
90         aSourceNodes.push_back(aNode);
91       }
92     }
93     aNode = xmlNextElementSibling(aNode);
94   }
95   //Remove "SOURCE" node.
96   std::list<xmlNodePtr>::iterator it = aSourceNodes.begin();
97   for (; it != aSourceNodes.end(); it++) {
98     xmlUnlinkNode(*it);
99     xmlFreeNode(*it);
100   }
101 }
102
103 std::string Config_WidgetReader::dumpNode(xmlNodePtr theNode)
104 {
105   std::string result = "";
106   if (!hasChild(theNode)) {
107     // feature which has the next property should be dumped itself
108     std::string anOwnPanel = getProperty(theNode, PROPERTY_PANEL_ID);
109     if (!anOwnPanel.empty()) {
110       xmlBufferPtr buffer = xmlBufferCreate();
111       int size = xmlNodeDump(buffer, theNode->doc, theNode, 0, 0);
112       result = std::string((char*) (buffer->content));
113       xmlBufferFree(buffer);
114     }
115     return result;
116   }
117   //Replace all "source" nodes with content;
118   resolveSourceNodes(theNode);
119   xmlBufferPtr buffer = xmlBufferCreate();
120   int size = xmlNodeDump(buffer, theNode->doc, theNode, 0, 0);
121   result = std::string((char*) (buffer->content));
122   xmlBufferFree(buffer);
123   return result;
124 }