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 
37 void CalibrationXML::openFile(const std::string & xmlFileName)
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  catch (...)
81  {
82  std::cerr << "An unknown error occured during parsing\n " << std::endl;
83  errorsOccured = true;
84  }
85  // If the parse was successful, build the structure we want to have
86  if(errorsOccured) {
87  std::cerr << "An error occured during parsing\n"
88  << "Please check your input with SAXCount or a similar tool.\n Exiting!\n" << std::endl;
89 abort();
90 //FIXME throw GenTerminate("An error occured during parsing\n Please check your input with SAXCount or a similar tool.\n Exiting!\n");
91  }
92 
93  doc = parser->getDocument();
94  DOMNode* n1 = doc->getFirstChild();
95 
96  while(n1)
97  {
98  if (n1->getNodeType() == DOMNode::ELEMENT_NODE ) break;
99  n1 = n1->getNextSibling();
100  }
101 
102  if(strcmp("Calibration",XMLString::transcode(n1->getNodeName())))
103 abort();
104 //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.");
105  else { edm::LogInfo("XMLCalibration") << "Calibration found" ; }
106 
107  m_calibrationDOM = (DOMElement *) n1;
108 
109 
110 
111 }
112 
113 void CalibrationXML::saveFile(const std::string & xmlFileName)
114 {
115  DOMImplementation * theImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
116  DOMWriter * theSerializer = ((DOMImplementation*)theImpl)->createDOMWriter();
117  theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
118  XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(XMLString::transcode(xmlFileName.c_str()));
119  theSerializer->writeNode(myFormTarget, *doc);
120  delete myFormTarget;
121 
122 }
123 DOMElement * CalibrationXML::addChild(DOMNode *dom,const std::string & name)
124 {
125  DOMNode *n1 = dom;
126  int level=0;
127  std::string indent="\n";
128  while(n1 && level < 100)
129  {
130  level++;
131  indent+=" ";
132  n1 = n1->getParentNode();
133  }
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
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