Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:02

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