CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/CalibCalorimetry/HcalTPGAlgos/src/XMLProcessor.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     XMLTools
00004 // Class  :     XMLProcessor
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  
00010 //         Created:  Fri Sep 21 15:56:27 CEST 2007
00011 // $Id: XMLProcessor.cc,v 1.3 2010/08/06 20:24:03 wmtan Exp $
00012 //
00013 
00014 // system include files
00015 #include <vector>
00016 #include <string>
00017 #include <iostream>
00018 #include <sys/types.h>
00019 #include <pwd.h>
00020 #include <unistd.h>
00021 #include <xercesc/parsers/XercesDOMParser.hpp>
00022 #include <xercesc/dom/DOM.hpp>
00023 #include <xercesc/sax/HandlerBase.hpp>
00024 #include <xercesc/util/XMLString.hpp>
00025 #include <xercesc/util/PlatformUtils.hpp>
00026 #include <xercesc/dom/DOMNode.hpp>
00027 #include <xercesc/framework/StdOutFormatTarget.hpp>
00028 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00029 
00030 // xalan-c init
00031 //#include <xalanc/Include/PlatformDefinitions.hpp>
00032 //#include <xalanc/XPath/XPathEvaluator.hpp>
00033 //#include <xalanc/XalanTransformer/XalanTransformer.hpp>
00034 //using namespace xalanc;
00035 
00036 using namespace std;
00037 XERCES_CPP_NAMESPACE_USE 
00038 
00039 // user include files
00040 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
00041 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLDOMBlock.h"
00042 
00043 XMLProcessor * XMLProcessor::instance = NULL;
00044 
00045 XMLProcessor::XMLProcessor()
00046 {
00047   // initializes Xerces, must be done only once
00048   init();
00049 }
00050 
00051 XMLProcessor::~XMLProcessor()
00052 {
00053   // terminates Xerces
00054   terminate();
00055 }
00056 
00057 //_loaderBaseConfig::_loaderBaseConfig
00058 XMLProcessor::loaderBaseConfig::_loaderBaseConfig()
00059 {
00060   extention_table_name = "HCAL_TRIG_PRIM_LOOKUP_TABLES";
00061   name = "HCAL trigger primitive lookup table";
00062   run_mode = "no-run";
00063   data_set_id = "-1";
00064   iov_id = "1";
00065   iov_begin = "0";
00066   iov_end = "1";
00067   tag_id = "2";
00068   tag_mode = "auto";
00069   tag_name = "dummy tag";
00070   detector_name = "HCAL";
00071   comment_description = "empty comment";
00072 }
00073 
00074 XMLProcessor::DBConfig::_DBConfig()
00075 {
00076   version = "test:2";
00077   subversion = "1";
00078   create_timestamp = time( NULL );
00079   created_by_user = getpwuid( getuid() ) -> pw_name;
00080 }
00081 
00082 XMLDOMBlock * XMLProcessor::createLMapHBEFXMLBase( std::string templateFileName )
00083 {
00084   XMLDOMBlock * result = new XMLDOMBlock( templateFileName );
00085   DOMDocument * loader = result -> getDocument();
00086   //DOMElement * root = loader -> getDocumentElement();
00087 
00088   loader -> getElementsByTagName( _toXMLCh( "NAME" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( "HCAL LMAP for HB, HE, HF" ) );
00089   //DOMElement * _tag = (DOMElement *)(loader -> getElementsByTagName( _toXMLCh( "TAG" ) ) -> item(0));
00090   //_tag -> setAttribute( _toXMLCh("mode"), _toXMLCh("test_mode") );
00091 
00092   return result;
00093 }
00094 
00095 int XMLProcessor::addLMapHBEFDataset( XMLDOMBlock * doc, LMapRowHBEF * row, std::string templateFileName )
00096 {
00097   DOMDocument * loader = doc -> getDocument();
00098   DOMElement * root = loader -> getDocumentElement();
00099 
00100   XMLDOMBlock dataSetDoc( templateFileName );
00101   DOMDocument * dataSet = dataSetDoc . getDocument();
00102   
00103   //Dataset
00104   dataSet -> getElementsByTagName( _toXMLCh( "SIDE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> side ) );  
00105   dataSet -> getElementsByTagName( _toXMLCh( "ETA" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->  eta) );  
00106   dataSet -> getElementsByTagName( _toXMLCh( "PHI" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> phi ) );  
00107   dataSet -> getElementsByTagName( _toXMLCh( "DELTA_PHI" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> dphi ) );  
00108 
00109   dataSet -> getElementsByTagName( _toXMLCh( "DEPTH" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> depth  ) );  
00110   dataSet -> getElementsByTagName( _toXMLCh( "SUBDETECTOR" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> det ) );  
00111   dataSet -> getElementsByTagName( _toXMLCh( "RBX_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rbx ) );  
00112   dataSet -> getElementsByTagName( _toXMLCh( "WEDGE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> wedge ) );
00113  
00114   dataSet -> getElementsByTagName( _toXMLCh( "RM_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rm  ) );  
00115   dataSet -> getElementsByTagName( _toXMLCh( "HPD_PIXEL" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> pixel ) );  
00116   dataSet -> getElementsByTagName( _toXMLCh( "QIE_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> qie ) );  
00117   dataSet -> getElementsByTagName( _toXMLCh( "ADC" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> adc ) );  
00118   dataSet -> getElementsByTagName( _toXMLCh( "RM_FIBER" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rm_fi ) );
00119 
00120   dataSet -> getElementsByTagName( _toXMLCh( "FIBER_CHANNEL" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> fi_ch));
00121   dataSet -> getElementsByTagName( _toXMLCh( "CRATE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> crate ) );  
00122   dataSet -> getElementsByTagName( _toXMLCh( "HTR_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->  htr ) );  
00123   dataSet -> getElementsByTagName( _toXMLCh( "HTR_FPGA" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> fpga ) );
00124 
00125   dataSet -> getElementsByTagName( _toXMLCh( "HTR_FIBER" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> htr_fi ));  
00126   dataSet -> getElementsByTagName( _toXMLCh( "DCC_SL" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> dcc_sl ) );  
00127   dataSet -> getElementsByTagName( _toXMLCh( "SPIGOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->  spigo ) );  
00128   dataSet -> getElementsByTagName( _toXMLCh( "DCC_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> dcc ) );  
00129   dataSet -> getElementsByTagName( _toXMLCh( "SLB_SITE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> slb ) );  
00130   dataSet -> getElementsByTagName( _toXMLCh( "SLB_CHANNEL" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> slbin )); 
00131   dataSet -> getElementsByTagName( _toXMLCh( "SLB_CHANNEL2" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> slbin2));
00132 
00133   dataSet -> getElementsByTagName( _toXMLCh( "SLB_CABLE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> slnam ) );  
00134   dataSet -> getElementsByTagName( _toXMLCh( "RCT_CRATE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rctcra ) ); 
00135   dataSet -> getElementsByTagName( _toXMLCh( "RCT_CARD" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rctcar ) );
00136   dataSet -> getElementsByTagName( _toXMLCh( "RCT_CONNECTOR" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->rctcon));
00137   dataSet -> getElementsByTagName( _toXMLCh( "RCT_NAME" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rctnam ) );  
00138   dataSet -> getElementsByTagName( _toXMLCh( "FED_ID" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> fedid ) );
00139   
00140       
00141   // copy the <data_set> node into the final XML
00142   DOMNode * cloneDataSet = loader -> importNode( dataSet -> getDocumentElement(), true );
00143   root -> appendChild( cloneDataSet );
00144 
00145   return 0;
00146 }
00147 
00148 XMLDOMBlock * XMLProcessor::createLMapHOXMLBase( std::string templateFileName )
00149 {
00150   XMLDOMBlock * result = new XMLDOMBlock( templateFileName );
00151   DOMDocument * loader = result -> getDocument();
00152   //DOMElement * root = loader -> getDocumentElement();
00153 
00154   loader -> getElementsByTagName( _toXMLCh( "NAME" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( "HCAL LMAP for HO" ) );
00155   //DOMElement * _tag = (DOMElement *)(loader -> getElementsByTagName( _toXMLCh( "TAG" ) ) -> item(0));
00156   //_tag -> setAttribute( _toXMLCh("mode"), _toXMLCh("test_mode") );
00157 
00158   return result;
00159 }
00160 
00161 int XMLProcessor::addLMapHODataset( XMLDOMBlock * doc, LMapRowHO * row, std::string templateFileName )
00162 {
00163   DOMDocument * loader = doc -> getDocument();
00164   DOMElement * root = loader -> getDocumentElement();
00165 
00166   XMLDOMBlock dataSetDoc( templateFileName );
00167   DOMDocument * dataSet = dataSetDoc . getDocument();
00168   
00169   //Dataset
00170   dataSet -> getElementsByTagName( _toXMLCh( "SIDE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> sideO ) );  
00171   dataSet -> getElementsByTagName( _toXMLCh( "ETA" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->  etaO ) );  
00172   dataSet -> getElementsByTagName( _toXMLCh( "PHI" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> phiO ) );  
00173   dataSet -> getElementsByTagName( _toXMLCh( "DELTA_PHI" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> dphiO ) );
00174   
00175   dataSet -> getElementsByTagName( _toXMLCh( "DEPTH" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> depthO  ) );  
00176   dataSet -> getElementsByTagName( _toXMLCh( "SUBDETECTOR" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> detO ) ); 
00177   dataSet -> getElementsByTagName( _toXMLCh( "RBX_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rbxO ) );  
00178   dataSet -> getElementsByTagName( _toXMLCh( "SECTOR" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> sectorO ) );
00179  
00180   dataSet -> getElementsByTagName( _toXMLCh( "RM_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rmO  ) );  
00181   dataSet -> getElementsByTagName( _toXMLCh( "HPD_PIXEL" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> pixelO ) ); 
00182   dataSet -> getElementsByTagName( _toXMLCh( "QIE_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> qieO ) );  
00183   dataSet -> getElementsByTagName( _toXMLCh( "ADC" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> adcO ) );  
00184   dataSet -> getElementsByTagName( _toXMLCh( "RM_FIBER" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> rm_fiO ) );  
00185   dataSet -> getElementsByTagName( _toXMLCh( "FIBER_CHANNEL" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->fi_chO));
00186 
00187   dataSet -> getElementsByTagName( _toXMLCh( "LETTER_CODE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh(row ->let_codeO));
00188   dataSet -> getElementsByTagName( _toXMLCh( "CRATE" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> crateO ) );  
00189   dataSet -> getElementsByTagName( _toXMLCh( "HTR_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->  htrO ) );  
00190   dataSet -> getElementsByTagName( _toXMLCh( "HTR_FPGA" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> fpgaO ) );
00191 
00192   dataSet -> getElementsByTagName( _toXMLCh( "HTR_FIBER" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> htr_fiO ) );
00193   dataSet -> getElementsByTagName( _toXMLCh( "DCC_SL" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> dcc_slO ) );  
00194   dataSet -> getElementsByTagName( _toXMLCh( "SPIGOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row ->  spigoO ) );  
00195   dataSet -> getElementsByTagName( _toXMLCh( "DCC_SLOT" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> dccO ) );  
00196   dataSet -> getElementsByTagName( _toXMLCh( "FED_ID" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( row -> fedidO ) );
00197   
00198       
00199   // copy the <data_set> node into the final XML
00200   DOMNode * cloneDataSet = loader -> importNode( dataSet -> getDocumentElement(), true );
00201   root -> appendChild( cloneDataSet );
00202 
00203   return 0;
00204 }
00205 
00206 int XMLProcessor::write( XMLDOMBlock * doc, std::string target )
00207 {
00208   DOMDocument * loader = doc -> getDocument();
00209   //DOMElement * root = loader -> getDocumentElement();
00210 
00211   XMLCh * _t;
00212   _t = serializeDOM( loader, target );
00213   delete _t;
00214 
00215   return 0;
00216 }
00217 
00218 int XMLProcessor::test( void )
00219 {
00220   //if ( init() != 0 ) return 1;
00221 
00222   XMLDOMBlock dataBlock( "HCAL_TRIG_PRIM_LOOKUP_TABLE.datablock.template" );
00223 
00224   DOMDocument * dataBlockDocument = dataBlock . getDocument();
00225 
00226   std::cout << "===> Tag length: " << dataBlockDocument -> getElementsByTagName( _toXMLCh( "CREATED_BY_USER" ) ) -> getLength() << std::endl;
00227   std::cout << "===> Tag name: " << XMLString::transcode( dataBlockDocument -> getElementsByTagName( _toXMLCh( "CREATED_BY_USER" ) ) -> item(0) -> getNodeName() ) << std::endl;
00228   dataBlockDocument -> getElementsByTagName( _toXMLCh( "CREATED_BY_USER" ) ) -> item(0) -> getFirstChild() -> setNodeValue( _toXMLCh( "kukarzev test" ) );
00229 
00230   XMLCh * _t;
00231   _t = serializeDOM( dataBlockDocument );
00232   delete _t;
00233 
00234   //terminate();
00235 
00236   return 0;
00237 }
00238 
00239 XMLCh * XMLProcessor::serializeDOM(DOMNode* node, std::string target)
00240 {
00241   XMLCh tempStr[100];
00242   XMLString::transcode("LS", tempStr, 99);
00243   DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
00244   DOMWriter* theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();
00245   
00246   if (theSerializer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, true))
00247     theSerializer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent, true);
00248   
00249   if (theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
00250     theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00251     
00252   XMLFormatTarget * myFormTarget = 0;
00253   XMLCh * _string = 0;
00254   if ( target == "stdout" || target == "string" )
00255     {
00256       myFormTarget = new StdOutFormatTarget();
00257     }
00258   //else if ( target == "memory" )
00259   //  {
00260   //    myFormTarget = new MemBufFormatTarget();
00261   //  }
00262   else
00263     {
00264       myFormTarget = new LocalFileFormatTarget( _toXMLCh( target ) );
00265     }
00266   
00267   try {
00268     if ( target == "string" ){
00269       _string = theSerializer->writeToString( *node );
00270     }
00271     else{
00272       theSerializer->writeNode(myFormTarget, *node);
00273     }
00274   }
00275   catch (const XMLException& toCatch) {
00276     char* message = XMLString::transcode(toCatch.getMessage());
00277     std::cout << "Exception message is: \n"
00278          << message << "\n";
00279     XMLString::release(&message);
00280     return 0;
00281   }
00282   catch (const DOMException& toCatch) {
00283     char* message = XMLString::transcode(toCatch.msg);
00284     std::cout << "Exception message is: \n"
00285          << message << "\n";
00286     XMLString::release(&message);
00287     return NULL;
00288   }
00289   catch (...) {
00290     std::cout << "Unexpected Exception \n" ;
00291     return NULL;
00292   }
00293     
00294   theSerializer->release();
00295   if ( myFormTarget ) delete myFormTarget;
00296   return _string;
00297 }
00298 
00299 int XMLProcessor::init( void )
00300 {
00301   std::cerr << "Intializing Xerces-c...";
00302   try {
00303     XMLPlatformUtils::Initialize();
00304     //
00305     //_____ following removed as a xalan-c component_____________________
00306     //
00307     //XPathEvaluator::initialize();
00308   }
00309   catch (const XMLException& toCatch) {
00310     std::cout << " FAILED! Exiting..." << std::endl;
00311     return 1;
00312   }
00313   std::cerr << " done" << std::endl;
00314 
00315   return 0;
00316 }
00317 
00318 int XMLProcessor::terminate( void )
00319 {
00320   //
00321   //_____ following removed as a xalan-c component_____________________
00322   //
00323   //std::cout << "Terminating Xalan-c...";
00324   //XPathEvaluator::terminate();
00325   //std::cout << " done" << std::endl;
00326 
00327   std::cout << "Terminating Xerces-c...";
00328   XMLPlatformUtils::Terminate();
00329   std::cout << " done" << std::endl;
00330 
00331 
00332   // Other terminations and cleanup.
00333 
00334   return 0;
00335 }
00336 
00337