]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeNode.cpp
add SOURCE_GROUP command to reorganize files in folder for visual studio project
[creaImageIO.git] / src2 / creaImageIOTreeNode.cpp
1 #include <creaImageIOTreeNode.h>
2 #include <creaImageIOTree.h>
3 #include <creaImageIOSystem.h>
4 #include <algorithm>
5
6 namespace creaImageIO
7 {
8   namespace tree
9   {
10
11     //=============================================================
12     /// Ctor with parent
13     Node::Node(Node* parent)
14       : mParent(parent),//mData(0),
15         mChildrenLoaded(false)
16     {
17       if (parent) 
18         {
19           GimmickDebugMessage(6,"Default Node constructor (level "<<GetLevel()<<")"
20                          << std::endl);
21           // Insert into parent's children list
22           InitializeAttributeMap();
23          parent->GetChildrenList().push_back(this);
24         }
25       else
26         {
27           GimmickDebugMessage(6,"Default Node constructor without parent"       
28                          << std::endl);
29         }
30     }
31     //=============================================================
32
33     //=============================================================
34     /// Ctor with parent and attributes map 
35     Node::Node(Node* parent, const AttributeMapType& attr)
36      : mParent(parent),//mData(0),
37         mChildrenLoaded(false)
38     {
39       GimmickDebugMessage(6,"Node constructor (level "<<GetLevel()<<")"
40                      << std::endl);
41
42      if (parent) 
43         {
44           // Insert into parent's children list
45           parent->GetChildrenList().push_back(this);
46           // Initialize attributes
47           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
48           for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
49                a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
50                ++a)
51             {
52               std::string v;
53               AttributeMapType::const_iterator i = attr.find(a->GetKey());
54               if ( i != attr.end() )  
55                 {
56                   v = i->second;
57                 }
58               GimmickDebugMessage(6,"Setting attribute '"<<a->GetName()<<"' = '"
59                              <<v<<"'"<<std::endl);
60               UnsafeSetAttribute( a->GetKey(), v );
61             }
62         }
63       
64     }
65     //=============================================================
66
67
68     //=============================================================
69     Node::~Node()
70     {
71       GimmickDebugMessage(6,"Node destructor"
72                      << std::endl);
73       ChildrenListType::iterator i;
74       for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
75         {
76           delete *i;
77         }
78
79     }
80     //=============================================================
81
82
83     //=============================================================
84     /// Initializes the attribute map i.e. creates the entries
85     void Node::InitializeAttributeMap()
86     {
87       // Initialize attributes
88       LevelDescriptor::AttributeDescriptorListType::const_iterator a;
89       for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
90            a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
91            ++a)
92         {
93           UnsafeSetAttribute( a->GetKey(), "" );
94         }
95     }
96     //=============================================================
97
98     //=============================================================
99     /// Returns the level descriptor of the node
100     const LevelDescriptor& Node::GetLevelDescriptor() const
101     { 
102       return GetTree()->GetLevelDescriptor(GetLevel()); 
103     }
104
105         //=============================================================
106
107     //=============================================================
108     /// Returns the attribute descriptor of the passed parameter
109         const AttributeDescriptor& Node::GetAttributeDescriptor(const std::string& k)const
110         {
111                 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
112       for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
113            a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
114            ++a)
115                 {
116                 
117                         if(a->GetKey()==k)
118                         {
119                                 return *a;
120                         }
121                 
122                 }
123                 return *a;
124         }
125     //=============================================================
126
127     //=============================================================
128     int Node::RemoveChildrenFromList(Node* node)
129     {
130       ChildrenListType::iterator i = find(GetChildrenList().begin(),
131                                           GetChildrenList().end(),
132                                           node);
133       if (i != GetChildrenList().end())
134         {
135           GetChildrenList().erase(i);
136         }
137         return GetChildrenList().size();
138     }
139     //=============================================================
140
141     //=============================================================
142     const std::string& Node::GetAttribute(const std::string& k) const
143     {
144       //    std::cout << "this = "<<(void*)this<<std::endl;
145       //    std::cout << "mFieldValueMap="<<(void*)(&mFieldValueMap)<<std::endl;
146       AttributeMapType::const_iterator i = mAttributeMap.find(k);
147       if (i == mAttributeMap.end())
148         {
149           static std::string def("");
150           return def;
151           //    CREAIMAGEIO_ERROR("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
152         }
153       return i->second;
154   }
155   //=============================================================
156
157   //=============================================================
158     void Node::SetAttribute(const std::string& k, 
159                                  const std::string& v)
160   {
161     AttributeMapType::iterator i = mAttributeMap.find(k);
162     if (i==mAttributeMap.end())
163       {
164         std::cout<<"[Gimmick!] Node::SetAttribute : no attribute with key '"
165                  <<k<<"'"<<std::endl;
166         creaError( "[Gimmick!] Node::SetAttribute : no attribute with key '"
167                    <<k<<"'");
168       }
169     i->second = v;
170   }
171     //=============================================================
172
173     //=============================================================
174     bool Node::Matches(  const AttributeMapType& m ) const
175     {
176       GimmickDebugMessage(2,"'"<<GetLabel()<<"' matching..."<<std::endl);
177       const std::vector<std::string>& id 
178         = GetLevelDescriptor().GetIdentifierList();
179       std::vector<std::string>::const_iterator i;
180       for (i = id.begin(); i != id.end(); ++i)
181         {
182           if (mAttributeMap.find(*i)->second != m.find(*i)->second ) 
183             {
184               GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values do not match"<<std::endl);
185               return false;
186             }
187           GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values match"<<std::endl);
188         }
189       return true;
190     }
191     //=============================================================
192     
193     //=============================================================
194     void Node::Print() const
195     {
196       std::string mess;
197       for (int i = 0; i<GetLevel(); ++i) mess += "  ";
198       mess += "|_ " + GetLabel();
199       GimmickMessage(1,mess<<std::endl);
200       ChildrenListType::const_iterator j;
201       for (j=GetChildrenList().begin(); j!=GetChildrenList().end(); j++)
202         {
203           (*j)->Print();
204         } 
205     }
206     //=============================================================
207
208     //=============================================================
209     std::string Node::GetLabel() const
210     {
211       std::string l;
212       const std::vector<std::string>& label 
213         = GetLevelDescriptor().GetLabelList();
214       
215       std::vector<std::string>::const_iterator i;
216       for (i = label.begin(); i != label.end(); )
217         {
218           GimmickDebugMessage(9,"LABEL '"<<*i<<"'"<<std::endl);
219           AttributeMapType::const_iterator j = mAttributeMap.find(*i);
220           if (j != mAttributeMap.end())
221             {
222               l += j->second;
223               ++i;
224               if (i != label.end()) l += "|";
225             }
226           else 
227             {
228               GimmickError("Node::GetLabel() : LABEL attribute '"
229                            <<*i
230                            <<"' is not in node attribute map (should be!)" );
231             }
232         }
233       if (l.size()==0) l="?";
234       return l;
235     }
236     //=============================================================
237
238   }
239
240 }
241