]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMUtils/GEOMUtils_XmlHandler.cxx
Salome HOME
0022616: [CEA 1038] Improve the quality of stl and vtk exports
[modules/geom.git] / src / GEOMUtils / GEOMUtils_XmlHandler.cxx
1 // Copyright (C) 2013-2014  CEA/DEN, EDF R&D, OPEN CASCADE
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 "GEOMUtils_XmlHandler.hxx"
21
22 #include <libxml/parser.h>
23 #include <algorithm>
24
25 //#define MYDEBUG
26
27 namespace
28 {
29   const char*    env_var     = "GEOM_PluginsList";
30   
31   const xmlChar* root_tag    = (xmlChar*)"geom-plugins";
32   const xmlChar* plugin_tag  = (xmlChar*)"geom-plugin";
33   const xmlChar* name_tag    = (xmlChar*)"name";
34   const xmlChar* server_tag  = (xmlChar*)"server-lib";
35   const xmlChar* gui_tag     = (xmlChar*)"gui-lib";
36   const xmlChar* actions_tag = (xmlChar*)"actions";
37   const xmlChar* action_tag  = (xmlChar*)"action";
38   const xmlChar* label_tag   = (xmlChar*)"label";
39   const xmlChar* icon_tag    = (xmlChar*)"icon";
40   const xmlChar* menu_tag    = (xmlChar*)"menu";
41   const xmlChar* tooltip_tag = (xmlChar*)"tooltip";
42   const xmlChar* status_tag  = (xmlChar*)"status-bar";
43   const xmlChar* accel_tag   = (xmlChar*)"accel";
44
45   std::string toUpper( const std::string& s )
46   {
47     std::string r = s;
48     std::transform( r.begin(), r.end(), r.begin(), toupper );
49     return r;
50   }
51
52   std::string toLower( const std::string& s )
53   {
54     std::string r = s;
55     std::transform( r.begin(), r.end(), r.begin(), tolower );
56     return r;
57   }
58
59   std::string readXmlAttribute(xmlNodePtr node, const xmlChar* attribute)
60   {
61     std::string result = "";
62     xmlChar* strAttr = xmlGetProp(node, attribute);
63     if (strAttr != NULL) {
64       result = (char*)strAttr;
65       xmlFree(strAttr);
66     }
67     return result;
68   }
69
70   std::list<std::string> getPluginXMLFiles()
71   {
72     std::list<std::string> xmlPaths;
73
74 #ifdef WIN32
75     std::string sep = "\\";
76 #else
77     std::string sep = "/";
78 #endif
79
80     if ( const char* var = getenv( env_var ) )
81     {
82       std::string plugins = var;
83
84       std::string::size_type from = 0, pos;
85       while ( from < plugins.size() )
86       {
87         pos = plugins.find( ':', from );
88         std::string plugin;
89         if ( pos != std::string::npos )
90           plugin = plugins.substr( from, pos-from );
91         else
92           plugin = plugins.substr( from ), pos = plugins.size();
93         from = pos + 1;
94         
95         if ( plugin.size() == 0 ) continue;
96         
97         std::string pluginRoot    = toUpper( plugin+"_ROOT_DIR" );
98
99         const char* rootDirGeom   = getenv( "GEOM_ROOT_DIR" );
100         const char* rootDirPlugin = getenv( pluginRoot.c_str() );
101
102         bool fileOK = false;
103         if ( rootDirGeom ) {
104           std::string xmlPath = rootDirGeom;
105           if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
106             xmlPath += sep;
107           xmlPath += "share" + sep + "salome" + sep + "resources" + sep + "geom" + sep + plugin + ".xml";
108 #ifdef WIN32
109           fileOK = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
110 #else
111           fileOK = (access(xmlPath.c_str(), F_OK) == 0);
112 #endif
113           if ( fileOK )
114             xmlPaths.push_back( xmlPath );
115         }
116         if ( !fileOK && rootDirPlugin ) {
117           std::string xmlPath = rootDirPlugin;
118           if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
119             xmlPath += sep;
120           xmlPath += "share" + sep + "salome" + sep + "resources" + sep + toLower(plugin) + sep + plugin + ".xml";
121 #ifdef WIN32
122           fileOK = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
123 #else
124           fileOK = (access(xmlPath.c_str(), F_OK) == 0);
125 #endif
126           if ( fileOK )
127             xmlPaths.push_back( xmlPath );
128         }
129       }
130     }
131     return xmlPaths;
132   }
133
134   void dumpinfo(const GEOMUtils::PluginInfo& info)
135   {
136     printf("DUMPING PLUGIN INFO\n");
137     GEOMUtils::PluginInfo::const_iterator it;
138     for (it = info.begin(); it != info.end(); ++it) {
139       GEOMUtils::PluginData pdata = *it;
140       printf("Plugin: %s\n", pdata.name.c_str());
141       printf("  serverLib = %s\n", pdata.serverLib.c_str());
142       printf("  clientLib = %s\n", pdata.clientLib.c_str());
143       printf("  actions:\n");
144       std::list<GEOMUtils::ActionData>::const_iterator ait;
145       for (ait = pdata.actions.begin(); ait != pdata.actions.end(); ++ait) {
146         GEOMUtils::ActionData adata = *ait;
147         printf("     label      = %s\n", adata.label.c_str());
148         printf("     icon       = %s\n", adata.icon.c_str());
149         printf("     menuText   = %s\n", adata.menuText.c_str());
150         printf("     toolTip    = %s\n", adata.toolTip.c_str());
151         printf("     statusText = %s\n", adata.statusText.c_str());
152         printf("\n");
153       }
154       printf("-----\n");
155     }
156   }
157 }
158
159 namespace GEOMUtils
160 {
161   PluginInfo ReadPluginInfo()
162   {
163     PluginInfo info;
164
165     std::list<std::string> xmlPaths = getPluginXMLFiles();
166
167     std::list<std::string>::const_iterator fit;
168
169     for ( fit = xmlPaths.begin(); fit != xmlPaths.end(); ++fit )
170     {
171       std::string fileName = *fit;
172       
173       int options = XML_PARSE_HUGE | XML_PARSE_NOCDATA;
174       xmlDocPtr doc = xmlReadFile( fileName.c_str(), NULL, options );
175       
176       if ( doc )
177       {
178         // get root node
179         xmlNodePtr root = xmlDocGetRootElement(doc);
180         
181         // check if it is plugins container node
182         if (xmlStrcmp(root->name, root_tag) == 0)
183         {
184           // iterate through children, to get plugins data
185           for (xmlNodePtr node = root->children; node; node = node->next)
186           {
187             if (xmlStrcmp(node->name, plugin_tag) == 0)
188             {
189               // plugin node
190               PluginData data;
191               data.name      = readXmlAttribute(node, name_tag);
192               data.serverLib = readXmlAttribute(node, server_tag);
193               data.clientLib = readXmlAttribute(node, gui_tag);
194               // iterate through children, to find actions container node
195               for (xmlNodePtr subnode = node->children; subnode; subnode = subnode->next)
196               {
197                 if (xmlStrcmp(subnode->name, actions_tag) == 0)
198                 {
199                   // actions container node
200                   // iterate through children, to get actions data
201                   for (xmlNodePtr subsubnode = subnode->children; subsubnode; subsubnode = subsubnode->next)
202                   {
203                     if (xmlStrcmp(subsubnode->name, action_tag) == 0)
204                     {
205                       // action node
206                       ActionData action;
207                       action.label      = readXmlAttribute(subsubnode, label_tag);
208                       action.icon       = readXmlAttribute(subsubnode, icon_tag);
209                       action.menuText   = readXmlAttribute(subsubnode, menu_tag);
210                       action.toolTip    = readXmlAttribute(subsubnode, tooltip_tag);
211                       action.statusText = readXmlAttribute(subsubnode, status_tag);
212                       action.accel      = readXmlAttribute(subsubnode, accel_tag);
213                       if (action.label != "")
214                         data.actions.push_back(action);
215                     } // end action node
216                   } // end iteration through actions container node children
217                 } // end actions container node
218               } // end iterations through plugin node children
219               
220               if (data.name != "")
221                 info.push_back(data);
222             } // end plugin node
223           } // end iterations through plugins container node children
224         } // end root node
225         
226         xmlFreeDoc(doc);
227         xmlCleanupParser();
228       } // end xml doc
229     }
230 #ifdef MYDEBUG
231     dumpinfo(info);
232 #endif
233     return info;
234   }
235 }