/home/fwph/code/wurde/rde/core/xmlparser.cpp

Go to the documentation of this file.
00001 
00026 #include <xercesc/util/PlatformUtils.hpp>
00027 #include <xercesc/dom/DOM.hpp>
00028 #include <xercesc/parsers/XercesDOMParser.hpp>
00029 #include <xercesc/util/XMLChar.hpp>
00030 #include <string>
00031 #include <iostream>
00032 #include <WURDEConfiguration.H>
00033 #include <Logger.H>
00034 #include <errno.h>
00035 #include <algorithm>
00036 #include <cctype>
00037 
00038 // Other include files, declarations, and non-Xerces-C++ initializations.
00039 XERCES_CPP_NAMESPACE_USE 
00040 
00041 using namespace WURDE;
00042 using namespace std;
00043 
00044 namespace WURDE {  
00045        
00046         void parseWURDEConfig(string config){
00047 
00048               FILE *ptr;
00049               ptr=fopen(config.c_str(),"r");
00050               if(!ptr){
00051                      cout << "Config file not found!\n";
00052                      return;
00053               }else{
00054                      fclose(ptr);
00055               }
00056               
00057               try {
00058                      XMLPlatformUtils::Initialize();
00059               }
00060               catch (const XMLException& toCatch) {           
00061                      cout << "Something went wrong.\n";
00062                      //we should probably do some more failure processing, and generally catch the exceptions
00063                      //that can be thrown by Xerces, but i'm not doing that right now.
00064                      return;
00065               }
00066 
00067               /*
00068                * Create a DOM parser, and turn schemas on. Namespace validation is necessary as well.
00069                */
00070               XercesDOMParser *domparser=new XercesDOMParser;
00071               domparser->setValidationScheme(XercesDOMParser::Val_Always);
00072               domparser->setDoNamespaces(true);
00073               domparser->setDoSchema(true);
00074               domparser->parse(config.c_str());
00075 
00076               XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
00077               DOMElement *root;
00078               DOMNode *child,*item;
00079               DOMNodeList *children,*theNodes;
00080               DOMNamedNodeMap *attr;
00081                 
00082               doc=domparser->getDocument();
00083               //              doc = parser->parseURI(config.c_str());
00084               if(!doc){
00085                      g_logfatal << "Config file " << config.c_str() << " not found!"<< endl;
00086                      exit(1);
00087               }
00088 
00089               
00090               root=doc->getDocumentElement();
00091               if(!root){
00092                      g_logfatal << "Document did not validate!" << endl;
00093                      exit(1);
00094               }
00095               //now the code
00096               string currentname, temp;
00097               
00098               //make sure we've got an mcp-config section
00099               /*              temp=XMLString::transcode(root->getTagName());
00100               if(temp!="robot"){
00101                      g_logfatal << "That is not a proper robot configuration file." << endl;
00102                      exit(1);
00103                      }*/
00104               
00105               //get the module nodes
00106               theNodes=root->getElementsByTagName(XMLString::transcode("wurde-config"));
00107               //children=doc->getChildNodes();        
00108               if(theNodes->getLength() > 0){
00109                      child=theNodes->item(0);
00110                      children=child->getChildNodes();
00111               }else{
00112                      g_logfatal << "Found no wurde-config elements. Invalid config!" << endl;
00113                      exit(1);
00114               }
00115 
00116 
00117               for(unsigned int j=0;j<children->getLength();j++){
00118                             child=children->item(j);
00119                             temp=XMLString::transcode(child->getNodeName());
00120                             
00121                             if(temp=="config-directory"){
00122                                    attr=child->getAttributes();
00123                                    item=attr->getNamedItem(XMLString::transcode("location"));
00124                                    if(item){
00125                                           g_globalConfiguration.setConfigDirectory(XMLString::transcode(item->getNodeValue()));
00126                                    }else{
00127                                           cout << "config-directory option present but no location!\n";
00128                                    }
00129                             }else if(temp=="data-directory"){
00130                                    attr=child->getAttributes();
00131                                    item=attr->getNamedItem(XMLString::transcode("location"));
00132                                    if(item){
00133                                           g_globalConfiguration.setDataDirectory(XMLString::transcode(item->getNodeValue()));
00134                                    }else{
00135                                           cout << "data-directory option present but no location!\n";
00136                                    }
00137                             }else if(temp=="bin-directory"){
00138                                    attr=child->getAttributes();
00139                                    item=attr->getNamedItem(XMLString::transcode("location"));
00140                                    if(item){
00141                                            g_globalConfiguration.setBinDirectory(XMLString::transcode(item->getNodeValue()));
00142                                    }else{
00143                                            g_logdebug << "bin-directory option present but no location!\n";
00144                                    }
00145 
00146                             }else if(temp=="mcp"){
00147                                    attr=child->getAttributes();
00148                                    item=attr->getNamedItem(XMLString::transcode("mcp-config"));
00149                                    if(item){
00150                                           g_globalConfiguration.setMCPConfigFile(XMLString::transcode(item->getNodeValue()));
00151                                    }else{
00152                                           g_logdebug << "mcp entity present but no mcp-config!\n";
00153                                    }
00154 
00155 
00156                             }else if(temp=="log"){
00157                                    attr=child->getAttributes();
00158                                    item=attr->getNamedItem(XMLString::transcode("directory"));
00159                                    if(item){
00160                                           g_globalConfiguration.setLogDirectory(XMLString::transcode(item->getNodeValue()));
00161                                    }
00162 
00163                                    item=attr->getNamedItem(XMLString::transcode("level"));
00164                                    if(item){
00165                                           string tmplog=XMLString::transcode(item->getNodeValue());
00166                                           std::transform (tmplog.begin(),tmplog.end(), tmplog.begin(), (int (*) (int)) tolower);
00167 
00168                                           if(tmplog=="debug"){
00169                                                  g_globalConfiguration.setLogLevel(LOG_DEBUG);
00170                                           }else if(tmplog=="info"){
00171                                                  g_globalConfiguration.setLogLevel(LOG_INFO);
00172                                           }else if(tmplog=="notice"){
00173                                                  g_globalConfiguration.setLogLevel(LOG_NOTICE);
00174                                           }else if(tmplog=="warn"){
00175                                                  g_globalConfiguration.setLogLevel(LOG_WARNING);
00176                                           }else if(tmplog=="error"){
00177                                                  g_globalConfiguration.setLogLevel(LOG_ERR);
00178                                           }else if(tmplog=="fatal"){
00179                                                  g_globalConfiguration.setLogLevel(LOG_CRIT);
00180                                           }else{
00181                                                  cout << "Invalid loging level requested. Using LOG_DEBUG\n";
00182                                                  g_globalConfiguration.setLogLevel(LOG_DEBUG);
00183                                           }
00184 
00185                                    }
00186 
00187                             }else if(temp=="option"){
00188                                    attr=child->getAttributes();
00189                                    item=attr->getNamedItem(XMLString::transcode("name"));
00190                                    std::string optname;
00191                                    std::string optval;
00192                                    if(item){
00193                                           optname=XMLString::transcode(item->getNodeValue());
00194                                    }
00195 
00196                                    item=attr->getNamedItem(XMLString::transcode("value"));
00197                                    if(item){
00198                                           optval=XMLString::transcode(item->getNodeValue());
00199                                    }
00200 
00201                                    g_globalConfiguration.setOption(optname,optval);
00202 
00203                             }else if(temp=="module"){
00204                                    DOMNodeList *tempchildren;
00205                                    tempchildren=child->getChildNodes();
00206                                    std::string mname;
00207                                    attr=child->getAttributes();
00208                                    item=attr->getNamedItem(XMLString::transcode("name"));
00209                                    if(item){
00210                                           mname=XMLString::transcode(item->getNodeValue());
00211                                    }
00212 
00213                                    for(unsigned int k=0;k<tempchildren->getLength();k++){
00214                                           child=tempchildren->item(k);
00215                                           temp=XMLString::transcode(child->getNodeName());
00216                                           if(temp=="option"){
00217                                                  std::string optname;
00218                                                  std::string optval;
00219                                                  
00220                                                  attr=child->getAttributes();
00221                                                  item=attr->getNamedItem(XMLString::transcode("name"));
00222                                                  if(item){
00223                                                         optname=XMLString::transcode(item->getNodeValue());
00224                                                  }
00225 
00226                                                  item=attr->getNamedItem(XMLString::transcode("value"));
00227                                                  optval=XMLString::transcode(item->getNodeValue());
00228                                                  
00229                                                  g_globalConfiguration.setModuleOption(mname,optname,optval);
00230                                           }
00231                                    }
00232                             }else if(temp!="#text"){
00233                                    g_logwarn << "Unrecognized tag " << temp << endl;
00234                             }
00235               }
00236               
00237               
00238               
00239               XMLPlatformUtils::Terminate();
00240               
00241               // Other terminations and cleanup.
00242               return;
00243        }
00244        
00245 }

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