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 }
static DOMElement * addChild(DOMNode *dom, const std::string &name)
DOMDocument * doc
DOMElement * m_calibrationDOM
XERCES_CPP_NAMESPACE::HandlerBase HandlerBase
void xercesTerminate()
Definition: Xerces.cc:23
void xercesInitialize()
Definition: Xerces.cc:18
assert(be >=bs)
XERCES_CPP_NAMESPACE::DOMNode DOMNode
HandlerBase * errHandler
void openFile(const std::string &xmlFileName)
Log< level::Info, false > LogInfo
std::string m_xmlFileName
XercesDOMParser * parser
XERCES_CPP_NAMESPACE::DOMElement DOMElement
void saveFile(const std::string &xmlFileName)
XERCES_CPP_NAMESPACE::XercesDOMParser XercesDOMParser