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
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
00044
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
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
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 catch (...)
00081 {
00082 std::cerr << "An unknown error occured during parsing\n " << std::endl;
00083 errorsOccured = true;
00084 }
00085
00086 if(errorsOccured) {
00087 std::cerr << "An error occured during parsing\n"
00088 << "Please check your input with SAXCount or a similar tool.\n Exiting!\n" << std::endl;
00089 abort();
00090
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
00105 else { edm::LogInfo("XMLCalibration") << "Calibration found" ; }
00106
00107 m_calibrationDOM = (DOMElement *) n1;
00108
00109
00110
00111 }
00112
00113 void CalibrationXML::saveFile(const std::string & xmlFileName)
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 }
00123 DOMElement * CalibrationXML::addChild(DOMNode *dom,const std::string & name)
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 }