CMS 3D CMS Logo

CalibrationXML.cc
Go to the documentation of this file.
1 
3 
5 
6 #include <xercesc/framework/LocalFileFormatTarget.hpp>
7 #include <xercesc/parsers/XercesDOMParser.hpp>
8 #include <xercesc/dom/DOM.hpp>
9 #include <xercesc/sax/HandlerBase.hpp>
10 #include <xercesc/util/XMLString.hpp>
12 #include <iostream>
13 #include <fstream>
14 #include <string>
15 
16 using namespace std;
17 
19 
20 CalibrationXML::CalibrationXML() : errHandler(nullptr), parser(nullptr) {}
21 
23  //TODO: delete!!!!
24  if (errHandler)
25  delete errHandler;
26  if (parser) {
27  delete parser;
29  }
30 }
31 
32 void CalibrationXML::openFile(const std::string& xmlFileName) {
33  if (errHandler)
34  delete errHandler;
35  if (parser) {
36  delete parser;
38  }
39 
40  m_xmlFileName = xmlFileName;
41  // std::cout << "Opening.." << std::endl;
42  // Initialize the XML4C2 system
43  try {
45  } catch (const XMLException& toCatch) {
46  std::cerr << "Error during Xerces-c Initialization.\n"
47  << " Exception message:" << XMLString::transcode(toCatch.getMessage()) << std::endl;
48  abort();
49  //FIXME throw GenTerminate("Error during Xerces-c Initialization.");
50  }
51  parser = new XercesDOMParser;
52  parser->setValidationScheme(XercesDOMParser::Val_Auto);
53  parser->setDoNamespaces(false);
54  parser->setDoSchema(false);
55  parser->setValidationSchemaFullChecking(false);
56  errHandler = new HandlerBase;
57  parser->setErrorHandler(errHandler);
58  parser->setCreateEntityReferenceNodes(false);
59  // Parse the XML file, catching any XML exceptions that might propogate out of it.
60  bool errorsOccured = false;
61  try {
62  edm::LogInfo("XMLCalibration") << "Calibration XML: parsing " << m_xmlFileName.c_str() << std::endl;
63  parser->parse(m_xmlFileName.c_str());
64  int errorCount = parser->getErrorCount();
65  if (errorCount > 0)
66  errorsOccured = true;
67  } catch (const XMLException& e) {
68  std::cerr << "A DOM error occured during parsing\n DOMException code: " << (long unsigned int)e.getCode()
69  << std::endl;
70  errorsOccured = true;
71  }
72  // If the parse was successful, build the structure we want to have
73  if (errorsOccured) {
74  std::cerr << "An error occured during parsing\n"
75  << "Please check your input with SAXCount or a similar tool.\n Exiting!\n"
76  << std::endl;
77  abort();
78  //FIXME throw GenTerminate("An error occured during parsing\n Please check your input with SAXCount or a similar tool.\n Exiting!\n");
79  }
80 
81  doc = parser->getDocument();
82  DOMNode* n1 = doc->getFirstChild();
83 
84  while (n1) {
85  if (n1->getNodeType() == DOMNode::ELEMENT_NODE)
86  break;
87  n1 = n1->getNextSibling();
88  }
89 
90  if (n1 == nullptr || strcmp("Calibration", XMLString::transcode(n1->getNodeName())))
91  abort();
92  //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.");
93  else {
94  edm::LogInfo("XMLCalibration") << "Calibration found";
95  }
96 
98 }
99 
100 void CalibrationXML::saveFile(const std::string& xmlFileName) {
101  DOMImplementation* theImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
102  DOMLSSerializer* theSerializer = ((DOMImplementation*)theImpl)->createLSSerializer();
103  DOMConfiguration* dc = theSerializer->getDomConfig();
104  dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
105 
106  XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(XMLString::transcode(xmlFileName.c_str()));
107  DOMLSOutput* outputDesc = ((DOMImplementationLS*)theImpl)->createLSOutput();
108  outputDesc->setByteStream(myFormTarget);
109 
110  theSerializer->write(doc, outputDesc);
111  delete myFormTarget;
112 }
113 
115  DOMNode* n1 = dom;
116  int level = 0;
117  std::string indent = "\n";
118  while (n1 && level < 100) {
119  level++;
120  indent += " ";
121  n1 = n1->getParentNode();
122  }
123  assert(dom);
124  if (dom->getFirstChild() == nullptr)
125  dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
126 
127  DOMElement* child =
128  (DOMElement*)dom->appendChild(dom->getOwnerDocument()->createElement(XMLString::transcode(name.c_str())));
129  dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
130  return child;
131 }
personalPlayback.level
level
Definition: personalPlayback.py:22
MessageLogger.h
contentValuesFiles.dom
dom
Definition: contentValuesFiles.py:50
edm::LogInfo
Definition: MessageLogger.h:254
cms::cuda::assert
assert(be >=bs)
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
XERCES_CPP_NAMESPACE_USE
Definition: XMLConfigWriter.cc:40
CalibrationXML.h
CalibrationXML::addChild
static DOMElement * addChild(DOMNode *dom, const std::string &name)
Definition: CalibrationXML.cc:114
CalibrationXML::errHandler
HandlerBase * errHandler
Definition: CalibrationXML.h:87
CalibrationXML::DOMNode
XERCES_CPP_NAMESPACE::DOMNode DOMNode
Definition: CalibrationXML.h:17
CalibrationXML::~CalibrationXML
~CalibrationXML()
Definition: CalibrationXML.cc:22
CalibrationXML::CalibrationXML
CalibrationXML()
Definition: CalibrationXML.cc:20
CalibrationXML::doc
DOMDocument * doc
Definition: CalibrationXML.h:86
CalibrationXML::XercesDOMParser
XERCES_CPP_NAMESPACE::XercesDOMParser XercesDOMParser
Definition: CalibrationXML.h:19
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
CalibrationXML::HandlerBase
XERCES_CPP_NAMESPACE::HandlerBase HandlerBase
Definition: CalibrationXML.h:18
beam_dqm_sourceclient-live_cfg.cerr
cerr
Definition: beam_dqm_sourceclient-live_cfg.py:17
createfilelist.int
int
Definition: createfilelist.py:10
Xerces.h
cms::concurrency::xercesTerminate
void xercesTerminate()
Definition: Xerces.cc:23
CalibrationXML::openFile
void openFile(const std::string &xmlFileName)
Definition: CalibrationXML.cc:32
CalibrationXML::parser
XercesDOMParser * parser
Definition: CalibrationXML.h:88
CalibrationXML::saveFile
void saveFile(const std::string &xmlFileName)
Definition: CalibrationXML.cc:100
std
Definition: JetResolutionObject.h:76
CalibrationXML::DOMElement
XERCES_CPP_NAMESPACE::DOMElement DOMElement
Definition: CalibrationXML.h:16
util.rrClient.indent
indent
Definition: rrClient.py:41
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
class-composition.child
child
Definition: class-composition.py:91
CalibrationXML::m_calibrationDOM
DOMElement * m_calibrationDOM
Definition: CalibrationXML.h:85
child
Definition: simpleInheritance.h:11
cms::concurrency::xercesInitialize
void xercesInitialize()
Definition: Xerces.cc:18
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
CalibrationXML::m_xmlFileName
std::string m_xmlFileName
Definition: CalibrationXML.h:84