Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:09:58

0001 
0002 /*!
0003   \file MonitorXMLParser.cc
0004   \brief monitor db xml elements parsing tool
0005   \author B. Gobbo
0006 */
0007 
0008 #include "Utilities/Xerces/interface/Xerces.h"
0009 
0010 #include <xercesc/dom/DOMDocument.hpp>
0011 #include <xercesc/dom/DOMNodeList.hpp>
0012 
0013 #include <sstream>
0014 #include <stdexcept>
0015 
0016 #include "DQM/EcalMonitorDbModule/interface/MonitorXMLParser.h"
0017 
0018 MonitorXMLParser::MonitorXMLParser(const std::string &fromFile) {
0019   try {
0020     cms::concurrency::xercesInitialize();
0021 
0022   } catch (xercesc::XMLException &e) {
0023     char *message = xercesc::XMLString::transcode(e.getMessage());
0024 
0025     std::cerr << "XML toolkit initialization error: " << message << std::endl;
0026 
0027     xercesc::XMLString::release(&message);
0028 
0029     exit(ERROR_XERCES_INIT);
0030   }
0031 
0032   xmlFile_ = fromFile;
0033   parser_ = new xercesc::XercesDOMParser();
0034   tags_ = new TagNames();
0035 }
0036 
0037 // - - - - - - - - - - - - - - - - -
0038 
0039 MonitorXMLParser::~MonitorXMLParser() throw() {
0040   try {
0041     cms::concurrency::xercesTerminate();
0042   } catch (xercesc::XMLException &e) {
0043     char *message = xercesc::XMLString::transcode(e.getMessage());
0044     std::cerr << "XML toolkit teardown error: " << message << std::endl;
0045     xercesc::XMLString::release(&message);
0046   }
0047 
0048   delete parser_;
0049   delete tags_;
0050 }
0051 
0052 // - - - - - - - - - - - - - - - - -
0053 
0054 void MonitorXMLParser::handleElement(xercesc::DOMElement *element) {
0055   if (xercesc::XMLString::equals(tags_->TAG_ME, element->getTagName())) {
0056     char *c;
0057     std::stringstream s;
0058     DB_ME me;
0059     bool meok;
0060 
0061     meok = false;
0062 
0063     xercesc::DOMNodeList *d1Nodes = element->getElementsByTagName(tags_->TAG_1D);
0064     const XMLSize_t d1Count = d1Nodes->getLength();
0065 
0066     for (XMLSize_t d1Index = 0; d1Index < d1Count; ++d1Index) {
0067       xercesc::DOMNode *d1Node = d1Nodes->item(d1Index);
0068 
0069       xercesc::DOMElement *d1Element = dynamic_cast<xercesc::DOMElement *>(d1Node);
0070 
0071       const XMLCh *d1titleXMLCh = d1Element->getAttribute(tags_->ATTR_TITLE);
0072       c = xercesc::XMLString::transcode(d1titleXMLCh);
0073       me.type = "th1d";
0074       me.title = c;
0075       meok = true;
0076       xercesc::XMLString::release(&c);
0077 
0078       const XMLCh *d1xbinsXMLCh = d1Element->getAttribute(tags_->ATTR_XBINS);
0079       c = xercesc::XMLString::transcode(d1xbinsXMLCh);
0080       s.clear();
0081       s.str(c);
0082       s >> me.xbins;
0083       xercesc::XMLString::release(&c);
0084 
0085       const XMLCh *d1xfromXMLCh = d1Element->getAttribute(tags_->ATTR_XFROM);
0086       c = xercesc::XMLString::transcode(d1xfromXMLCh);
0087       s.clear();
0088       s.str(c);
0089       s >> me.xfrom;
0090       xercesc::XMLString::release(&c);
0091 
0092       const XMLCh *d1xtoXMLCh = d1Element->getAttribute(tags_->ATTR_XTO);
0093       c = xercesc::XMLString::transcode(d1xtoXMLCh);
0094       s.clear();
0095       s.str(c);
0096       s >> me.xto;
0097       xercesc::XMLString::release(&c);
0098 
0099       const XMLCh *d1ncycleXMLCh = d1Element->getAttribute(tags_->ATTR_NCYCLE);
0100       c = xercesc::XMLString::transcode(d1ncycleXMLCh);
0101       s.clear();
0102       s.str(c);
0103       s >> me.ncycle;
0104       xercesc::XMLString::release(&c);
0105 
0106       const XMLCh *d1loopXMLCh = d1Element->getAttribute(tags_->ATTR_LOOP);
0107       c = xercesc::XMLString::transcode(d1loopXMLCh);
0108       s.clear();
0109       s.str(c);
0110       s >> me.loop;
0111       xercesc::XMLString::release(&c);
0112 
0113       me.ybins = 0;
0114       me.yfrom = 0.0;
0115       me.yto = 0.0;
0116       me.zbins = 0;
0117       me.zfrom = 0.0;
0118       me.zto = 0.0;
0119     }
0120 
0121     xercesc::DOMNodeList *d2Nodes = element->getElementsByTagName(tags_->TAG_2D);
0122     const XMLSize_t d2Count = d2Nodes->getLength();
0123 
0124     for (XMLSize_t d2Index = 0; d2Index < d2Count; ++d2Index) {
0125       xercesc::DOMNode *d2Node = d2Nodes->item(d2Index);
0126 
0127       xercesc::DOMElement *d2Element = dynamic_cast<xercesc::DOMElement *>(d2Node);
0128 
0129       const XMLCh *d2titleXMLCh = d2Element->getAttribute(tags_->ATTR_TITLE);
0130       c = xercesc::XMLString::transcode(d2titleXMLCh);
0131       me.type = "th2d";
0132       me.title = c;
0133       meok = true;
0134       xercesc::XMLString::release(&c);
0135 
0136       const XMLCh *d2xbinsXMLCh = d2Element->getAttribute(tags_->ATTR_XBINS);
0137       c = xercesc::XMLString::transcode(d2xbinsXMLCh);
0138       s.clear();
0139       s.str(c);
0140       s >> me.xbins;
0141       xercesc::XMLString::release(&c);
0142 
0143       const XMLCh *d2xfromXMLCh = d2Element->getAttribute(tags_->ATTR_XFROM);
0144       c = xercesc::XMLString::transcode(d2xfromXMLCh);
0145       s.clear();
0146       s.str(c);
0147       s >> me.xfrom;
0148       xercesc::XMLString::release(&c);
0149 
0150       const XMLCh *d2xtoXMLCh = d2Element->getAttribute(tags_->ATTR_XTO);
0151       c = xercesc::XMLString::transcode(d2xtoXMLCh);
0152       s.clear();
0153       s.str(c);
0154       s >> me.xto;
0155       xercesc::XMLString::release(&c);
0156 
0157       const XMLCh *d2ybinsXMLCh = d2Element->getAttribute(tags_->ATTR_YBINS);
0158       c = xercesc::XMLString::transcode(d2ybinsXMLCh);
0159       s.clear();
0160       s.str(c);
0161       s >> me.ybins;
0162       xercesc::XMLString::release(&c);
0163 
0164       const XMLCh *d2yfromXMLCh = d2Element->getAttribute(tags_->ATTR_YFROM);
0165       c = xercesc::XMLString::transcode(d2yfromXMLCh);
0166       s.clear();
0167       s.str(c);
0168       s >> me.yfrom;
0169       xercesc::XMLString::release(&c);
0170 
0171       const XMLCh *d2ytoXMLCh = d2Element->getAttribute(tags_->ATTR_YTO);
0172       c = xercesc::XMLString::transcode(d2ytoXMLCh);
0173       s.clear();
0174       s.str(c);
0175       s >> me.yto;
0176       xercesc::XMLString::release(&c);
0177 
0178       const XMLCh *d2ncycleXMLCh = d2Element->getAttribute(tags_->ATTR_NCYCLE);
0179       c = xercesc::XMLString::transcode(d2ncycleXMLCh);
0180       s.clear();
0181       s.str(c);
0182       s >> me.ncycle;
0183       xercesc::XMLString::release(&c);
0184 
0185       const XMLCh *d2loopXMLCh = d2Element->getAttribute(tags_->ATTR_LOOP);
0186       c = xercesc::XMLString::transcode(d2loopXMLCh);
0187       s.clear();
0188       s.str(c);
0189       s >> me.loop;
0190       xercesc::XMLString::release(&c);
0191 
0192       me.zbins = 0;
0193       me.zfrom = 0.0;
0194       me.zto = 0.0;
0195     }
0196 
0197     xercesc::DOMNodeList *tpNodes = element->getElementsByTagName(tags_->TAG_TPROFILE);
0198     const XMLSize_t tpCount = tpNodes->getLength();
0199 
0200     for (XMLSize_t tpIndex = 0; tpIndex < tpCount; ++tpIndex) {
0201       xercesc::DOMNode *tpNode = tpNodes->item(tpIndex);
0202 
0203       xercesc::DOMElement *tpElement = dynamic_cast<xercesc::DOMElement *>(tpNode);
0204 
0205       const XMLCh *tptitleXMLCh = tpElement->getAttribute(tags_->ATTR_TITLE);
0206       c = xercesc::XMLString::transcode(tptitleXMLCh);
0207       me.type = "tprofile";
0208       me.title = c;
0209       meok = true;
0210       xercesc::XMLString::release(&c);
0211 
0212       const XMLCh *tpxbinsXMLCh = tpElement->getAttribute(tags_->ATTR_XBINS);
0213       c = xercesc::XMLString::transcode(tpxbinsXMLCh);
0214       s.clear();
0215       s.str(c);
0216       s >> me.xbins;
0217       xercesc::XMLString::release(&c);
0218 
0219       const XMLCh *tpxfromXMLCh = tpElement->getAttribute(tags_->ATTR_XFROM);
0220       c = xercesc::XMLString::transcode(tpxfromXMLCh);
0221       s.clear();
0222       s.str(c);
0223       s >> me.xfrom;
0224       xercesc::XMLString::release(&c);
0225 
0226       const XMLCh *tpxtoXMLCh = tpElement->getAttribute(tags_->ATTR_XTO);
0227       c = xercesc::XMLString::transcode(tpxtoXMLCh);
0228       s.clear();
0229       s.str(c);
0230       s >> me.xto;
0231       xercesc::XMLString::release(&c);
0232 
0233       const XMLCh *tpybinsXMLCh = tpElement->getAttribute(tags_->ATTR_YBINS);
0234       c = xercesc::XMLString::transcode(tpybinsXMLCh);
0235       s.clear();
0236       s.str(c);
0237       s >> me.ybins;
0238       xercesc::XMLString::release(&c);
0239 
0240       const XMLCh *tpyfromXMLCh = tpElement->getAttribute(tags_->ATTR_YFROM);
0241       c = xercesc::XMLString::transcode(tpyfromXMLCh);
0242       s.clear();
0243       s.str(c);
0244       s >> me.yfrom;
0245       xercesc::XMLString::release(&c);
0246 
0247       const XMLCh *tpytoXMLCh = tpElement->getAttribute(tags_->ATTR_YTO);
0248       c = xercesc::XMLString::transcode(tpytoXMLCh);
0249       s.clear();
0250       s.str(c);
0251       s >> me.yto;
0252       xercesc::XMLString::release(&c);
0253 
0254       const XMLCh *tpncycleXMLCh = tpElement->getAttribute(tags_->ATTR_NCYCLE);
0255       c = xercesc::XMLString::transcode(tpncycleXMLCh);
0256       s.clear();
0257       s.str(c);
0258       s >> me.ncycle;
0259       xercesc::XMLString::release(&c);
0260 
0261       const XMLCh *tploopXMLCh = tpElement->getAttribute(tags_->ATTR_LOOP);
0262       c = xercesc::XMLString::transcode(tploopXMLCh);
0263       s.clear();
0264       s.str(c);
0265       s >> me.loop;
0266       xercesc::XMLString::release(&c);
0267 
0268       me.zbins = 0;
0269       me.zfrom = 0.0;
0270       me.zto = 0.0;
0271     }
0272 
0273     xercesc::DOMNodeList *tp2dNodes = element->getElementsByTagName(tags_->TAG_TPROFILE2D);
0274     const XMLSize_t tp2dCount = tp2dNodes->getLength();
0275 
0276     for (XMLSize_t tp2dIndex = 0; tp2dIndex < tp2dCount; ++tp2dIndex) {
0277       xercesc::DOMNode *tp2dNode = tp2dNodes->item(tp2dIndex);
0278 
0279       xercesc::DOMElement *tp2dElement = dynamic_cast<xercesc::DOMElement *>(tp2dNode);
0280 
0281       const XMLCh *tp2dtitleXMLCh = tp2dElement->getAttribute(tags_->ATTR_TITLE);
0282       c = xercesc::XMLString::transcode(tp2dtitleXMLCh);
0283       me.type = "tprofile2d";
0284       me.title = c;
0285       meok = true;
0286       xercesc::XMLString::release(&c);
0287 
0288       const XMLCh *tp2dxbinsXMLCh = tp2dElement->getAttribute(tags_->ATTR_XBINS);
0289       c = xercesc::XMLString::transcode(tp2dxbinsXMLCh);
0290       s.clear();
0291       s.str(c);
0292       s >> me.xbins;
0293       xercesc::XMLString::release(&c);
0294 
0295       const XMLCh *tp2dxfromXMLCh = tp2dElement->getAttribute(tags_->ATTR_XFROM);
0296       c = xercesc::XMLString::transcode(tp2dxfromXMLCh);
0297       s.clear();
0298       s.str(c);
0299       s >> me.xfrom;
0300       xercesc::XMLString::release(&c);
0301 
0302       const XMLCh *tp2dxtoXMLCh = tp2dElement->getAttribute(tags_->ATTR_XTO);
0303       c = xercesc::XMLString::transcode(tp2dxtoXMLCh);
0304       s.clear();
0305       s.str(c);
0306       s >> me.xto;
0307       xercesc::XMLString::release(&c);
0308 
0309       const XMLCh *tp2dybinsXMLCh = tp2dElement->getAttribute(tags_->ATTR_YBINS);
0310       c = xercesc::XMLString::transcode(tp2dybinsXMLCh);
0311       s.clear();
0312       s.str(c);
0313       s >> me.ybins;
0314       xercesc::XMLString::release(&c);
0315 
0316       const XMLCh *tp2dyfromXMLCh = tp2dElement->getAttribute(tags_->ATTR_YFROM);
0317       c = xercesc::XMLString::transcode(tp2dyfromXMLCh);
0318       s.clear();
0319       s.str(c);
0320       s >> me.yfrom;
0321       xercesc::XMLString::release(&c);
0322 
0323       const XMLCh *tp2dytoXMLCh = tp2dElement->getAttribute(tags_->ATTR_YTO);
0324       c = xercesc::XMLString::transcode(tp2dytoXMLCh);
0325       s.clear();
0326       s.str(c);
0327       s >> me.yto;
0328       xercesc::XMLString::release(&c);
0329 
0330       const XMLCh *tp2dzbinsXMLCh = tp2dElement->getAttribute(tags_->ATTR_ZBINS);
0331       c = xercesc::XMLString::transcode(tp2dzbinsXMLCh);
0332       s.clear();
0333       s.str(c);
0334       s >> me.zbins;
0335       xercesc::XMLString::release(&c);
0336 
0337       const XMLCh *tp2dzfromXMLCh = tp2dElement->getAttribute(tags_->ATTR_ZFROM);
0338       c = xercesc::XMLString::transcode(tp2dzfromXMLCh);
0339       s.clear();
0340       s.str(c);
0341       s >> me.zfrom;
0342       xercesc::XMLString::release(&c);
0343 
0344       const XMLCh *tp2dztoXMLCh = tp2dElement->getAttribute(tags_->ATTR_ZTO);
0345       c = xercesc::XMLString::transcode(tp2dztoXMLCh);
0346       s.clear();
0347       s.str(c);
0348       s >> me.zto;
0349       xercesc::XMLString::release(&c);
0350 
0351       const XMLCh *tp2dncycleXMLCh = tp2dElement->getAttribute(tags_->ATTR_NCYCLE);
0352       c = xercesc::XMLString::transcode(tp2dncycleXMLCh);
0353       s.clear();
0354       s.str(c);
0355       s >> me.ncycle;
0356       xercesc::XMLString::release(&c);
0357 
0358       const XMLCh *tp2dloopXMLCh = tp2dElement->getAttribute(tags_->ATTR_LOOP);
0359       c = xercesc::XMLString::transcode(tp2dloopXMLCh);
0360       s.clear();
0361       s.str(c);
0362       s >> me.loop;
0363       xercesc::XMLString::release(&c);
0364     }
0365 
0366     xercesc::DOMNodeList *qNodes = element->getElementsByTagName(tags_->TAG_QUERY);
0367     const XMLSize_t qCount = qNodes->getLength();
0368 
0369     for (XMLSize_t qIndex = 0; qIndex < qCount; ++qIndex) {
0370       xercesc::DOMNode *qNode = qNodes->item(qIndex);
0371 
0372       xercesc::DOMElement *qElement = dynamic_cast<xercesc::DOMElement *>(qNode);
0373 
0374       const XMLCh *nameXMLCh = qElement->getAttribute(tags_->ATTR_NAME);
0375       c = xercesc::XMLString::transcode(nameXMLCh);
0376 
0377       const XMLCh *argXMLCh = qElement->getAttribute(tags_->ATTR_ARG);
0378       char *d = xercesc::XMLString::transcode(argXMLCh);
0379 
0380       const XMLCh *aliasXMLCh = qElement->getAttribute(tags_->ATTR_ALIAS);
0381       char *e = xercesc::XMLString::transcode(aliasXMLCh);
0382 
0383       DbQuery tmpQuery;
0384       tmpQuery.query = c;
0385       tmpQuery.arg = d;
0386       tmpQuery.alias = e;
0387 
0388       me.queries.push_back(tmpQuery);
0389 
0390       xercesc::XMLString::release(&c);
0391       xercesc::XMLString::release(&d);
0392       xercesc::XMLString::release(&e);
0393     }
0394 
0395     if (meok)
0396       DBMonitoringElements_.push_back(me);
0397   }
0398 }  // handleElement()
0399 
0400 // - - - - - - - - - - - - - - - - - - -
0401 
0402 void MonitorXMLParser::load() noexcept(false) {
0403   parser_->setValidationScheme(xercesc::XercesDOMParser::Val_Never);
0404   parser_->setDoNamespaces(false);
0405   parser_->setDoSchema(false);
0406   parser_->setLoadExternalDTD(false);
0407 
0408   try {
0409     parser_->parse(xmlFile_.c_str());
0410 
0411     xercesc::DOMDocument *xmlDoc = parser_->getDocument();
0412 
0413     xercesc::DOMElement *dbe = xmlDoc->getDocumentElement();
0414 
0415     if (nullptr == dbe) {
0416       throw(std::runtime_error("empty XML document"));
0417     }
0418 
0419     if (xercesc::XMLString::equals(tags_->TAG_DBE, dbe->getTagName())) {
0420       xercesc::DOMNodeList *children = dbe->getChildNodes();
0421       const XMLSize_t nodeCount = children->getLength();
0422 
0423       for (XMLSize_t ix = 0; ix < nodeCount; ++ix) {
0424         xercesc::DOMNode *currentNode = children->item(ix);
0425         if (nullptr == currentNode) {
0426           // null node...
0427           continue;
0428         }
0429 
0430         if (xercesc::DOMNode::ELEMENT_NODE != currentNode->getNodeType()) {
0431           continue;
0432         }
0433 
0434         xercesc::DOMElement *currentElement = dynamic_cast<xercesc::DOMElement *>(currentNode);
0435 
0436         handleElement(currentElement);
0437       }
0438     }
0439 
0440   } catch (xercesc::XMLException &e) {
0441     char *message = xercesc::XMLString::transcode(e.getMessage());
0442 
0443     std::ostringstream buf;
0444     buf << "Error parsing file: " << message << std::endl;
0445 
0446     xercesc::XMLString::release(&message);
0447 
0448     throw(std::runtime_error(buf.str()));
0449 
0450   } catch (const xercesc::DOMException &e) {
0451     char *message = xercesc::XMLString::transcode(e.getMessage());
0452 
0453     std::ostringstream buf;
0454     buf << "Encountered DOM Exception: " << message << std::endl;
0455 
0456     xercesc::XMLString::release(&message);
0457 
0458     throw(std::runtime_error(buf.str()));
0459   }
0460 
0461   return;
0462 
0463 }  // load()