/home/fwph/code/wurde/modules/visionModule/xmlparser.cpp

Go to the documentation of this file.
00001 #include <xercesc/util/PlatformUtils.hpp>
00002 #include <xercesc/dom/DOM.hpp>
00003 #include <xercesc/parsers/XercesDOMParser.hpp>
00004 #include <xercesc/util/XMLChar.hpp>
00005 #include <string>
00006 #include <iostream>
00007 #include <visionModule.H>
00008 #include <Logger.H>
00009 #include <WURDEConfiguration.H>
00010 
00011 // Other include files, declarations, and non-Xerces-C++ initializations.
00012 XERCES_CPP_NAMESPACE_USE 
00013 
00014 using namespace WURDEVision;
00015 using namespace WURDE;
00016 using namespace std;
00017 
00018 namespace WURDEVision {  
00019        
00020        std::vector<CameraConfig> parseCameraFile(std::string config){
00021               vector<CameraConfig> theCameras;
00022               try {
00023                      XMLPlatformUtils::Initialize();
00024               }
00025               catch (const XMLException& toCatch) {           
00026                      cout << "Something went wrong.\n";
00027                      //we should probably do some more failure processing, and generally catch the exceptions
00028                      //that can be thrown by Xerces, but i'm not doing that right now.
00029                      return theCameras;
00030               }
00031               
00032               //this is all startup stuff for Xerces.we may not actually be validating now. should check on that.
00033               static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
00034               DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
00035               DOMBuilder        *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
00036               XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
00037               DOMElement *root;
00038               DOMNode *curr,*name,*child;
00039               DOMNodeList *theNodes,*children;
00040               DOMNamedNodeMap *attr;
00041               
00042               doc = parser->parseURI(config.c_str());
00043               if(!doc){
00044                      g_logfatal << "Config file " << config.c_str() << " not found!"<< endl;
00045                      exit(1);
00046               }
00047 
00048               root=doc->getDocumentElement();
00049               
00050               //now the code
00051               string currentname, temp,currentval;
00052               
00053               //make sure we've got an mcp-config section
00054               temp=XMLString::transcode(root->getTagName());
00055               if(temp!="camera-config"){
00056                      cout << temp << " is not a proper camera-config file.\n";
00057                      exit(1);
00058               }
00059               
00060               //get the module nodes
00061               theNodes=doc->getElementsByTagName(XMLString::transcode("camera"));
00062               
00063               for(unsigned int i=0;i<theNodes->getLength();i++){
00064                      string id;
00065                      CameraConfig currConfig;
00066                      
00067                      curr=theNodes->item(i);
00068                      attr=curr->getAttributes();
00069 
00070                      name=attr->getNamedItem(XMLString::transcode("name"));
00071                      currentname=XMLString::transcode(name->getNodeValue());
00072                      currConfig.name=currentname;
00073 
00074                      name=attr->getNamedItem(XMLString::transcode("type"));
00075                      currentval=XMLString::transcode(name->getNodeValue());
00076                      currConfig.options["type"]=currentval;
00077                      
00078                      children=curr->getChildNodes();
00079                      for(int j=0;children&&j<children->getLength();j++){
00080                             child=children->item(j);
00081                             string tmp=XMLString::transcode(child->getNodeName());
00082                             if(tmp=="calibration"){
00083                                    Calibration calib;
00084                                    attr=child->getAttributes();
00085 
00086                                    name=attr->getNamedItem(XMLString::transcode("centerX"));
00087                                    calib.centerX=strtol(XMLString::transcode(name->getNodeValue()),(char **)NULL,10);
00088 
00089                                    name=attr->getNamedItem(XMLString::transcode("centerY"));
00090                                    calib.centerY=strtol(XMLString::transcode(name->getNodeValue()),(char **)NULL,10);
00091 
00092                                    name=attr->getNamedItem(XMLString::transcode("focalLength"));
00093                                    calib.focalLength=strtod(XMLString::transcode(name->getNodeValue()),(char **)NULL);
00094                             }else if(tmp=="option"){
00095                                     attr=child->getAttributes();
00096                                     
00097                                     name=attr->getNamedItem(XMLString::transcode("name"));
00098                                     currentname=XMLString::transcode(name->getNodeValue());
00099                                     name=attr->getNamedItem(XMLString::transcode("value"));
00100                                     currentval=XMLString::transcode(name->getNodeValue());
00101                                     g_logdebug << "name: " << currentname << " value: " << currentval << endl;
00102                                     currConfig.options[currentname]=currentval;
00103                             }
00104                      }
00105                      
00106                      theCameras.push_back(currConfig);
00107               }
00108               
00109               
00110               XMLPlatformUtils::Terminate();
00111               
00112               return theCameras;
00113        }
00114        
00115        
00116        VisionModuleConfig parseVisionFile(string config,string modName, vector<CameraConfig> &cameras){
00117               
00118               VisionModuleConfig theConfig;
00119 
00120               try {
00121                      XMLPlatformUtils::Initialize();
00122               }
00123               catch (const XMLException& toCatch) {           
00124                      cout << "Something went wrong.\n";
00125                      //we should probably do some more failure processing, and generally catch the exceptions
00126                      //that can be thrown by Xerces, but i'm not doing that right now.
00127                      return theConfig;
00128               }
00129               
00130               //this is all startup stuff for Xerces.we may not actually be validating now. should check on that.
00131               static const XMLCh gLS2[] = { chLatin_L, chLatin_S, chNull };
00132               DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS2);
00133               DOMBuilder        *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
00134 
00135               XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
00136               DOMElement *root;
00137               DOMNode *curr,*name,*child,*item;
00138               DOMNodeList *theNodes,*children,*subchildren;
00139               DOMNamedNodeMap *attr;
00140               
00141               
00142               doc = parser->parseURI(config.c_str());
00143               if(!doc){
00144                      g_logfatal << "Config file " << config.c_str() << " not found!"<< endl;
00145                      exit(1);
00146               }
00147 
00148               root=doc->getDocumentElement();
00149 
00150               //now the code
00151               string currentname, temp;
00152               
00153               //make sure we've got an mcp-mapping section
00154               temp=XMLString::transcode(root->getTagName());
00155               if(temp!="vision-config"){
00156                      cout << config << " is not a proper vision-config file..\n";
00157                      exit(1);
00158               }
00159               
00160               //get the module nodes
00161               theNodes=doc->getElementsByTagName(XMLString::transcode("module"));
00162               
00163               for(unsigned int i=0;i<theNodes->getLength();i++){
00164 
00165                      curr=theNodes->item(i);
00166                      attr=curr->getAttributes();
00167 
00168                      name=attr->getNamedItem(XMLString::transcode("name"));
00169                      currentname=XMLString::transcode(name->getNodeValue());
00170                      
00171                      if(currentname!=modName){
00172                             continue;
00173                      }
00174 
00175                      g_debug("Found my XML section");
00176                      //              children=curr->getElementsByTagName(XMLString::transcode("input"))
00177                      children=curr->getChildNodes();
00178                      
00179                      for(unsigned int j=0;j<children->getLength();j++){
00180                             string tmpstring;
00181 
00182                             child=children->item(j);            
00183                             temp=XMLString::transcode(child->getNodeName());     
00184 
00185                             if(temp=="camera"){
00186                                    bool found=false;
00187                                    attr=child->getAttributes();
00188                                    item=attr->getNamedItem(XMLString::transcode("name"));
00189                                    tmpstring=XMLString::transcode(item->getNodeValue());
00190 
00191                                    vector<CameraConfig>::iterator iter;
00192                                    g_logdebug << "Need to find camera with name " << tmpstring << endl;
00193                                    for(iter=cameras.begin(); iter!=cameras.end();iter++){
00194                                           if((*iter).name==tmpstring){
00195                                                  g_debug("Found a camera for the vision config");
00196                                                  found=true;
00197                                                  theConfig.cameras.push_back(*iter);
00198                                           }
00199                                    }
00200                                    if(!found){
00201                                           g_error("Couldn't find the camera for vision config...");
00202                                    }
00203                             }else if(temp=="operator"){
00204                                    OperatorConfig theoperator;
00205                                    attr=child->getAttributes();
00206                                    item=attr->getNamedItem(XMLString::transcode("name"));
00207                                    theoperator.name=XMLString::transcode(item->getNodeValue());
00208 
00209                                    item=attr->getNamedItem(XMLString::transcode("type"));
00210 
00211                                    theoperator.type=XMLString::transcode(item->getNodeValue());
00212 
00213                                    subchildren=child->getChildNodes();
00214 
00215                                    for(unsigned int k=0;k<subchildren->getLength();k++){
00216 
00217                                           child=subchildren->item(k);
00218                                           temp=XMLString::transcode(child->getNodeName());
00219 
00220                                           if(temp=="option"){
00221                                                  string opstringname, opstringdata;
00222                                                  attr=child->getAttributes();
00223                                                  item=attr->getNamedItem(XMLString::transcode("name"));
00224                                                  opstringname=XMLString::transcode(item->getNodeValue());
00225 
00226                                                  item=attr->getNamedItem(XMLString::transcode("value"));
00227                                                  opstringdata=XMLString::transcode(item->getNodeValue());
00228 
00229                                                  //                                              theoperator.options.insert(std::pair<std::string,std::string>(opstringname,opstringdata));
00230                                                  g_globalConfiguration.setModuleOption(theoperator.name,opstringname,opstringdata);
00231                                           }else if(temp=="camera"){
00232                                                  string camname;
00233                                                  attr=child->getAttributes();
00234                                                  item=attr->getNamedItem(XMLString::transcode("name"));
00235                                                  camname=XMLString::transcode(item->getNodeValue());
00236                                                  std::vector<CameraConfig>::iterator iter;
00237                                                  for(iter=theConfig.cameras.begin();iter!=theConfig.cameras.end();iter++){
00238                                                         if((*iter).name==camname){
00239                                                                cout << "Got the camera.\n";
00240                                                                theoperator.cameras.push_back(*iter);
00241                                                                break;
00242                                                         }else{
00243                                                                cout << (*iter).name << " is not " << camname << endl;
00244                                                         }
00245                                                  }
00246                                           }
00247                                                 
00248                                                                                   
00249                                    }
00250                                    if(theoperator.cameras.size()<1){
00251                                           if(theConfig.cameras.size()>0){
00252                                                  theoperator.cameras.push_back(theConfig.cameras[0]);
00253                                           }
00254                                    }
00255                                    theConfig.operators.push_back(theoperator);
00256                             }else{
00257                                    char buffer[512];
00258                                    /*                              sprintf(buffer,"Couldn't recognize entity: %s",temp.c_str());
00259                                                                    g_warn(buffer);*/
00260                             }
00261                      }
00262                      
00263               }
00264 
00265               theNodes=doc->getElementsByTagName(XMLString::transcode("plugin"));
00266 
00267               for(unsigned int i=0;i<theNodes->getLength();i++){
00268                      curr=theNodes->item(i);
00269                      attr=curr->getAttributes();
00270                      item=attr->getNamedItem(XMLString::transcode("path"));
00271                      theConfig.plugins.push_back(XMLString::transcode(item->getNodeValue()));
00272               }
00273               
00274               XMLPlatformUtils::Terminate();
00275               
00276               // Other terminations and cleanup.
00277               return theConfig;
00278        }
00279 
00280 }

Generated on Thu Feb 1 15:31:52 2007 for WURDE by  doxygen 1.5.1