creaImageIOTreeNode.cpp

Go to the documentation of this file.
00001 #include <creaImageIOTreeNode.h>
00002 #include <creaImageIOTree.h>
00003 #include <creaImageIOSystem.h>
00004 #include <algorithm>
00005 #include <creaImageIOGimmick.h>
00006 
00007 namespace creaImageIO
00008 {
00009   namespace tree
00010   {
00011 
00012     //=============================================================
00014     Node::Node(Node* parent)
00015       : mParent(parent),//mData(0),
00016         mChildrenLoaded(false)
00017     {
00018         mData.reset();
00019       if (parent) 
00020         {
00021           GimmickDebugMessage(6,"Default Node constructor (level "<<GetLevel()<<")"
00022                          << std::endl);
00023           // Insert into parent's children list
00024           InitializeAttributeMap();
00025          parent->GetChildrenList().push_back(this);
00026         }
00027       else
00028         {
00029           GimmickDebugMessage(6,"Default Node constructor without parent"       
00030                          << std::endl);
00031         }
00032     }
00033     //=============================================================
00034 
00035     //=============================================================
00037     Node::Node(Node* parent, const AttributeMapType& attr)
00038      : mParent(parent),//mData(0),
00039         mChildrenLoaded(false)
00040     {
00041         mData.reset();
00042       GimmickDebugMessage(6,"Node constructor (level "<<GetLevel()<<")"
00043                      << std::endl);
00044 
00045      if (parent) 
00046         {
00047           // Insert into parent's children list
00048           parent->GetChildrenList().push_back(this);
00049           // Initialize attributes
00050           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
00051           for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
00052                a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
00053                ++a)
00054             {
00055               std::string v;
00056               AttributeMapType::const_iterator i = attr.find(a->GetKey());
00057               if ( i != attr.end() )  
00058                 {
00059                   v = i->second;
00060                 }
00061               GimmickDebugMessage(6,"Setting attribute '"<<a->GetName()<<"' = '"
00062                              <<v<<"'"<<std::endl);
00063               UnsafeSetAttribute( a->GetKey(), v );
00064             }
00065         }
00066       
00067     }
00068     //=============================================================
00069 
00070 
00071     //=============================================================
00072     Node::~Node()
00073     {
00074       GimmickDebugMessage(6,"Node destructor"
00075                      << std::endl);
00076       ChildrenListType::iterator i;
00077       for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
00078         {
00079           delete *i;
00080         }
00081         mData.reset();
00082     }
00083     //=============================================================
00084 
00085 
00086     //=============================================================
00088     void Node::InitializeAttributeMap()
00089     {
00090       // Initialize attributes
00091       LevelDescriptor::AttributeDescriptorListType::const_iterator a;
00092       for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
00093            a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
00094            ++a)
00095         {
00096           UnsafeSetAttribute( a->GetKey(), "" );
00097         }
00098     }
00099     //=============================================================
00100 
00101     //=============================================================
00103     const LevelDescriptor& Node::GetLevelDescriptor() const
00104     { 
00105       return GetTree()->GetLevelDescriptor(GetLevel()); 
00106     }
00107 
00108         //=============================================================
00109 
00110     //=============================================================
00112         const AttributeDescriptor& Node::GetAttributeDescriptor(const std::string& k)const
00113         {
00114                 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
00115       for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
00116            a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
00117            ++a)
00118                 {
00119                 
00120                         if(a->GetKey()==k)
00121                         {
00122                                 return *a;
00123                         }
00124                 
00125                 }
00126                 return *a;
00127         }
00128     //=============================================================
00129 
00130     //=============================================================
00131     int Node::RemoveChildrenFromList(Node* node)
00132     {
00133       ChildrenListType::iterator i = find(GetChildrenList().begin(),
00134                                           GetChildrenList().end(),
00135                                           node);
00136       if (i != GetChildrenList().end())
00137         {
00138           GetChildrenList().erase(i);
00139         }
00140         return GetChildrenList().size();
00141     }
00142     //=============================================================
00143 
00144     //=============================================================
00145     const std::string& Node::GetAttribute(const std::string& k) const
00146     {
00147       //    std::cout << "this = "<<(void*)this<<std::endl;
00148       //    std::cout << "mFieldValueMap="<<(void*)(&mFieldValueMap)<<std::endl;
00149       AttributeMapType::const_iterator i = mAttributeMap.find(k);
00150       if (i == mAttributeMap.end())
00151         {
00152           static std::string def("");
00153           return def;
00154           //    CREAIMAGEIO_ERROR("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
00155         }
00156       return i->second;
00157   }
00158   //=============================================================
00159 
00160   //=============================================================
00161     void Node::SetAttribute(const std::string& k, 
00162                                  const std::string& v)
00163   {
00164     AttributeMapType::iterator i = mAttributeMap.find(k);
00165     if (i==mAttributeMap.end())
00166       {
00167         std::cout<<"[Gimmick!] Node::SetAttribute : no attribute with key '"
00168                  <<k<<"'"<<std::endl;
00169         creaError( "[Gimmick!] Node::SetAttribute : no attribute with key '"
00170                    <<k<<"'");
00171       }
00172     i->second = v;
00173   }
00174     //=============================================================
00175 
00176     //=============================================================
00177     bool Node::Matches(  const AttributeMapType& m ) const
00178     {
00179       GimmickDebugMessage(2,"'"<<GetLabel()<<"' matching..."<<std::endl);
00180       const std::vector<std::string>& id 
00181         = GetLevelDescriptor().GetIdentifierList();
00182       std::vector<std::string>::const_iterator i;
00183       for (i = id.begin(); i != id.end(); ++i)
00184         {
00185           if (mAttributeMap.find(*i)->second != m.find(*i)->second ) 
00186             {
00187               GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values do not match"<<std::endl);
00188               return false;
00189             }
00190           GimmickDebugMessage(2,"IDENTIFIER '"<<*i<<"' values match"<<std::endl);
00191         }
00192       return true;
00193     }
00194     //=============================================================
00195     
00196     //=============================================================
00197     void Node::Print() const
00198     {
00199       std::string mess;
00200       for (int i = 0; i<GetLevel(); ++i) mess += "  ";
00201       mess += "|_ " + GetLabel();
00202       GimmickMessage(1,mess<<std::endl);
00203       ChildrenListType::const_iterator j;
00204       for (j=GetChildrenList().begin(); j!=GetChildrenList().end(); j++)
00205         {
00206           (*j)->Print();
00207         } 
00208     }
00209     //=============================================================
00210 
00211     //=============================================================
00212     std::string Node::GetLabel() const
00213     {
00214       std::string l;
00215       const std::vector<std::string>& label 
00216         = GetLevelDescriptor().GetLabelList();
00217       
00218       std::vector<std::string>::const_iterator i;
00219       for (i = label.begin(); i != label.end(); )
00220         {
00221           GimmickDebugMessage(9,"LABEL '"<<*i<<"'"<<std::endl);
00222           AttributeMapType::const_iterator j = mAttributeMap.find(*i);
00223           if (j != mAttributeMap.end())
00224             {
00225               l += j->second;
00226               ++i;
00227               if (i != label.end()) l += "|";
00228             }
00229           else 
00230             {
00231               GimmickError("Node::GetLabel() : LABEL attribute '"
00232                            <<*i
00233                            <<"' is not in node attribute map (should be!)" );
00234             }
00235         }
00236       if (l.size()==0) l="?";
00237       return l;
00238     }
00239     //=============================================================
00240 
00241   }
00242 
00243 }
00244