1 // SALOME DataTypeCatalog : catalog of data types
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_DataTypeCatalog_impl.cxx
25 // Author : Estelle Deville
30 #include "SALOME_DataTypeCatalog_impl.hxx"
33 //----------------------------------------------------------------------
34 // Function : SALOME_DataTypeCatalogImpl
35 // Purpose : Constructor
36 //----------------------------------------------------------------------
37 SALOME_DataTypeCatalogImpl::SALOME_DataTypeCatalogImpl(int argc, char** argv)
39 MESSAGE("DataType Catalog creation");
41 // Empty used variables
42 _datatype_list.resize(0);
44 // Parse the arguments given at server run
45 if (!_parseArguments(argc, argv,&_path_datatype))
46 MESSAGE( "Error while argument parsing" )
48 // Test existency of files
49 if (_path_datatype == NULL)
50 MESSAGE( "Error the data type catalog should be indicated" )
53 MESSAGE("Parse data type catalog");
54 // Affect the variable _datatype_list
55 _parse_xml_file(_path_datatype,_datatype_list);
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");
64 //----------------------------------------------------------------------
65 // Function : ~SALOME_DataTypeCatalogImpl
66 // Purpose : Destructor
67 //----------------------------------------------------------------------
68 SALOME_DataTypeCatalogImpl::~SALOME_DataTypeCatalogImpl()
70 MESSAGE("DataType Catalog Destruction");
74 //----------------------------------------------------------------------
75 // Function : GetDataTypeList
76 // Purpose : get a data type name list
77 //----------------------------------------------------------------------
78 SALOME_DataTypeCatalog::ListOfDataTypeName*
79 SALOME_DataTypeCatalogImpl::GetDataTypeList()
81 MESSAGE("Begin of GetDataTypeList");
82 SALOME_DataTypeCatalog::ListOfDataTypeName_var _list_data =
83 new SALOME_DataTypeCatalog::ListOfDataTypeName;
85 _list_data->length(_datatype_list.size());
87 // Duplicate each data type name defined in the catalog
88 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
90 _list_data[ind]=CORBA::string_dup(_datatype_list[ind].Parserdata_name.c_str());
91 // SCRUTE(_list_data[ind]) ;
94 return _list_data._retn();
97 //----------------------------------------------------------------------
98 // Function : isDerivedFrom
99 // Purpose : return true if type_out is derived from type_in
100 //----------------------------------------------------------------------
102 SALOME_DataTypeCatalogImpl::isDerivedFrom(const char* type_in,
103 const char* type_out)
104 throw(SALOME_DataTypeCatalog::NotFound)
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++)
112 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type_out) == 0)
116 // Looking if type_in is indicated in the parents list
117 _return_value = _explore_parents(type_in,_datatype_list[ind]);
119 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type_in) == 0)
125 MESSAGE("The data type " << type_in << " was not found in the data type catalog")
126 throw SALOME_DataTypeCatalog::NotFound() ;
130 MESSAGE( "The data type " << type_out << " was not found in the data type catalog")
131 throw SALOME_DataTypeCatalog::NotFound() ;
134 return _return_value;
138 //----------------------------------------------------------------------
139 // Function : GetDataInterfaceRead
140 // Purpose : get the read interface name
141 //----------------------------------------------------------------------
143 SALOME_DataTypeCatalogImpl::GetDataInterfaceRead(const char* type)
144 throw(SALOME_DataTypeCatalog::NotFound)
146 char* return_value = NULL;
148 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
150 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
153 return_value = CORBA::string_dup(_datatype_list[ind].Parserdata_interface_read.c_str());
157 // NotFound Exceptin is thrown if the data type indicated is not found in the catalog
160 MESSAGE( "The data type " << type << " was not found in the data type catalog")
161 throw SALOME_DataTypeCatalog::NotFound() ;
167 //----------------------------------------------------------------------
168 // Function : GetDataInterfaceWrite
169 // Purpose : get the write interface name
170 //----------------------------------------------------------------------
172 SALOME_DataTypeCatalogImpl::GetDataInterfaceWrite(const char* type)
173 throw(SALOME_DataTypeCatalog::NotFound)
175 char* return_value = NULL;
177 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
179 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
182 return_value = CORBA::string_dup(_datatype_list[ind].Parserdata_interface_write.c_str());
186 // NotFound Exceptin is thrown if the data type indicated is not found in the catalog
189 MESSAGE( "The data type " << type << " was not found in the data type catalog")
190 throw SALOME_DataTypeCatalog::NotFound() ;
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)
204 SALOME_DataTypeCatalog::ListOfDataTypeName_var _list_data =
205 new SALOME_DataTypeCatalog::ListOfDataTypeName;
207 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
209 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
211 // Wanted type is found
212 // Get all the parents data type name of the type
214 _list_data->length(_datatype_list[ind].Parserdata_parents.size());
215 for (unsigned int ind1=0; ind1 < _datatype_list[ind].Parserdata_parents.size();ind1++)
217 _list_data[ind1]=CORBA::string_dup(_datatype_list[ind].Parserdata_parents[ind1].c_str());
218 // SCRUTE(_list_data[ind1]) ;
223 // NotFound Exception is thrown if the data type indicated is not found
227 MESSAGE( "The data type " << type << " was not found in the data type catalog")
228 throw SALOME_DataTypeCatalog::NotFound() ;
231 return _list_data._retn();
234 //----------------------------------------------------------------------
235 // Function : _explore_parents
236 // Purpose : return true if type_out is derived from type_in
237 //----------------------------------------------------------------------
239 SALOME_DataTypeCatalogImpl::_explore_parents(const char* type_in,
240 ParserDataType& data_out)
243 MESSAGE( "Begin of _explore_parents with data_out_name " << data_out.Parserdata_name);
245 CORBA::Boolean found = false ;
246 if (data_out.Parserdata_name == type_in)
249 if (data_out.Parserdata_parents.size() >0)
250 for (unsigned int ind=0; ind < data_out.Parserdata_parents.size();ind++)
252 if (data_out.Parserdata_parents[ind] == type_in)
255 for (unsigned ip =0; ip < _datatype_list.size();ip++)
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]);
265 //----------------------------------------------------------------------
266 // Function : _parse_xml_file
267 // Purpose : parse one module catalog
268 //----------------------------------------------------------------------
270 SALOME_DataTypeCatalogImpl::_parse_xml_file(const char* file,
271 ListOfParserDataType& datatypelist)
273 SALOME_DataTypeCatalog_Handler* handler = new SALOME_DataTypeCatalog_Handler();
276 QXmlInputSource source(xmlFile);
278 QXmlSimpleReader reader;
279 reader.setContentHandler( handler );
280 reader.setErrorHandler( handler );
281 reader.parse( source );
283 datatypelist = _datatypelist;
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
292 //----------------------------------------------------------------------
294 SALOME_DataTypeCatalogImpl::_verify_data_type(ListOfParserDataType datatypelist)
296 bool _return_value = true;
298 vector<string> _data_name_list;
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) ;
304 // Parse if parents data type name of a data type are defined in the
306 for (unsigned int ind = 0; ind < _datatype_list.size(); ind++)
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++)
312 // MESSAGE("Looking for " << _datatype_list[ind].Parserdata_parents[ind1] << " in the catalog data type");
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++)
317 if(_datatype_list[ind].Parserdata_parents[ind1].compare(_data_name_list[ind2]) == 0)
320 MESSAGE("The parents data type " << _datatype_list[ind].Parserdata_parents[ind1] << " of " << _datatype_list[ind].Parserdata_name << " was found in the catalog type");
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 ;
331 return _return_value;
335 //----------------------------------------------------------------------
336 // Function : _parseArguments
337 // Purpose : parse arguments to get general and personal catalog files
338 //----------------------------------------------------------------------
340 SALOME_DataTypeCatalogImpl::_parseArguments(int argc, char **argv,
343 bool _return_value = true;
345 for (int ind = 0; ind < argc ; ind++)
348 if (strcmp(argv[ind],"-help") == 0)
350 INFOS( "Usage: " << argv[0] << " -common 'path to data type catalog' -ORBInitRef NameService=corbaname::localhost");
351 _return_value = false ;
353 if (strcmp(argv[ind],"-common") == 0)
357 // General catalog file
358 *_path_data = argv[ind + 1] ;
359 ifstream _path_data_file(*_path_data);
360 if (!_path_data_file)
362 MESSAGE( "Sorry the file " << *_path_data << " can't be open" )
364 _return_value = false;
370 return _return_value;