Salome HOME
Merge branch 'oscar/imps_2017'
[modules/gui.git] / src / DDS / DDS_DicGroup.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "DDS_DicGroup.h"
24
25 #include "DDS_Dictionary.h"
26
27 #include <LDOMString.hxx>
28 #include <LDOM_Element.hxx>
29
30 #include <UnitsAPI.hxx>
31
32 #include <Standard_Failure.hxx>
33 #include <Standard_ErrorHandler.hxx>
34
35 IMPLEMENT_STANDARD_RTTIEXT(DDS_DicGroup, Standard_Transient)
36
37 /*!
38   \class DDS_DicGroup
39   \brief This class provides a set of DDS_DicItem objects from one component.
40 */
41
42 /*!
43   \brief Constructor.
44
45   Create the group with name \a name.
46
47   \param name group name
48 */
49 DDS_DicGroup::DDS_DicGroup( const TCollection_AsciiString& name )
50 : Standard_Transient(),
51   myName( name ),
52   myActiveSystem( UNIT_SYSTEM_SI )
53 {
54 }
55
56 /*!
57   \brief Copy constructor (put in private section to prevent object copying).
58 */
59 DDS_DicGroup::DDS_DicGroup( const DDS_DicGroup& )
60 {
61 }
62
63 /*!
64   \brief Get the name of group (component).
65   \return group name
66 */
67 TCollection_AsciiString DDS_DicGroup::GetName() const
68 {
69   return myName;
70 }
71
72 /*!
73   \brief Get the names of all defined units systems.
74   \param theSystemsSeq returning sequence of names
75 */
76 void DDS_DicGroup::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystemSeq ) const
77 {
78   theSystemSeq.Clear();
79   for ( UnitSystemMap::Iterator it( myUnitSystem ); it.More(); it.Next() )
80   {
81     if ( it.Key() == TCollection_AsciiString( UNIT_SYSTEM_SI ) )
82       theSystemSeq.Prepend( it.Key() );
83     else
84       theSystemSeq.Append( it.Key() );
85   }
86 }
87
88 /*!
89   \brief Get the label of units system \a name.
90   
91   If units system is not found, empty string is returned.
92
93   \param make units system name
94   \return units system label
95 */
96 TCollection_ExtendedString DDS_DicGroup::GetUnitSystemLabel( const TCollection_AsciiString& name ) const
97 {
98   TCollection_ExtendedString aLabel;
99   if ( myUnitSystem.IsBound( name ) )
100     aLabel = myUnitSystem.Find( name );
101   return aLabel;
102 }
103
104 /*!
105   \brief Get the name of active units system.
106   \return active units system name
107 */
108 TCollection_AsciiString DDS_DicGroup::GetActiveUnitSystem() const
109 {
110   return myActiveSystem;
111 }
112
113 /*!
114   \brief Set the active unit system.
115   \param theSystem name of the units system to be made active
116 */
117 void DDS_DicGroup::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
118 {
119   if ( myUnitSystem.IsBound( theSystem ) )
120     myActiveSystem = theSystem;
121 }
122
123 /*!
124   \brief Assignment operator (put in private section to prevent object copying).
125 */
126 void DDS_DicGroup::operator=( const DDS_DicGroup& )
127 {
128 }
129
130 /*!
131   \brief Fill the internal data structures from XML parsed structures.
132   \param theComponentData component data DOM node
133   \param theDocElement document element DOM node
134 */
135 void DDS_DicGroup::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
136 {
137   TCollection_AsciiString aCompName = theComponentData.getAttribute( DDS_Dictionary::KeyWord( "COMPONENT_NAME" ) );
138
139   LDOM_Element systems = theComponentData.GetChildByTagName( DDS_Dictionary::KeyWord( "UNIT_SYSTEMS" ) );
140   if ( !systems.isNull() )
141   {
142     LDOM_NodeList systemList = systems.getElementsByTagName( DDS_Dictionary::KeyWord( "UNIT_SYSTEM" ) );
143     for ( Standard_Integer i = 0; i < systemList.getLength(); i++ )
144     {
145       //const LDOM_Element& aSystem = (const LDOM_Element &)systemList.item( i );
146       LDOM_Node aNode = systemList.item( i );
147       const LDOM_Element& anElem = (const LDOM_Element&) aNode;
148       LDOM_Element aSystem(anElem);
149       TCollection_AsciiString aName = aSystem.getAttribute( DDS_Dictionary::KeyWord( "UNIT_SYSTEM_NAME" ) );
150       TCollection_ExtendedString aLabel = aSystem.getAttribute( DDS_Dictionary::KeyWord( "UNIT_SYSTEM_LABEL" ) );
151
152       if ( aName.IsEmpty() )
153         continue;
154
155       if ( !myUnitSystem.IsBound( aName ) )
156         myUnitSystem.Bind( aName, aLabel );
157
158
159     }
160   }
161
162   if ( !myUnitSystem.IsBound( UNIT_SYSTEM_SI ) )
163   {
164     printf( "Warning: Mandatory unit system SI not defined in component: \"%s\". Added automaticaly", aCompName.ToCString() );
165     myUnitSystem.Bind( UNIT_SYSTEM_SI, TCollection_ExtendedString( "System international" ) );
166   }
167
168   TColStd_SequenceOfAsciiString unitSystems;
169   GetUnitSystems( unitSystems );
170
171   LDOM_NodeList aData = theComponentData.getElementsByTagName( DDS_Dictionary::KeyWord( "DATUM" ) );
172   if ( !aData.getLength() )
173     return;
174
175   for ( Standard_Integer i = 0; i < aData.getLength(); i++ )
176   {
177     //LDOM_Element aQuantity = (const LDOM_Element&)aData.item( i );
178     LDOM_Node aNode = aData.item( i );
179     const LDOM_Element& anElem = (const LDOM_Element&) aNode;
180     LDOM_Element aQuantity(anElem);
181
182     // 1. Attributes (id,label,units?,format?,required?)
183     TCollection_AsciiString anID = aQuantity.getAttribute( DDS_Dictionary::KeyWord( "DATUM_ID" ) );
184     Handle(DDS_DicItem) aDicItem = CreateItem();
185
186     aDicItem->myComponent = this;
187     aDicItem->FillDataMap( anID, aQuantity, theComponentData, theDocElement, unitSystems );
188     myDataMap.Add( anID, aDicItem );
189     
190     bool exist = false;
191     for( int i=1, n=myKeys.Length(); i<=n && !exist; i++ )
192       if( myKeys.Value( i )==anID )
193       {
194         cout << "Doubled key:" << anID << endl;
195         exist = true;
196       }
197     if( !exist )
198       myKeys.Append( anID );
199   }
200 }
201
202 /*!
203   \brief Get the dictionary item with specified identifier \a theID.
204
205   If dictionary item is not found, null handle is returned.
206
207   \param theID item identifier
208   \return dictionary item
209 */
210 Handle(DDS_DicItem) DDS_DicGroup::GetDicItem( const TCollection_AsciiString& theID ) const
211 {
212   Handle(DDS_DicItem) aDicItem;
213   // get dictionary item by id
214   if ( myDataMap.Contains( theID ) )
215     aDicItem = myDataMap.FindFromKey( theID );
216
217   return aDicItem;
218 }
219
220 /*!
221   \brief Return all keys of the group
222   \param seq - string container to be filled with keys
223 */
224 void DDS_DicGroup::GetKeys( TColStd_SequenceOfAsciiString& seq ) const
225 {
226   seq = myKeys;
227 }
228
229 /*!
230   \brief Instantiate new dictionary item, used for customization of
231          data dictionary items.
232   \return New dictionary item instance.
233 */
234 Handle(DDS_DicItem) DDS_DicGroup::CreateItem() const
235 {
236   return new DDS_DicItem();
237 }
238
239 /*!
240   \brief Bind dictionary item to ID
241   \return Standard_True if the item has been succesfully bound.
242           Standard_False if there is an item with same id.
243 */
244 Standard_Boolean DDS_DicGroup::AddDicItem( const TCollection_AsciiString& theID,
245                                            const Handle(DDS_DicItem)& theDicItem )
246 {
247   if ( myDataMap.Contains( theID ) )
248     return Standard_False;
249
250   myDataMap.Add( theID, theDicItem );
251   myKeys.Append( theID );
252
253   return Standard_True;
254 }
255
256 /*!
257   \brief Check if there is item bounded by id.
258   \return Standard_True if there is an item bound in map with given id.
259 */
260 Standard_Boolean DDS_DicGroup::HasDicItem( const TCollection_AsciiString& theID ) const 
261 {
262   return myDataMap.Contains( theID );
263 }
264
265 /*!
266   \brief Clear dictionary items map
267 */
268 void DDS_DicGroup::RemoveAllDicItems() 
269 {
270   myDataMap.Clear();
271   myKeys.Clear();
272 }
273
274 /*!
275   \brief Returns a reference to a map. Can be used to iterate through
276          dictionary items.
277 */
278 const DDS_IndexedDataMapOfDicItems& DDS_DicGroup::GetItemMap() const
279 {
280   return myDataMap;
281 }
282
283 /*!
284   \brief Add new unit system and bind its label name.
285   \return Standard_False if there is other system bound by this key.
286 */
287 Standard_Boolean DDS_DicGroup::AddUnitSystem( const TCollection_AsciiString& theSystemKey,
288                                               const TCollection_ExtendedString& theSystemLabel )
289 {
290   return myUnitSystem.Bind( theSystemKey, theSystemLabel );
291 }