CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQM/EcalBarrelMonitorDbModule/src/MonitorXMLParser.cc

Go to the documentation of this file.
00001 // $Id: MonitorXMLParser.cc,v 1.8 2008/04/08 18:32:10 dellaric Exp $
00002 
00011 #include <xercesc/util/PlatformUtils.hpp>
00012 
00013 #include <xercesc/dom/DOMDocument.hpp>
00014 #include <xercesc/dom/DOMNodeList.hpp>
00015 
00016 #include <sstream>
00017 #include <stdexcept>
00018 
00019 #include "DQM/EcalBarrelMonitorDbModule/interface/MonitorXMLParser.h"
00020 
00021 MonitorXMLParser::MonitorXMLParser( const std::string& fromFile ) {
00022 
00023   try{
00024 
00025     xercesc::XMLPlatformUtils::Initialize();
00026 
00027   }catch( xercesc::XMLException& e ){
00028 
00029     char* message = xercesc::XMLString::transcode( e.getMessage() ) ;
00030 
00031     std::cerr << "XML toolkit initialization error: " << message << std::endl;
00032 
00033     xercesc::XMLString::release( &message );
00034 
00035     exit( ERROR_XERCES_INIT );
00036 
00037   }
00038 
00039   xmlFile_ = fromFile;
00040   parser_  = new xercesc::XercesDOMParser();
00041   tags_    = new TagNames();
00042 
00043 }
00044 
00045 // - - - - - - - - - - - - - - - - -
00046 
00047 MonitorXMLParser::~MonitorXMLParser() throw() {
00048 
00049   try{
00050     xercesc::XMLPlatformUtils::Terminate();
00051   } catch ( xercesc::XMLException& e ){
00052     char* message = xercesc::XMLString::transcode( e.getMessage() );
00053     std::cerr << "XML toolkit teardown error: " << message << std::endl;
00054     xercesc::XMLString::release( &message ) ;
00055   }
00056 
00057 }
00058 
00059 
00060 
00061 // - - - - - - - - - - - - - - - - -
00062 
00063 void MonitorXMLParser::handleElement( xercesc::DOMElement* element ){
00064 
00065   if( xercesc::XMLString::equals( tags_->TAG_ME, element->getTagName() ) ) {
00066 
00067     char* c;
00068     std::stringstream s;
00069     DB_ME me;
00070     bool meok;
00071 
00072     meok = false;
00073 
00074     xercesc::DOMNodeList* d1Nodes = element->getElementsByTagName( tags_->TAG_1D );
00075     const XMLSize_t d1Count = d1Nodes->getLength();
00076 
00077     for( XMLSize_t d1Index = 0; d1Index < d1Count; ++d1Index ){
00078 
00079       xercesc::DOMNode* d1Node = d1Nodes->item( d1Index ) ;
00080 
00081       xercesc::DOMElement* d1Element = dynamic_cast< xercesc::DOMElement* >( d1Node ) ;
00082 
00083       const XMLCh* d1titleXMLCh = d1Element->getAttribute( tags_->ATTR_TITLE ) ;
00084       c = xercesc::XMLString::transcode( d1titleXMLCh );
00085       me.type = "th1d";
00086       me.title = c;
00087       meok = true;
00088       xercesc::XMLString::release( &c );
00089 
00090       const XMLCh* d1xbinsXMLCh = d1Element->getAttribute( tags_->ATTR_XBINS ) ;
00091       c = xercesc::XMLString::transcode( d1xbinsXMLCh );
00092       s.clear(); s.str( c );
00093       s >> me.xbins;
00094       xercesc::XMLString::release( &c );
00095 
00096       const XMLCh* d1xfromXMLCh = d1Element->getAttribute( tags_->ATTR_XFROM ) ;
00097       c = xercesc::XMLString::transcode( d1xfromXMLCh );
00098       s.clear(); s.str( c );
00099       s >> me.xfrom;
00100       xercesc::XMLString::release( &c );
00101 
00102       const XMLCh* d1xtoXMLCh = d1Element->getAttribute( tags_->ATTR_XTO ) ;
00103       c = xercesc::XMLString::transcode( d1xtoXMLCh );
00104       s.clear(); s.str( c );
00105       s >> me.xto;
00106       xercesc::XMLString::release( &c );
00107 
00108       const XMLCh* d1ncycleXMLCh = d1Element->getAttribute( tags_->ATTR_NCYCLE ) ;
00109       c = xercesc::XMLString::transcode( d1ncycleXMLCh );
00110       s.clear(); s.str( c );
00111       s >> me.ncycle;
00112       xercesc::XMLString::release( &c );
00113 
00114       const XMLCh* d1loopXMLCh = d1Element->getAttribute( tags_->ATTR_LOOP ) ;
00115       c = xercesc::XMLString::transcode( d1loopXMLCh );
00116       s.clear(); s.str( c );
00117       s >> me.loop;
00118       xercesc::XMLString::release( &c );
00119 
00120       me.ybins = 0;
00121       me.yfrom = 0.0;
00122       me.yto = 0.0;
00123       me.zbins = 0;
00124       me.zfrom = 0.0;
00125       me.zto = 0.0;
00126 
00127     }
00128 
00129     xercesc::DOMNodeList* d2Nodes = element->getElementsByTagName( tags_->TAG_2D );
00130     const XMLSize_t d2Count = d2Nodes->getLength();
00131 
00132     for( XMLSize_t d2Index = 0; d2Index < d2Count; ++d2Index ){
00133 
00134       xercesc::DOMNode* d2Node = d2Nodes->item( d2Index ) ;
00135 
00136       xercesc::DOMElement* d2Element = dynamic_cast< xercesc::DOMElement* >( d2Node ) ;
00137 
00138       const XMLCh* d2titleXMLCh = d2Element->getAttribute( tags_->ATTR_TITLE ) ;
00139       c = xercesc::XMLString::transcode( d2titleXMLCh );
00140       me.type = "th2d";
00141       me.title = c;
00142       meok = true;
00143       xercesc::XMLString::release( &c );
00144 
00145       const XMLCh* d2xbinsXMLCh = d2Element->getAttribute( tags_->ATTR_XBINS ) ;
00146       c = xercesc::XMLString::transcode( d2xbinsXMLCh );
00147       s.clear(); s.str( c );
00148       s >> me.xbins;
00149       xercesc::XMLString::release( &c );
00150 
00151       const XMLCh* d2xfromXMLCh = d2Element->getAttribute( tags_->ATTR_XFROM ) ;
00152       c = xercesc::XMLString::transcode( d2xfromXMLCh );
00153       s.clear(); s.str( c );
00154       s >> me.xfrom;
00155       xercesc::XMLString::release( &c );
00156 
00157       const XMLCh* d2xtoXMLCh = d2Element->getAttribute( tags_->ATTR_XTO ) ;
00158       c = xercesc::XMLString::transcode( d2xtoXMLCh );
00159       s.clear(); s.str( c );
00160       s >> me.xto;
00161       xercesc::XMLString::release( &c );
00162 
00163       const XMLCh* d2ybinsXMLCh = d2Element->getAttribute( tags_->ATTR_YBINS ) ;
00164       c = xercesc::XMLString::transcode( d2ybinsXMLCh );
00165       s.clear(); s.str( c );
00166       s >> me.ybins;
00167       xercesc::XMLString::release( &c );
00168 
00169       const XMLCh* d2yfromXMLCh = d2Element->getAttribute( tags_->ATTR_YFROM ) ;
00170       c = xercesc::XMLString::transcode( d2yfromXMLCh );
00171       s.clear(); s.str( c );
00172       s >> me.yfrom;
00173       xercesc::XMLString::release( &c );
00174 
00175       const XMLCh* d2ytoXMLCh = d2Element->getAttribute( tags_->ATTR_YTO ) ;
00176       c = xercesc::XMLString::transcode( d2ytoXMLCh );
00177       s.clear(); s.str( c );
00178       s >> me.yto;
00179       xercesc::XMLString::release( &c );
00180 
00181       const XMLCh* d2ncycleXMLCh = d2Element->getAttribute( tags_->ATTR_NCYCLE ) ;
00182       c = xercesc::XMLString::transcode( d2ncycleXMLCh );
00183       s.clear(); s.str( c );
00184       s >> me.ncycle;
00185       xercesc::XMLString::release( &c );
00186 
00187       const XMLCh* d2loopXMLCh = d2Element->getAttribute( tags_->ATTR_LOOP ) ;
00188       c = xercesc::XMLString::transcode( d2loopXMLCh );
00189       s.clear(); s.str( c );
00190       s >> me.loop;
00191       xercesc::XMLString::release( &c );
00192 
00193       me.zbins = 0;
00194       me.zfrom = 0.0;
00195       me.zto = 0.0;
00196 
00197     }
00198 
00199     xercesc::DOMNodeList* tpNodes = element->getElementsByTagName( tags_->TAG_TPROFILE );
00200     const XMLSize_t tpCount = tpNodes->getLength();
00201 
00202     for( XMLSize_t tpIndex = 0; tpIndex < tpCount; ++tpIndex ){
00203 
00204       xercesc::DOMNode* tpNode = tpNodes->item( tpIndex ) ;
00205 
00206       xercesc::DOMElement* tpElement = dynamic_cast< xercesc::DOMElement* >( tpNode ) ;
00207 
00208       const XMLCh* tptitleXMLCh = tpElement->getAttribute( tags_->ATTR_TITLE ) ;
00209       c = xercesc::XMLString::transcode( tptitleXMLCh );
00210       me.type = "tprofile";
00211       me.title = c;
00212       meok = true;
00213       xercesc::XMLString::release( &c );
00214 
00215       const XMLCh* tpxbinsXMLCh = tpElement->getAttribute( tags_->ATTR_XBINS ) ;
00216       c = xercesc::XMLString::transcode( tpxbinsXMLCh );
00217       s.clear(); s.str( c );
00218       s >> me.xbins;
00219       xercesc::XMLString::release( &c );
00220 
00221       const XMLCh* tpxfromXMLCh = tpElement->getAttribute( tags_->ATTR_XFROM ) ;
00222       c = xercesc::XMLString::transcode( tpxfromXMLCh );
00223       s.clear(); s.str( c );
00224       s >> me.xfrom;
00225       xercesc::XMLString::release( &c );
00226 
00227       const XMLCh* tpxtoXMLCh = tpElement->getAttribute( tags_->ATTR_XTO ) ;
00228       c = xercesc::XMLString::transcode( tpxtoXMLCh );
00229       s.clear(); s.str( c );
00230       s >> me.xto;
00231       xercesc::XMLString::release( &c );
00232 
00233       const XMLCh* tpybinsXMLCh = tpElement->getAttribute( tags_->ATTR_YBINS ) ;
00234       c = xercesc::XMLString::transcode( tpybinsXMLCh );
00235       s.clear(); s.str( c );
00236       s >> me.ybins;
00237       xercesc::XMLString::release( &c );
00238 
00239       const XMLCh* tpyfromXMLCh = tpElement->getAttribute( tags_->ATTR_YFROM ) ;
00240       c = xercesc::XMLString::transcode( tpyfromXMLCh );
00241       s.clear(); s.str( c );
00242       s >> me.yfrom;
00243       xercesc::XMLString::release( &c );
00244 
00245       const XMLCh* tpytoXMLCh = tpElement->getAttribute( tags_->ATTR_YTO ) ;
00246       c = xercesc::XMLString::transcode( tpytoXMLCh );
00247       s.clear(); s.str( c );
00248       s >> me.yto;
00249       xercesc::XMLString::release( &c );
00250 
00251       const XMLCh* tpncycleXMLCh = tpElement->getAttribute( tags_->ATTR_NCYCLE ) ;
00252       c = xercesc::XMLString::transcode( tpncycleXMLCh );
00253       s.clear(); s.str( c );
00254       s >> me.ncycle;
00255       xercesc::XMLString::release( &c );
00256 
00257       const XMLCh* tploopXMLCh = tpElement->getAttribute( tags_->ATTR_LOOP ) ;
00258       c = xercesc::XMLString::transcode( tploopXMLCh );
00259       s.clear(); s.str( c );
00260       s >> me.loop;
00261       xercesc::XMLString::release( &c );
00262 
00263       me.zbins = 0;
00264       me.zfrom = 0.0;
00265       me.zto = 0.0;
00266 
00267     }
00268 
00269     xercesc::DOMNodeList* tp2dNodes = element->getElementsByTagName( tags_->TAG_TPROFILE2D );
00270     const XMLSize_t tp2dCount = tp2dNodes->getLength();
00271 
00272     for( XMLSize_t tp2dIndex = 0; tp2dIndex < tp2dCount; ++tp2dIndex ){
00273 
00274       xercesc::DOMNode* tp2dNode = tp2dNodes->item( tp2dIndex ) ;
00275 
00276       xercesc::DOMElement* tp2dElement = dynamic_cast< xercesc::DOMElement* >( tp2dNode ) ;
00277 
00278       const XMLCh* tp2dtitleXMLCh = tp2dElement->getAttribute( tags_->ATTR_TITLE ) ;
00279       c = xercesc::XMLString::transcode( tp2dtitleXMLCh );
00280       me.type = "tprofile2d";
00281       me.title = c;
00282       meok = true;
00283       xercesc::XMLString::release( &c );
00284 
00285       const XMLCh* tp2dxbinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_XBINS ) ;
00286       c = xercesc::XMLString::transcode( tp2dxbinsXMLCh );
00287       s.clear(); s.str( c );
00288       s >> me.xbins;
00289       xercesc::XMLString::release( &c );
00290 
00291       const XMLCh* tp2dxfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_XFROM ) ;
00292       c = xercesc::XMLString::transcode( tp2dxfromXMLCh );
00293       s.clear(); s.str( c );
00294       s >> me.xfrom;
00295       xercesc::XMLString::release( &c );
00296 
00297       const XMLCh* tp2dxtoXMLCh = tp2dElement->getAttribute( tags_->ATTR_XTO ) ;
00298       c = xercesc::XMLString::transcode( tp2dxtoXMLCh );
00299       s.clear(); s.str( c );
00300       s >> me.xto;
00301       xercesc::XMLString::release( &c );
00302 
00303       const XMLCh* tp2dybinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_YBINS ) ;
00304       c = xercesc::XMLString::transcode( tp2dybinsXMLCh );
00305       s.clear(); s.str( c );
00306       s >> me.ybins;
00307       xercesc::XMLString::release( &c );
00308 
00309       const XMLCh* tp2dyfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_YFROM ) ;
00310       c = xercesc::XMLString::transcode( tp2dyfromXMLCh );
00311       s.clear(); s.str( c );
00312       s >> me.yfrom;
00313       xercesc::XMLString::release( &c );
00314 
00315       const XMLCh* tp2dytoXMLCh = tp2dElement->getAttribute( tags_->ATTR_YTO ) ;
00316       c = xercesc::XMLString::transcode( tp2dytoXMLCh );
00317       s.clear(); s.str( c );
00318       s >> me.yto;
00319       xercesc::XMLString::release( &c );
00320 
00321       const XMLCh* tp2dzbinsXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZBINS ) ;
00322       c = xercesc::XMLString::transcode( tp2dzbinsXMLCh );
00323       s.clear(); s.str( c );
00324       s >> me.zbins;
00325       xercesc::XMLString::release( &c );
00326 
00327       const XMLCh* tp2dzfromXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZFROM ) ;
00328       c = xercesc::XMLString::transcode( tp2dzfromXMLCh );
00329       s.clear(); s.str( c );
00330       s >> me.zfrom;
00331       xercesc::XMLString::release( &c );
00332 
00333       const XMLCh* tp2dztoXMLCh = tp2dElement->getAttribute( tags_->ATTR_ZTO ) ;
00334       c = xercesc::XMLString::transcode( tp2dztoXMLCh );
00335       s.clear(); s.str( c );
00336       s >> me.zto;
00337       xercesc::XMLString::release( &c );
00338 
00339       const XMLCh* tp2dncycleXMLCh = tp2dElement->getAttribute( tags_->ATTR_NCYCLE ) ;
00340       c = xercesc::XMLString::transcode( tp2dncycleXMLCh );
00341       s.clear(); s.str( c );
00342       s >> me.ncycle;
00343       xercesc::XMLString::release( &c );
00344 
00345       const XMLCh* tp2dloopXMLCh = tp2dElement->getAttribute( tags_->ATTR_LOOP ) ;
00346       c = xercesc::XMLString::transcode( tp2dloopXMLCh );
00347       s.clear(); s.str( c );
00348       s >> me.loop;
00349       xercesc::XMLString::release( &c );
00350 
00351     }
00352 
00353 
00354     xercesc::DOMNodeList* qNodes = element->getElementsByTagName( tags_->TAG_QUERY );
00355     const XMLSize_t qCount = qNodes->getLength();
00356 
00357     for( XMLSize_t qIndex = 0; qIndex < qCount; ++qIndex ){
00358 
00359       xercesc::DOMNode* qNode = qNodes->item( qIndex ) ;
00360 
00361       xercesc::DOMElement* qElement = dynamic_cast< xercesc::DOMElement* >( qNode ) ;
00362 
00363       const XMLCh* nameXMLCh = qElement->getAttribute( tags_->ATTR_NAME ) ;
00364       c = xercesc::XMLString::transcode( nameXMLCh );
00365 
00366       const XMLCh* argXMLCh = qElement->getAttribute( tags_->ATTR_ARG ) ;
00367       char* d = xercesc::XMLString::transcode( argXMLCh );
00368 
00369       const XMLCh* aliasXMLCh = qElement->getAttribute( tags_->ATTR_ALIAS ) ;
00370       char* e = xercesc::XMLString::transcode( aliasXMLCh );
00371 
00372       DbQuery tmpQuery;
00373       tmpQuery.query = c;
00374       tmpQuery.arg = d;
00375       tmpQuery.alias = e;
00376 
00377       me.queries.push_back( tmpQuery );
00378 
00379       xercesc::XMLString::release( &c );
00380       xercesc::XMLString::release( &d );
00381       xercesc::XMLString::release( &e );
00382 
00383 
00384     }
00385 
00386     if( meok ) DBMonitoringElements_.push_back( me );
00387 
00388   }
00389 } // handleElement()
00390 
00391 
00392 // - - - - - - - - - - - - - - - - - - -
00393 
00394 void MonitorXMLParser::load() throw( std::runtime_error ) {
00395 
00396   parser_->setValidationScheme( xercesc::XercesDOMParser::Val_Never );
00397   parser_->setDoNamespaces( false );
00398   parser_->setDoSchema( false );
00399   parser_->setLoadExternalDTD( false );
00400 
00401   try{
00402 
00403     parser_->parse( xmlFile_.c_str() );
00404 
00405     xercesc::DOMDocument* xmlDoc = parser_->getDocument();
00406 
00407     xercesc::DOMElement* dbe = xmlDoc->getDocumentElement();
00408 
00409     if( NULL == dbe ){
00410       throw( std::runtime_error( "empty XML document" ) ) ;
00411     }
00412 
00413     if( xercesc::XMLString::equals( tags_->TAG_DBE, dbe->getTagName() ) ) {
00414 
00415       xercesc::DOMNodeList* children = dbe->getChildNodes();
00416       const XMLSize_t nodeCount = children->getLength();
00417 
00418       for( XMLSize_t ix = 0 ; ix < nodeCount ; ++ix ){
00419         xercesc::DOMNode* currentNode = children->item( ix );
00420         if( NULL == currentNode ){
00421           // null node...
00422           continue;
00423         }
00424 
00425         if( xercesc::DOMNode::ELEMENT_NODE != currentNode->getNodeType() ){
00426           continue;
00427         }
00428 
00429         xercesc::DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >( currentNode );
00430 
00431         handleElement( currentElement );
00432 
00433       }
00434     }
00435 
00436   }catch( xercesc::XMLException& e ){
00437 
00438     char* message = xercesc::XMLString::transcode( e.getMessage() );
00439 
00440     std::ostringstream buf ;
00441     buf << "Error parsing file: " << message << std::endl;
00442 
00443     xercesc::XMLString::release( &message );
00444 
00445     throw( std::runtime_error( buf.str() ) );
00446 
00447   }catch( const xercesc::DOMException& e ){
00448 
00449     char* message = xercesc::XMLString::transcode( e.getMessage() );
00450 
00451     std::ostringstream buf;
00452     buf << "Encountered DOM Exception: " << message << std::endl;
00453 
00454     xercesc::XMLString::release( &message );
00455 
00456     throw( std::runtime_error( buf.str() ) );
00457 
00458   }
00459 
00460   return;
00461 
00462 } // load()
00463