Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:51

0001 
0002 #include "RecoBTag/XMLCalibration/interface/CalibrationXML.h"
0003 
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 
0006 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0007 #include <xercesc/parsers/XercesDOMParser.hpp>
0008 #include <xercesc/dom/DOM.hpp>
0009 #include <xercesc/sax/HandlerBase.hpp>
0010 #include <xercesc/util/XMLString.hpp>
0011 #include "Utilities/Xerces/interface/Xerces.h"
0012 #include <iostream>
0013 #include <fstream>
0014 #include <string>
0015 
0016 using namespace std;
0017 
0018 XERCES_CPP_NAMESPACE_USE
0019 
0020 CalibrationXML::CalibrationXML() : errHandler(nullptr), parser(nullptr) {}
0021 
0022 CalibrationXML::~CalibrationXML() {
0023   //TODO: delete!!!!
0024   if (errHandler)
0025     delete errHandler;
0026   if (parser) {
0027     delete parser;
0028     cms::concurrency::xercesTerminate();
0029   }
0030 }
0031 
0032 void CalibrationXML::openFile(const std::string& xmlFileName) {
0033   if (errHandler)
0034     delete errHandler;
0035   if (parser) {
0036     delete parser;
0037     cms::concurrency::xercesTerminate();
0038   }
0039 
0040   m_xmlFileName = xmlFileName;
0041   // std::cout << "Opening.." << std::endl;
0042   // Initialize the XML4C2 system
0043   try {
0044     cms::concurrency::xercesInitialize();
0045   } catch (const XMLException& toCatch) {
0046     std::cerr << "Error during Xerces-c Initialization.\n"
0047               << "  Exception message:" << XMLString::transcode(toCatch.getMessage()) << std::endl;
0048     abort();
0049     //FIXME     throw GenTerminate("Error during Xerces-c Initialization.");
0050   }
0051   parser = new XercesDOMParser;
0052   parser->setValidationScheme(XercesDOMParser::Val_Auto);
0053   parser->setDoNamespaces(false);
0054   parser->setDoSchema(false);
0055   parser->setValidationSchemaFullChecking(false);
0056   errHandler = new HandlerBase;
0057   parser->setErrorHandler(errHandler);
0058   parser->setCreateEntityReferenceNodes(false);
0059   //  Parse the XML file, catching any XML exceptions that might propogate out of it.
0060   bool errorsOccured = false;
0061   try {
0062     edm::LogInfo("XMLCalibration") << "Calibration XML: parsing " << m_xmlFileName.c_str() << std::endl;
0063     parser->parse(m_xmlFileName.c_str());
0064     int errorCount = parser->getErrorCount();
0065     if (errorCount > 0)
0066       errorsOccured = true;
0067   } catch (const XMLException& e) {
0068     std::cerr << "A DOM error occured during parsing\n   DOMException code: " << (long unsigned int)e.getCode()
0069               << std::endl;
0070     errorsOccured = true;
0071   }
0072   // If the parse was successful, build the structure we want to have
0073   if (errorsOccured) {
0074     std::cerr << "An error occured during parsing\n"
0075               << "Please check your input with SAXCount or a similar tool.\n Exiting!\n"
0076               << std::endl;
0077     abort();
0078     //FIXME     throw GenTerminate("An error occured during parsing\n Please check your input with SAXCount or a similar tool.\n Exiting!\n");
0079   }
0080 
0081   doc = parser->getDocument();
0082   DOMNode* n1 = doc->getFirstChild();
0083 
0084   while (n1) {
0085     if (n1->getNodeType() == DOMNode::ELEMENT_NODE)
0086       break;
0087     n1 = n1->getNextSibling();
0088   }
0089 
0090   if (n1 == nullptr || strcmp("Calibration", XMLString::transcode(n1->getNodeName())))
0091     abort();
0092   //FIXME       throw GenTerminate("The root element in the XML Calibration file is not a Calibration element.\n This should be forbidden at the DTD level.");
0093   else {
0094     edm::LogInfo("XMLCalibration") << "Calibration found";
0095   }
0096 
0097   m_calibrationDOM = (DOMElement*)n1;
0098 }
0099 
0100 void CalibrationXML::saveFile(const std::string& xmlFileName) {
0101   DOMImplementation* theImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
0102   DOMLSSerializer* theSerializer = ((DOMImplementation*)theImpl)->createLSSerializer();
0103   DOMConfiguration* dc = theSerializer->getDomConfig();
0104   dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0105 
0106   XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(XMLString::transcode(xmlFileName.c_str()));
0107   DOMLSOutput* outputDesc = ((DOMImplementationLS*)theImpl)->createLSOutput();
0108   outputDesc->setByteStream(myFormTarget);
0109 
0110   theSerializer->write(doc, outputDesc);
0111   delete myFormTarget;
0112 }
0113 
0114 DOMElement* CalibrationXML::addChild(DOMNode* dom, const std::string& name) {
0115   DOMNode* n1 = dom;
0116   int level = 0;
0117   std::string indent = "\n";
0118   while (n1 && level < 100) {
0119     level++;
0120     indent += "  ";
0121     n1 = n1->getParentNode();
0122   }
0123   assert(dom);
0124   if (dom->getFirstChild() == nullptr)
0125     dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
0126 
0127   DOMElement* child =
0128       (DOMElement*)dom->appendChild(dom->getOwnerDocument()->createElement(XMLString::transcode(name.c_str())));
0129   dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
0130   return child;
0131 }