1 // SALOME ResourcesCatalog : implementation of catalog resources parsing (SALOME_ModuleCatalog.idl)
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_ResourcesCatalog_Handler.cxx
25 // Author : Estelle Deville
29 #include "SALOME_ResourcesCatalog_Handler.hxx"
33 #include "utilities.h"
37 //=============================================================================
40 * \param listOfResources: map of ParserResourcesType to fill when parsing
42 //=============================================================================
44 SALOME_ResourcesCatalog_Handler::
45 SALOME_ResourcesCatalog_Handler(MapOfParserResourcesType& listOfResources):
46 _resources_list(listOfResources)
48 MESSAGE("SALOME_ResourcesCatalog_Handler creation");
49 //XML tags initialisation
50 test_machine = "machine";
51 test_resources = "resources";
53 test_hostname = "hostname";
55 test_protocol = "protocol";
57 test_user_name = "userName";
58 test_appli_path = "appliPath";
59 test_modules = "modules";
60 test_module_name = "moduleName";
61 test_module_path = "modulePath";
62 test_pre_req_file_path = "preReqFilePath";
64 test_mem_in_mb = "memInMB";
65 test_cpu_freq_mhz = "CPUFreqMHz";
66 test_nb_of_nodes = "nbOfNodes";
67 test_nb_of_proc_per_node = "nbOfProcPerNode";
70 //=============================================================================
74 //=============================================================================
76 SALOME_ResourcesCatalog_Handler::~SALOME_ResourcesCatalog_Handler()
78 // MESSAGE("SALOME_ResourcesCatalog_Handler destruction");
81 //=============================================================================
83 * Retrieves DS after the file parse.
85 //=============================================================================
87 const MapOfParserResourcesType&
88 SALOME_ResourcesCatalog_Handler::GetResourcesAfterParsing() const
90 return _resources_list;
93 //=============================================================================
95 * Overload handler function startDocument.
96 * Called before an xml file is parsed.
97 * Clears the list of resources.
98 * \return true (if no error detected...)
100 //=============================================================================
102 bool SALOME_ResourcesCatalog_Handler::startDocument()
104 // MESSAGE("Begin parse document");
106 // --- Empty private elements
108 _resources_list.clear();
112 //=============================================================================
114 * Overload handler function startElement.
115 * \param QString argument by reference (not used here ?)
116 * \param QString argument by reference (not used here ?)
117 * \param name (not used here ?)
119 * \return true if no error was detected
121 //=============================================================================
124 SALOME_ResourcesCatalog_Handler::
125 startElement( const QString&,
128 const QXmlAttributes& attrs )
130 for (int i = 0;i < attrs.count();i++)
132 QString qName(attrs.localName(i));
133 std::string content(attrs.value(i).latin1());
135 if ((qName.compare(QString(test_hostname)) == 0))
136 _resource.DataForSort._hostName = content;
138 if ((qName.compare(QString(test_alias)) == 0))
139 _resource.Alias = content;
141 if ((qName.compare(QString(test_protocol)) == 0))
147 _resource.Protocol = rsh;
151 _resource.Protocol = ssh;
155 // If it'not in all theses cases, the protocol is affected to rsh
156 _resource.Protocol = rsh;
161 if ((qName.compare(QString(test_mode)) == 0))
167 _resource.Mode = interactive;
171 _resource.Mode = batch;
175 // If it'not in all theses cases, the mode is affected to interactive
176 _resource.Mode = interactive;
181 if ((qName.compare(QString(test_user_name)) == 0))
182 _resource.UserName = content;
184 if ((qName.compare(QString(test_appli_path)) == 0))
185 _resource.AppliPath = content;
187 if ((qName.compare(QString(test_module_name)) == 0))
188 previous_module_name = content;
190 if ((qName.compare(QString(test_module_path)) == 0))
191 previous_module_path = content;
193 if ((qName.compare(QString(test_pre_req_file_path)) == 0))
194 _resource.PreReqFilePath = content;
196 if ((qName.compare(QString(test_os)) == 0))
197 _resource.OS = content;
199 if ((qName.compare(QString(test_mem_in_mb)) == 0))
200 _resource.DataForSort._memInMB = atoi(content.c_str());
202 if ((qName.compare(QString(test_cpu_freq_mhz)) == 0))
203 _resource.DataForSort._CPUFreqMHz = atoi(content.c_str());
205 if ((qName.compare(QString(test_nb_of_nodes)) == 0))
206 _resource.DataForSort._nbOfNodes = atoi(content.c_str());
208 if ((qName.compare(QString(test_nb_of_proc_per_node)) == 0))
209 _resource.DataForSort._nbOfProcPerNode = atoi(content.c_str());
215 //=============================================================================
217 * Overload handler function endElement.
218 * \param QString argument by reference (not used here ?)
219 * \param QString argument by reference (not used here ?)
221 * \return true (if no error detected ...)
223 //=============================================================================
225 bool SALOME_ResourcesCatalog_Handler::
226 endElement(const QString&,
228 const QString& qName)
230 if ((qName.compare(QString(test_modules)) == 0))
231 _resource.ModulesPath[previous_module_name] = previous_module_path;
233 if ((qName.compare(QString(test_machine)) == 0))
234 _resources_list[_resource.DataForSort._hostName] = _resource;
239 //=============================================================================
241 * Overload handler function characters.
242 * fills the private attribute string 'content'.
244 * \return true (if no error detected ...)
246 //=============================================================================
248 bool SALOME_ResourcesCatalog_Handler::characters(const QString& chars)
250 content = (const char *)chars ;
254 //=============================================================================
256 * Overload handler function endDocument.
257 * Called after the document has been parsed.
258 * \return true (if no error detected ...)
260 //=============================================================================
262 bool SALOME_ResourcesCatalog_Handler::endDocument()
264 // for (map<string, ParserResourcesType>::const_iterator iter =
265 // _resources_list.begin();
266 // iter != _resources_list.end();
269 // SCRUTE((*iter).second.Alias);
270 // SCRUTE((*iter).second.UserName);
271 // SCRUTE((*iter).second.AppliPath);
272 // SCRUTE((*iter).second.PreReqFilePath);
273 // SCRUTE((*iter).second.OS);
274 // SCRUTE((*iter).second.Protocol);
275 // SCRUTE((*iter).second.Mode);
278 // MESSAGE("This is the end of document");
282 //=============================================================================
284 * Overload handler function errorProtocol.
285 * \return the error message.
287 //=============================================================================
289 QString SALOME_ResourcesCatalog_Handler::errorProtocol()
291 INFOS(" ------------- error protocol !");
295 //=============================================================================
297 * Overload handler function fatalError.
298 * Fills the private string errorProt with details on error.
299 * \param exception from parser
300 * \return boolean (meaning ?)
302 //=============================================================================
305 SALOME_ResourcesCatalog_Handler::fatalError
306 (const QXmlParseException& exception)
308 INFOS(" ------------- fatal error !");
309 errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
310 .arg( exception.message() )
311 .arg( exception.lineNumber() )
312 .arg( exception.columnNumber() );
314 return QXmlDefaultHandler::fatalError( exception );
317 //=============================================================================
319 * Fill the document tree in xml file, used to write in an xml file.
320 * \param doc document to fill.
322 //=============================================================================
324 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(QDomDocument& doc)
326 QDomElement root = doc.createElement("resources");
327 doc.appendChild(root);
329 for (map<string, ParserResourcesType>::iterator iter =
330 _resources_list.begin();
331 iter != _resources_list.end();
334 QDomElement eltRoot = doc.createElement(test_machine);
335 root.appendChild( eltRoot );
336 eltRoot.setAttribute((char *)test_hostname, (*iter).first.c_str());
337 eltRoot.setAttribute((char *)test_alias, (*iter).second.Alias.c_str());
339 switch ((*iter).second.Protocol)
343 eltRoot.setAttribute((char *)test_protocol, "rsh");
347 eltRoot.setAttribute((char *)test_protocol, "ssh");
351 eltRoot.setAttribute((char *)test_protocol, "rsh");
354 switch ((*iter).second.Mode)
358 eltRoot.setAttribute((char *)test_mode, "interactive");
362 eltRoot.setAttribute((char *)test_mode, "batch");
366 eltRoot.setAttribute((char *)test_mode, "interactive");
369 eltRoot.setAttribute((char *)test_user_name,
370 (*iter).second.UserName.c_str());
372 for (map<string, string>::const_iterator iter2 =
373 (*iter).second.ModulesPath.begin();
374 iter2 != (*iter).second.ModulesPath.end();
377 QDomElement rootForModulesPaths = doc.createElement(test_modules);
378 rootForModulesPaths.setAttribute(test_module_name,
379 (*iter2).first.c_str());
380 rootForModulesPaths.setAttribute(test_module_path,
381 (*iter2).second.c_str());
382 eltRoot.appendChild(rootForModulesPaths);
385 eltRoot.setAttribute(test_pre_req_file_path,
386 (*iter).second.PreReqFilePath.c_str());
387 eltRoot.setAttribute(test_os, (*iter).second.OS.c_str());
388 eltRoot.setAttribute(test_mem_in_mb,
389 (*iter).second.DataForSort._memInMB);
390 eltRoot.setAttribute(test_cpu_freq_mhz,
391 (*iter).second.DataForSort._CPUFreqMHz);
392 eltRoot.setAttribute(test_nb_of_nodes,
393 (*iter).second.DataForSort._nbOfNodes);
394 eltRoot.setAttribute(test_nb_of_proc_per_node,
395 (*iter).second.DataForSort._nbOfProcPerNode);