]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOTreeNode.cpp
No newline at end of file
[creaImageIO.git] / src2 / creaImageIOTreeNode.cpp
index 8f03924236e30d7e0dd2b824d9d17e4a288028d2..a8cf7929dc6a04e589c0cf228eb3486cbc90a8c3 100644 (file)
@@ -1,6 +1,6 @@
 #include <creaImageIOTreeNode.h>
 #include <creaImageIOTree.h>
-#include <creaMessageManager.h>
+#include <creaImageIOSystem.h>
 #include <algorithm>
 
 namespace creaImageIO
@@ -16,6 +16,31 @@ namespace creaImageIO
        mChildrenLoaded(false)
     {
       if (parent) 
+       {
+         GimmickDebugMessage(6,"Default Node constructor (level "<<GetLevel()<<")"
+                        << std::endl);
+         // Insert into parent's children list
+         InitializeAttributeMap();
+        parent->GetChildrenList().push_back(this);
+       }
+      else
+       {
+         GimmickDebugMessage(6,"Default Node constructor without parent"       
+                        << std::endl);
+       }
+    }
+    //=============================================================
+
+    //=============================================================
+    /// Ctor with parent and attributes map 
+    Node::Node(Node* parent, const AttributeMapType& attr)
+     : mParent(parent),
+       mData(0),
+       mChildrenLoaded(false)
+    {
+      GimmickDebugMessage(6,"Node constructor (level "<<GetLevel()<<")"
+                    << std::endl);
+     if (parent) 
        {
          // Insert into parent's children list
          parent->GetChildrenList().push_back(this);
@@ -25,17 +50,27 @@ namespace creaImageIO
               a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
               ++a)
            {
-             UnsafeSetAttribute( a->GetName(), "" );
+             std::string v;
+             AttributeMapType::const_iterator i = attr.find(a->GetKey());
+             if ( i != attr.end() )  
+               {
+                 v = i->second;
+               }
+             GimmickDebugMessage(6,"Setting attribute '"<<a->GetName()<<"' = '"
+                            <<v<<"'"<<std::endl);
+             UnsafeSetAttribute( a->GetKey(), v );
            }
        }
+      
     }
     //=============================================================
 
 
-
     //=============================================================
     Node::~Node()
     {
+      GimmickDebugMessage(6,"Node destructor"
+                    << std::endl);
       ChildrenListType::iterator i;
       for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
        {
@@ -49,8 +84,53 @@ namespace creaImageIO
     }
     //=============================================================
 
-  //=============================================================
-    void Node::RemoveChildrenFromList(Node* node)
+
+    //=============================================================
+    /// Initializes the attribute map i.e. creates the entries
+    void Node::InitializeAttributeMap()
+    {
+      // Initialize attributes
+      LevelDescriptor::AttributeDescriptorListType::const_iterator a;
+      for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
+          a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
+          ++a)
+       {
+         UnsafeSetAttribute( a->GetKey(), "" );
+       }
+    }
+    //=============================================================
+
+    //=============================================================
+    /// Returns the level descriptor of the node
+    const LevelDescriptor& Node::GetLevelDescriptor() const
+    { 
+      return GetTree()->GetLevelDescriptor(GetLevel()); 
+    }
+
+       //=============================================================
+
+    //=============================================================
+    /// Returns the attribute descriptor of the passed parameter
+       const AttributeDescriptor& Node::GetAttributeDescriptor(const std::string& k)const
+       {
+               LevelDescriptor::AttributeDescriptorListType::const_iterator a;
+      for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
+          a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
+          ++a)
+               {
+               
+                       if(a->GetKey()==k)
+                       {
+                               return *a;
+                       }
+               
+               }
+               return *a;
+       }
+    //=============================================================
+
+    //=============================================================
+    int Node::RemoveChildrenFromList(Node* node)
     {
       ChildrenListType::iterator i = find(GetChildrenList().begin(),
                                          GetChildrenList().end(),
@@ -59,6 +139,7 @@ namespace creaImageIO
        {
          GetChildrenList().erase(i);
        }
+       return GetChildrenList().size();
     }
     //=============================================================
 
@@ -85,15 +166,79 @@ namespace creaImageIO
     AttributeMapType::iterator i = mAttributeMap.find(k);
     if (i==mAttributeMap.end())
       {
-       std::cout<<"[Gimmick!] Node::SetAttribute : no attribute with name '"
+       std::cout<<"[Gimmick!] Node::SetAttribute : no attribute with key '"
                 <<k<<"'"<<std::endl;
-       creaError( "[Gimmick!] Node::SetAttribute : no attribute with name '"
+       creaError( "[Gimmick!] Node::SetAttribute : no attribute with key '"
                   <<k<<"'");
       }
     i->second = v;
   }
-  //=============================================================
+    //=============================================================
 
+    //=============================================================
+    bool Node::Matches(  const AttributeMapType& m ) const
+    {
+      GimmickDebugMessage(2,"'"<<GetLabel()<<"' matching..."<<std::endl);
+      const std::vector<std::string>& id 
+       = GetLevelDescriptor().GetIdentifierList();
+      std::vector<std::string>::const_iterator i;
+      for (i = id.begin(); i != id.end(); ++i)
+       {
+         if (mAttributeMap.find(*i)->second != m.find(*i)->second ) 
+           {
+             GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values do not match"<<std::endl);
+             return false;
+           }
+         GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values match"<<std::endl);
+       }
+      return true;
+    }
+    //=============================================================
+    
+    //=============================================================
+    void Node::Print() const
+    {
+      std::string mess;
+      for (int i = 0; i<GetLevel(); ++i) mess += "  ";
+      mess += "|_ " + GetLabel();
+      GimmickMessage(1,mess<<std::endl);
+      ChildrenListType::const_iterator j;
+      for (j=GetChildrenList().begin(); j!=GetChildrenList().end(); j++)
+       {
+         (*j)->Print();
+       } 
+    }
+    //=============================================================
+
+    //=============================================================
+    std::string Node::GetLabel() const
+    {
+      std::string l;
+      const std::vector<std::string>& label 
+       = GetLevelDescriptor().GetLabelList();
+      
+      std::vector<std::string>::const_iterator i;
+      for (i = label.begin(); i != label.end(); )
+       {
+         GimmickDebugMessage(9,"LABEL '"<<*i<<"'"<<std::endl);
+         AttributeMapType::const_iterator j = mAttributeMap.find(*i);
+         if (j != mAttributeMap.end())
+           {
+             l += j->second;
+             ++i;
+             if (i != label.end()) l += "|";
+           }
+         else 
+           {
+             GimmickError("Node::GetLabel() : LABEL attribute '"
+                          <<*i
+                          <<"' is not in node attribute map (should be!)" );
+           }
+       }
+      if (l.size()==0) l="?";
+      return l;
+    }
+    //=============================================================
 
   }