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