Salome HOME
sources v1.2
[modules/kernel.git] / src / DataTypeCatalog / SALOME_DataTypeCatalog_impl.cxx
1 //  SALOME DataTypeCatalog : catalog of data types
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_DataTypeCatalog_impl.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "SALOME_DataTypeCatalog_impl.hxx"
31 #include <fstream>
32
33 //----------------------------------------------------------------------
34 // Function : SALOME_DataTypeCatalogImpl
35 // Purpose  : Constructor 
36 //----------------------------------------------------------------------
37 SALOME_DataTypeCatalogImpl::SALOME_DataTypeCatalogImpl(int argc, char** argv)
38 {
39   MESSAGE("DataType Catalog creation");
40
41   // Empty used variables
42   _datatype_list.resize(0);
43
44   // Parse the arguments given at server run
45   if (!_parseArguments(argc, argv,&_path_datatype))
46     MESSAGE( "Error while argument parsing" )
47
48   // Test existency of files
49   if (_path_datatype == NULL)
50     MESSAGE( "Error the data type catalog should be indicated" )
51   else
52     {
53       MESSAGE("Parse data type catalog");
54       // Affect the variable _datatype_list 
55       _parse_xml_file(_path_datatype,_datatype_list);
56
57       // Verification of _datatype_list content
58       if(!_verify_data_type(_datatype_list))
59         MESSAGE( "Error while parsing the data type list" )
60       else MESSAGE("Data Type list OK");
61     }
62 }
63
64 //----------------------------------------------------------------------
65 // Function : ~SALOME_DataTypeCatalogImpl
66 // Purpose  : Destructor 
67 //----------------------------------------------------------------------
68 SALOME_DataTypeCatalogImpl::~SALOME_DataTypeCatalogImpl()
69 {
70   MESSAGE("DataType Catalog Destruction");
71 }
72
73
74 //----------------------------------------------------------------------
75 // Function : GetDataTypeList
76 // Purpose  : get a data type name list
77 //----------------------------------------------------------------------
78 SALOME_DataTypeCatalog::ListOfDataTypeName* 
79 SALOME_DataTypeCatalogImpl::GetDataTypeList()
80 {
81   MESSAGE("Begin of GetDataTypeList");
82   SALOME_DataTypeCatalog::ListOfDataTypeName_var _list_data = 
83     new SALOME_DataTypeCatalog::ListOfDataTypeName;
84
85   _list_data->length(_datatype_list.size());
86
87   // Duplicate each data type name defined in the catalog
88   for (unsigned int ind=0; ind < _datatype_list.size();ind++)
89     {
90        _list_data[ind]=CORBA::string_dup(_datatype_list[ind].Parserdata_name.c_str());
91        // SCRUTE(_list_data[ind]) ;
92     }
93
94   return _list_data._retn();
95 }
96
97 //----------------------------------------------------------------------
98 // Function : isDerivedFrom
99 // Purpose  : return true if type_out is derived from type_in
100 //----------------------------------------------------------------------
101 CORBA::Boolean 
102 SALOME_DataTypeCatalogImpl::isDerivedFrom(const char* type_in,
103                                           const char* type_out)
104                             throw(SALOME_DataTypeCatalog::NotFound)
105 {
106   
107   CORBA::Boolean _return_value = false ;
108   bool _found_in = false;
109   bool _found_out = false;
110   for (unsigned int ind=0; ind < _datatype_list.size();ind++)
111     {
112       if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type_out) == 0)
113         {
114           //type_out found
115           _found_out = true;
116           // Looking if type_in is indicated in the parents list
117           _return_value = _explore_parents(type_in,_datatype_list[ind]); 
118         }
119       if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type_in) == 0)
120         _found_in = true;
121     }
122
123   if(!_found_in)
124     {
125       MESSAGE("The data type " << type_in << " was not found in the data type catalog")
126       throw SALOME_DataTypeCatalog::NotFound() ;
127     }
128   if(!_found_out)
129     {
130       MESSAGE( "The data type " << type_out << " was not found in the data type catalog")
131       throw SALOME_DataTypeCatalog::NotFound() ;
132     }
133
134   return _return_value;
135
136 }
137
138 //----------------------------------------------------------------------
139 // Function : GetDataInterfaceRead
140 // Purpose  : get the read interface name
141 //----------------------------------------------------------------------
142 char*
143 SALOME_DataTypeCatalogImpl::GetDataInterfaceRead(const char* type)
144                             throw(SALOME_DataTypeCatalog::NotFound)
145 {
146   char* return_value = NULL;
147   bool _find = false;
148   for (unsigned int ind=0; ind < _datatype_list.size();ind++)
149     {
150       if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
151         {
152           _find = true;
153           return_value =  CORBA::string_dup(_datatype_list[ind].Parserdata_interface_read.c_str());
154         }
155     }
156   
157   // NotFound Exceptin is thrown if the data type indicated is not found in the catalog
158   if(!_find)
159     {
160       MESSAGE( "The data type " << type << " was not found in the data type catalog")
161       throw SALOME_DataTypeCatalog::NotFound() ;
162     }
163
164   return return_value;
165 }
166
167 //----------------------------------------------------------------------
168 // Function : GetDataInterfaceWrite
169 // Purpose  : get the write interface name
170 //----------------------------------------------------------------------
171 char*
172 SALOME_DataTypeCatalogImpl::GetDataInterfaceWrite(const char* type)
173                             throw(SALOME_DataTypeCatalog::NotFound)
174 {
175   char* return_value = NULL;
176   bool _find = false;
177   for (unsigned int ind=0; ind < _datatype_list.size();ind++)
178     {
179       if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
180         {
181           _find = true;
182           return_value =  CORBA::string_dup(_datatype_list[ind].Parserdata_interface_write.c_str());
183         }
184     }
185
186   // NotFound Exceptin is thrown if the data type indicated is not found in the catalog
187   if(!_find)
188     {
189       MESSAGE( "The data type " << type << " was not found in the data type catalog")
190       throw SALOME_DataTypeCatalog::NotFound() ;
191     }
192   return return_value;
193 }
194
195 //----------------------------------------------------------------------
196 // Function : GetDataTypeParents
197 // Purpose  : get Parents data type name list
198 //----------------------------------------------------------------------
199 SALOME_DataTypeCatalog::ListOfDataTypeName* 
200 SALOME_DataTypeCatalogImpl::GetDataTypeParents(const char* type) throw(SALOME_DataTypeCatalog::NotFound)
201 {
202   bool _find = false ;
203
204   SALOME_DataTypeCatalog::ListOfDataTypeName_var _list_data = 
205     new SALOME_DataTypeCatalog::ListOfDataTypeName;
206
207   for (unsigned int ind=0; ind < _datatype_list.size();ind++)
208     {
209       if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
210         {
211           // Wanted type is found
212           // Get all the parents data type name of the type
213           _find = true; 
214           _list_data->length(_datatype_list[ind].Parserdata_parents.size());
215           for (unsigned int ind1=0; ind1 < _datatype_list[ind].Parserdata_parents.size();ind1++)
216             {
217               _list_data[ind1]=CORBA::string_dup(_datatype_list[ind].Parserdata_parents[ind1].c_str());
218               // SCRUTE(_list_data[ind1]) ;
219             }
220         }
221     }
222
223   // NotFound Exception is thrown if the data type indicated is not found 
224   // in the catalog
225   if(!_find)
226     {
227       MESSAGE( "The data type " << type << " was not found in the data type catalog")
228       throw SALOME_DataTypeCatalog::NotFound() ;
229     }
230
231   return _list_data._retn();
232 }
233
234 //----------------------------------------------------------------------
235 // Function : _explore_parents
236 // Purpose  : return true if type_out is derived from type_in
237 //----------------------------------------------------------------------
238 CORBA::Boolean 
239 SALOME_DataTypeCatalogImpl::_explore_parents(const char* type_in,
240                                              ParserDataType& data_out)
241 {
242
243   MESSAGE( "Begin of _explore_parents with data_out_name  " << data_out.Parserdata_name);
244
245   CORBA::Boolean found = false ;
246   if (data_out.Parserdata_name == type_in)
247     found = true ;
248   else
249     if (data_out.Parserdata_parents.size() >0)
250       for (unsigned int ind=0; ind < data_out.Parserdata_parents.size();ind++)
251         {
252           if (data_out.Parserdata_parents[ind] == type_in)
253             found = true ;
254           else
255             for (unsigned ip =0; ip < _datatype_list.size();ip++)
256               {
257                 // run _explore_parents for each parent data of data_out
258                 if (_datatype_list[ip].Parserdata_name == data_out.Parserdata_parents[ind])
259                   found = _explore_parents(type_in, _datatype_list[ip]);
260               }
261         }
262   return found ;
263 }
264
265 //----------------------------------------------------------------------
266 // Function : _parse_xml_file
267 // Purpose  : parse one module catalog 
268 //----------------------------------------------------------------------
269 void 
270 SALOME_DataTypeCatalogImpl::_parse_xml_file(const char* file,
271                                             ListOfParserDataType& datatypelist) 
272 {
273   SALOME_DataTypeCatalog_Handler* handler = new SALOME_DataTypeCatalog_Handler();
274   QFile xmlFile(file);
275
276   QXmlInputSource source(xmlFile);
277
278   QXmlSimpleReader reader;
279   reader.setContentHandler( handler );
280   reader.setErrorHandler( handler );
281   reader.parse( source );
282   xmlFile.close();
283   datatypelist = _datatypelist;
284 }
285
286
287 //----------------------------------------------------------------------
288 // Function : _verify_data_type
289 // Purpose  : verify the data type structures from the catalog parsing
290 //            Verify that the parents type associated to a type are defined
291 //            in the catalog
292 //----------------------------------------------------------------------
293 bool
294 SALOME_DataTypeCatalogImpl::_verify_data_type(ListOfParserDataType datatypelist)
295 {
296    bool _return_value = true;
297    bool _bool = false ;
298    vector<string> _data_name_list;
299    
300    _data_name_list.resize(0);;
301   for (unsigned int ind = 0; ind < _datatype_list.size(); ind++)
302     _data_name_list.push_back(_datatype_list[ind].Parserdata_name) ;
303
304    // Parse if parents data type name of a data type are defined in the 
305    // datatype catalog
306   for (unsigned int ind = 0; ind < _datatype_list.size(); ind++)
307     {
308       // Scrute data type parents
309       // MESSAGE("Treatment of " << _datatype_list[ind].Parserdata_name);
310       for (unsigned int ind1 = 0 ; ind1 < _datatype_list[ind].Parserdata_parents.size(); ind1++)
311        {
312          // MESSAGE("Looking for " << _datatype_list[ind].Parserdata_parents[ind1] << " in the catalog data type");
313          _bool = false;
314          // Compare parent data type name to all data type names defined in the catalog
315          for (unsigned int ind2 = 0 ; ind2 < _data_name_list.size(); ind2++)
316            {
317              if(_datatype_list[ind].Parserdata_parents[ind1].compare(_data_name_list[ind2]) == 0)
318                {
319                  // Type found : OK
320                  MESSAGE("The parents data type " << _datatype_list[ind].Parserdata_parents[ind1] << " of " << _datatype_list[ind].Parserdata_name << " was found in the catalog type");
321                  _bool = true; 
322                }
323            }
324          if(!_bool)
325            {
326              MESSAGE( "The parents data type " << _datatype_list[ind].Parserdata_parents[ind1] << " of " << _datatype_list[ind].Parserdata_name << " is not defined in the data type catalog" )
327              _return_value = false ;
328            }
329        }
330     }
331   return _return_value;
332 }
333
334
335 //----------------------------------------------------------------------
336 // Function : _parseArguments
337 // Purpose  : parse arguments to get general and personal catalog files
338 //----------------------------------------------------------------------
339 bool
340 SALOME_DataTypeCatalogImpl::_parseArguments(int argc, char **argv, 
341                                             char **_path_data)
342 {
343   bool _return_value = true;
344   *_path_data = NULL;
345   for (int ind = 0; ind < argc ; ind++)
346     {
347
348       if (strcmp(argv[ind],"-help") == 0)
349         {
350           INFOS( "Usage: " << argv[0] << " -common 'path to data type catalog' -ORBInitRef NameService=corbaname::localhost");
351             _return_value = false ;
352         }
353       if (strcmp(argv[ind],"-common") == 0)
354         {
355           if (ind + 1 <= argc)
356             {
357               // General catalog file
358               *_path_data = argv[ind + 1] ;
359               ifstream _path_data_file(*_path_data);
360               if (!_path_data_file)
361                 {
362                   MESSAGE( "Sorry the file " << *_path_data << " can't be open" )
363                   *_path_data = NULL;
364                   _return_value = false;
365                 }
366             }
367         }
368      
369     }
370   return _return_value;
371 }