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