NIREP
|
00001 #include "NIREPResourceDatabase.h" 00002 00003 vtkStandardNewMacro(NIREPResourceDatabase); 00004 00005 NIREPResourceDatabase::NIREPResourceDatabase(void) 00006 { 00007 cron = 0; 00008 00009 //tags 00010 tag.push_back(UpToLow(std::string("database"))); 00011 tag.push_back(UpToLow(std::string("subject"))); 00012 00013 00014 //define available types 00015 typelist.push_back(UpToLow(std::string("spatialdata"))); 00016 //typelist.push_back(UpToLow(std::string("image"))); 00017 //typelist.push_back(UpToLow(std::string("objMap"))); 00018 //typelist.push_back(UpToLow(std::string("contour"))); 00019 //typelist.push_back(UpToLow(std::string("landmark"))); 00020 //typelist.push_back(UpToLow(std::string("surface"))); 00021 } 00022 00023 00024 NIREPResourceDatabase::~NIREPResourceDatabase(void) 00025 { 00026 /* Nothing to do! */ 00027 } 00028 00029 00030 //Programmer: 00031 //Date: 00032 //Function: 00033 //Copyright: University of Iowa 00034 //Inputs: 00035 //Outputs: 00036 void NIREPResourceDatabase::StartElement(const char* name, const char** atts) 00037 { 00038 if(!history.empty()) { 00039 if(cron == 0) return; 00040 this->previous = this->history[cron-1]; 00041 00042 // NOT SURE IF I UNDERSTAND THIS LINE - Paul // 00043 previous = (previous); 00045 } 00046 //dbresource, level 1 00047 if(!tag[0].compare(UpToLow(name))) { 00048 Resource item; 00049 this->resource.push_back(item); 00050 int i = 0; 00051 while(atts[i]) { 00052 this->resource.end()[-1].attribute[std::string(atts[i])] = 00053 std::string(atts[i+1]); 00054 i = i + 2; 00055 } 00056 } 00057 //dataset, level 2 00058 if(!tag[0].compare(previous) && !tag[1].compare(name)) { 00059 Dataset item; 00060 this->resource.end()[-1].dataset.push_back(item); 00061 int i = 0; 00062 while(atts[i]) { 00063 this->resource.end()[-1].dataset.end()[-1].attribute[ 00064 std::string(atts[i])] = std::string(atts[i+1]); 00065 i = i + 2; 00066 } 00067 } 00068 //defined type, level 3 00069 if(!tag[1].compare(previous) && IsDefinedType(name)) { 00070 if(this->resource.empty()) return; 00071 std::map<std::string, std::string> item; 00072 this->resource.end()[-1].dataset.end()[-1].entry.push_back(item); 00073 int i = 0; 00074 while(atts[i]) { 00075 std::map<std::string, std::string>* iter = 00076 &this->resource.end()[-1].dataset.end()[-1].entry.end()[-1]; 00077 (*iter)[atts[i]] = std::string(atts[i+1]); 00078 i = i + 2; 00079 } 00080 std::map<std::string, std::string>* iter = 00081 &this->resource.end()[-1].dataset.end()[-1].entry.end()[-1]; 00082 (*iter)["content::type"] = name; 00083 } 00084 00085 this->current = std::string(UpToLow(name)); 00086 this->history.push_back(std::string(UpToLow(name))); 00087 this->cron++; 00088 } 00089 00090 00091 //Programmer: 00092 //Date: 00093 //Function: 00094 //Copyright: University of Iowa 00095 //Inputs: 00096 //Outputs: 00097 void NIREPResourceDatabase::EndElement(const char* name) 00098 { 00099 if(this->history.empty()) return; 00100 this->history.pop_back(); 00101 this->cron--; 00102 } 00103 00104 00105 //Programmer: 00106 //Date: 00107 //Function: 00108 //Copyright: University of Iowa 00109 //Inputs: 00110 //Outputs: 00111 void NIREPResourceDatabase::CharacterDataHandler(const char* data, int length) 00112 { 00113 if(length <= 1) return; 00114 if(this->current.empty()) return; 00115 if(data == NULL) return; 00116 if(!this->DataIntegrityChecker(data)) return; 00117 if(this->resource.empty()) return; 00118 00119 if(!tag[0].compare(previous) && tag[1].compare(current)) { 00120 this->resource.end()[-1].attribute[current] = std::string(data, length); 00121 } 00122 if(!tag[1].compare(previous) && !IsDefinedType(current)) { 00123 this->resource.end()[-1].dataset.end()[-1].attribute[current] = 00124 std::string(data, length); 00125 } 00126 00127 if(IsDefinedType(previous)) { 00128 std::map<std::string, std::string>* iter = 00129 &this->resource.end()[-1].dataset.end()[-1].entry.end()[-1]; 00130 (*iter)[current] = std::string(data, length); 00131 } 00132 } 00133 00134 00135 //Programmer: 00136 //Date: 00137 //Function: 00138 //Copyright: University of Iowa 00139 //Inputs: 00140 //Outputs: 00141 bool NIREPResourceDatabase::IsDefinedType(std::string type) 00142 { 00143 for(unsigned int i=0; i<typelist.size(); i++) { 00144 if(!typelist[i].compare(UpToLow(type))) { 00145 return true; 00146 } 00147 } 00148 return false; 00149 } 00150 00151 00152 //Programmer: 00153 //Date: 00154 //Function: 00155 //Copyright: University of Iowa 00156 //Inputs: 00157 //Outputs: 00158 std::string NIREPResourceDatabase::ParseThisString(std::string str, 00159 const char* delimiter, short partition) 00160 { 00161 size_t loc = str.find(delimiter); 00162 std::string ret; 00163 00164 switch(partition) { 00165 case LEFT: 00166 ret = str.substr(0, loc); 00167 break; 00168 case RIGHT: 00169 ret = str.substr(loc+1); 00170 break; 00171 default: 00172 ret = std::string("error"); 00173 break; 00174 } 00175 return ret; 00176 } 00177 00178 00179 //Programmer: 00180 //Date: 00181 //Function: 00182 //Copyright: University of Iowa 00183 //Inputs: 00184 //Outputs: 00185 std::string NIREPResourceDatabase::QueryFilename(std::string str) 00186 { 00187 std::string ret = std::string("empty"); 00188 00189 std::string lstr = ParseThisString(str, ":", LEFT); 00190 std::string rstr = ParseThisString(str, ":", RIGHT); 00191 std::string mstr = ParseThisString(rstr, "-", LEFT); 00192 rstr = ParseThisString(rstr, "-", RIGHT); 00193 00194 bool empty = false; 00195 00196 Resource res = SearchResource(lstr, empty); 00197 if(empty) return ret; 00198 Dataset ds = SearchDataset(rstr, res, empty); 00199 if(empty) return ret; 00200 std::map<std::string, std::string> em = SearchEntry(mstr, ds, empty); 00201 if(empty) return ret; 00202 00203 return em["filename"]; 00204 } 00205 00206 00207 //Programmer: 00208 //Date: 00209 //Function: 00210 //Copyright: University of Iowa 00211 //Inputs: 00212 //Outputs: 00213 std::map<std::string,std::string> NIREPResourceDatabase::QueryList( 00214 std::string str) 00215 { 00216 std::map<std::string,std::string> ret; 00217 ret[std::string("empty")] = std::string("empty"); 00218 00219 std::string lstr = ParseThisString(str, ":", LEFT); 00220 std::string rstr = ParseThisString(str, ":", RIGHT); 00221 std::string mstr = ParseThisString(rstr, "-", LEFT); 00222 rstr = ParseThisString(rstr, "-", RIGHT); 00223 00224 bool empty = false; 00225 00226 Resource res = SearchResource(lstr, empty); 00227 if(empty) return ret; 00228 ret = res.attribute; 00229 Dataset ds = SearchDataset(rstr, res, empty); 00230 if(empty) return ret; 00231 ret = ds.attribute; 00232 std::map<std::string, std::string> em = SearchEntry(mstr, ds, empty); 00233 if(empty) return ret; 00234 ret = em; 00235 00236 return ret; 00237 } 00238 00239 00240 //Programmer: 00241 //Date: 00242 //Function: 00243 //Copyright: University of Iowa 00244 //Inputs: 00245 //Outputs: 00246 Resource& NIREPResourceDatabase::SearchResource(std::string str, bool& empty) 00247 { 00248 for(unsigned int i=0; i<resource.size(); i++) { 00249 if(!resource[i].attribute["id"].compare(str)) { 00250 return resource[i]; 00251 } 00252 } 00253 empty = true; 00254 return resource[0]; 00255 } 00256 00257 00258 //Programmer: 00259 //Date: 00260 //Function: 00261 //Copyright: University of Iowa 00262 //Inputs: 00263 //Outputs: 00264 Dataset& NIREPResourceDatabase::SearchDataset(std::string str, 00265 Resource& resource, bool& empty) 00266 { 00267 for(unsigned int i=0; i<resource.dataset.size(); i++) { 00268 if(!resource.dataset[i].attribute["id"].compare(str)) { 00269 return resource.dataset[i]; 00270 } 00271 } 00272 empty = true; 00273 return resource.dataset[0]; 00274 } 00275 00276 00277 //Programmer: 00278 //Date: 00279 //Function: 00280 //Copyright: University of Iowa 00281 //Inputs: 00282 //Outputs: 00283 std::map<std::string,std::string>& NIREPResourceDatabase::SearchEntry( 00284 std::string str, Dataset& dataset, bool& empty) 00285 { 00286 for(unsigned int i=0; i<dataset.entry.size(); i++) { 00287 std::map<std::string, std::string>* iter = &dataset.entry[i]; 00288 if(!(*iter)["id"].compare(str)) { 00289 return dataset.entry[i]; 00290 } 00291 } 00292 empty = true; 00293 return dataset.entry[0]; 00294 } 00295 00296 00297 //Programmer: 00298 //Date: 00299 //Function: 00300 //Copyright: University of Iowa 00301 //Inputs: 00302 //Outputs: 00303 std::vector<std::string> NIREPResourceDatabase::GetTags() 00304 { 00305 return this->tag; 00306 } 00307 00308 00309 //Programmer: 00310 //Date: 00311 //Function: 00312 //Copyright: University of Iowa 00313 //Inputs: 00314 //Outputs: 00315 std::vector<std::string> NIREPResourceDatabase::GetTypeList() 00316 { 00317 return this->typelist; 00318 }