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 
18 
20 
22 {
23 
24 }
25 
27 {
28 //TODO: delete!!!!
29 if(errHandler) delete errHandler;
30 if(parser) {
31  delete parser;
33  }
34 }
35 
37 {
38 if(errHandler) delete errHandler;
40 
42 // std::cout << "Opening.." << std::endl;
43  // Initialize the XML4C2 system
44  try
45  {
47  }
48  catch(const XMLException& toCatch)
49  {
50  std::cerr << "Error during Xerces-c Initialization.\n"
51  << " Exception message:"
52  << XMLString::transcode(toCatch.getMessage()) << std::endl;
53  abort();
54 //FIXME throw GenTerminate("Error during Xerces-c Initialization.");
55  }
56  parser = new XercesDOMParser;
57  parser->setValidationScheme(XercesDOMParser::Val_Auto);
58  parser->setDoNamespaces(false);
59  parser->setDoSchema(false);
60  parser->setValidationSchemaFullChecking(false);
61  errHandler = new HandlerBase;
62  parser->setErrorHandler(errHandler);
63  parser->setCreateEntityReferenceNodes(false);
64  // Parse the XML file, catching any XML exceptions that might propogate out of it.
65  bool errorsOccured = false;
66  try
67  {
68  edm::LogInfo("XMLCalibration") << "Calibration XML: parsing " << m_xmlFileName.c_str() << std::endl;
69  parser->parse(m_xmlFileName.c_str());
70  int errorCount = parser->getErrorCount();
71  if (errorCount > 0) errorsOccured = true;
72  }
73  catch (const XMLException& e)
74  {
75  std::cerr << "A DOM error occured during parsing\n DOMException code: "
76  << (long unsigned int)e.getCode() << std::endl;
77  errorsOccured = true;
78  }
79  // If the parse was successful, build the structure we want to have
80  if(errorsOccured) {
81  std::cerr << "An error occured during parsing\n"
82  << "Please check your input with SAXCount or a similar tool.\n Exiting!\n" << std::endl;
83 abort();
84 //FIXME throw GenTerminate("An error occured during parsing\n Please check your input with SAXCount or a similar tool.\n Exiting!\n");
85  }
86 
87  doc = parser->getDocument();
88  DOMNode* n1 = doc->getFirstChild();
89 
90  while(n1)
91  {
92  if (n1->getNodeType() == DOMNode::ELEMENT_NODE ) break;
93  n1 = n1->getNextSibling();
94  }
95 
96  if(n1 == nullptr || strcmp("Calibration",XMLString::transcode(n1->getNodeName())))
97 abort();
98 //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.");
99  else { edm::LogInfo("XMLCalibration") << "Calibration found" ; }
100 
101  m_calibrationDOM = (DOMElement *) n1;
102 
103 
104 
105 }
106 
108 {
109  DOMImplementation * theImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
110  DOMLSSerializer * theSerializer = ((DOMImplementation*)theImpl)->createLSSerializer();
111  DOMConfiguration* dc = theSerializer->getDomConfig();
112  dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
113 
114  XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(XMLString::transcode(xmlFileName.c_str()));
115  DOMLSOutput* outputDesc = ((DOMImplementationLS*)theImpl)->createLSOutput();
116  outputDesc->setByteStream(myFormTarget);
117 
118  theSerializer->write(doc, outputDesc);
119  delete myFormTarget;
120 }
121 
123 {
124  DOMNode *n1 = dom;
125  int level=0;
126  std::string indent="\n";
127  while(n1 && level < 100)
128  {
129  level++;
130  indent+=" ";
131  n1 = n1->getParentNode();
132  }
133  assert(dom);
134  if(dom->getFirstChild()==0)
135  dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
136 
137  DOMElement * child = (DOMElement *)dom->appendChild(dom->getOwnerDocument()->createElement(XMLString::transcode(name.c_str())));
138  dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
139  return child;
140 }
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
XERCES_CPP_NAMESPACE::DOMNode DOMNode
HandlerBase * errHandler
void openFile(const std::string &xmlFileName)
std::string m_xmlFileName
XercesDOMParser * parser
XERCES_CPP_NAMESPACE::DOMElement DOMElement
void saveFile(const std::string &xmlFileName)
XERCES_CPP_NAMESPACE::XercesDOMParser XercesDOMParser