Back to index

salome-gui  6.5.0
DDS_Dictionary.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "DDS_Dictionary.h"
00024 
00025 #include "DDS_KeyWords.h"
00026 
00027 #include <Basics_OCCTVersion.hxx>
00028 
00029 #include <LDOMString.hxx>
00030 #include <LDOMParser.hxx>
00031 
00032 #include <UnitsAPI.hxx>
00033 
00034 #include <TColStd_SequenceOfInteger.hxx>
00035 #include <TColStd_SequenceOfAsciiString.hxx>
00036 #include <TColStd_SequenceOfExtendedString.hxx>
00037 
00038 #include <NCollection_Map.hxx>
00039 
00040 #include <Standard_Failure.hxx>
00041 #include <Standard_ErrorHandler.hxx>
00042 
00043 IMPLEMENT_STANDARD_HANDLE(DDS_Dictionary, MMgt_TShared)
00044 IMPLEMENT_STANDARD_RTTIEXT(DDS_Dictionary, MMgt_TShared)
00045 
00262 DDS_Dictionary::DDS_Dictionary()
00263 : MMgt_TShared()
00264 {
00265 }
00266 
00270 DDS_Dictionary::DDS_Dictionary( const DDS_Dictionary& )
00271 {
00272 }
00273 
00277 void DDS_Dictionary::operator=( const DDS_Dictionary& )
00278 {
00279 }
00280 
00285 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems ) const
00286 {
00287   theSystems.Clear();
00288 
00289   NCollection_Map<TCollection_AsciiString> aMap;
00290   for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
00291   {
00292     TColStd_SequenceOfAsciiString theSeq;
00293     myGroupMap.FindFromIndex( i )->GetUnitSystems( theSeq );
00294     for ( Standard_Integer s = 1; s <= theSeq.Length(); s++ )
00295     {
00296       if ( aMap.Contains( theSeq.Value( s ) ) )
00297         continue;
00298 
00299       theSystems.Append( theSeq.Value( s ) );
00300       aMap.Add( theSeq.Value( s ) );
00301     }
00302   }
00303 
00304 }
00305 
00315 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems,
00316                                      const TCollection_AsciiString& theComponent ) const
00317 {
00318   theSystems.Clear();
00319   if ( myGroupMap.Contains( theComponent ) )
00320     myGroupMap.FindFromKey( theComponent )->GetUnitSystems( theSystems );
00321 }
00322 
00332 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem ) const
00333 {
00334   TCollection_ExtendedString aLabel;
00335   for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && !aLabel.Length(); i++ )
00336     aLabel = myGroupMap.FindFromIndex( i )->GetUnitSystemLabel( theSystem );
00337   return aLabel;
00338 }
00339 
00351 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem,
00352                                                                const TCollection_AsciiString& theComponent ) const
00353 {
00354   TCollection_ExtendedString aLabel;
00355   if ( myGroupMap.Contains( theComponent ) )
00356     aLabel = myGroupMap.FindFromKey( theComponent )->GetUnitSystemLabel( theSystem );
00357   return aLabel;
00358 }
00359 
00368 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem() const
00369 {
00370   TCollection_AsciiString aSystem;
00371   if ( myGroupMap.Extent() )
00372     aSystem = myGroupMap.FindFromIndex( 1 )->GetActiveUnitSystem();
00373   return aSystem;
00374 }
00375 
00385 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem( const TCollection_AsciiString& theComponent ) const
00386 {
00387   TCollection_AsciiString aSystem;
00388   if ( myGroupMap.Contains( theComponent ) )
00389     aSystem = myGroupMap.FindFromKey( theComponent )->GetActiveUnitSystem();
00390   return aSystem;
00391 }
00392 
00401 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
00402 {
00403   for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
00404     myGroupMap.FindFromIndex( i )->SetActiveUnitSystem( theSystem );
00405 }
00406 
00415 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem,
00416                                           const TCollection_AsciiString& theComponent )
00417 {
00418   if ( myGroupMap.Contains( theComponent ) )
00419     myGroupMap.FindFromKey( theComponent )->SetActiveUnitSystem( theSystem );
00420 }
00421 
00426 Handle_DDS_Dictionary DDS_Dictionary::Get()
00427 {
00428   static Handle(DDS_Dictionary) sDictionary;
00429 
00430   if ( sDictionary.IsNull() )
00431     sDictionary = new DDS_Dictionary();
00432 
00433   return sDictionary;
00434 }
00435 
00442 Standard_Boolean DDS_Dictionary::Load( const TCollection_AsciiString theFileName )
00443 {
00444   static NCollection_Map<TCollection_AsciiString> _LoadMap;
00445 
00446   if ( _LoadMap.Contains( theFileName ) )
00447     return Standard_True;
00448 
00449   Handle(DDS_Dictionary) aDic = Get();
00450   if ( aDic.IsNull() )
00451     return Standard_False;
00452 
00453   LDOMParser aParser;
00454   if ( aParser.parse( theFileName.ToCString() ) )
00455     return Standard_False;
00456 
00457   LDOM_Document aDoc = aParser.getDocument();
00458   LDOM_Element aDocElement = aDoc.getDocumentElement();
00459   for ( LDOM_Element aComponentElem = aDocElement.GetChildByTagName( KeyWord( "COMPONENT" ) );
00460         !aComponentElem.isNull(); aComponentElem = aComponentElem.GetSiblingByTagName() )
00461     aDic->FillDataMap( aComponentElem, aDocElement );
00462 
00463   _LoadMap.Add( theFileName );
00464 
00465   return Standard_True;
00466 }
00467 
00477 LDOMString DDS_Dictionary::KeyWord( const TCollection_AsciiString& key )
00478 {
00479   LDOMString keyWord;
00480   Handle(DDS_KeyWords) aKeyWords = DDS_KeyWords::Get();
00481   if ( !aKeyWords.IsNull() )
00482   {
00483     TCollection_AsciiString aStr = aKeyWords->GetKeyWord( key );
00484     if ( aStr.Length() )
00485       keyWord = LDOMString( aStr.ToCString() );
00486   }
00487   return keyWord;
00488 }
00489 
00499 Handle_DDS_DicItem DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID,
00500                                                 const TCollection_AsciiString& theComponent ) const
00501 {
00502   Handle(DDS_DicItem) aDicItem;
00503   Handle(DDS_DicGroup) aDicGroup;
00504   if ( myGroupMap.Contains( theComponent ) )
00505     aDicGroup = myGroupMap.FindFromKey( theComponent );
00506   if ( !aDicGroup.IsNull() )
00507     aDicItem = aDicGroup->GetDicItem( theID );
00508   return aDicItem;
00509 }
00510 
00520 Handle_DDS_DicItem DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID ) const
00521 {
00522   Handle(DDS_DicItem) aDicItem;
00523   for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && aDicItem.IsNull(); i++ )
00524     aDicItem = myGroupMap.FindFromIndex( i )->GetDicItem( theID );
00525   return aDicItem;
00526 }
00527 
00533 void DDS_Dictionary::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
00534 {
00535   TCollection_AsciiString aCompName = theComponentData.getAttribute( KeyWord( "COMPONENT_NAME" ) );
00536   if ( !myGroupMap.Contains( aCompName ) )
00537     myGroupMap.Add( aCompName, new DDS_DicGroup( aCompName ) );
00538   Handle(DDS_DicGroup) aDicGroup = myGroupMap.FindFromKey( aCompName );
00539   aDicGroup->FillDataMap( theComponentData, theDocElement );
00540   myGroupMap.Add( aCompName, aDicGroup );
00541 }
00542 
00550 Standard_Real DDS_Dictionary::ToSI( const Standard_Real theValue, const Standard_CString theUnits )
00551 {
00552   Standard_Real aRetValue = theValue;
00553   if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
00554   {
00555     try {
00556 #if OCC_VERSION_LARGE > 0x06010000
00557       OCC_CATCH_SIGNALS;
00558 #endif
00559       aRetValue = UnitsAPI::AnyToSI( theValue, theUnits );
00560     }
00561     catch( Standard_Failure ) {
00562     }
00563   }
00564   else if ( theUnits && *theUnits )
00565     aRetValue = theValue / 100.0;
00566 
00567   return aRetValue;
00568 }
00569 
00577 Standard_Real DDS_Dictionary::FromSI( const Standard_Real theValue, const Standard_CString theUnits )
00578 {
00579   Standard_Real aRetValue = theValue;
00580   if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
00581   {
00582     try {
00583 #if OCC_VERSION_LARGE > 0x06010000
00584       OCC_CATCH_SIGNALS;
00585 #endif
00586       aRetValue = UnitsAPI::AnyFromSI( theValue, theUnits );
00587     }
00588     catch( Standard_Failure ) {
00589     }
00590   }
00591   else if ( theUnits && *theUnits )
00592     aRetValue = theValue * 100.0;
00593 
00594   return aRetValue;
00595 }
00596 
00602 void DDS_Dictionary::GetKeys( const TCollection_AsciiString& theComponent, TColStd_SequenceOfAsciiString& seq ) const
00603 {
00604   Handle( DDS_DicGroup ) aDicGroup;
00605   if( myGroupMap.Contains( theComponent ) )
00606     aDicGroup = myGroupMap.FindFromKey( theComponent );
00607 
00608   if( !aDicGroup.IsNull() )
00609     aDicGroup->GetKeys( seq );
00610 }