Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / ModuleCatalog / SALOME_ModuleCatalog_Handler.cxx
1 //  SALOME ModuleCatalog : implementation of ModuleCatalog server which parsers xml description of modules
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_ModuleCatalog_Handler.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //  $Header$ 
28
29 #define WRITE_CATA_COMPONENT
30
31 #include "SALOME_ModuleCatalog_Handler.hxx"
32 #include "SALOME_ModuleCatalog_Parser_IO.hxx"
33 #include "utilities.h"
34
35 #include <sstream>
36 using namespace std;
37
38 #ifdef _DEBUG_
39 static int MYDEBUG = 0;
40 #else
41 static int MYDEBUG = 0;
42 #endif
43
44 //----------------------------------------------------------------------
45 // Function : SALOME_ModuleCatalog_Handler
46 // Purpose  : Constructor
47 //----------------------------------------------------------------------
48 SALOME_ModuleCatalog_Handler::SALOME_ModuleCatalog_Handler()
49 {
50   if(MYDEBUG) BEGIN_OF("SALOME_ModuleCatalog_Handler");
51
52   // XML Tags initialisation
53   // Used in the function endElement
54   test_path_prefix_name     = "path-prefix-name";
55   test_computer_name        = "computer-name" ;
56   test_path_prefix          = "path-prefix" ;
57
58   test_component_name       = "component-name";
59   test_component_username   = "component-username";
60   test_component_type       = "component-type" ;
61   test_component_multistudy = "component-multistudy";
62   test_component_icon       = "component-icone" ;
63   test_component_impltype   = "component-impltype";
64   test_component_version    = "component-version";
65   test_component_comment    = "component-comment";
66
67   test_interface_name       = "component-interface-name" ;
68   
69   test_service_name         = "service-name";
70   test_defaultservice       = "service-by-default";
71
72   test_typeofnode           = "type-of-node";
73
74   test_inParameter_type     = "inParameter-type";
75   test_inParameter_name     = "inParameter-name";
76   test_inParameter          = "inParameter";
77   test_inParameter_list     = "inParameter-list";
78
79   test_outParameter_type    = "outParameter-type";
80   test_outParameter_name    = "outParameter-name";
81   test_outParameter         = "outParameter";
82   test_outParameter_list    = "outParameter-list";
83
84   test_inDataStreamParameter_type = "inParameter-type";
85   test_inDataStreamParameter_name = "inParameter-name";
86   test_inDataStreamParameter_dependency = "inParameter-dependency";
87   test_inDataStreamParameter = "inParameter";
88   test_inDataStreamParameter_list = "DataStream-list";
89
90   test_outDataStreamParameter_type = "outParameter-type";
91   test_outDataStreamParameter_name = "outParameter-name";
92   test_outDataStreamParameter_dependency = "outParameter-dependency";
93   test_outDataStreamParameter = "outParameter";
94   test_outDataStreamParameter_list = "DataStream-list";
95
96   test_service =  "component-service";
97   test_service_list = "component-service-list";
98   test_interface_list = "component-interface-list";
99
100   test_constraint = "constraint";
101
102   test_component="component";
103
104   if(MYDEBUG) END_OF("SALOME_ModuleCatalog_Handler");
105 }
106
107 //----------------------------------------------------------------------
108 // Function : ~SALOME_ModuleCatalog_Handler
109 // Purpose  : Destructor
110 //----------------------------------------------------------------------
111 SALOME_ModuleCatalog_Handler::~SALOME_ModuleCatalog_Handler()
112 {
113   if(MYDEBUG) BEGIN_OF("~SALOME_ModuleCatalog_Handler()")
114   if(MYDEBUG) END_OF("~SALOME_ModuleCatalog_Handler()")
115 }
116
117 //----------------------------------------------------------------------
118 // Function : startDocument
119 // Purpose  : overload handler function
120 //----------------------------------------------------------------------
121 bool SALOME_ModuleCatalog_Handler::startDocument()
122 {
123   if(MYDEBUG) MESSAGE("Begin parse document");
124   // Empty the private elements
125   _pathList.resize(0);
126   _pathPrefix.listOfComputer.resize(0);
127   _serviceList.resize(0);
128   _interfaceList.resize(0);
129   _moduleList.resize(0);
130   _inDataStreamParamList.resize(0);
131   _outDataStreamParamList.resize(0);
132   _inParamList.resize(0);
133   _outParamList.resize(0);
134   return true;
135 }
136
137 //----------------------------------------------------------------------
138 // Function : startElement
139 // Purpose  : overload handler function
140 //----------------------------------------------------------------------
141 bool SALOME_ModuleCatalog_Handler::startElement(const QString&, 
142                                                 const QString &,
143                                                 const QString& qName, 
144                                                 const QXmlAttributes& atts)
145 {
146   _xml_pile.push(qName);
147
148   return true;
149
150
151 //----------------------------------------------------------------------
152 // Function : endElement
153 // Purpose  : overload handler function
154 //----------------------------------------------------------------------
155 bool SALOME_ModuleCatalog_Handler::endElement(const QString&, 
156                                               const QString &,
157                                               const QString& qName)
158 {
159   QString parent, grandparent;
160   _xml_pile.pop();
161   if (!_xml_pile.empty()) {
162     parent = _xml_pile.top();
163     _xml_pile.pop();
164     if (!_xml_pile.empty()) grandparent = _xml_pile.top();
165     _xml_pile.push(parent);
166   }
167
168   // Path prefix
169
170   // tag test_path_prefix_name
171   if((qName.compare(test_path_prefix_name)==0)) {
172     _pathPrefix.path = content;
173     return true;
174   }
175
176   // tag test_computer_name
177   if((qName.compare(test_computer_name)==0)) {
178     _pathPrefix.listOfComputer.push_back(content);
179     return true;
180   }
181    
182   // tag test_path_prefix
183   if((qName.compare(test_path_prefix)==0)) {
184     _pathList.push_back(_pathPrefix);
185     _pathPrefix.listOfComputer.resize(0);
186     return true;
187   }
188
189   // Component identification
190
191   // tag test_component_name
192   if((qName.compare(test_component_name)==0)) {
193     _aModule.name = content ;
194     return true;
195   }
196
197   // tag test_component_username
198   if((qName.compare(test_component_username)==0)) {
199     _aModule.username = content ;
200     return true;
201   }
202
203   // tag test_component_type
204    if((qName.compare(test_component_type)==0)) 
205      {
206        if ((content.compare("MESH") == 0) ||
207            (content.compare("Mesh") == 0) ||
208            (content.compare("mesh") == 0))
209          _aModule.type = MESH ;
210        else if((content.compare("MED") == 0) ||
211                (content.compare("Med") == 0) ||
212                (content.compare("med") == 0))
213          _aModule.type = Med ;
214        else if((content.compare("GEOM") == 0) ||
215                (content.compare("Geom") == 0) ||
216                (content.compare("geom") == 0))
217          _aModule.type = GEOM ;
218        else if((content.compare("SOLVER") == 0) ||
219                (content.compare("Solver") == 0) ||
220                (content.compare("solver") == 0))
221          _aModule.type = SOLVER ;
222        else if((content.compare("SUPERV") == 0) ||
223                (content.compare("Superv") == 0) ||
224                (content.compare("Supervision") == 0) ||
225                (content.compare("superv") == 0))
226          _aModule.type = SUPERV ;
227        else if((content.compare("DATA") == 0) ||
228                (content.compare("Data") == 0) ||
229                (content.compare("data") == 0))
230          _aModule.type = DATA ; 
231        else if((content.compare("VISU") == 0) ||
232                (content.compare("Visu") == 0) ||
233                (content.compare("visu") == 0))
234          _aModule.type = VISU ; 
235        else if((content.compare("OTHER") == 0) ||
236                (content.compare("Other") == 0) ||
237                (content.compare("other") == 0))                
238          _aModule.type = OTHER ;
239        else
240          // If it'not in all theses cases, the type is affected to OTHER
241          _aModule.type = OTHER ;
242        return true;
243      }
244
245    // tag test_component_multistudy
246    if((qName.compare(test_component_multistudy)==0)) {
247     _aModule.multistudy = atoi(content.c_str()) ;
248     return true;
249    }
250
251    // tag test_component_impltype
252   if((qName.compare(QString(test_component_impltype))==0)) 
253     _aModule.implementationType = atoi(content.c_str()) ;
254
255   // tag test_component_icon
256    if((qName.compare(test_component_icon)==0)) {
257      _aModule.icon = content ;
258      return true;
259    }
260
261   // tag test_component_version
262    if((qName.compare(test_component_version)==0)) {
263      _aModule.version = content ;
264      return true;
265    }
266
267   // tag test_component_comment
268    if((qName.compare(test_component_comment)==0)) {
269      _aModule.comment = content ;
270      return true;
271    }
272
273    // interface identification
274
275    // tag test_interface_name
276    if ((qName.compare(test_interface_name)==0)) {
277        _aInterface.name = content ;
278        return true;
279    }
280
281    // Service identification
282
283    // tag test_service_name
284    if ((qName.compare(test_service_name)==0)) {
285      _aService.name = content ;
286      return true;
287    }
288
289    //tag test_defaultservice
290    if ((qName.compare(test_defaultservice)==0)) {
291      _aService.byDefault = atoi(content.c_str()) ;
292      return true;
293    }
294
295    //tag test_typeofnode
296    if((qName.compare(QString(test_typeofnode))==0))
297      _aService.typeOfNode = atoi(content.c_str()) ;
298
299    // Parameter in
300
301    // tag test_inDataStreamParameter_type
302    if ((qName.compare(test_inDataStreamParameter_type)==0)) {
303      if (grandparent.compare(test_inDataStreamParameter_list) == 0)
304        _inDataStreamParam.type = content ;
305      else 
306         _inParam.type = content ;
307      return true;
308    }
309          
310    // tag test_inParameter_type
311    if ((qName.compare(test_inParameter_type)==0)) {
312      if (grandparent.compare(test_inDataStreamParameter_list) == 0)
313        _inDataStreamParam.type = content ;
314      else 
315         _inParam.type = content ;
316      return true;
317    }
318          
319    //tag test_inDataStreamParameter_name
320    if ((qName.compare(test_inDataStreamParameter_name)==0)) {
321      if (grandparent.compare(test_inDataStreamParameter_list) == 0)
322        _inDataStreamParam.name = content ;
323      else 
324         _inParam.name = content ;
325      return true;
326    }
327
328    //tag test_inParameter_name
329    if ((qName.compare(test_inParameter_name)==0)) {
330      if(MYDEBUG) SCRUTE(parent);
331      if(MYDEBUG) SCRUTE(grandparent);
332      if (grandparent.compare(test_inDataStreamParameter_list) == 0)
333        _inDataStreamParam.name = content ;
334      else 
335        _inParam.name = content ;
336      return true;
337    }
338
339    //tag test_inDataStreamParameter_dependency
340    if ((qName.compare(test_inDataStreamParameter_dependency)==0)) {
341        _inDataStreamParam.dependency = content ;
342      return true;
343    }
344
345    //tag test_inParameter
346   if ((qName.compare(test_inParameter)==0))
347     {
348       if (parent.compare(test_inParameter_list)==0) {
349         
350         if(MYDEBUG) MESSAGE("add inParameter : " << _inParam.name);
351         _inParamList.push_back(_inParam) ; 
352         
353         // Empty temporary structures
354         _inParam.type = "";
355         _inParam.name = "";
356       }
357       else if ((qName.compare(test_inDataStreamParameter)==0)) {
358         
359         if(MYDEBUG) MESSAGE("add inDataStreamParameter : " << _inDataStreamParam.name);
360         _inDataStreamParamList.push_back(_inDataStreamParam) ; 
361         
362         // Empty temporary structures
363         _inDataStreamParam.type = "";
364         _inDataStreamParam.name = "";
365         _inDataStreamParam.dependency = "";
366       }
367       return true;
368     }
369   
370    //tag test_inParameter_list
371    if((qName.compare(test_inParameter_list)==0))
372      {
373        if(MYDEBUG) SCRUTE(_inParamList.size());
374        _aService.inParameters = _inParamList;
375        _inParamList.resize(0);
376        return true;
377      }
378   
379    //tag test_inDataStreamParameter_list
380    if((qName.compare(test_inDataStreamParameter_list)==0))
381      {
382        if(MYDEBUG) SCRUTE(_inDataStreamParamList.size());
383        _aService.inDataStreamParameters = _inDataStreamParamList;
384        _inDataStreamParamList.resize(0);
385      }
386    //tag test_outDataStreamParameter_list
387    if((qName.compare(test_outDataStreamParameter_list)==0))
388      {
389        if(MYDEBUG) SCRUTE(_outDataStreamParamList.size());
390        _aService.outDataStreamParameters = _outDataStreamParamList;
391        _outDataStreamParamList.resize(0);
392        return true;
393      }
394
395
396
397    // Parameter out
398    if(MYDEBUG) SCRUTE(qName);
399
400    // tag test_outParameter_type
401    if ((qName.compare(test_outParameter_type)==0)) {
402      if (grandparent.compare(test_outDataStreamParameter_list) == 0)
403        _outDataStreamParam.type = content ;
404      else 
405         _outParam.type = content ;
406      return true;
407    }
408          
409    // tag test_outDataStreamParameter_type
410    if ((qName.compare(test_outDataStreamParameter_type)==0)) {
411      if (grandparent.compare(test_outDataStreamParameter_list) == 0)
412        _outDataStreamParam.type = content ;
413      else 
414         _outParam.type = content ;
415      return true;
416    }
417          
418    //tag test_outParameter_name
419    if ((qName.compare(test_outParameter_name)==0)) {
420      if (grandparent.compare(test_outDataStreamParameter_list) == 0)
421        _outDataStreamParam.name = content ;
422      else 
423         _outParam.name = content ;
424      return true;
425    }
426          
427    //tag test_outDataStreamParameter_name
428    if ((qName.compare(test_outDataStreamParameter_name)==0)) {
429      if(MYDEBUG) SCRUTE(grandparent);
430      if(MYDEBUG) SCRUTE(test_outDataStreamParameter_list);
431      if (grandparent.compare(test_outDataStreamParameter_list) == 0)
432        _outDataStreamParam.name = content ;
433      else 
434         _outParam.name = content ;
435      return true;
436    }
437          
438    //tag test_outParameter_dependency
439    if ((qName.compare(test_outDataStreamParameter_dependency)==0)) {
440      _outDataStreamParam.dependency = content ;
441      return true;
442    }
443
444    //tag test_outParameter
445   if ((qName.compare(test_outParameter)==0))
446     {
447       if (parent.compare(test_outParameter_list)==0) {
448         
449         if(MYDEBUG) MESSAGE("add outParameter : " << _outParam.name);
450         _outParamList.push_back(_outParam) ; 
451         
452         // Empty temporary structures
453         _outParam.type = "";
454         _outParam.name = "";
455       }
456       else if ((qName.compare(test_outDataStreamParameter)==0)) {
457         
458         if(MYDEBUG) MESSAGE("add outDataStreamParameter : " << _outDataStreamParam.name);
459         _outDataStreamParamList.push_back(_outDataStreamParam) ; 
460         
461         // Empty temporary structures
462         _outDataStreamParam.type = "";
463         _outDataStreamParam.name = "";
464         _outDataStreamParam.dependency = "";
465       }
466       return true;
467     }
468   
469    //tag test_outParameter_list
470    if((qName.compare(test_outParameter_list)==0))
471      {
472        if(MYDEBUG) SCRUTE(_outParamList.size());
473        _aService.outParameters = _outParamList;
474        _outParamList.resize(0);
475        return true;
476      }
477   
478    // tag   test_service
479    if((qName.compare(test_service)==0))
480      {
481        _serviceList.push_back(_aService);
482
483        // Empty temporary structures
484        _aService.name = "";
485        _aService.typeOfNode = 1;
486        _aService.inParameters.resize(0);
487        _aService.outParameters.resize(0);
488        _aService.inDataStreamParameters.resize(0);
489        _aService.outDataStreamParameters.resize(0);
490      }
491
492    // tag   test_service_list
493    if((qName.compare(test_service_list)==0))
494      {
495        _aInterface.services = _serviceList ;
496
497        // Empty temporary structures
498        _serviceList.resize(0); 
499        _interfaceList.push_back(_aInterface);  
500        _aInterface.name ="";    
501        _aInterface.services.resize(0);
502
503      }
504
505    //tag test_interface_list
506    if((qName.compare(test_interface_list)==0))
507      {
508        _aModule.interfaces = _interfaceList ;
509        _interfaceList.resize(0);
510      }
511
512    //tag test_constraint
513    if((qName.compare(test_constraint)==0))
514      _aModule.constraint = content ;
515
516    // tag test_component
517    if((qName.compare(test_component)==0))
518      {
519        _moduleList.push_back(_aModule) ;
520        
521        // Empty temporary structures
522        _aModule.name = "";
523        _aModule.constraint = "";
524        _aModule.icon="";       
525        _aModule.interfaces.resize(0);
526      }
527     
528   return true;
529 }
530   
531 //----------------------------------------------------------------------
532 // Function : characters
533 // Purpose  : overload handler function
534 //----------------------------------------------------------------------
535 bool SALOME_ModuleCatalog_Handler::characters(const QString& chars)
536 {
537   content = (const char*)chars ;
538   return true;
539 }
540
541 //----------------------------------------------------------------------
542 // Function : endDocument
543 // Purpose  : overload handler function
544 //            Print all informations find in the catalog 
545 //            (only in DEBUG mode!!)
546 //----------------------------------------------------------------------  
547 #include <fstream>
548 bool SALOME_ModuleCatalog_Handler::endDocument()
549 {
550 //  ofstream f("/tmp/logs/xxx.log", std::ofstream::app);
551 //  f << "---------------------------------------------------------" << std::endl;
552   if(MYDEBUG) BEGIN_OF("endDocument");
553   //_pathlist
554   for (unsigned int ind = 0; ind < _pathList.size(); ind++)
555     {
556       if(MYDEBUG) MESSAGE("Path :"<<_pathList[ind].path);
557       for (unsigned int i = 0; i < _pathList[ind].listOfComputer.size(); i++)
558         if(MYDEBUG) MESSAGE("Computer name :" << _pathList[ind].listOfComputer[i]);
559     }
560
561    // _moduleList
562 //  if(MYDEBUG) SCRUTE(_moduleList.size());
563 //  for (unsigned int ind = 0; ind < _moduleList.size(); ind++)
564 //    {
565 //      f << _moduleList[ind] << std::endl;
566 //    }
567
568   if(MYDEBUG) MESSAGE("Document parsed");
569   if(MYDEBUG) END_OF("endDocument");
570   return true;
571 }
572  
573 //----------------------------------------------------------------------
574 // Function : errorProtocol
575 // Purpose  : overload handler function
576 //----------------------------------------------------------------------  
577 QString SALOME_ModuleCatalog_Handler::errorProtocol()
578 {
579   return errorProt ;
580 }
581
582
583 //----------------------------------------------------------------------
584 // Function : fatalError
585 // Purpose  : overload handler function
586 //----------------------------------------------------------------------  
587 bool SALOME_ModuleCatalog_Handler::fatalError(const QXmlParseException& exception)
588 {
589     errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
590     .arg( exception.message() )
591     .arg( exception.lineNumber() )
592     .arg( exception.columnNumber() );
593
594   return QXmlDefaultHandler::fatalError( exception );
595 }