1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "DDS_Dictionary.h"
21 #include "DDS_KeyWords.h"
23 #include <LDOMString.hxx>
24 #include <LDOMParser.hxx>
26 #include <UnitsAPI.hxx>
28 #include <TColStd_SequenceOfInteger.hxx>
29 #include <TColStd_SequenceOfAsciiString.hxx>
30 #include <TColStd_SequenceOfExtendedString.hxx>
32 #include <NCollection_Map.hxx>
34 #include <Standard_Failure.hxx>
35 #include <Standard_ErrorHandler.hxx>
37 IMPLEMENT_STANDARD_HANDLE(DDS_Dictionary, MMgt_TShared)
38 IMPLEMENT_STANDARD_RTTIEXT(DDS_Dictionary, MMgt_TShared)
40 DDS_Dictionary::DDS_Dictionary()
45 DDS_Dictionary::DDS_Dictionary( const DDS_Dictionary& )
49 void DDS_Dictionary::operator=( const DDS_Dictionary& )
53 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems ) const
57 NCollection_Map<TCollection_AsciiString> aMap;
58 for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
60 TColStd_SequenceOfAsciiString theSeq;
61 myGroupMap.FindFromIndex( i )->GetUnitSystems( theSeq );
62 for ( Standard_Integer s = 1; s <= theSeq.Length(); s++ )
64 if ( aMap.Contains( theSeq.Value( s ) ) )
67 theSystems.Append( theSeq.Value( s ) );
68 aMap.Add( theSeq.Value( s ) );
74 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems,
75 const TCollection_AsciiString& theComponent ) const
78 if ( myGroupMap.Contains( theComponent ) )
79 myGroupMap.FindFromKey( theComponent )->GetUnitSystems( theSystems );
82 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem ) const
84 TCollection_ExtendedString aLabel;
85 for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && !aLabel.Length(); i++ )
86 aLabel = myGroupMap.FindFromIndex( i )->GetUnitSystemLabel( theSystem );
90 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem,
91 const TCollection_AsciiString& theComponent ) const
93 TCollection_ExtendedString aLabel;
94 if ( myGroupMap.Contains( theComponent ) )
95 aLabel = myGroupMap.FindFromKey( theComponent )->GetUnitSystemLabel( theSystem );
99 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem() const
101 TCollection_AsciiString aSystem;
102 if ( myGroupMap.Extent() )
103 aSystem = myGroupMap.FindFromIndex( 1 )->GetActiveUnitSystem();
107 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem( const TCollection_AsciiString& theComponent ) const
109 TCollection_AsciiString aSystem;
110 if ( myGroupMap.Contains( theComponent ) )
111 aSystem = myGroupMap.FindFromKey( theComponent )->GetActiveUnitSystem();
115 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
117 for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
118 myGroupMap.FindFromIndex( i )->SetActiveUnitSystem( theSystem );
121 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem,
122 const TCollection_AsciiString& theComponent )
124 if ( myGroupMap.Contains( theComponent ) )
125 myGroupMap.FindFromKey( theComponent )->SetActiveUnitSystem( theSystem );
129 Returns the instance of dictionary. Create instance if it is NULL.
131 Handle(DDS_Dictionary) DDS_Dictionary::Get()
133 static Handle(DDS_Dictionary) sDictionary;
135 if ( sDictionary.IsNull() )
136 sDictionary = new DDS_Dictionary();
141 Standard_Boolean DDS_Dictionary::Load( const TCollection_AsciiString theFileName )
143 static NCollection_Map<TCollection_AsciiString> _LoadMap;
145 if ( _LoadMap.Contains( theFileName ) )
146 return Standard_True;
148 Handle(DDS_Dictionary) aDic = Get();
150 return Standard_False;
153 if ( aParser.parse( theFileName.ToCString() ) )
154 return Standard_False;
156 LDOM_Document aDoc = aParser.getDocument();
157 LDOM_Element aDocElement = aDoc.getDocumentElement();
158 for ( LDOM_Element aComponentElem = aDocElement.GetChildByTagName( KeyWord( "COMPONENT" ) );
159 !aComponentElem.isNull(); aComponentElem = aComponentElem.GetSiblingByTagName() )
160 aDic->FillDataMap( aComponentElem, aDocElement );
162 _LoadMap.Add( theFileName );
164 return Standard_True;
167 LDOMString DDS_Dictionary::KeyWord( const TCollection_AsciiString& key )
170 Handle(DDS_KeyWords) aKeyWords = DDS_KeyWords::Get();
171 if ( !aKeyWords.IsNull() )
173 TCollection_AsciiString aStr = aKeyWords->GetKeyWord( key );
175 keyWord = LDOMString( aStr.ToCString() );
181 Returns DicItem from specified group with all attached data
184 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID,
185 const TCollection_AsciiString& theGroup ) const
187 Handle(DDS_DicItem) aDicItem;
188 Handle(DDS_DicGroup) aDicGroup;
189 if ( myGroupMap.Contains( theGroup ) )
190 aDicGroup = myGroupMap.FindFromKey( theGroup );
191 if ( !aDicGroup.IsNull() )
192 aDicItem = aDicGroup->GetDicItem( theID );
197 Returns DicItem with all attached data
200 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID ) const
202 Handle(DDS_DicItem) aDicItem;
203 for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && aDicItem.IsNull(); i++ )
204 aDicItem = myGroupMap.FindFromIndex( i )->GetDicItem( theID );
208 void DDS_Dictionary::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
210 TCollection_AsciiString aCompName = theComponentData.getAttribute( KeyWord( "COMPONENT_NAME" ) );
211 if ( !myGroupMap.Contains( aCompName ) )
212 myGroupMap.Add( aCompName, new DDS_DicGroup( aCompName ) );
213 Handle(DDS_DicGroup) aDicGroup = myGroupMap.FindFromKey( aCompName );
214 aDicGroup->FillDataMap( theComponentData, theDocElement );
215 myGroupMap.Add( aCompName, aDicGroup );
218 Standard_Real DDS_Dictionary::ToSI( const Standard_Real theValue, const Standard_CString theUnits )
220 Standard_Real aRetValue = theValue;
221 if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
224 aRetValue = UnitsAPI::AnyToSI( theValue, theUnits );
226 catch( Standard_Failure ) {
229 else if ( theUnits && *theUnits )
230 aRetValue = theValue / 100.0;
235 Standard_Real DDS_Dictionary::FromSI( const Standard_Real theValue, const Standard_CString theUnits )
237 Standard_Real aRetValue = theValue;
238 if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
241 aRetValue = UnitsAPI::AnyFromSI( theValue, theUnits );
243 catch( Standard_Failure ) {
246 else if ( theUnits && *theUnits )
247 aRetValue = theValue * 100.0;