NIREP

ResourceDescription.cxx

Go to the documentation of this file.
00001 /***************************************************************************************
00002 *   Programmer: Gary E. Christensen
00003 *   Date: 6/24/2010
00004 *   Name: ResourceDescription
00005 *   Purpose: This class holds information about the resources stored in the database.
00006 *      Each resource must contain a unique resource id and a list of data descriptions
00007 *      that describe the data (images, object maps, contours, landmarks, etc.) associated
00008 *      with the resource.  Optionally, a resource can contain a list of non-required user
00009 *      defined attributes such as age, race, handeness, etc.
00010 ******************************************************************************************/
00011 #include "ResourceDescription.h"
00012 
00015 void ResourceDescription::Clear(){
00016   m_label.clear();
00017   m_dmid.clear();
00018   ClearCSList();
00019 
00020   ClearAlgorithms();
00021   m_format.clear();
00022 
00023   ClearFilenames();
00024   m_transform_units.clear();
00025   m_datatype.clear();
00026 
00027   ClearAttributes(); 
00028 };
00029 
00037 TiXmlElement * ResourceDescription::CreateDOM(const std::string & tag){
00038 
00039         // descrip is the main element to hold the DOM tree for this object
00040         TiXmlElement * descrip = new TiXmlElement( tag.c_str() );
00041 
00042   if (descrip){
00043     // Only save description label if there is one
00044     if (!m_label.empty()){
00045       descrip->SetAttribute("label", m_label.c_str());
00046     }
00047     // Only save dmid if there is one
00048     if (!m_dmid.empty()){
00049       descrip->SetAttribute("dmid", m_dmid.c_str());
00050     }
00051   } else {
00052     return NULL; // Failed to allocate memory
00053   }
00054 
00055   // Add coordinate system descriptions to resource descrip
00056   if (!m_cs.empty()){
00057     CSList::iterator cs_iter;
00058     for (cs_iter=m_cs.begin(); cs_iter != m_cs.end(); cs_iter++) {
00059       const std::string & key= cs_iter->first;
00060       const CoordinateSystem value= cs_iter->second;
00061 
00062       TiXmlElement * cs_tag = new TiXmlElement( "coordinate_system" );
00063 
00064       if (cs_tag){
00065         // Only save index if there is one
00066         if (!value.index.empty()){
00067           cs_tag->SetAttribute("index", value.index.c_str());
00068         }
00069         // Only save namespace if there is one
00070         if (!value.ns.empty()){
00071           cs_tag->SetAttribute("ns", value.ns.c_str());
00072         }
00073         // Coordinate system id is required
00074         if (!value.id.empty()){
00075           cs_tag->SetAttribute("id", value.id.c_str());
00076         } else {
00077           return NULL; // Coordinate system id is missing
00078         }
00079 
00080         // Add coordinate system to description
00081         descrip->LinkEndChild(cs_tag);
00082       } else {
00083         return NULL; // Faild to allocate memory
00084       }
00085     }
00086   }
00087 
00088   // Add algorithm descriptions to resource descrip
00089   if (!m_alg.empty()){
00090     AlgList::iterator alg_iter;
00091     for (alg_iter=m_alg.begin(); alg_iter != m_alg.end(); alg_iter++) {
00092       const std::string & key= alg_iter->first;
00093       const std::string & value= alg_iter->second;
00094 
00095       TiXmlElement * alg_tag = new TiXmlElement( "algorithm" );
00096       TiXmlText * algorithm = new TiXmlText(value.c_str());
00097 
00098       if (alg_tag && algorithm){
00099         // Only save index if there is one
00100         if (!key.empty()){
00101           alg_tag->SetAttribute("index", key.c_str());
00102         }
00103         // Set algorithm
00104         alg_tag->LinkEndChild(algorithm);
00105 
00106         // Add algorithm to description
00107         descrip->LinkEndChild(alg_tag);
00108       } else {
00109         return NULL; // Faild to allocate memory
00110       }
00111     }
00112   }
00113 
00114   // Add transform format to descrip if present
00115   if (!m_format.empty()){
00116     TiXmlElement * format_tag = new TiXmlElement( "format" );
00117     TiXmlText * format = new TiXmlText( m_format );
00118     if (format_tag && format){
00119       format_tag->LinkEndChild(format);
00120       descrip->LinkEndChild(format_tag);
00121     } else {
00122       return NULL; // Failed to allocate memory
00123     }
00124   }
00125 
00126   // Add filename descriptions to resource descrip
00127         FilenameList::iterator fn_iter;
00128         for (fn_iter=m_filename.begin(); fn_iter != m_filename.end(); fn_iter++) {
00129     const std::string & key= fn_iter->first;
00130                 const std::string & value= fn_iter->second;
00131                 
00132     TiXmlElement * fn_tag = new TiXmlElement( "filename" );
00133     TiXmlText * filename = new TiXmlText(value.c_str());
00134 
00135     if (fn_tag && filename){
00136       // Only save index if there is one
00137       if (!key.empty()){
00138         fn_tag->SetAttribute("index", key.c_str());
00139       }
00140 
00141       // Set filename
00142       fn_tag->LinkEndChild(filename);
00143 
00144       // Add filename to description
00145       descrip->LinkEndChild(fn_tag);
00146     } else {
00147       return NULL; // Faild to allocate memory
00148     }
00149         }
00150 
00151   // Add transform units to descrip if present
00152   if (!m_transform_units.empty()){
00153     TiXmlElement * tu_tag = new TiXmlElement( "transformation_units" );
00154     TiXmlText * transform_units = new TiXmlText( m_transform_units );
00155     if (tu_tag && transform_units){
00156       tu_tag->LinkEndChild(transform_units);
00157       descrip->LinkEndChild(tu_tag);
00158     } else {
00159       return NULL; // Failed to allocate memory
00160     }
00161   }
00162 
00163   // Add data type to descrip if present
00164   if (!m_datatype.empty()){
00165     TiXmlElement * dt_tag = new TiXmlElement( "datatype" );
00166     TiXmlText * datatype = new TiXmlText( m_datatype );
00167     if (dt_tag && datatype){
00168       dt_tag->LinkEndChild(datatype);
00169       descrip->LinkEndChild(dt_tag);
00170     } else {
00171       return NULL; // Failed to allocate memory
00172     }
00173   }
00174 
00175 
00176   // Add non-required attributes to descrip
00177         // Cycle through all the attributes and store them in DOM
00178   if (!m_attribute.empty()){
00179     TiXmlElement * attributes = new TiXmlElement( "attributes" );
00180     descrip->LinkEndChild( attributes );
00181 
00182     AttributeList::iterator a_iter;
00183     for (a_iter=m_attribute.begin(); a_iter != m_attribute.end(); a_iter++) {
00184       const std::string & key= a_iter->first;
00185       const std::string & value= a_iter->second;
00186 
00187       TiXmlElement * p_attribute = new TiXmlElement(key.c_str()); 
00188       TiXmlText * p_text = new TiXmlText(value.c_str());
00189 
00190       if (p_attribute && p_text){
00191         p_attribute->LinkEndChild( p_text );  
00192         attributes->LinkEndChild( p_attribute );
00193       } else {
00194         return NULL; // Failed to allocate memory
00195       }
00196     }
00197   }
00198 
00199         return descrip;
00200 }
00201 
00213 bool ResourceDescription::FillUsingDom(TiXmlElement * descrip){
00214   // Return false if called with NULL pointer
00215         if (!descrip){
00216                 return false;
00217         }
00218 
00219   std::string buf;
00220   int readOkay;
00221  
00222   // set label if there is one
00223   readOkay = descrip->QueryStringAttribute("label", &buf);
00224   if (readOkay == TIXML_SUCCESS){
00225     SetLabel(buf);
00226   }
00227 
00228   //set dmid if there is one
00229   readOkay = descrip->QueryStringAttribute("dmid", &buf);
00230   if (readOkay == TIXML_SUCCESS){
00231     SetDMID(buf);
00232   }
00233 
00234         // pElem used to move through children of descrip 
00235         TiXmlElement* pElem; 
00236         TiXmlHandle hRoot(descrip);
00237 
00238   //Go to first attribute child of description and set attributes
00239         //Use hRoot to ensure that pElem is set to NULL if database_attributes is missing
00240         pElem=hRoot.FirstChildElement().Element();
00241         for( pElem; pElem; pElem=pElem->NextSiblingElement()) {
00242                 const std::string pKey = std::string(pElem->Value());
00243                 
00244 
00245     // Create and fill a coordinate system list entry
00246     if (pKey.compare("coordinate_system") == 0){
00247       CoordinateSystem cs;// = new CoordinateSystem;
00248       //if (cs){
00249         readOkay = pElem->QueryStringAttribute("index", &buf);
00250         if (readOkay == TIXML_SUCCESS){
00251           cs.index = buf;
00252         } else {
00253           // Use index "0" if index is missing
00254           cs.index = "0";
00255         }
00256         readOkay = pElem->QueryStringAttribute("ns", &buf);
00257         if (readOkay == TIXML_SUCCESS){
00258           cs.ns = buf;
00259         }
00260         readOkay = pElem->QueryStringAttribute("id", &buf);
00261         if (readOkay == TIXML_SUCCESS){
00262           cs.id = buf;
00263           // Add coordinate system to list using index as key
00264           // Note, must set the index, ns, and id values of cs before adding
00265           AddCS(cs.index,cs);
00266         } else {
00267           return false; // Missing coordinate system id
00268         }
00269       //} else {
00270       //  return false; // Failed to allocate memory
00271       //}
00272     }
00273 
00274     // Add and fill an algorithm list entry
00275     if (pKey.compare("algorithm") == 0){
00276       const std::string pText = std::string(pElem->GetText());
00277       readOkay = pElem->QueryStringAttribute("index", &buf);
00278       if (readOkay == TIXML_SUCCESS){
00279         // Add algorithm to list at given index
00280         AddAlgorithm(buf,pText);
00281       } else {
00282         // Assume index is "0" if missing
00283         AddAlgorithm("0",pText);
00284       }
00285     }
00286 
00287     // Set format
00288     if (pKey.compare("format") == 0){
00289       const std::string pText = std::string(pElem->GetText());
00290       SetFormat(pText);
00291     }
00292 
00293     // Add and fill an filename list entry
00294     if (pKey.compare("filename") == 0){
00295       readOkay = pElem->QueryStringAttribute("index", &buf);
00296       const std::string pText = std::string(pElem->GetText());
00297       if (readOkay == TIXML_SUCCESS){
00298         // Add algorithm to list at given index
00299         AddFilename(buf,pText);
00300       } else {
00301         // Assume index is "0" if missing
00302         AddFilename("0",pText);
00303       }
00304     }
00305 
00306     // Set transformation units
00307     if (pKey.compare("transformation_units") == 0){
00308       const std::string pText = std::string(pElem->GetText());
00309       SetTransformUnits(pText);
00310     }
00311 
00312     // Set data type
00313     if (pKey.compare("datatype") == 0){
00314       const std::string pText = std::string(pElem->GetText());
00315       SetDataType(pText);
00316     }
00317   }
00318 
00319   // Go to first attribute child of description and set attributes
00320   // Using hDescrip ensures that pElem is NULL if resource_attributes tag is missing
00321   pElem=hRoot.FirstChildElement("attributes").FirstChildElement().Element();
00322   for( pElem; pElem; pElem=pElem->NextSiblingElement()) {
00323     const char *pKey=pElem->Value();
00324     const char *pText=pElem->GetText();
00325     if (pKey && pText) {
00326       AddAttribute(pKey,pText);
00327     }
00328   }
00329 
00330   return true;
00331 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines