Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:57

0001 #include "CondTools/Ecal/interface/EcalClusterEnergyCorrectionXMLTranslator.h"
0002 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0003 #include "Utilities/Xerces/interface/Xerces.h"
0004 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0005 #include <xercesc/dom/DOMNode.hpp>
0006 #include <xercesc/dom/DOM.hpp>
0007 #include <xercesc/parsers/XercesDOMParser.hpp>
0008 #include <xercesc/util/XMLString.hpp>
0009 #include <xercesc/sax/SAXException.hpp>
0010 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0011 #include <iostream>
0012 #include <sstream>
0013 #include <fstream>
0014 
0015 using namespace XERCES_CPP_NAMESPACE;
0016 using namespace xuti;
0017 using namespace std;
0018 
0019 int EcalClusterEnergyCorrectionXMLTranslator::readXML(const string& filename,
0020                                                       EcalCondHeader& header,
0021                                                       EcalFunParams& record) {
0022   cms::concurrency::xercesInitialize();
0023 
0024   XercesDOMParser* parser = new XercesDOMParser;
0025   parser->setValidationScheme(XercesDOMParser::Val_Never);
0026   parser->setDoNamespaces(false);
0027   parser->setDoSchema(false);
0028 
0029   parser->parse(filename.c_str());
0030 
0031   DOMDocument* xmlDoc = parser->getDocument();
0032 
0033   if (!xmlDoc) {
0034     std::cout << "EcalClusterEnergyCorrectionXMLTranslator::Error parsing document" << std::endl;
0035     return -1;
0036   }
0037 
0038   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0039 
0040   xuti::readHeader(elementRoot, header);
0041 
0042   // need some extra code here
0043 
0044   delete parser;
0045   cms::concurrency::xercesTerminate();
0046   return 0;
0047 }
0048 
0049 std::string EcalClusterEnergyCorrectionXMLTranslator::dumpXML(const EcalCondHeader& header,
0050                                                               const EcalFunParams& record) {
0051   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0052 
0053   DOMLSSerializer* writer = impl->createLSSerializer();
0054   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0055     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0056 
0057   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0058 
0059   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr("EcalClusterEnergyCorrection").ptr(), doctype);
0060 
0061   DOMElement* root = doc->getDocumentElement();
0062   xuti::writeHeader(root, header);
0063 
0064   for (auto it : record.params()) {
0065     DOMElement* ECEC = root->getOwnerDocument()->createElement(cms::xerces::uStr("ClusterEnergy").ptr());
0066     root->appendChild(ECEC);
0067 
0068     WriteNodeWithValue(ECEC, Value_tag, it);
0069   }
0070 
0071   std::string dump = cms::xerces::toString(writer->writeToString(root));
0072   doc->release();
0073   doctype->release();
0074   writer->release();
0075 
0076   return dump;
0077 }
0078 
0079 int EcalClusterEnergyCorrectionXMLTranslator::writeXML(const std::string& filename,
0080                                                        const EcalCondHeader& header,
0081                                                        const EcalFunParams& record) {
0082   cms::concurrency::xercesInitialize();
0083 
0084   std::fstream fs(filename.c_str(), ios::out);
0085   fs << dumpXML(header, record);
0086 
0087   cms::concurrency::xercesTerminate();
0088 
0089   return 0;
0090 }