CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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/dom/DOMWriter.hpp>
10 #include <xercesc/sax/HandlerBase.hpp>
11 #include <xercesc/util/XMLString.hpp>
12 #include <xercesc/util/PlatformUtils.hpp>
13 #include <iostream>
14 #include <fstream>
15 #include <string>
16 
17 using namespace std;
18 
19 
20 XERCES_CPP_NAMESPACE_USE
21 
23 {
24 
25 }
26 
28 {
29 //TODO: delete!!!!
30 if(errHandler) delete errHandler;
31 if(parser) {
32  delete parser;
33  XMLPlatformUtils::Terminate();
34  }
35 }
36 
38 {
39 if(errHandler) delete errHandler;
40 if(parser) { delete parser; XMLPlatformUtils::Terminate(); }
41 
43 // std::cout << "Opening.." << std::endl;
44  // Initialize the XML4C2 system
45  try
46  {
47  XMLPlatformUtils::Initialize();
48  }
49  catch(const XMLException& toCatch)
50  {
51  std::cerr << "Error during Xerces-c Initialization.\n"
52  << " Exception message:"
53  << XMLString::transcode(toCatch.getMessage()) << std::endl;
54  abort();
55 //FIXME throw GenTerminate("Error during Xerces-c Initialization.");
56  }
57  parser = new XercesDOMParser;
58  parser->setValidationScheme(XercesDOMParser::Val_Auto);
59  parser->setDoNamespaces(false);
60  parser->setDoSchema(false);
61  parser->setValidationSchemaFullChecking(false);
62  errHandler = new HandlerBase;
63  parser->setErrorHandler(errHandler);
64  parser->setCreateEntityReferenceNodes(false);
65  // Parse the XML file, catching any XML exceptions that might propogate out of it.
66  bool errorsOccured = false;
67  try
68  {
69  edm::LogInfo("XMLCalibration") << "Calibration XML: parsing " << m_xmlFileName.c_str() << std::endl;
70  parser->parse(m_xmlFileName.c_str());
71  int errorCount = parser->getErrorCount();
72  if (errorCount > 0) errorsOccured = true;
73  }
74  catch (const XMLException& e)
75  {
76  std::cerr << "A DOM error occured during parsing\n DOMException code: "
77  << (long unsigned int)e.getCode() << std::endl;
78  errorsOccured = true;
79  }
80  // If the parse was successful, build the structure we want to have
81  if(errorsOccured) {
82  std::cerr << "An error occured during parsing\n"
83  << "Please check your input with SAXCount or a similar tool.\n Exiting!\n" << std::endl;
84 abort();
85 //FIXME throw GenTerminate("An error occured during parsing\n Please check your input with SAXCount or a similar tool.\n Exiting!\n");
86  }
87 
88  doc = parser->getDocument();
89  DOMNode* n1 = doc->getFirstChild();
90 
91  while(n1)
92  {
93  if (n1->getNodeType() == DOMNode::ELEMENT_NODE ) break;
94  n1 = n1->getNextSibling();
95  }
96 
97  if(strcmp("Calibration",XMLString::transcode(n1->getNodeName())))
98 abort();
99 //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.");
100  else { edm::LogInfo("XMLCalibration") << "Calibration found" ; }
101 
102  m_calibrationDOM = (DOMElement *) n1;
103 
104 
105 
106 }
107 
109 {
110  DOMImplementation * theImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
111  DOMWriter * theSerializer = ((DOMImplementation*)theImpl)->createDOMWriter();
112  theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
113  XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(XMLString::transcode(xmlFileName.c_str()));
114  theSerializer->writeNode(myFormTarget, *doc);
115  delete myFormTarget;
116 
117 }
119 {
120  DOMNode *n1 = dom;
121  int level=0;
122  std::string indent="\n";
123  while(n1 && level < 100)
124  {
125  level++;
126  indent+=" ";
127  n1 = n1->getParentNode();
128  }
129  if(dom->getFirstChild()==0)
130  dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
131 
132  DOMElement * child = (DOMElement *)dom->appendChild(dom->getOwnerDocument()->createElement(XMLString::transcode(name.c_str())));
133  dom->appendChild(dom->getOwnerDocument()->createTextNode(XMLString::transcode(indent.c_str())));
134  return child;
135 }
static DOMElement * addChild(DOMNode *dom, const std::string &name)
DOMDocument * doc
DOMElement * m_calibrationDOM
XERCES_CPP_NAMESPACE::HandlerBase HandlerBase
XERCES_CPP_NAMESPACE::DOMNode DOMNode
HandlerBase * errHandler
void openFile(const std::string &xmlFileName)
std::string m_xmlFileName
XercesDOMParser * parser
tuple level
Definition: testEve_cfg.py:34
XERCES_CPP_NAMESPACE::DOMElement DOMElement
void saveFile(const std::string &xmlFileName)
XERCES_CPP_NAMESPACE::XercesDOMParser XercesDOMParser