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