/home/fwph/code/wurde/rde/mcp/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 <mcp.H>
00006 #include <string>
00007 #include <iostream>
00008 
00009 // Other include files, declarations, and non-Xerces-C++ initializations.
00010 XERCES_CPP_NAMESPACE_USE 
00011 
00012 using namespace WURDE;
00013 using namespace std;
00014 
00015 namespace MCP {  
00016        
00017        vector<Module> parseConfigFile(string config){
00018               vector<Module> theModules;
00019               vector<Mapping> theMappings;
00020               try {
00021                      XMLPlatformUtils::Initialize();
00022               }
00023               catch (const XMLException& toCatch) {           
00024                      g_logerror << "Something went wrong.\n";
00025                      //we should probably do some more failure processing, and generally catch the exceptions
00026                      //that can be thrown by Xerces, but i'm not doing that right now.
00027                      return theModules;
00028               }
00029               
00030               /*              //this is all startup stuff for Xerces.we may not actually be validating now. should check on that.
00031               static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
00032               DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
00033               DOMBuilder        *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
00034               */
00035 
00036               /*
00037                * Create a DOM parser, and turn schemas on. Namespace validation is necessary as well.
00038                */
00039               XercesDOMParser *domparser=new XercesDOMParser;
00040               domparser->setValidationScheme(XercesDOMParser::Val_Always);
00041               domparser->setDoNamespaces(true);
00042               domparser->setDoSchema(true);
00043               domparser->parse(config.c_str());
00044 
00045               XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
00046               DOMElement *root;
00047               DOMNode *curr,*name,*child,*item;
00048               DOMNodeList *theNodes,*children,*subchildren;
00049               DOMNamedNodeMap *attr;
00050 
00051               Mapping newmapping;             
00052              
00053               //      doc = parser->parseURI(config.c_str());
00054               doc=domparser->getDocument();
00055               if(!doc){
00056                      g_logfatal << "Config file " << config.c_str() << " not found!"<< endl;
00057                      exit(1);
00058               }
00059               root=doc->getDocumentElement();
00060               if(!root){
00061                      g_logfatal << "Document did not validate!" << endl;
00062                      exit(1);
00063               }
00064 
00065               //now the code
00066               string currentname, temp;
00067               Module *newmodule;
00068               
00069               //make sure we've got an mcp-config section
00070               temp=XMLString::transcode(root->getTagName());
00071               /*              if(temp!="robot"){
00072                      g_logfatal << "That is not a proper robot configuration file." << endl;
00073                      exit(1);
00074               }
00075               */
00076               theNodes=root->getElementsByTagName(XMLString::transcode("mcp-config"));
00077         
00078               if(theNodes->getLength() > 0){
00079                      child=theNodes->item(0);
00080                      children=child->getChildNodes();
00081               }else{
00082                      g_logfatal << "Found no mcp-config elements. Invalid config!" << endl;
00083                      exit(1);
00084               }
00085               
00086 
00087               for(unsigned int i=0;i<children->getLength();i++){
00088                      
00089                      curr=children->item(i);
00090                      string stmp=XMLString::transcode(curr->getNodeName());
00091                      if(stmp=="module"){
00092                             attr=curr->getAttributes();
00093 
00094                             name=attr->getNamedItem(XMLString::transcode("name"));
00095                             currentname=XMLString::transcode(name->getNodeValue());   
00096                             g_logdebug << "Found module with name " << currentname << endl;
00097                             newmodule = new Module(currentname);
00098                             //make some assumptions
00099                             newmodule->setType(PROC_MANAGED);
00100                             newmodule->setLocale(LOCALE_INTERNAL);                   
00101                             newmodule->setState(STATE_INACTIVE);
00102                             
00103                             item=attr->getNamedItem(XMLString::transcode("binary"));
00104                             if(item){
00105                                     temp=XMLString::transcode(item->getNodeValue());
00106                                     newmodule->setBinary(temp);                         
00107                             }else{
00108                                     newmodule->setBinary("empty");
00109                             }
00110                             
00111                             item=attr->getNamedItem(XMLString::transcode("managed"));
00112                             if(!item){
00113                                    g_logwarn << "Found no managed attribute. Looks like we're not validating. Assuming managed." << endl;
00114                             }else{
00115                                    temp=XMLString::transcode(item->getNodeValue());
00116                                    if(temp=="true"){
00117                                           newmodule->setType(PROC_MANAGED);
00118                                    }else if(temp=="critical"){
00119                                           newmodule->setType(PROC_CRITICAL);
00120                                    }else{
00121                                           newmodule->setType(PROC_UNMANAGED);
00122                                    }
00123                             }
00124                             
00125                             item=attr->getNamedItem(XMLString::transcode("locale"));
00126                             if(!item){
00127                                    g_logwarn << "Found no locale attribute. Looks like we're not validating. Assuming internal." << endl;
00128                             }else{
00129                                    temp=XMLString::transcode(item->getNodeValue());
00130                                    if(temp=="external"){
00131                                           newmodule->setLocale(LOCALE_EXTERNAL);
00132                                    }else{
00133                                           newmodule->setLocale(LOCALE_INTERNAL);
00134                                    }
00135                             }
00136                             
00137                             // arguments have to be separated into a list of strings; so i'm going to avoid the
00138                             // parsing
00139                             /*                      item=attr->getNamedItem(XMLString::transcode("options"));
00140                             if(item){
00141                                    temp=XMLString::transcode(item->getNodeValue());
00142                                    newmodule->setCommandLineOptions(temp);
00143                             }                      
00144                             */
00148                             theMappings.clear();
00149                             subchildren=curr->getChildNodes();
00150 
00151                             for(unsigned int j=0;j<subchildren->getLength();j++){
00152                                    child=subchildren->item(j);          
00153                                    stmp=XMLString::transcode(child->getNodeName());
00154                                    if(stmp=="arg"){
00155                                           attr=child->getAttributes();
00156                                           item=attr->getNamedItem(XMLString::transcode("value"));
00157                                           if(item){
00158                                                  newmodule->addCommandLineOption(XMLString::transcode(item->getNodeValue()));
00159                                           }
00160                                    }else if(stmp=="connect"){
00161                                           attr=child->getAttributes();
00162                                           item=attr->getNamedItem(XMLString::transcode("supplier"));
00163                                           if(item){
00164                                                  newmapping.source=XMLString::transcode(item->getNodeValue());
00165                                                  g_logdebug << "Source is " << newmapping.source << endl;
00166                                           }else{
00167                                                  continue;
00168                                           }
00169 
00170                                           item=attr->getNamedItem(XMLString::transcode("interface"));
00171                                           if(item){
00172                                                  newmapping.interface=XMLString::transcode(item->getNodeValue());
00173                                           }else{
00174                                                  continue;
00175                                           }
00176 
00177                                           item=attr->getNamedItem(XMLString::transcode("consumer"));
00178                                           if(item){
00179                                                  newmapping.object=XMLString::transcode(item->getNodeValue());
00180                                           }else{
00181                                                  newmapping.object="";
00182                                           }
00183                                           theMappings.push_back(newmapping);
00184                                           g_logdebug << "Found a new connect entity: map " << newmapping.source << " to " << newmapping.object << "of type " << newmapping.interface << endl;
00185                                    }
00186                             }
00187 
00188                             newmodule->setMappings(theMappings);                                          
00189                             
00190                             theModules.push_back(*newmodule);
00191                             delete newmodule;
00192                      }else{
00193                              g_logdebug << "Didn't recognize " << stmp << endl;
00194                      }
00195               }       
00196               
00197               XMLPlatformUtils::Terminate();
00198               
00199               // Other terminations and cleanup.
00200               return theModules;
00201        }
00202        
00203        
00204        vector<Module> parseMapFile(string config,vector<Module> &modules){
00205               vector<Mapping> theMappings;
00206               try {
00207                      XMLPlatformUtils::Initialize();
00208               }
00209               catch (const XMLException& toCatch) {           
00210                      cout << "Something went wrong.\n";
00211                      //we should probably do some more failure processing, and generally catch the exceptions
00212                      //that can be thrown by Xerces, but i'm not doing that right now.
00213                      return modules;
00214               }
00215               
00216               /*
00217                * Create a DOM parser, and turn schemas on. Namespace validation is necessary as well.
00218                */
00219               XercesDOMParser *domparser=new XercesDOMParser;
00220               domparser->setValidationScheme(XercesDOMParser::Val_Always);
00221               domparser->setDoNamespaces(true);
00222               domparser->setDoSchema(true);
00223               domparser->parse(config.c_str());
00224 
00225               XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
00226               DOMElement *root;
00227               DOMNode *curr,*name,*child,*item;
00228               DOMNodeList *theNodes,*children,*subchildren;
00229               DOMNamedNodeMap *attr;
00230               
00231               
00232               doc = domparser->getDocument();
00233               if(!doc){
00234                      g_logfatal << "Config file " << config.c_str() << " not found!"<< endl;
00235                      exit(1);
00236               }
00237 
00238               root=doc->getDocumentElement();
00239               if(!root){
00240                      g_logfatal << "Document did not validate!" << endl;
00241                      exit(1);
00242               }
00243 
00244               //now the code
00245               string currentname, temp;
00246               Mapping newmapping;
00247 
00248               
00249               //make sure we've got an mcp-config section
00250               temp=XMLString::transcode(root->getTagName());
00251               if(temp!="robot"){
00252                      g_logfatal << "That is not a proper robot configuration file." << endl;
00253                      exit(1);
00254               }
00255 
00256               theNodes=root->getElementsByTagName(XMLString::transcode("mcp-mappings"));
00257 
00258               if(theNodes->getLength() > 0){
00259                      child=theNodes->item(0);
00260                      children=child->getChildNodes();
00261               }else{
00262                      g_logfatal << "Found no mcp-mappings elements. Invalid config!" << endl;
00263                      exit(1);
00264               }
00265               
00266 
00267 
00268               for(unsigned int i=0;i<children->getLength();i++){
00269                      curr=children->item(i);
00270                      string stmp=XMLString::transcode(curr->getNodeName());
00271         
00272                      if(stmp=="module-map"){    
00273                             theMappings.clear();
00274 
00275                             attr=curr->getAttributes();
00276                             name=attr->getNamedItem(XMLString::transcode("name"));
00277                             currentname=XMLString::transcode(name->getNodeValue());
00278                             
00279                             g_logdebug << "Got module " << currentname << endl;
00280                             
00281                             subchildren=curr->getChildNodes();
00282                             
00283                             for(unsigned int j=0;j<subchildren->getLength();j++){
00284                                    child=subchildren->item(j);          
00285                                    stmp=XMLString::transcode(child->getNodeName());
00286         
00287                                    if(stmp=="connect"){
00288                                           attr=child->getAttributes();
00289                                           item=attr->getNamedItem(XMLString::transcode("supplier"));
00290                                           if(item){
00291                                                  newmapping.source=XMLString::transcode(item->getNodeValue());
00292                                                  g_logdebug << "Source is " << newmapping.source << endl;
00293                                           }else{
00294                                                  continue;
00295                                           }
00296 
00297                                           item=attr->getNamedItem(XMLString::transcode("interface"));
00298                                           if(item){
00299                                                  newmapping.interface=XMLString::transcode(item->getNodeValue());
00300                                           }else{
00301                                                  continue;
00302                                           }
00303 
00304                                           item=attr->getNamedItem(XMLString::transcode("consumer"));
00305                                           if(item){
00306                                                  newmapping.object=XMLString::transcode(item->getNodeValue());
00307                                           }else{
00308                                                  newmapping.object="";
00309                                           }
00310                                           theMappings.push_back(newmapping);
00311                                    }
00312                             }
00313                             
00314                             vector<Module>::iterator iter;
00315                             bool found=false;
00316                             for(iter=modules.begin();iter!=modules.end();iter++){
00317                                    if(iter->getModuleName()==currentname){
00318                                           iter->setMappings(theMappings);
00319                                           found=true;
00320                                           break;
00321                                    }
00322                             }
00323                             if(!found){
00324                                    g_logdebug << "Making new module " << currentname << endl;
00325                                    Module newmodule(currentname);
00326                                    newmodule.setMappings(theMappings);
00327                                    newmodule.setState(STATE_INACTIVE);
00328                                    newmodule.setLocale(LOCALE_EXTERNAL);
00329                                    newmodule.setType(PROC_MANAGED);
00330                                    modules.push_back(newmodule);
00331                             }
00332                             
00333                      }else{
00334                              g_logdebug << "Not a module section:" << stmp << endl;
00335                      }
00336               }     
00337               
00338               XMLPlatformUtils::Terminate();
00339               
00340               // Other terminations and cleanup.
00341               return modules;
00342        }
00343        
00344 }

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