1 // Copyright (C) 2013-2019 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GEOMUtils_XmlHandler.hxx"
22 #include <libxml/parser.h>
35 const char* env_var = "GEOM_PluginsList";
37 const xmlChar* root_tag = (xmlChar*)"geom-plugins";
38 const xmlChar* plugin_tag = (xmlChar*)"geom-plugin";
39 const xmlChar* name_tag = (xmlChar*)"name";
40 const xmlChar* server_tag = (xmlChar*)"server-lib";
41 const xmlChar* gui_tag = (xmlChar*)"gui-lib";
42 const xmlChar* actions_tag = (xmlChar*)"actions";
43 const xmlChar* action_tag = (xmlChar*)"action";
44 const xmlChar* label_tag = (xmlChar*)"label";
45 const xmlChar* icon_tag = (xmlChar*)"icon";
46 const xmlChar* menu_tag = (xmlChar*)"menu";
47 const xmlChar* tooltip_tag = (xmlChar*)"tooltip";
48 const xmlChar* status_tag = (xmlChar*)"status-bar";
49 const xmlChar* accel_tag = (xmlChar*)"accel";
51 std::string toUpper( const std::string& s )
54 std::transform( r.begin(), r.end(), r.begin(), toupper );
58 std::string toLower( const std::string& s )
61 std::transform( r.begin(), r.end(), r.begin(), tolower );
65 std::string readXmlAttribute(xmlNodePtr node, const xmlChar* attribute)
67 std::string result = "";
68 xmlChar* strAttr = xmlGetProp(node, attribute);
69 if (strAttr != NULL) {
70 result = (char*)strAttr;
76 std::list<std::string> getPluginXMLFiles()
78 std::list<std::string> xmlPaths;
81 std::string sep = "\\";
83 std::string sep = "/";
86 if ( const char* var = getenv( env_var ) )
88 std::string plugins = var;
90 std::string::size_type from = 0, pos;
91 while ( from < plugins.size() )
93 pos = plugins.find( ':', from );
95 if ( pos != std::string::npos )
96 plugin = plugins.substr( from, pos-from );
98 plugin = plugins.substr( from ), pos = plugins.size();
101 if ( plugin.size() == 0 ) continue;
103 std::string pluginRoot = toUpper( plugin+"_ROOT_DIR" );
105 const char* rootDirGeom = getenv( "GEOM_ROOT_DIR" );
106 const char* rootDirPlugin = getenv( pluginRoot.c_str() );
110 std::string xmlPath = rootDirGeom;
111 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
113 xmlPath += "share" + sep + "salome" + sep + "resources" + sep + "geom" + sep + plugin + ".xml";
117 //RNV: this is workaround for providing compilation,
118 // path should be processed as unicode string.
119 size_t length = strlen(xmlPath.c_str()) + sizeof(char);
120 wchar_t* aPath = new wchar_t[length + 1];
121 memset(aPath, '\0', length);
122 mbstowcs(aPath, xmlPath.c_str(), length);
124 const char* aPath = xmlPath.c_str();
126 fileOK = (GetFileAttributes(aPath) != INVALID_FILE_ATTRIBUTES);
131 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
134 xmlPaths.push_back( xmlPath );
136 if ( !fileOK && rootDirPlugin ) {
137 std::string xmlPath = rootDirPlugin;
138 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
140 xmlPath += "share" + sep + "salome" + sep + "resources" + sep + toLower(plugin) + sep + plugin + ".xml";
143 size_t length = strlen(xmlPath.c_str()) + sizeof(char);
144 wchar_t* aPath = new wchar_t[length+1];
145 memset(aPath, '\0', length);
146 mbstowcs(aPath, xmlPath.c_str(), length);
148 const char* aPath = xmlPath.c_str();
150 fileOK = (GetFileAttributes(aPath) != INVALID_FILE_ATTRIBUTES);
155 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
158 xmlPaths.push_back( xmlPath );
166 void dumpinfo(const GEOMUtils::PluginInfo& info)
168 printf("DUMPING PLUGIN INFO\n");
169 GEOMUtils::PluginInfo::const_iterator it;
170 for (it = info.begin(); it != info.end(); ++it) {
171 GEOMUtils::PluginData pdata = *it;
172 printf("Plugin: %s\n", pdata.name.c_str());
173 printf(" serverLib = %s\n", pdata.serverLib.c_str());
174 printf(" clientLib = %s\n", pdata.clientLib.c_str());
175 printf(" actions:\n");
176 std::list<GEOMUtils::ActionData>::const_iterator ait;
177 for (ait = pdata.actions.begin(); ait != pdata.actions.end(); ++ait) {
178 GEOMUtils::ActionData adata = *ait;
179 printf(" label = %s\n", adata.label.c_str());
180 printf(" icon = %s\n", adata.icon.c_str());
181 printf(" menuText = %s\n", adata.menuText.c_str());
182 printf(" toolTip = %s\n", adata.toolTip.c_str());
183 printf(" statusText = %s\n", adata.statusText.c_str());
194 PluginInfo ReadPluginInfo()
198 std::list<std::string> xmlPaths = getPluginXMLFiles();
200 std::list<std::string>::const_iterator fit;
202 for ( fit = xmlPaths.begin(); fit != xmlPaths.end(); ++fit )
204 std::string fileName = *fit;
206 int options = XML_PARSE_HUGE | XML_PARSE_NOCDATA;
207 xmlDocPtr doc = xmlReadFile( fileName.c_str(), NULL, options );
212 xmlNodePtr root = xmlDocGetRootElement(doc);
214 // check if it is plugins container node
215 if (xmlStrcmp(root->name, root_tag) == 0)
217 // iterate through children, to get plugins data
218 for (xmlNodePtr node = root->children; node; node = node->next)
220 if (xmlStrcmp(node->name, plugin_tag) == 0)
224 data.name = readXmlAttribute(node, name_tag);
225 data.serverLib = readXmlAttribute(node, server_tag);
226 data.clientLib = readXmlAttribute(node, gui_tag);
227 // iterate through children, to find actions container node
228 for (xmlNodePtr subnode = node->children; subnode; subnode = subnode->next)
230 if (xmlStrcmp(subnode->name, actions_tag) == 0)
232 // actions container node
233 // iterate through children, to get actions data
234 for (xmlNodePtr subsubnode = subnode->children; subsubnode; subsubnode = subsubnode->next)
236 if (xmlStrcmp(subsubnode->name, action_tag) == 0)
240 action.label = readXmlAttribute(subsubnode, label_tag);
241 action.icon = readXmlAttribute(subsubnode, icon_tag);
242 action.menuText = readXmlAttribute(subsubnode, menu_tag);
243 action.toolTip = readXmlAttribute(subsubnode, tooltip_tag);
244 action.statusText = readXmlAttribute(subsubnode, status_tag);
245 action.accel = readXmlAttribute(subsubnode, accel_tag);
246 if (action.label != "")
247 data.actions.push_back(action);
249 } // end iteration through actions container node children
250 } // end actions container node
251 } // end iterations through plugin node children
254 info.push_back(data);
256 } // end iterations through plugins container node children
260 //xmlCleanupParser();//vsr: xmlCleanupParser should not be called from the application