CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/RecoBTag/XMLCalibration/interface/CalibrationXML.h

Go to the documentation of this file.
00001 #ifndef CALIBRATIONXML_H 
00002 #define CALIBRATIONXML_H 
00003 
00004 #include <xercesc/util/XMLString.hpp>
00005 #include <xercesc/dom/DOM.hpp>
00006 #include <xercesc/dom/DOMElement.hpp>
00007 #include <string>
00008 #include <sstream>
00009 #include <xercesc/sax/HandlerBase.hpp>
00010 #include <xercesc/parsers/XercesDOMParser.hpp>
00011 
00012 class CalibrationXML  
00013 {
00014 public:
00015         typedef XERCES_CPP_NAMESPACE::DOMDocument DOMDocument;
00016         typedef XERCES_CPP_NAMESPACE::DOMElement DOMElement;
00017         typedef XERCES_CPP_NAMESPACE::DOMNode DOMNode;
00018         typedef XERCES_CPP_NAMESPACE::HandlerBase HandlerBase;
00019         typedef XERCES_CPP_NAMESPACE::XercesDOMParser XercesDOMParser;
00020         typedef XERCES_CPP_NAMESPACE::XMLPlatformUtils XMLPlatformUtils;
00021         typedef XERCES_CPP_NAMESPACE::XMLString XMLString;
00022 
00023         CalibrationXML();
00024         ~CalibrationXML();
00025         
00029         void openFile(const std::string & xmlFileName);
00030 
00034         void saveFile(const std::string & xmlFileName);
00035 
00036 
00037         void closeFile() 
00038         {
00039           if(errHandler) delete errHandler;
00040           if(parser) {  delete parser; XMLPlatformUtils::Terminate(); } 
00041           errHandler=0;
00042           parser=0;
00043         }       
00047         DOMElement * calibrationDOM() { return m_calibrationDOM;}
00048 
00049 
00050 //Static function to make everything easier, less transcode and type conversion
00054            template <class T> static void writeAttribute(DOMElement *dom, const std::string & name, const T & value)
00055         {
00056             std::ostringstream buffer;
00057             buffer << value;
00058             XMLCh * nameStr = XMLString::transcode(name.c_str());
00059             XMLCh * valueStr = XMLString::transcode(buffer.str().c_str());
00060             dom->setAttribute(nameStr, valueStr );
00061             XMLString::release(&nameStr);
00062             XMLString::release(&valueStr);
00063         }
00064 
00068         template <class T> static T readAttribute(DOMElement *dom, const std::string & name)
00069         {
00070             XMLCh* nameStr  = XMLString::transcode(name.c_str());
00071             char * valueStr = XMLString::transcode(dom->getAttribute(nameStr));
00072             std::istringstream buffer(valueStr);
00073             T value;
00074             buffer >> value;
00075             XMLString::release(&nameStr);
00076             XMLString::release(&valueStr);
00077             return value;
00078         }
00079         
00083         static DOMElement * addChild(DOMNode *dom,const std::string & name);
00084                 
00085 private:
00086         std::string m_xmlFileName;
00087         DOMElement * m_calibrationDOM;
00088         DOMDocument* doc;
00089         HandlerBase* errHandler;
00090         XercesDOMParser *parser;
00091 };
00092 #endif
00093