Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:37

0001 #ifndef CALIBRATIONXML_H
0002 #define CALIBRATIONXML_H
0003 
0004 #include <xercesc/util/XMLString.hpp>
0005 #include <xercesc/dom/DOM.hpp>
0006 #include <xercesc/dom/DOMElement.hpp>
0007 #include <string>
0008 #include <sstream>
0009 #include <xercesc/sax/HandlerBase.hpp>
0010 #include <xercesc/parsers/XercesDOMParser.hpp>
0011 #include "Utilities/Xerces/interface/Xerces.h"
0012 
0013 class CalibrationXML {
0014 public:
0015   typedef XERCES_CPP_NAMESPACE::DOMDocument DOMDocument;
0016   typedef XERCES_CPP_NAMESPACE::DOMElement DOMElement;
0017   typedef XERCES_CPP_NAMESPACE::DOMNode DOMNode;
0018   typedef XERCES_CPP_NAMESPACE::HandlerBase HandlerBase;
0019   typedef XERCES_CPP_NAMESPACE::XercesDOMParser XercesDOMParser;
0020   typedef XERCES_CPP_NAMESPACE::XMLPlatformUtils XMLPlatformUtils;
0021   typedef XERCES_CPP_NAMESPACE::XMLString XMLString;
0022 
0023   CalibrationXML();
0024   ~CalibrationXML();
0025 
0026   /**
0027     * Open an XML file
0028     */
0029   void openFile(const std::string &xmlFileName);
0030 
0031   /**
0032     * Save DOM to file
0033     */
0034   void saveFile(const std::string &xmlFileName);
0035 
0036   void closeFile() {
0037     delete errHandler;
0038     delete parser;
0039     cms::concurrency::xercesTerminate();
0040     errHandler = nullptr;
0041     parser = nullptr;
0042   }
0043   /**
0044     * Return the root DOM Element of the opened XML calibration file
0045     */
0046   DOMElement *calibrationDOM() { return m_calibrationDOM; }
0047 
0048   //Static function to make everything easier, less transcode and type conversion
0049   /**
0050     * Helper static function to write an attribute in a DOM Element
0051     */
0052   template <class T>
0053   static void writeAttribute(DOMElement *dom, const std::string &name, const T &value) {
0054     std::ostringstream buffer;
0055     buffer << value;
0056     XMLCh *nameStr = XMLString::transcode(name.c_str());
0057     XMLCh *valueStr = XMLString::transcode(buffer.str().c_str());
0058     dom->setAttribute(nameStr, valueStr);
0059     XMLString::release(&nameStr);
0060     XMLString::release(&valueStr);
0061   }
0062 
0063   /**
0064         * Helper static function to read an attribute in a DOM Element
0065         */
0066   template <class T>
0067   static T readAttribute(DOMElement *dom, const std::string &name) {
0068     XMLCh *nameStr = XMLString::transcode(name.c_str());
0069     char *valueStr = XMLString::transcode(dom->getAttribute(nameStr));
0070     std::istringstream buffer(valueStr);
0071     T value;
0072     buffer >> value;
0073     XMLString::release(&nameStr);
0074     XMLString::release(&valueStr);
0075     return value;
0076   }
0077 
0078   /**
0079     * Helper static function to add a child in a DOM Element with indentation
0080     */
0081   static DOMElement *addChild(DOMNode *dom, const std::string &name);
0082 
0083 private:
0084   std::string m_xmlFileName;
0085   DOMElement *m_calibrationDOM;
0086   DOMDocument *doc;
0087   HandlerBase *errHandler;
0088   XercesDOMParser *parser;
0089 };
0090 #endif