1 #include <creaImageIOTreeNode.h>
2 #include <creaImageIOTree.h>
3 #include <creaImageIOSystem.h>
5 #include <creaImageIOGimmick.h>
14 //=============================================================
16 Node::Node(Node* parent)
17 : mParent(parent),//mData(0),
18 mChildrenLoaded(false)
23 GimmickDebugMessage(6,"Default Node constructor (level "<<GetLevel()<<")"
25 // Insert into parent's children list
26 InitializeAttributeMap();
27 parent->GetChildrenList().push_back(this);
31 GimmickDebugMessage(6,"Default Node constructor without parent"
35 //=============================================================
37 //=============================================================
38 /// Ctor with parent and attributes map
39 Node::Node(Node* parent, const AttributeMapType& attr)
40 : mParent(parent),//mData(0),
41 mChildrenLoaded(false)
44 GimmickDebugMessage(6,"Node constructor (level "<<GetLevel()<<")"
49 // Insert into parent's children list
50 parent->GetChildrenList().push_back(this);
51 // Initialize attributes
52 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
53 for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
54 a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
58 AttributeMapType::const_iterator i = attr.find(a->GetKey());
59 if ( i != attr.end() )
63 GimmickDebugMessage(6,"Setting attribute '"<<a->GetName()<<"' = '"
65 UnsafeSetAttribute( a->GetKey(), v );
70 //=============================================================
73 //=============================================================
76 GimmickDebugMessage(6,"Node destructor"
78 ChildrenListType::iterator i;
79 for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
85 //=============================================================
88 //=============================================================
89 /// Initializes the attribute map i.e. creates the entries
90 void Node::InitializeAttributeMap()
92 // Initialize attributes
93 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
94 for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
95 a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
98 UnsafeSetAttribute( a->GetKey(), "" );
101 //=============================================================
103 //=============================================================
104 /// Returns the level descriptor of the node
105 const LevelDescriptor& Node::GetLevelDescriptor() const
107 return GetTree()->GetLevelDescriptor(GetLevel());
110 //=============================================================
112 //=============================================================
113 /// Returns the attribute descriptor of the passed parameter
114 const AttributeDescriptor& Node::GetAttributeDescriptor(const std::string& k)const
116 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
117 for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
118 a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
130 //=============================================================
132 //=============================================================
133 int Node::RemoveChildrenFromList(Node* node)
135 ChildrenListType::iterator i = find(GetChildrenList().begin(),
136 GetChildrenList().end(),
138 if (i != GetChildrenList().end())
140 GetChildrenList().erase(i);
142 return GetChildrenList().size();
144 //=============================================================
146 //=============================================================
147 const std::string& Node::GetAttribute(const std::string& k) const
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())
154 static std::string def("");
156 // CREAIMAGEIO_ERROR("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
160 //=============================================================
162 //=============================================================
163 void Node::SetAttribute(const std::string& k,
164 const std::string& v)
166 AttributeMapType::iterator i = mAttributeMap.find(k);
167 if (i==mAttributeMap.end())
169 std::cout<<"[Gimmick!] Node::SetAttribute : no attribute with key '"
171 creaError( "[Gimmick!] Node::SetAttribute : no attribute with key '"
176 //=============================================================
178 //=============================================================
179 bool Node::Matches( const AttributeMapType& m ) const
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)
187 if (mAttributeMap.find(*i)->second != m.find(*i)->second )
189 GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values do not match"<<std::endl);
192 GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values match"<<std::endl);
196 //=============================================================
198 //=============================================================
199 void Node::Print() const
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++)
211 //=============================================================
213 //=============================================================
214 std::string Node::GetLabel() const
217 const std::vector<std::string>& label
218 = GetLevelDescriptor().GetLabelList();
220 std::vector<std::string>::const_iterator i;
221 for (i = label.begin(); i != label.end(); )
223 GimmickDebugMessage(9,"LABEL '"<<*i<<"'"<<std::endl);
224 AttributeMapType::const_iterator j = mAttributeMap.find(*i);
225 if (j != mAttributeMap.end())
229 if (i != label.end()) l += "|";
233 GimmickError("Node::GetLabel() : LABEL attribute '"
235 <<"' is not in node attribute map (should be!)" );
238 if (l.size()==0) l="?";
241 //=============================================================