Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / DDS / DDS_DicGroup.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/ or email : webmaster.salome@opencascade.com
18 //
19 #include "DDS_DicGroup.h"
20
21 #include "DDS_Dictionary.h"
22
23 #include <LDOMString.hxx>
24 #include <LDOM_Element.hxx>
25
26 #include <UnitsAPI.hxx>
27
28 #include <TColStd_SequenceOfAsciiString.hxx>
29
30 #include <Standard_Failure.hxx>
31 #include <Standard_ErrorHandler.hxx>
32
33 IMPLEMENT_STANDARD_HANDLE(DDS_DicGroup, MMgt_TShared)
34 IMPLEMENT_STANDARD_RTTIEXT(DDS_DicGroup, MMgt_TShared)
35
36 /*!
37   \class DDS_DicGroup
38   
39   This class to provide set of DDS_DicItem objects from one component.
40 */
41
42 /*!
43   Constructor. Create the group with name \aname.
44 */
45 DDS_DicGroup::DDS_DicGroup( const TCollection_AsciiString& name )
46 : MMgt_TShared(),
47 myName( name ),
48 myActiveSystem( UNIT_SYSTEM_SI )
49 {
50 }
51
52 /*!
53   Copy constructor.
54 */
55 DDS_DicGroup::DDS_DicGroup( const DDS_DicGroup& )
56 {
57 }
58
59 /*!
60   Get the name of group (component).
61 */
62 TCollection_AsciiString DDS_DicGroup::GetName() const
63 {
64   return myName;
65 }
66
67 /*!
68   Returns the names list of defined unit systems.
69   Parameter \atheSystems will contains the sequence of string names.
70 */
71 void DDS_DicGroup::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystemSeq ) const
72 {
73   theSystemSeq.Clear();
74   for ( UnitSystemMap::Iterator it( myUnitSystem ); it.More(); it.Next() )
75   {
76     if ( it.Key() == TCollection_AsciiString( UNIT_SYSTEM_SI ) )
77       theSystemSeq.Prepend( it.Key() );
78     else
79       theSystemSeq.Append( it.Key() );
80   }
81 }
82
83 /*!
84   Returns the label of unit system \aname. If unit system not found then empty string returned.
85 */
86 TCollection_ExtendedString DDS_DicGroup::GetUnitSystemLabel( const TCollection_AsciiString& name ) const
87 {
88   TCollection_ExtendedString aLabel;
89   if ( myUnitSystem.IsBound( name ) )
90     aLabel = myUnitSystem.Find( name );
91   return aLabel;
92 }
93
94 /*!
95   Gets the name of active unit system.
96 */
97 TCollection_AsciiString DDS_DicGroup::GetActiveUnitSystem() const
98 {
99   return myActiveSystem;
100 }
101
102 /*!
103   Sets the name of active unit system.
104 */
105 void DDS_DicGroup::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
106 {
107   if ( myUnitSystem.IsBound( theSystem ) )
108     myActiveSystem = theSystem;
109 }
110
111 /*!
112   Assignment operator.
113 */
114 void DDS_DicGroup::operator=( const DDS_DicGroup& )
115 {
116 }
117
118 /*!
119   Fill the internal data structures from XML parsed structures. Internal.
120 */
121 void DDS_DicGroup::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
122 {
123   TCollection_AsciiString aCompName = theComponentData.getAttribute( DDS_Dictionary::KeyWord( "COMPONENT_NAME" ) );
124
125   LDOM_Element systems = theComponentData.GetChildByTagName( DDS_Dictionary::KeyWord( "UNIT_SYSTEMS" ) );
126   if ( !systems.isNull() )
127   {
128     LDOM_NodeList systemList = systems.getElementsByTagName( DDS_Dictionary::KeyWord( "UNIT_SYSTEM" ) );
129     for ( Standard_Integer i = 0; i < systemList.getLength(); i++ )
130     {
131       //const LDOM_Element& aSystem = (const LDOM_Element &)systemList.item( i );
132       LDOM_Node aNode = systemList.item( i );
133       const LDOM_Element& anElem = (const LDOM_Element&) aNode;
134       LDOM_Element aSystem(anElem);
135       TCollection_AsciiString aName = aSystem.getAttribute( DDS_Dictionary::KeyWord( "UNIT_SYSTEM_NAME" ) );
136       TCollection_ExtendedString aLabel = aSystem.getAttribute( DDS_Dictionary::KeyWord( "UNIT_SYSTEM_LABEL" ) );
137
138       if ( aName.IsEmpty() )
139         continue;
140
141       if ( !myUnitSystem.IsBound( aName ) )
142         myUnitSystem.Bind( aName, aLabel );
143     }
144   }
145
146   if ( !myUnitSystem.IsBound( UNIT_SYSTEM_SI ) )
147   {
148     printf( "Warning: Mandatory unit system SI not defined in component: \"%s\". Added automaticaly", aCompName.ToCString() );
149     myUnitSystem.Bind( UNIT_SYSTEM_SI, TCollection_ExtendedString( "System international" ) );
150   }
151
152   TColStd_SequenceOfAsciiString unitSystems;
153   GetUnitSystems( unitSystems );
154
155   LDOM_NodeList aData = theComponentData.getElementsByTagName( DDS_Dictionary::KeyWord( "DATUM" ) );
156   if ( !aData.getLength() )
157     return;
158
159   for ( Standard_Integer i = 0; i < aData.getLength(); i++ )
160   {
161     //LDOM_Element aQuantity = (const LDOM_Element&)aData.item( i );
162     LDOM_Node aNode = aData.item( i );
163     const LDOM_Element& anElem = (const LDOM_Element&) aNode;
164     LDOM_Element aQuantity(anElem);
165
166     // 1. Attributes (id,label,units?,format?,required?)
167     TCollection_AsciiString anID = aQuantity.getAttribute( DDS_Dictionary::KeyWord( "DATUM_ID" ) );
168     Handle(DDS_DicItem) aDicItem = new DDS_DicItem();
169
170     aDicItem->myComponent = this;
171     aDicItem->FillDataMap( anID, aQuantity, theComponentData, theDocElement, unitSystems );
172     myDataMap.Add( anID, aDicItem );
173   }
174 }
175
176 /*!
177   Gets dictionary item with specified identifier \atheID.
178   If dictionary item not found then null handle returned.
179 */
180 Handle(DDS_DicItem) DDS_DicGroup::GetDicItem( const TCollection_AsciiString& theID ) const
181 {
182   Handle(DDS_DicItem) aDicItem;
183   // get dictionary item by id
184   if ( myDataMap.Contains( theID ) )
185     aDicItem = myDataMap.FindFromKey( theID );
186
187   return aDicItem;
188 }