CMS 3D CMS Logo

CalibrationXML Class Reference

#include <RecoBTag/XMLCalibration/interface/CalibrationXML.h>

List of all members.

Public Member Functions

DOMElement * calibrationDOM ()
 Return the root DOM Element of the opened XML calibration file.
 CalibrationXML ()
void closeFile ()
void openFile (const std::string &xmlFileName)
 Open an XML file.
void saveFile (const std::string &xmlFileName)
 Save DOM to file.
 ~CalibrationXML ()

Static Public Member Functions

static DOMElement * addChild (DOMNode *dom, const std::string &name)
 Helper static function to add a child in a DOM Element with indentation.
template<class T>
static T readAttribute (DOMElement *dom, const std::string &name)
 Helper static function to read an attribute in a DOM Element.
template<class T>
static void writeAttribute (DOMElement *dom, const std::string &name, const T &value)
 Helper static function to write an attribute in a DOM Element.

Private Attributes

DOMDocument * doc
HandlerBase * errHandler
DOMElement * m_calibrationDOM
std::string m_xmlFileName
XercesDOMParser * parser


Detailed Description

Definition at line 14 of file CalibrationXML.h.


Constructor & Destructor Documentation

XERCES_CPP_NAMESPACE_USE CalibrationXML::CalibrationXML (  ) 

Definition at line 22 of file CalibrationXML.cc.

00022                                : errHandler(0), parser(0)
00023 {
00024 
00025 }

CalibrationXML::~CalibrationXML (  ) 

Definition at line 27 of file CalibrationXML.cc.

References errHandler, and parser.

00028 {
00029 //TODO: delete!!!!      
00030 if(errHandler) delete errHandler;
00031 if(parser)  { 
00032               delete parser;
00033               XMLPlatformUtils::Terminate();
00034             }
00035 }


Member Function Documentation

DOMElement * CalibrationXML::addChild ( DOMNode *  dom,
const std::string &  name 
) [static]

Helper static function to add a child in a DOM Element with indentation.

Definition at line 123 of file CalibrationXML.cc.

References lat::indent(), level, and transcode().

Referenced by CalibratedHistogramXML::write().

00124 { 
00125           DOMNode *n1 = dom;
00126           int level=0;
00127           std::string indent="\n";
00128           while(n1 && level < 100)
00129           {
00130            level++;
00131            indent+="  ";
00132            n1 = n1->getParentNode();
00133           } 
00134           if(dom->getFirstChild()==0)
00135              dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str()))); 
00136          
00137           DOMElement * child = (DOMElement *)dom->appendChild(dom->getOwnerDocument()->createElement(XMLString::transcode(name.c_str()))); 
00138           dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));           
00139           return child;
00140 }  

DOMElement* CalibrationXML::calibrationDOM (  )  [inline]

Return the root DOM Element of the opened XML calibration file.

Definition at line 41 of file CalibrationXML.h.

References m_calibrationDOM.

Referenced by AlgorithmCalibration< T, CO >::dom().

00041 { return m_calibrationDOM;}

void CalibrationXML::closeFile (  )  [inline]

Definition at line 31 of file CalibrationXML.h.

References errHandler, and parser.

00032         {
00033           if(errHandler) delete errHandler;
00034           if(parser) {  delete parser; XMLPlatformUtils::Terminate(); } 
00035           errHandler=0;
00036           parser=0;
00037         }       

void CalibrationXML::openFile ( const std::string &  xmlFileName  ) 

Open an XML file.

Definition at line 37 of file CalibrationXML.cc.

References TestMuL1L2Filter_cff::cerr, doc, e, lat::endl(), errHandler, int, m_calibrationDOM, m_xmlFileName, parser, and transcode().

Referenced by AlgorithmCalibration< T, CO >::dom().

