Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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 DDS_DicGroup::DDS_DicGroup( const TCollection_AsciiString& name )
37 : MMgt_TShared(),
38 myName( name ),
39 myActiveSystem( UNIT_SYSTEM_SI )
40 {
41 }
42
43 DDS_DicGroup::DDS_DicGroup( const DDS_DicGroup& )
44 {
45 }
46
47 TCollection_AsciiString DDS_DicGroup::GetName() const
48 {
49   return myName;
50 }
51
52 void DDS_DicGroup::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystemSeq ) const
53 {
54   theSystemSeq.Clear();
55   for ( UnitSystemMap::Iterator it( myUnitSystem ); it.More(); it.Next() )
56   {
57     if ( it.Key() == TCollection_AsciiString( UNIT_SYSTEM_SI ) )
58       theSystemSeq.Prepend( it.Key() );
59     else
60       theSystemSeq.Append( it.Key() );
61   }
62 }
63
64 TCollection_ExtendedString DDS_DicGroup::GetUnitSystemLabel( const TCollection_AsciiString& name ) const
65 {
66   TCollection_ExtendedString aLabel;
67   if ( myUnitSystem.IsBound( name ) )
68     aLabel = myUnitSystem.Find( name );
69   return aLabel;
70 }
71
72 TCollection_AsciiString DDS_DicGroup::GetActiveUnitSystem() const
73 {
74   return myActiveSystem;
75 }
76
77 void DDS_DicGroup::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
78 {
79   if ( myUnitSystem.IsBound( theSystem ) )
80     myActiveSystem = theSystem;
81 }
82
83 void DDS_DicGroup::operator=( const DDS_DicGroup& )
84 {
85 }
86
87 void DDS_DicGroup::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
88 {
89   TCollection_AsciiString aCompName = theComponentData.getAttribute( DDS_Dictionary::KeyWord( "COMPONENT_NAME" ) );
90
91   LDOM_Element systems = theComponentData.GetChildByTagName( DDS_Dictionary::KeyWord( "UNIT_SYSTEMS" ) );
92   if ( !systems.isNull() )
93   {
94     LDOM_NodeList systemList = systems.getElementsByTagName( DDS_Dictionary::KeyWord( "UNIT_SYSTEM" ) );
95     for ( Standard_Integer i = 0; i < systemList.getLength(); i++ )
96     {
97       //const LDOM_Element& aSystem = (const LDOM_Element &)systemList.item( i );
98       LDOM_Node aNode = systemList.item( i );
99       const LDOM_Element& anElem = (const LDOM_Element&) aNode;
100       LDOM_Element aSystem(anElem);
101       TCollection_AsciiString aName = aSystem.getAttribute( DDS_Dictionary::KeyWord( "UNIT_SYSTEM_NAME" ) );
102       TCollection_ExtendedString aLabel = aSystem.getAttribute( DDS_Dictionary::KeyWord( "UNIT_SYSTEM_LABEL" ) );
103
104       if ( aName.IsEmpty() )
105         continue;
106
107       if ( !myUnitSystem.IsBound( aName ) )
108         myUnitSystem.Bind( aName, aLabel );
109     }
110   }
111
112   if ( !myUnitSystem.IsBound( UNIT_SYSTEM_SI ) )
113   {
114     printf( "Warning: Mandatory unit system SI not defined in component: \"%s\". Added automaticaly", aCompName.ToCString() );
115     myUnitSystem.Bind( UNIT_SYSTEM_SI, TCollection_ExtendedString( "System international" ) );
116   }
117
118   TColStd_SequenceOfAsciiString unitSystems;
119   GetUnitSystems( unitSystems );
120
121   LDOM_NodeList aData = theComponentData.getElementsByTagName( DDS_Dictionary::KeyWord( "DATUM" ) );
122   if ( !aData.getLength() )
123     return;
124
125   for ( Standard_Integer i = 0; i < aData.getLength(); i++ )
126   {
127     //LDOM_Element aQuantity = (const LDOM_Element&)aData.item( i );
128     LDOM_Node aNode = aData.item( i );
129     const LDOM_Element& anElem = (const LDOM_Element&) aNode;
130     LDOM_Element aQuantity(anElem);
131
132     // 1. Attributes (id,label,units?,format?,required?)
133     TCollection_AsciiString anID = aQuantity.getAttribute( DDS_Dictionary::KeyWord( "DATUM_ID" ) );
134     Handle(DDS_DicItem) aDicItem = new DDS_DicItem();
135
136     aDicItem->myComponent = this;
137     aDicItem->FillDataMap( anID, aQuantity, theComponentData, theDocElement, unitSystems );
138     myDataMap.Add( anID, aDicItem );
139   }
140 }
141
142 /*!
143   Returns DicItem with all attached data
144 */
145
146 Handle(DDS_DicItem) DDS_DicGroup::GetDicItem( const TCollection_AsciiString& theID ) const
147 {
148   Handle(DDS_DicItem) aDicItem;
149   // get dictionary item by id
150   if ( myDataMap.Contains( theID ) )
151     aDicItem = myDataMap.FindFromKey( theID );
152
153   return aDicItem;
154 }