]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeNode.cpp
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / creaImageIOTreeNode.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28 #include <creaImageIOTreeNode.h>
29 #include <creaImageIOTree.h>
30 #include <creaImageIOSystem.h>
31 #include <algorithm>
32 #include <creaImageIOGimmick.h>
33
34 namespace creaImageIO
35 {
36   namespace tree
37   {
38
39     //=============================================================
40     /// Ctor with parent
41     Node::Node(Node* parent)
42       : mParent(parent),//mData(0),
43         mChildrenLoaded(false)
44     {
45         mData.reset();
46       if (parent) 
47         {
48           GimmickDebugMessage(6,"Default Node constructor (level "<<GetLevel()<<")"
49                          << std::endl);
50           // Insert into parent's children list
51           InitializeAttributeMap();
52          parent->GetChildrenList().push_back(this);
53         }
54       else
55         {
56           GimmickDebugMessage(6,"Default Node constructor without parent"       
57                          << std::endl);
58         }
59     }
60     //=============================================================
61
62     //=============================================================
63     /// Ctor with parent and attributes map 
64     Node::Node(Node* parent, const AttributeMapType& attr)
65      : mParent(parent),//mData(0),
66         mChildrenLoaded(false)
67     {
68         mData.reset();
69       GimmickDebugMessage(6,"Node constructor (level "<<GetLevel()<<")"
70                      << std::endl);
71
72      if (parent) 
73         {
74           // Insert into parent's children list
75           parent->GetChildrenList().push_back(this);
76           // Initialize attributes
77           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
78           for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
79                a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
80                ++a)
81             {
82               std::string v;
83               AttributeMapType::const_iterator i = attr.find(a->GetKey());
84               if ( i != attr.end() )  
85                 {
86                   v = i->second;
87                 }
88               GimmickDebugMessage(6,"Setting attribute '"<<a->GetName()<<"' = '"
89                              <<v<<"'"<<std::endl);
90               UnsafeSetAttribute( a->GetKey(), v );
91             }
92         }
93       
94     }
95     //=============================================================
96
97
98     //=============================================================
99     Node::~Node()
100     {
101       GimmickDebugMessage(6,"Node destructor"
102                      << std::endl);
103       ChildrenListType::iterator i;
104       for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
105         {
106           delete *i;
107         }
108         mData.reset();
109     }
110     //=============================================================
111
112
113     //=============================================================
114     /// Initializes the attribute map i.e. creates the entries
115     void Node::InitializeAttributeMap()
116     {
117       // Initialize attributes
118       LevelDescriptor::AttributeDescriptorListType::const_iterator a;
119       for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
120            a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
121            ++a)
122         {
123           UnsafeSetAttribute( a->GetKey(), "" );
124         }
125     }
126     //=============================================================
127
128     //=============================================================
129     /// Returns the level descriptor of the node
130     const LevelDescriptor& Node::GetLevelDescriptor() const
131     { 
132       return GetTree()->GetLevelDescriptor(GetLevel()); 
133     }
134
135         //=============================================================
136
137     //=============================================================
138     /// Returns the attribute descriptor of the passed parameter
139         const AttributeDescriptor& Node::GetAttributeDescriptor(const std::string& k)const
140         {
141                 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
142       for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
143            a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
144            ++a)
145                 {
146                 
147                         if(a->GetKey()==k)
148                         {
149                                 return *a;
150                         }
151                 
152                 }
153                 return *a;
154         }
155     //=============================================================
156
157     //=============================================================
158     int Node::RemoveChildrenFromList(Node* node)
159     {
160       ChildrenListType::iterator i = find(GetChildrenList().begin(),
161                                           GetChildrenList().end(),
162                                           node);
163       if (i != GetChildrenList().end())
164         {
165           GetChildrenList().erase(i);
166         }
167         return (int)GetChildrenList().size();
168     }
169     //=============================================================
170
171     //=============================================================
172     const std::string& Node::GetAttribute(const std::string& k) const
173     {
174       //    std::cout << "this = "<<(void*)this<<std::endl;
175       //    std::cout << "mFieldValueMap="<<(void*)(&mFieldValueMap)<<std::endl;
176       AttributeMapType::const_iterator i = mAttributeMap.find(k);
177       if (i == mAttributeMap.end())
178         {
179           static std::string def("");
180           return def;
181           //    CREAIMAGEIO_ERROR("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
182         }
183       return i->second;
184   }
185   //=============================================================
186
187   //=============================================================
188     void Node::SetAttribute(const std::string& k, 
189                                  const std::string& v)
190   {
191     AttributeMapType::iterator i = mAttributeMap.find(k);
192     if (i==mAttributeMap.end())
193       {
194         std::cout<<"[Gimmick!] Node::SetAttribute : no attribute with key '"
195                  <<k<<"'"<<std::endl;
196         creaError( "[Gimmick!] Node::SetAttribute : no attribute with key '"
197                    <<k<<"'");
198       }
199     i->second = v;
200   }
201     //=============================================================
202
203     //=============================================================
204     bool Node::Matches(  const AttributeMapType& m ) const
205     {
206       GimmickDebugMessage(2,"'"<<GetLabel()<<"' matching..."<<std::endl);
207       const std::vector<std::string>& id 
208         = GetLevelDescriptor().GetIdentifierList();
209       std::vector<std::string>::const_iterator i;
210       for (i = id.begin(); i != id.end(); ++i)
211         {
212           if (mAttributeMap.find(*i)->second != m.find(*i)->second ) 
213             {
214               GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values do not match"<<std::endl);
215               return false;
216             }
217           GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values match"<<std::endl);
218         }
219       return true;
220     }
221     //=============================================================
222     
223     //=============================================================
224     void Node::Print() const
225     {
226       std::string mess;
227       for (int i = 0; i<GetLevel(); ++i) mess += "  ";
228       mess += "|_ " + GetLabel();
229       GimmickMessage(1,mess<<std::endl);
230       ChildrenListType::const_iterator j;
231       for (j=GetChildrenList().begin(); j!=GetChildrenList().end(); j++)
232         {
233           (*j)->Print();
234         } 
235     }
236     //=============================================================
237
238     //=============================================================
239     std::string Node::GetLabel() const
240     {
241       std::string l;
242       const std::vector<std::string>& label 
243         = GetLevelDescriptor().GetLabelList();
244       
245       std::vector<std::string>::const_iterator i;
246       for (i = label.begin(); i != label.end(); )
247         {
248           GimmickDebugMessage(9,"LABEL '"<<*i<<"'"<<std::endl);
249           AttributeMapType::const_iterator j = mAttributeMap.find(*i);
250           if (j != mAttributeMap.end())
251             {
252               l += j->second;
253               ++i;
254               if (i != label.end()) l += "|";
255             }
256           else 
257             {
258               GimmickError("Node::GetLabel() : LABEL attribute '"
259                            <<*i
260                            <<"' is not in node attribute map (should be!)" );
261             }
262         }
263       if (l.size()==0) l="?";
264       return l;
265     }
266     //=============================================================
267
268   }
269
270 }
271