00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Learn/SVMObjectDB.H"
00039
00040 #ifdef HAVE_LIBXML
00041
00042 void SVMObjectDB::getNodeContent(xmlDocPtr doc, xmlNodePtr nodePtr,
00043 const char* nodeName, std::string &result)
00044 {
00045 xmlChar *tmp = NULL;
00046 if (!xmlStrcmp(nodePtr->name, (const xmlChar *)nodeName))
00047 tmp = xmlNodeGetContent(nodePtr);
00048
00049
00050 if (tmp != NULL) {
00051 result = std::string((const char*)tmp);
00052 xmlFree(tmp);
00053 }
00054 }
00055
00056
00057
00058 void SVMObjectDB::loadObjDB(const std::string& filename)
00059 {
00060 xmlDocPtr doc;
00061
00062
00063 LINFO("Reading xml formatted object db file: %s", filename.c_str());
00064
00065
00066 doc = xmlReadFile(filename.c_str(), NULL, 0);
00067 if (doc == NULL) {
00068 LINFO("Failed to parse %s", filename.c_str());
00069 return;
00070 }
00071
00072
00073 xmlNode *root_element = xmlDocGetRootElement(doc);
00074
00075
00076 xmlNodePtr cur = root_element->xmlChildrenNode;
00077 while (cur != NULL) {
00078
00079
00080 if ((!xmlStrcmp(cur->name, (const xmlChar *)"object"))) {
00081
00082 std::string objId, objName, objDescription;
00083
00084 xmlNodePtr objectPtr = cur->xmlChildrenNode;
00085
00086 while(objectPtr != NULL) {
00087 getNodeContent(doc, objectPtr, "id", objId);
00088 getNodeContent(doc, objectPtr, "name", objName);
00089 getNodeContent(doc, objectPtr, "description", objDescription);
00090
00091 objectPtr = objectPtr->next;
00092 }
00093 SVMObject obj;
00094 obj.id = atoi(objId.c_str());
00095 obj.name = objName;
00096 obj.description = objDescription;
00097 LINFO("Reading in object %s[%s]",objName.c_str(),objId.c_str());
00098 objdb.insert(obj);
00099 }
00100 cur = cur->next;
00101 }
00102
00103 xmlFreeDoc(doc);
00104 xmlCleanupParser();
00105 }
00106
00107 void SVMObjectDB::writeObjDB(const std::string& filename)
00108 {
00109 xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
00110
00111
00112 LINFO("Writing out xml formatted object db file: %s", filename.c_str());
00113
00114
00115
00116
00117 xmlNode *root_element = xmlNewNode(0, (xmlChar *)"objectDatabase");
00118 xmlDocSetRootElement(doc,root_element);
00119 char idbuf[50];
00120 std::set<SVMObject>::iterator it;
00121 for(it=objdb.begin();it!=objdb.end();it++) {
00122 xmlNode *objXML = xmlNewNode(0, (xmlChar *)"object");
00123 xmlAddChild(root_element,objXML);
00124 SVMObject objRef = (SVMObject) *it;
00125 LINFO("Writing out object %d[%s]\n",objRef.id,objRef.name.c_str());
00126 sprintf(idbuf,"%d",objRef.id);
00127 xmlNewTextChild(objXML,NULL,(xmlChar *)"id",(xmlChar *) idbuf);
00128 xmlNewTextChild(objXML,NULL,(xmlChar *)"name",(xmlChar *) objRef.name.c_str());
00129 xmlNewTextChild(objXML,NULL,(xmlChar *)"description",(xmlChar *) objRef.description.c_str());
00130
00131
00132
00133 }
00134
00135 xmlSaveFileEnc(filename.c_str(),doc,"UTF-8");
00136 xmlFreeDoc(doc);
00137 xmlCleanupParser();
00138
00139 }
00140
00141 SVMObject SVMObjectDB::updateObject(const int id, const std::string& name)
00142 {
00143 SVMObject h;
00144
00145 if(id == -1) {
00146 if(name.compare("")==0)
00147 LFATAL("When updating an object, the name must be defined");
00148 h=getObject(name);
00149 }
00150 else {
00151 h=getObject(id);
00152 if(!h.initialized() && name.compare("")==0)
00153 LFATAL("New id is given, with no name");
00154 }
00155 if(h.initialized())
00156 return h;
00157 else
00158 return newObject(id,name);
00159 }
00160
00161 SVMObject SVMObjectDB::newObject(const int id, const std::string& name)
00162 {
00163 SVMObject h;
00164 int maxId=0;
00165 h.name = name;
00166 std::set<SVMObject>::iterator it;
00167 if(id == -1){
00168 SVMObject tmp;
00169 for(it=objdb.begin();it!=objdb.end();it++){
00170 tmp = (SVMObject) *it;
00171 if(tmp.id > maxId)
00172 maxId = tmp.id;
00173 }
00174 h.id = maxId+1;
00175 }
00176 else {
00177 h.id = id;
00178 }
00179 objdb.insert(h);
00180 return h;
00181 }
00182
00183 SVMObject SVMObjectDB::getObject(const std::string& name)
00184 {
00185 std::set<SVMObject>::iterator it;
00186 SVMObject h;
00187 SVMObject tmp;
00188 for(it=objdb.begin();it!=objdb.end();it++){
00189 tmp = (SVMObject) *it;
00190 if(tmp.name.compare(name)==0)
00191 return tmp;
00192 }
00193 return h;
00194 }
00195
00196 SVMObject SVMObjectDB::getObject(const int id)
00197 {
00198 std::set<SVMObject>::iterator it;
00199 SVMObject h;
00200 SVMObject tmp;
00201 for(it=objdb.begin();it!=objdb.end();it++){
00202 tmp = (SVMObject) *it;
00203 if(tmp.id==id)
00204 return tmp;
00205 }
00206 return h;
00207 }
00208
00209 #else
00210
00211
00212 void SVMObjectDB::getNodeContent(xmlDocPtr doc, xmlNodePtr nodePtr,
00213 const char* nodeName, std::string &result){
00214 LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00215 }
00216
00217 void SVMObjectDB::loadObjDB(const std::string& filename){
00218 LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00219 }
00220
00221 void SVMObjectDB::writeObjDB(const std::string& filename) {
00222 LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00223 }
00224
00225
00226 SVMObject SVMObjectDB::updateObject(const int id, const std::string& name) {
00227 LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00228 return SVMObject() ;
00229 }
00230
00231 SVMObject SVMObjectDB::getObject(const std::string& name) {
00232 LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00233 return SVMObject() ;
00234 }
00235
00236 SVMObject SVMObjectDB::getObject(const int id) {
00237 LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00238 return SVMObject() ;
00239 }
00240
00241
00242 #endif