Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:18

0001 // -*- C++ -*-
0002 //
0003 // Package:     XMLTools
0004 // Class  :     XMLProcessor
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:
0010 //         Created:  Fri Sep 21 15:56:27 CEST 2007
0011 //
0012 
0013 // system include files
0014 #include <vector>
0015 #include <string>
0016 #include <iostream>
0017 #include <sys/types.h>
0018 #include <pwd.h>
0019 #include <unistd.h>
0020 #include <xercesc/parsers/XercesDOMParser.hpp>
0021 #include <xercesc/dom/DOM.hpp>
0022 #include <xercesc/sax/HandlerBase.hpp>
0023 #include <xercesc/util/XMLString.hpp>
0024 #include "Utilities/Xerces/interface/Xerces.h"
0025 #include <xercesc/dom/DOMNode.hpp>
0026 #include <xercesc/framework/StdOutFormatTarget.hpp>
0027 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0028 
0029 // xalan-c init
0030 //#include <xalanc/Include/PlatformDefinitions.hpp>
0031 //#include <xalanc/XPath/XPathEvaluator.hpp>
0032 //#include <xalanc/XalanTransformer/XalanTransformer.hpp>
0033 //using namespace xalanc;
0034 
0035 using namespace std;
0036 XERCES_CPP_NAMESPACE_USE
0037 
0038 // user include files
0039 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
0040 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLDOMBlock.h"
0041 
0042 XMLProcessor* XMLProcessor::instance = nullptr;
0043 
0044 XMLProcessor::XMLProcessor() {
0045   // initializes Xerces, must be done only once
0046   init();
0047 }
0048 
0049 XMLProcessor::~XMLProcessor() {
0050   // terminates Xerces
0051   terminate();
0052 }
0053 
0054 //_loaderBaseConfig::_loaderBaseConfig
0055 XMLProcessor::loaderBaseConfig::_loaderBaseConfig() {
0056   extention_table_name = "HCAL_TRIG_PRIM_LOOKUP_TABLES";
0057   name = "HCAL trigger primitive lookup table";
0058   run_mode = "no-run";
0059   data_set_id = "-1";
0060   iov_id = "1";
0061   iov_begin = "0";
0062   iov_end = "1";
0063   tag_id = "2";
0064   tag_mode = "auto";
0065   tag_name = "dummy tag";
0066   detector_name = "HCAL";
0067   comment_description = "empty comment";
0068 }
0069 
0070 XMLProcessor::DBConfig::_DBConfig() {
0071   version = "test:2";
0072   subversion = "1";
0073   create_timestamp = time(nullptr);
0074   created_by_user = getpwuid(getuid())->pw_name;
0075 }
0076 
0077 XMLDOMBlock* XMLProcessor::createLMapHBEFXMLBase(std::string templateFileName) {
0078   XMLDOMBlock* result = new XMLDOMBlock(templateFileName);
0079   DOMDocument* loader = result->getDocument();
0080   //DOMElement * root = loader -> getDocumentElement();
0081 
0082   loader->getElementsByTagName(_toXMLCh("NAME"))
0083       ->item(0)
0084       ->getFirstChild()
0085       ->setNodeValue(_toXMLCh("HCAL LMAP for HB, HE, HF"));
0086   //DOMElement * _tag = (DOMElement *)(loader -> getElementsByTagName( _toXMLCh( "TAG" ) ) -> item(0));
0087   //_tag -> setAttribute( _toXMLCh("mode"), _toXMLCh("test_mode") );
0088 
0089   return result;
0090 }
0091 
0092 int XMLProcessor::addLMapHBEFDataset(XMLDOMBlock* doc, LMapRowHBEF* row, std::string templateFileName) {
0093   DOMDocument* loader = doc->getDocument();
0094   DOMElement* root = loader->getDocumentElement();
0095 
0096   XMLDOMBlock dataSetDoc(templateFileName);
0097   DOMDocument* dataSet = dataSetDoc.getDocument();
0098 
0099   //Dataset
0100   dataSet->getElementsByTagName(_toXMLCh("SIDE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->side));
0101   dataSet->getElementsByTagName(_toXMLCh("ETA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->eta));
0102   dataSet->getElementsByTagName(_toXMLCh("PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->phi));
0103   dataSet->getElementsByTagName(_toXMLCh("DELTA_PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dphi));
0104 
0105   dataSet->getElementsByTagName(_toXMLCh("DEPTH"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->depth));
0106   dataSet->getElementsByTagName(_toXMLCh("SUBDETECTOR"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->det));
0107   dataSet->getElementsByTagName(_toXMLCh("RBX_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rbx));
0108   dataSet->getElementsByTagName(_toXMLCh("WEDGE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->wedge));
0109 
0110   dataSet->getElementsByTagName(_toXMLCh("RM_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rm));
0111   dataSet->getElementsByTagName(_toXMLCh("HPD_PIXEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->pixel));
0112   dataSet->getElementsByTagName(_toXMLCh("QIE_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->qie));
0113   dataSet->getElementsByTagName(_toXMLCh("ADC"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->adc));
0114   dataSet->getElementsByTagName(_toXMLCh("RM_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rm_fi));
0115 
0116   dataSet->getElementsByTagName(_toXMLCh("FIBER_CHANNEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fi_ch));
0117   dataSet->getElementsByTagName(_toXMLCh("CRATE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->crate));
0118   dataSet->getElementsByTagName(_toXMLCh("HTR_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htr));
0119   dataSet->getElementsByTagName(_toXMLCh("HTR_FPGA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fpga));
0120 
0121   dataSet->getElementsByTagName(_toXMLCh("HTR_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htr_fi));
0122   dataSet->getElementsByTagName(_toXMLCh("DCC_SL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dcc_sl));
0123   dataSet->getElementsByTagName(_toXMLCh("SPIGOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->spigo));
0124   dataSet->getElementsByTagName(_toXMLCh("DCC_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dcc));
0125   dataSet->getElementsByTagName(_toXMLCh("SLB_SITE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slb));
0126   dataSet->getElementsByTagName(_toXMLCh("SLB_CHANNEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slbin));
0127   dataSet->getElementsByTagName(_toXMLCh("SLB_CHANNEL2"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slbin2));
0128 
0129   dataSet->getElementsByTagName(_toXMLCh("SLB_CABLE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slnam));
0130   dataSet->getElementsByTagName(_toXMLCh("RCT_CRATE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rctcra));
0131   dataSet->getElementsByTagName(_toXMLCh("RCT_CARD"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rctcar));
0132   dataSet->getElementsByTagName(_toXMLCh("RCT_CONNECTOR"))
0133       ->item(0)
0134       ->getFirstChild()
0135       ->setNodeValue(_toXMLCh(row->rctcon));
0136   dataSet->getElementsByTagName(_toXMLCh("RCT_NAME"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rctnam));
0137   dataSet->getElementsByTagName(_toXMLCh("FED_ID"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fedid));
0138 
0139   // copy the <data_set> node into the final XML
0140   DOMNode* cloneDataSet = loader->importNode(dataSet->getDocumentElement(), true);
0141   root->appendChild(cloneDataSet);
0142 
0143   return 0;
0144 }
0145 
0146 XMLDOMBlock* XMLProcessor::createLMapHOXMLBase(std::string templateFileName) {
0147   XMLDOMBlock* result = new XMLDOMBlock(templateFileName);
0148   DOMDocument* loader = result->getDocument();
0149   //DOMElement * root = loader -> getDocumentElement();
0150 
0151   loader->getElementsByTagName(_toXMLCh("NAME"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh("HCAL LMAP for HO"));
0152   //DOMElement * _tag = (DOMElement *)(loader -> getElementsByTagName( _toXMLCh( "TAG" ) ) -> item(0));
0153   //_tag -> setAttribute( _toXMLCh("mode"), _toXMLCh("test_mode") );
0154 
0155   return result;
0156 }
0157 
0158 int XMLProcessor::addLMapHODataset(XMLDOMBlock* doc, LMapRowHO* row, std::string templateFileName) {
0159   DOMDocument* loader = doc->getDocument();
0160   DOMElement* root = loader->getDocumentElement();
0161 
0162   XMLDOMBlock dataSetDoc(templateFileName);
0163   DOMDocument* dataSet = dataSetDoc.getDocument();
0164 
0165   //Dataset
0166   dataSet->getElementsByTagName(_toXMLCh("SIDE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->sideO));
0167   dataSet->getElementsByTagName(_toXMLCh("ETA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->etaO));
0168   dataSet->getElementsByTagName(_toXMLCh("PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->phiO));
0169   dataSet->getElementsByTagName(_toXMLCh("DELTA_PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dphiO));
0170 
0171   dataSet->getElementsByTagName(_toXMLCh("DEPTH"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->depthO));
0172   dataSet->getElementsByTagName(_toXMLCh("SUBDETECTOR"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->detO));
0173   dataSet->getElementsByTagName(_toXMLCh("RBX_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rbxO));
0174   dataSet->getElementsByTagName(_toXMLCh("SECTOR"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->sectorO));
0175 
0176   dataSet->getElementsByTagName(_toXMLCh("RM_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rmO));
0177   dataSet->getElementsByTagName(_toXMLCh("HPD_PIXEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->pixelO));
0178   dataSet->getElementsByTagName(_toXMLCh("QIE_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->qieO));
0179   dataSet->getElementsByTagName(_toXMLCh("ADC"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->adcO));
0180   dataSet->getElementsByTagName(_toXMLCh("RM_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rm_fiO));
0181   dataSet->getElementsByTagName(_toXMLCh("FIBER_CHANNEL"))
0182       ->item(0)
0183       ->getFirstChild()
0184       ->setNodeValue(_toXMLCh(row->fi_chO));
0185 
0186   dataSet->getElementsByTagName(_toXMLCh("LETTER_CODE"))
0187       ->item(0)
0188       ->getFirstChild()
0189       ->setNodeValue(_toXMLCh(row->let_codeO));
0190   dataSet->getElementsByTagName(_toXMLCh("CRATE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->crateO));
0191   dataSet->getElementsByTagName(_toXMLCh("HTR_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htrO));
0192   dataSet->getElementsByTagName(_toXMLCh("HTR_FPGA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fpgaO));
0193 
0194   dataSet->getElementsByTagName(_toXMLCh("HTR_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htr_fiO));
0195   dataSet->getElementsByTagName(_toXMLCh("DCC_SL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dcc_slO));
0196   dataSet->getElementsByTagName(_toXMLCh("SPIGOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->spigoO));
0197   dataSet->getElementsByTagName(_toXMLCh("DCC_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dccO));
0198   dataSet->getElementsByTagName(_toXMLCh("FED_ID"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fedidO));
0199 
0200   // copy the <data_set> node into the final XML
0201   DOMNode* cloneDataSet = loader->importNode(dataSet->getDocumentElement(), true);
0202   root->appendChild(cloneDataSet);
0203 
0204   return 0;
0205 }
0206 
0207 int XMLProcessor::write(XMLDOMBlock* doc, std::string target) {
0208   DOMDocument* loader = doc->getDocument();
0209   //DOMElement * root = loader -> getDocumentElement();
0210 
0211   XMLCh* _t;
0212   _t = serializeDOM(loader, target);
0213   delete _t;
0214 
0215   return 0;
0216 }
0217 
0218 int XMLProcessor::test(void) {
0219   //if ( init() != 0 ) return 1;
0220 
0221   XMLDOMBlock dataBlock("HCAL_TRIG_PRIM_LOOKUP_TABLE.datablock.template");
0222 
0223   DOMDocument* dataBlockDocument = dataBlock.getDocument();
0224 
0225   std::cout << "===> Tag length: " << dataBlockDocument->getElementsByTagName(_toXMLCh("CREATED_BY_USER"))->getLength()
0226             << std::endl;
0227   std::cout << "===> Tag name: "
0228             << XMLString::transcode(
0229                    dataBlockDocument->getElementsByTagName(_toXMLCh("CREATED_BY_USER"))->item(0)->getNodeName())
0230             << std::endl;
0231   dataBlockDocument->getElementsByTagName(_toXMLCh("CREATED_BY_USER"))
0232       ->item(0)
0233       ->getFirstChild()
0234       ->setNodeValue(_toXMLCh("kukarzev test"));
0235 
0236   XMLCh* _t;
0237   _t = serializeDOM(dataBlockDocument);
0238   delete _t;
0239 
0240   //terminate();
0241 
0242   return 0;
0243 }
0244 
0245 XMLCh* XMLProcessor::serializeDOM(DOMNode* node, std::string target) {
0246   XMLCh tempStr[100];
0247   XMLString::transcode("LS", tempStr, 99);
0248   DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
0249   DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
0250   DOMConfiguration* dc = theSerializer->getDomConfig();
0251   dc->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
0252   dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0253 
0254   XMLFormatTarget* myFormTarget = nullptr;
0255   XMLCh* _string = nullptr;
0256   if (target == "stdout" || target == "string") {
0257     myFormTarget = new StdOutFormatTarget();
0258   }
0259   //else if ( target == "memory" )
0260   //  {
0261   //    myFormTarget = new MemBufFormatTarget();
0262   //  }
0263   else {
0264     myFormTarget = new LocalFileFormatTarget(_toXMLCh(target));
0265   }
0266 
0267   try {
0268     if (target == "string") {
0269       _string = theSerializer->writeToString(node);
0270     } else {
0271       DOMLSOutput* outputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
0272       outputDesc->setByteStream(myFormTarget);
0273       theSerializer->write(node, outputDesc);
0274     }
0275   } catch (const XMLException& toCatch) {
0276     char* message = XMLString::transcode(toCatch.getMessage());
0277     std::cout << "Exception message is: \n" << message << "\n";
0278     XMLString::release(&message);
0279     return nullptr;
0280   } catch (const DOMException& toCatch) {
0281     char* message = XMLString::transcode(toCatch.msg);
0282     std::cout << "Exception message is: \n" << message << "\n";
0283     XMLString::release(&message);
0284     return nullptr;
0285   } catch (...) {
0286     std::cout << "Unexpected Exception \n";
0287     return nullptr;
0288   }
0289 
0290   theSerializer->release();
0291   if (myFormTarget)
0292     delete myFormTarget;
0293   return _string;
0294 }
0295 
0296 int XMLProcessor::init(void) {
0297   std::cerr << "Intializing Xerces-c...";
0298   try {
0299     cms::concurrency::xercesInitialize();
0300     //
0301     //_____ following removed as a xalan-c component_____________________
0302     //
0303     //XPathEvaluator::initialize();
0304   } catch (const XMLException& toCatch) {
0305     std::cout << " FAILED! Exiting..." << std::endl;
0306     return 1;
0307   }
0308   std::cerr << " done" << std::endl;
0309 
0310   return 0;
0311 }
0312 
0313 int XMLProcessor::terminate(void) {
0314   //
0315   //_____ following removed as a xalan-c component_____________________
0316   //
0317   //std::cout << "Terminating Xalan-c...";
0318   //XPathEvaluator::terminate();
0319   //std::cout << " done" << std::endl;
0320 
0321   std::cout << "Terminating Xerces-c...";
0322   cms::concurrency::xercesTerminate();
0323   std::cout << " done" << std::endl;
0324 
0325   // Other terminations and cleanup.
0326 
0327   return 0;
0328 }