CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/RecoBTag/XMLCalibration/src/CalibrationXML.cc

Go to the documentation of this file.
00001 
00002 #include "RecoBTag/XMLCalibration/interface/CalibrationXML.h"
00003 
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 
00006 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00007 #include <xercesc/parsers/XercesDOMParser.hpp>
00008 #include <xercesc/dom/DOM.hpp>
00009 #include <xercesc/dom/DOMWriter.hpp>
00010 #include <xercesc/sax/HandlerBase.hpp>
00011 #include <xercesc/util/XMLString.hpp>
00012 #include <xercesc/util/PlatformUtils.hpp>
00013 #include <iostream>
00014 #include <fstream>
00015 #include <string>
00016 
00017 using namespace std;
00018 
00019 
00020 XERCES_CPP_NAMESPACE_USE
00021 
00022 CalibrationXML::CalibrationXML() : errHandler(0), parser(0)
00023 {
00024 
00025 }
00026 
00027 CalibrationXML::~CalibrationXML()
00028 {
00029 //TODO: delete!!!!      
00030 if(errHandler) delete errHandler;
00031 if(parser)  { 
00032               delete parser;
00033               XMLPlatformUtils::Terminate();
00034             }
00035 }
00036 
00037 void CalibrationXML::openFile(const std::string & xmlFileName) 
00038 {
00039 if(errHandler) delete errHandler;
00040 if(parser) { delete parser; XMLPlatformUtils::Terminate(); }
00041 
00042  m_xmlFileName = xmlFileName;
00043 // std::cout << "Opening.." << std::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()) << std::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() << std::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                 std::cerr << "A DOM error occured during parsing\n   DOMException code: "
00077                      << (long unsigned int)e.getCode() << std::endl;
00078                 errorsOccured = true;
00079         }
00080         // If the parse was successful, build the structure we want to have
00081         if(errorsOccured) { 
00082                 std::cerr << "An error occured during parsing\n"
00083                      << "Please check your input with SAXCount or a similar tool.\n Exiting!\n" << std::endl; 
00084 abort();
00085 //FIXME         throw GenTerminate("An error occured during parsing\n Please check your input with SAXCount or a similar tool.\n Exiting!\n");
00086         }
00087 
00088         doc = parser->getDocument();
00089         DOMNode* n1 = doc->getFirstChild();
00090 
00091         while(n1)
00092         {
00093                 if (n1->getNodeType() == DOMNode::ELEMENT_NODE   ) break;
00094                 n1 = n1->getNextSibling();
00095         }
00096         
00097         if(strcmp("Calibration",XMLString::transcode(n1->getNodeName())))
00098 abort();
00099 //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.");
00100         else {   edm::LogInfo("XMLCalibration")  << "Calibration found" ; }     
00101 
00102         m_calibrationDOM = (DOMElement *) n1;
00103     
00104 
00105 
00106 }
00107 
00108 void CalibrationXML::saveFile(const std::string & xmlFileName)
00109 {
00110     DOMImplementation * theImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
00111     DOMWriter         *   theSerializer = ((DOMImplementation*)theImpl)->createDOMWriter();
00112     theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00113     XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(XMLString::transcode(xmlFileName.c_str()));
00114     theSerializer->writeNode(myFormTarget, *doc);
00115      delete myFormTarget;
00116           
00117 }
00118 DOMElement * CalibrationXML::addChild(DOMNode *dom,const std::string & name)
00119 { 
00120           DOMNode *n1 = dom;
00121           int level=0;
00122           std::string indent="\n";
00123           while(n1 && level < 100)
00124           {
00125            level++;
00126            indent+="  ";
00127            n1 = n1->getParentNode();
00128           } 
00129           if(dom->getFirstChild()==0)
00130              dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str()))); 
00131          
00132           DOMElement * child = (DOMElement *)dom->appendChild(dom->getOwnerDocument()->createElement(XMLString::transcode(name.c_str()))); 
00133           dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));           
00134           return child;
00135 }