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