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
29 #include "SALOME_DataTypeCatalog_impl.hxx"
30 #include "Utils_ExceptHandlers.hxx"
34 UNEXPECT_CATCH(DTC_NotFound, SALOME_DataTypeCatalog::NotFound);
36 //----------------------------------------------------------------------
37 // Function : SALOME_DataTypeCatalogImpl
38 // Purpose : Constructor
39 //----------------------------------------------------------------------
40 SALOME_DataTypeCatalogImpl::SALOME_DataTypeCatalogImpl(int argc, char** argv)
42 MESSAGE("DataType Catalog creation");
44 // Empty used variables
45 _datatype_list.resize(0);
47 // Parse the arguments given at server run
48 if (!_parseArguments(argc, argv,&_path_datatype))
49 MESSAGE( "Error while argument parsing" )
51 // Test existency of files
52 if (_path_datatype == NULL)
53 MESSAGE( "Error the data type catalog should be indicated" )
56 MESSAGE("Parse data type catalog");
57 // Affect the variable _datatype_list
58 _parse_xml_file(_path_datatype,_datatype_list);
60 // Verification of _datatype_list content
61 if(!_verify_data_type(_datatype_list))
62 MESSAGE( "Error while parsing the data type list" )
63 else MESSAGE("Data Type list OK");
67 //----------------------------------------------------------------------
68 // Function : ~SALOME_DataTypeCatalogImpl
69 // Purpose : Destructor
70 //----------------------------------------------------------------------
71 SALOME_DataTypeCatalogImpl::~SALOME_DataTypeCatalogImpl()
73 MESSAGE("DataType Catalog Destruction");
77 //----------------------------------------------------------------------
78 // Function : GetDataTypeList
79 // Purpose : get a data type name list
80 //----------------------------------------------------------------------
81 SALOME_DataTypeCatalog::ListOfDataTypeName*
82 SALOME_DataTypeCatalogImpl::GetDataTypeList()
84 MESSAGE("Begin of GetDataTypeList");
85 SALOME_DataTypeCatalog::ListOfDataTypeName_var _list_data =
86 new SALOME_DataTypeCatalog::ListOfDataTypeName;
88 _list_data->length(_datatype_list.size());
90 // Duplicate each data type name defined in the catalog
91 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
93 _list_data[ind]=CORBA::string_dup(_datatype_list[ind].Parserdata_name.c_str());
94 // SCRUTE(_list_data[ind]) ;
97 return _list_data._retn();
100 //----------------------------------------------------------------------
101 // Function : isDerivedFrom
102 // Purpose : return true if type_out is derived from type_in
103 //----------------------------------------------------------------------
105 SALOME_DataTypeCatalogImpl::isDerivedFrom(const char* type_in,
106 const char* type_out)
107 throw(SALOME_DataTypeCatalog::NotFound)
109 Unexpect aCatch(DTC_NotFound);
110 CORBA::Boolean _return_value = false ;
111 bool _found_in = false;
112 bool _found_out = false;
113 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
115 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type_out) == 0)
119 // Looking if type_in is indicated in the parents list
120 _return_value = _explore_parents(type_in,_datatype_list[ind]);
122 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type_in) == 0)
128 MESSAGE("The data type " << type_in << " was not found in the data type catalog")
129 throw SALOME_DataTypeCatalog::NotFound() ;
133 MESSAGE( "The data type " << type_out << " was not found in the data type catalog")
134 throw SALOME_DataTypeCatalog::NotFound() ;
137 return _return_value;
141 //----------------------------------------------------------------------
142 // Function : GetDataInterfaceRead
143 // Purpose : get the read interface name
144 //----------------------------------------------------------------------
146 SALOME_DataTypeCatalogImpl::GetDataInterfaceRead(const char* type)
147 throw(SALOME_DataTypeCatalog::NotFound)
149 Unexpect aCatch(DTC_NotFound);
150 char* return_value = NULL;
152 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
154 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
157 return_value = CORBA::string_dup(_datatype_list[ind].Parserdata_interface_read.c_str());
161 // NotFound Exceptin is thrown if the data type indicated is not found in the catalog
164 MESSAGE( "The data type " << type << " was not found in the data type catalog")
165 throw SALOME_DataTypeCatalog::NotFound() ;
171 //----------------------------------------------------------------------
172 // Function : GetDataInterfaceWrite
173 // Purpose : get the write interface name
174 //----------------------------------------------------------------------
176 SALOME_DataTypeCatalogImpl::GetDataInterfaceWrite(const char* type)
177 throw(SALOME_DataTypeCatalog::NotFound)
179 Unexpect aCatch(DTC_NotFound);
180 char* return_value = NULL;
182 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
184 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
187 return_value = CORBA::string_dup(_datatype_list[ind].Parserdata_interface_write.c_str());
191 // NotFound Exceptin is thrown if the data type indicated is not found in the catalog
194 MESSAGE( "The data type " << type << " was not found in the data type catalog")
195 throw SALOME_DataTypeCatalog::NotFound() ;
200 //----------------------------------------------------------------------
201 // Function : GetDataTypeParents
202 // Purpose : get Parents data type name list
203 //----------------------------------------------------------------------
204 SALOME_DataTypeCatalog::ListOfDataTypeName*
205 SALOME_DataTypeCatalogImpl::GetDataTypeParents(const char* type) throw(SALOME_DataTypeCatalog::NotFound)
207 Unexpect aCatch(DTC_NotFound);
211 SALOME_DataTypeCatalog::ListOfDataTypeName_var _list_data =
212 new SALOME_DataTypeCatalog::ListOfDataTypeName;
214 for (unsigned int ind=0; ind < _datatype_list.size();ind++)
216 if (strcmp((_datatype_list[ind].Parserdata_name).c_str(),type) == 0)
218 // Wanted type is found
219 // Get all the parents data type name of the type
221 _list_data->length(_datatype_list[ind].Parserdata_parents.size());
222 for (unsigned int ind1=0; ind1 < _datatype_list[ind].Parserdata_parents.size();ind1++)
224 _list_data[ind1]=CORBA::string_dup(_datatype_list[ind].Parserdata_parents[ind1].c_str());
225 // SCRUTE(_list_data[ind1]) ;
230 // NotFound Exception is thrown if the data type indicated is not found
234 MESSAGE( "The data type " << type << " was not found in the data type catalog")
235 throw SALOME_DataTypeCatalog::NotFound() ;
238 return _list_data._retn();
241 //----------------------------------------------------------------------
242 // Function : _explore_parents
243 // Purpose : return true if type_out is derived from type_in
244 //----------------------------------------------------------------------
246 SALOME_DataTypeCatalogImpl::_explore_parents(const char* type_in,
247 ParserDataType& data_out)
250 MESSAGE( "Begin of _explore_parents with data_out_name " << data_out.Parserdata_name);
252 CORBA::Boolean found = false ;
253 if (data_out.Parserdata_name == type_in)
256 if (data_out.Parserdata_parents.size() >0)
257 for (unsigned int ind=0; ind < data_out.Parserdata_parents.size();ind++)
259 if (data_out.Parserdata_parents[ind] == type_in)
262 for (unsigned ip =0; ip < _datatype_list.size();ip++)
264 // run _explore_parents for each parent data of data_out
265 if (_datatype_list[ip].Parserdata_name == data_out.Parserdata_parents[ind])
266 found = _explore_parents(type_in, _datatype_list[ip]);
272 //----------------------------------------------------------------------
273 // Function : _parse_xml_file
274 // Purpose : parse one module catalog
275 //----------------------------------------------------------------------
277 SALOME_DataTypeCatalogImpl::_parse_xml_file(const char* file,
278 ListOfParserDataType& datatypelist)
280 SALOME_DataTypeCatalog_Handler* handler = new SALOME_DataTypeCatalog_Handler();
283 QXmlInputSource source(xmlFile);
285 QXmlSimpleReader reader;
286 reader.setContentHandler( handler );
287 reader.setErrorHandler( handler );
288 reader.parse( source );
290 datatypelist = _datatypelist;
294 //----------------------------------------------------------------------
295 // Function : _verify_data_type
296 // Purpose : verify the data type structures from the catalog parsing
297 // Verify that the parents type associated to a type are defined
299 //----------------------------------------------------------------------
301 SALOME_DataTypeCatalogImpl::_verify_data_type(ListOfParserDataType datatypelist)
303 bool _return_value = true;
305 vector<string> _data_name_list;
307 _data_name_list.resize(0);;
308 for (unsigned int ind = 0; ind < _datatype_list.size(); ind++)
309 _data_name_list.push_back(_datatype_list[ind].Parserdata_name) ;
311 // Parse if parents data type name of a data type are defined in the
313 for (unsigned int ind = 0; ind < _datatype_list.size(); ind++)
315 // Scrute data type parents
316 // MESSAGE("Treatment of " << _datatype_list[ind].Parserdata_name);
317 for (unsigned int ind1 = 0 ; ind1 < _datatype_list[ind].Parserdata_parents.size(); ind1++)
319 // MESSAGE("Looking for " << _datatype_list[ind].Parserdata_parents[ind1] << " in the catalog data type");
321 // Compare parent data type name to all data type names defined in the catalog
322 for (unsigned int ind2 = 0 ; ind2 < _data_name_list.size(); ind2++)
324 if(_datatype_list[ind].Parserdata_parents[ind1].compare(_data_name_list[ind2]) == 0)
327 MESSAGE("The parents data type " << _datatype_list[ind].Parserdata_parents[ind1] << " of " << _datatype_list[ind].Parserdata_name << " was found in the catalog type");
333 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" )
334 _return_value = false ;
338 return _return_value;
342 //----------------------------------------------------------------------
343 // Function : _parseArguments
344 // Purpose : parse arguments to get general and personal catalog files
345 //----------------------------------------------------------------------
347 SALOME_DataTypeCatalogImpl::_parseArguments(int argc, char **argv,
350 bool _return_value = true;
352 for (int ind = 0; ind < argc ; ind++)
355 if (strcmp(argv[ind],"-help") == 0)
357 MESSAGE( "Usage: " << argv[0] << " -common 'path to data type catalog' -ORBInitRef NameService=corbaname::localhost");
358 _return_value = false ;
360 if (strcmp(argv[ind],"-common") == 0)
364 // General catalog file
365 *_path_data = argv[ind + 1] ;
366 ifstream _path_data_file(*_path_data);
367 if (!_path_data_file)
369 MESSAGE( "Sorry the file " << *_path_data << " can't be open" )
371 _return_value = false;
377 return _return_value;