1 #include "DDS_Dictionary.h"
3 #include "DDS_KeyWords.h"
5 #include <LDOMString.hxx>
6 #include <LDOMParser.hxx>
8 #include <UnitsAPI.hxx>
10 #include <TColStd_SequenceOfInteger.hxx>
11 #include <TColStd_SequenceOfAsciiString.hxx>
12 #include <TColStd_SequenceOfExtendedString.hxx>
14 #include <NCollection_Map.hxx>
16 #include <Standard_Failure.hxx>
17 #include <Standard_ErrorHandler.hxx>
19 IMPLEMENT_STANDARD_HANDLE(DDS_Dictionary, MMgt_TShared)
20 IMPLEMENT_STANDARD_RTTIEXT(DDS_Dictionary, MMgt_TShared)
22 DDS_Dictionary::DDS_Dictionary()
27 DDS_Dictionary::DDS_Dictionary( const DDS_Dictionary& )
31 void DDS_Dictionary::operator=( const DDS_Dictionary& )
35 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems ) const
39 NCollection_Map<TCollection_AsciiString> aMap;
40 for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
42 TColStd_SequenceOfAsciiString theSeq;
43 myGroupMap.FindFromIndex( i )->GetUnitSystems( theSeq );
44 for ( Standard_Integer s = 1; s <= theSeq.Length(); s++ )
46 if ( aMap.Contains( theSeq.Value( s ) ) )
49 theSystems.Append( theSeq.Value( s ) );
50 aMap.Add( theSeq.Value( s ) );
56 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems,
57 const TCollection_AsciiString& theComponent ) const
60 if ( myGroupMap.Contains( theComponent ) )
61 myGroupMap.FindFromKey( theComponent )->GetUnitSystems( theSystems );
64 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem ) const
66 TCollection_ExtendedString aLabel;
67 for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && !aLabel.Length(); i++ )
68 aLabel = myGroupMap.FindFromIndex( i )->GetUnitSystemLabel( theSystem );
72 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem,
73 const TCollection_AsciiString& theComponent ) const
75 TCollection_ExtendedString aLabel;
76 if ( myGroupMap.Contains( theComponent ) )
77 aLabel = myGroupMap.FindFromKey( theComponent )->GetUnitSystemLabel( theSystem );
81 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem() const
83 TCollection_AsciiString aSystem;
84 if ( myGroupMap.Extent() )
85 aSystem = myGroupMap.FindFromIndex( 1 )->GetActiveUnitSystem();
89 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem( const TCollection_AsciiString& theComponent ) const
91 TCollection_AsciiString aSystem;
92 if ( myGroupMap.Contains( theComponent ) )
93 aSystem = myGroupMap.FindFromKey( theComponent )->GetActiveUnitSystem();
97 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
99 for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
100 myGroupMap.FindFromIndex( i )->SetActiveUnitSystem( theSystem );
103 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem,
104 const TCollection_AsciiString& theComponent )
106 if ( myGroupMap.Contains( theComponent ) )
107 myGroupMap.FindFromKey( theComponent )->SetActiveUnitSystem( theSystem );
111 Returns the instance of dictionary. Create instance if it is NULL.
113 Handle(DDS_Dictionary) DDS_Dictionary::Get()
115 static Handle(DDS_Dictionary) sDictionary;
117 if ( sDictionary.IsNull() )
118 sDictionary = new DDS_Dictionary();
123 Standard_Boolean DDS_Dictionary::Load( const TCollection_AsciiString theFileName )
125 static NCollection_Map<TCollection_AsciiString> _LoadMap;
127 if ( _LoadMap.Contains( theFileName ) )
128 return Standard_True;
130 Handle(DDS_Dictionary) aDic = Get();
132 return Standard_False;
135 if ( aParser.parse( theFileName.ToCString() ) )
136 return Standard_False;
138 LDOM_Document aDoc = aParser.getDocument();
139 LDOM_Element aDocElement = aDoc.getDocumentElement();
140 for ( LDOM_Element aComponentElem = aDocElement.GetChildByTagName( KeyWord( "COMPONENT" ) );
141 !aComponentElem.isNull(); aComponentElem = aComponentElem.GetSiblingByTagName() )
142 aDic->FillDataMap( aComponentElem, aDocElement );
144 _LoadMap.Add( theFileName );
146 return Standard_True;
149 LDOMString DDS_Dictionary::KeyWord( const TCollection_AsciiString& key )
152 Handle(DDS_KeyWords) aKeyWords = DDS_KeyWords::Get();
153 if ( !aKeyWords.IsNull() )
155 TCollection_AsciiString aStr = aKeyWords->GetKeyWord( key );
157 keyWord = LDOMString( aStr.ToCString() );
163 Returns DicItem from specified group with all attached data
166 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID,
167 const TCollection_AsciiString& theGroup ) const
169 Handle(DDS_DicItem) aDicItem;
170 Handle(DDS_DicGroup) aDicGroup;
171 if ( myGroupMap.Contains( theGroup ) )
172 aDicGroup = myGroupMap.FindFromKey( theGroup );
173 if ( !aDicGroup.IsNull() )
174 aDicItem = aDicGroup->GetDicItem( theID );
179 Returns DicItem with all attached data
182 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID ) const
184 Handle(DDS_DicItem) aDicItem;
185 for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && aDicItem.IsNull(); i++ )
186 aDicItem = myGroupMap.FindFromIndex( i )->GetDicItem( theID );
190 void DDS_Dictionary::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
192 TCollection_AsciiString aCompName = theComponentData.getAttribute( KeyWord( "COMPONENT_NAME" ) );
193 if ( !myGroupMap.Contains( aCompName ) )
194 myGroupMap.Add( aCompName, new DDS_DicGroup( aCompName ) );
195 Handle(DDS_DicGroup) aDicGroup = myGroupMap.FindFromKey( aCompName );
196 aDicGroup->FillDataMap( theComponentData, theDocElement );
197 myGroupMap.Add( aCompName, aDicGroup );
200 Standard_Real DDS_Dictionary::ToSI( const Standard_Real theValue, const Standard_CString theUnits )
202 Standard_Real aRetValue = theValue;
203 if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
206 aRetValue = UnitsAPI::AnyToSI( theValue, theUnits );
208 catch( Standard_Failure ) {
211 else if ( theUnits && *theUnits )
212 aRetValue = theValue / 100.0;
217 Standard_Real DDS_Dictionary::FromSI( const Standard_Real theValue, const Standard_CString theUnits )
219 Standard_Real aRetValue = theValue;
220 if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
223 aRetValue = UnitsAPI::AnyFromSI( theValue, theUnits );
225 catch( Standard_Failure ) {
228 else if ( theUnits && *theUnits )
229 aRetValue = theValue * 100.0;