00038 {
00039 if(errHandler) delete errHandler;
00040 if(parser) { delete parser; XMLPlatformUtils::Terminate(); }
00041 
00042  m_xmlFileName = xmlFileName;
00043 // cout << "Opening.." << endl;
00044         // Initialize the XML4C2 system
00045         try
00046         {
00047                 XMLPlatformUtils::Initialize();
00048         }
00049         catch(const XMLException& toCatch)
00050         {
00051                 std::cerr << "Error during Xerces-c Initialization.\n"
00052                      << "  Exception message:"
00053                      << XMLString::transcode(toCatch.getMessage()) << endl;
00054    abort();
00055 //FIXME         throw GenTerminate("Error during Xerces-c Initialization.");
00056         }
00057         parser = new XercesDOMParser;
00058         parser->setValidationScheme(XercesDOMParser::Val_Auto);
00059         parser->setDoNamespaces(false);
00060         parser->setDoSchema(false);
00061         parser->setValidationSchemaFullChecking(false);
00062         errHandler = new HandlerBase;
00063         parser->setErrorHandler(errHandler);
00064         parser->setCreateEntityReferenceNodes(false);
00065         //  Parse the XML file, catching any XML exceptions that might propogate out of it.
00066         bool errorsOccured = false;
00067         try
00068         {
00069                   edm::LogInfo("XMLCalibration") << "Calibration XML: parsing " << m_xmlFileName.c_str() << endl;
00070                 parser->parse(m_xmlFileName.c_str());
00071                 int errorCount = parser->getErrorCount();
00072                 if (errorCount > 0) errorsOccured = true;
00073         }
00074         catch (const XMLException& e)
00075         {
00076                 cerr << "A DOM error occured during parsing\n   DOMException code: "
00077                      << (long unsigned int)e.getCode() << endl;
00078                 errorsOccured = true;
00079         }
00080         catch (...)
00081         {
00082                 cerr << "An unknown error occured during parsing\n " << endl;
00083                 errorsOccured = true;
00084         }
00085         // If the parse was successful, build the structure we want to have
00086         if(errorsOccured) { 
00087                 cerr << "An error occured during parsing\n"
00088                      << "Please check your input with SAXCount or a similar tool.\n Exiting!\n" << endl; 
00089 abort();
00090 //FIXME         throw GenTerminate("An error occured during parsing\n Please check your input with SAXCount or a similar tool.\n Exiting!\n");
00091         }
00092 
00093         doc = parser->getDocument();
00094         DOMNode* n1 = doc->getFirstChild();
00095 
00096         while(n1)
00097         {
00098                 if (n1->getNodeType() == DOMNode::ELEMENT_NODE   ) break;
00099                 n1 = n1->getNextSibling();
00100         }
00101         
00102         if(strcmp("Calibration",XMLString::transcode(n1->getNodeName())))
00103 abort();
00104 //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.");
00105         else {   edm::LogInfo("XMLCalibration")  << "Calibration found" ; }     
00106 
00107         m_calibrationDOM = (DOMElement *) n1;
00108     
00109 
00110 
00111 }

template<class T>
static T CalibrationXML::readAttribute ( DOMElement *  dom,
const std::string &  name 
) [inline, static]

Helper static function to read an attribute in a DOM Element.

Definition at line 62 of file CalibrationXML.h.

References release(), transcode(), and value.

00063         {
00064             XMLCh* nameStr  = XMLString::transcode(name.c_str());
00065             char * valueStr = XMLString::transcode(dom->getAttribute(nameStr));
00066             std::istringstream buffer(valueStr);
00067             T value;
00068             buffer >> value;
00069             XMLString::release(&nameStr);
00070             XMLString::release(&valueStr);
00071             return value;
00072         }

void CalibrationXML::saveFile ( const std::string &  xmlFileName  ) 

Save DOM to file.

Definition at line 113 of file CalibrationXML.cc.

References doc, and transcode().

00114 {
00115     DOMImplementation * theImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
00116     DOMWriter         *   theSerializer = ((DOMImplementation*)theImpl)->createDOMWriter();
00117     theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00118     XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(XMLString::transcode(xmlFileName.c_str()));
00119     theSerializer->writeNode(myFormTarget, *doc);
00120      delete myFormTarget;
00121           
00122 }

template<class T>
static void CalibrationXML::writeAttribute ( DOMElement *  dom,
const std::string &  name,
const T &  value 
) [inline, static]

Helper static function to write an attribute in a DOM Element.

Definition at line 48 of file CalibrationXML.h.

References release(), and transcode().

Referenced by CalibratedHistogramXML::write().

00049         {
00050             std::ostringstream buffer;
00051             buffer << value;
00052             XMLCh * nameStr = XMLString::transcode(name.c_str());
00053             XMLCh * valueStr = XMLString::transcode(buffer.str().c_str());
00054             dom->setAttribute(nameStr, valueStr );
00055             XMLString::release(&nameStr);
00056             XMLString::release(&valueStr);
00057         }


Member Data Documentation

DOMDocument* CalibrationXML::doc [private]

Definition at line 82 of file CalibrationXML.h.

Referenced by openFile(), and saveFile().

HandlerBase* CalibrationXML::errHandler [private]

Definition at line 83 of file CalibrationXML.h.

Referenced by closeFile(), openFile(), and ~CalibrationXML().

DOMElement* CalibrationXML::m_calibrationDOM [private]

Definition at line 81 of file CalibrationXML.h.

Referenced by calibrationDOM(), and openFile().

std::string CalibrationXML::m_xmlFileName [private]

Definition at line 80 of file CalibrationXML.h.

Referenced by openFile().

XercesDOMParser* CalibrationXML::parser [private]

Definition at line 84 of file CalibrationXML.h.

Referenced by closeFile(), openFile(), and ~CalibrationXML().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:15:38 2009 for CMSSW by  doxygen 1.5.4