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