Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / DDS / DDS_Dictionary.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "DDS_Dictionary.h"
20
21 #include "DDS_KeyWords.h"
22
23 #include <LDOMString.hxx>
24 #include <LDOMParser.hxx>
25
26 #include <UnitsAPI.hxx>
27
28 #include <TColStd_SequenceOfInteger.hxx>
29 #include <TColStd_SequenceOfAsciiString.hxx>
30 #include <TColStd_SequenceOfExtendedString.hxx>
31
32 #include <NCollection_Map.hxx>
33
34 #include <Standard_Failure.hxx>
35 #include <Standard_ErrorHandler.hxx>
36
37 IMPLEMENT_STANDARD_HANDLE(DDS_Dictionary, MMgt_TShared)
38 IMPLEMENT_STANDARD_RTTIEXT(DDS_Dictionary, MMgt_TShared)
39
40 DDS_Dictionary::DDS_Dictionary()
41 : MMgt_TShared()
42 {
43 }
44
45 DDS_Dictionary::DDS_Dictionary( const DDS_Dictionary& )
46 {
47 }
48
49 void DDS_Dictionary::operator=( const DDS_Dictionary& )
50 {
51 }
52
53 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems ) const
54 {
55   theSystems.Clear();
56
57   NCollection_Map<TCollection_AsciiString> aMap;
58   for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
59   {
60     TColStd_SequenceOfAsciiString theSeq;
61     myGroupMap.FindFromIndex( i )->GetUnitSystems( theSeq );
62     for ( Standard_Integer s = 1; s <= theSeq.Length(); s++ )
63     {
64       if ( aMap.Contains( theSeq.Value( s ) ) )
65         continue;
66
67       theSystems.Append( theSeq.Value( s ) );
68       aMap.Add( theSeq.Value( s ) );
69     }
70   }
71
72 }
73
74 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems,
75                                      const TCollection_AsciiString& theComponent ) const
76 {
77   theSystems.Clear();
78   if ( myGroupMap.Contains( theComponent ) )
79     myGroupMap.FindFromKey( theComponent )->GetUnitSystems( theSystems );
80 }
81
82 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem ) const
83 {
84   TCollection_ExtendedString aLabel;
85   for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && !aLabel.Length(); i++ )
86     aLabel = myGroupMap.FindFromIndex( i )->GetUnitSystemLabel( theSystem );
87   return aLabel;
88 }
89
90 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem,
91                                                                const TCollection_AsciiString& theComponent ) const
92 {
93   TCollection_ExtendedString aLabel;
94   if ( myGroupMap.Contains( theComponent ) )
95     aLabel = myGroupMap.FindFromKey( theComponent )->GetUnitSystemLabel( theSystem );
96   return aLabel;
97 }
98
99 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem() const
100 {
101   TCollection_AsciiString aSystem;
102   if ( myGroupMap.Extent() )
103     aSystem = myGroupMap.FindFromIndex( 1 )->GetActiveUnitSystem();
104   return aSystem;
105 }
106
107 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem( const TCollection_AsciiString& theComponent ) const
108 {
109   TCollection_AsciiString aSystem;
110   if ( myGroupMap.Contains( theComponent ) )
111     aSystem = myGroupMap.FindFromKey( theComponent )->GetActiveUnitSystem();
112   return aSystem;
113 }
114
115 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
116 {
117   for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
118     myGroupMap.FindFromIndex( i )->SetActiveUnitSystem( theSystem );
119 }
120
121 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem,
122                                           const TCollection_AsciiString& theComponent )
123 {
124   if ( myGroupMap.Contains( theComponent ) )
125     myGroupMap.FindFromKey( theComponent )->SetActiveUnitSystem( theSystem );
126 }
127
128 /*!
129   Returns the instance of dictionary. Create instance if it is NULL.
130 */
131 Handle(DDS_Dictionary) DDS_Dictionary::Get()
132 {
133   static Handle(DDS_Dictionary) sDictionary;
134
135   if ( sDictionary.IsNull() )
136     sDictionary = new DDS_Dictionary();
137
138   return sDictionary;
139 }
140
141 Standard_Boolean DDS_Dictionary::Load( const TCollection_AsciiString theFileName )
142 {
143   static NCollection_Map<TCollection_AsciiString> _LoadMap;
144
145   if ( _LoadMap.Contains( theFileName ) )
146     return Standard_True;
147
148   Handle(DDS_Dictionary) aDic = Get();
149   if ( aDic.IsNull() )
150     return Standard_False;
151
152   LDOMParser aParser;
153   if ( aParser.parse( theFileName.ToCString() ) )
154     return Standard_False;
155
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 );
161
162   _LoadMap.Add( theFileName );
163
164   return Standard_True;
165 }
166
167 LDOMString DDS_Dictionary::KeyWord( const TCollection_AsciiString& key )
168 {
169   LDOMString keyWord;
170   Handle(DDS_KeyWords) aKeyWords = DDS_KeyWords::Get();
171   if ( !aKeyWords.IsNull() )
172   {
173     TCollection_AsciiString aStr = aKeyWords->GetKeyWord( key );
174     if ( aStr.Length() )
175       keyWord = LDOMString( aStr.ToCString() );
176   }
177   return keyWord;
178 }
179
180 /*!
181   Returns DicItem from specified group with all attached data
182 */
183
184 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID,
185                                                 const TCollection_AsciiString& theGroup ) const
186 {
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 );
193   return aDicItem;
194 }
195
196 /*!
197   Returns DicItem with all attached data
198 */
199
200 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID ) const
201 {
202   Handle(DDS_DicItem) aDicItem;
203   for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && aDicItem.IsNull(); i++ )
204     aDicItem = myGroupMap.FindFromIndex( i )->GetDicItem( theID );
205   return aDicItem;
206 }
207
208 void DDS_Dictionary::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
209 {
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 );
216 }
217
218 Standard_Real DDS_Dictionary::ToSI( const Standard_Real theValue, const Standard_CString theUnits )
219 {
220   Standard_Real aRetValue = theValue;
221   if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
222   {
223     try {
224       aRetValue = UnitsAPI::AnyToSI( theValue, theUnits );
225     }
226     catch( Standard_Failure ) {
227     }
228   }
229   else if ( theUnits && *theUnits )
230     aRetValue = theValue / 100.0;
231
232   return aRetValue;
233 }
234
235 Standard_Real DDS_Dictionary::FromSI( const Standard_Real theValue, const Standard_CString theUnits )
236 {
237   Standard_Real aRetValue = theValue;
238   if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
239   {
240     try {
241       aRetValue = UnitsAPI::AnyFromSI( theValue, theUnits );
242     }
243     catch( Standard_Failure ) {
244     }
245   }
246   else if ( theUnits && *theUnits )
247     aRetValue = theValue * 100.0;
248
249   return aRetValue;
250 }