/home/fwph/code/wurde/modules/rFlexBase/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 <Logger.H>
00008 #include <WURDEConfiguration.H>
00009 
00010 // Other include files, declarations, and non-Xerces-C++ initializations.
00011 XERCES_CPP_NAMESPACE_USE 
00012 
00013 using namespace WURDE;
00014 using namespace std;
00015 
00016 string parseRflexConfig(std::string config,std::string rflexName){
00017        string port=" ";
00018        
00019        try {
00020               XMLPlatformUtils::Initialize();
00021        }
00022        catch (const XMLException& toCatch) {          
00023               cout << "Something went wrong.\n";
00024               //we should probably do some more failure processing, and generally catch the exceptions
00025               //that can be thrown by Xerces, but i'm not doing that right now.
00026               return port;
00027        }
00028        
00029        //this is all startup stuff for Xerces.we may not actually be validating now. should check on that.
00030        static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
00031        DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
00032        DOMBuilder        *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
00033        XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
00034        DOMElement *root;
00035        DOMNode *curr,*name,*child, *item;
00036        DOMNodeList *theNodes,*children;
00037        DOMNamedNodeMap *attr;
00038        bool found=false;
00039 
00040        doc = parser->parseURI(config.c_str());
00041        if(!doc){
00042               g_logfatal << "Config file " << config.c_str() << " not found!"<< endl;
00043               exit(1);
00044        }
00045        
00046        root=doc->getDocumentElement();
00047        
00048        //now the code
00049        string currentname, temp;
00050        
00051        //make sure we've got an mcp-config section
00052        temp=XMLString::transcode(root->getTagName());
00053        if(temp!="rflex-config"){
00054               g_logfatal << temp << " has no rflex-config.\n";
00055               exit(1);
00056        }
00057        
00058        //get the module nodes
00059        theNodes=doc->getElementsByTagName(XMLString::transcode("device"));
00060        
00061        for(unsigned int i=0;i<theNodes->getLength();i++){
00062               string id;
00063               
00064               curr=theNodes->item(i);
00065               attr=curr->getAttributes();
00066               
00067               name=attr->getNamedItem(XMLString::transcode("name"));
00068               currentname=XMLString::transcode(name->getNodeValue());
00069 
00070               if(currentname==rflexName){
00071                      found=true;
00072                      
00073                      name=attr->getNamedItem(XMLString::transcode("port"));
00074                      port=XMLString::transcode(name->getNodeValue());
00075 
00076                      name=attr->getNamedItem(XMLString::transcode("type"));
00077                      string type=XMLString::transcode(name->getNodeValue());
00078                      g_globalConfiguration.setOption("rFlexType",type);
00079               }
00080        }
00081 
00082        theNodes=doc->getElementsByTagName(XMLString::transcode("limits"));
00083        
00084        for(unsigned int i=0;i<theNodes->getLength();i++){
00085               string id;
00086               string val;
00087 
00088               curr=theNodes->item(i);
00089               attr=curr->getAttributes();
00090               
00091               name=attr->getNamedItem(XMLString::transcode("maxTrans"));
00092               if(name){
00093                      val=XMLString::transcode(name->getNodeValue());
00094                      g_globalConfiguration.setOption("rFlexMaxTrans",val);
00095               }
00096 
00097               name=attr->getNamedItem(XMLString::transcode("maxSteer"));
00098               
00099               if(name){
00100                      val=XMLString::transcode(name->getNodeValue());
00101                      g_globalConfiguration.setOption("rFlexMaxTrans",val);
00102               }
00103 
00104               name=attr->getNamedItem(XMLString::transcode("rFlexUnsafeBrake"));
00105               
00106               if(name){
00107                      val=XMLString::transcode(name->getNodeValue());
00108                      g_globalConfiguration.setOption("rFlexUnsafeBrake",val);
00109               }
00110 
00111        }
00112 
00113        
00114        if(!found){
00115               g_logfatal << "Couldn't find rflex " << rflexName << endl;
00116               exit(1);
00117        }
00118        
00119        XMLPlatformUtils::Terminate();
00120        
00121        return port;
00122 }
00123         

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