ATLAS Offline Software
XMLCoreParser.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <cstdio>
8 #include <cstdlib>
9 #include <sstream>
10 #include <iostream>
11 
12 namespace{
13  class XMLCoreParserDebugger{
14  public:
15  static bool get_debug_state(){
16  return ::getenv ("XMLDEBUG") != 0;
17  }
18  static bool debug (){
19  static const bool debug_state = get_debug_state();
20  return debug_state;
21  }
22  };
23 }
24 
25 #include "ExpatCoreParser.h"
26 
27 
29  if (this != &other) {
30  if (m_owns) delete m_node;
31  m_node = other.m_node;
32  m_owns = false;
33  }
34  return *this;
35 }
36 
38  if (this != &other) {
39  if (m_owns) delete m_node;
40  m_node = other.m_node;
41  m_owns = other.m_owns;
42  other.m_node = nullptr;
43  other.m_owns = false;
44  }
45  return *this;
46 }
47 
49  if (m_owns) delete m_node;
50 }
51 
52 
53 /*
54  *
55  * XMLCoreFactory implementation
56  *
57  */
58 
61  std::cout << "XMLCoreFactory::~XMLCoreFactory> factory=" << this << std::endl;
62  }
63 }
64 
65 void
68  std::cout << "XMLCoreFactory::start> factory=" << this << std::endl;
69  }
70  do_start (parser, node);
71 }
72 
73 void
76  std::cout << "XMLCoreFactory::end>" << std::endl;
77  }
78  do_end (parser, node);
79 }
80 
81 void
84  std::cout << "XMLCoreFactory::comment>" << std::endl;
85  }
87 }
88 
89 void
90 XMLCoreFactory::do_start (XMLCoreParser& /*parser*/, const XMLCoreNode& /*node*/) {
92  std::cout << "XMLCoreFactory::do_start>" << std::endl;
93  }
94 }
95 
96 void
97 XMLCoreFactory::do_end (XMLCoreParser& /*parser*/, const XMLCoreNode& /*node*/) {
99  std::cout << "XMLCoreFactory::do_end>" << std::endl;
100  }
101 }
102 
103 void
104 XMLCoreFactory::do_comment (XMLCoreParser& /*parser*/, const std::string& /*comment*/) {
106  std::cout << "XMLCoreFactory::do_comment>" << std::endl;
107  }
108 }
109 
110 int
112  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
113  return (attrs.size ());
114 }
115 
116 bool
117 XMLCoreFactory::has_attribute (const XMLCoreNode& node, const std::string& name) {
118  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
119  CoreParser::DOMNamedNodeMap::const_iterator it = attrs.find (name);
120  if (it == attrs.end ()) return (false);
121  return (true);
122 }
123 
124 int
125 XMLCoreFactory::get_int (const XMLCoreNode& node, const std::string& name) {
126  int result = 0;
127  std::string s = get_value (node, name);
128  sscanf (s.c_str (), "%80d", &result);
129  return (result);
130 }
131 
132 double
133 XMLCoreFactory::get_double (const XMLCoreNode& node, const std::string& name) {
134  double result = 0;
135  std::string s = get_value (node, name);
136  sscanf (s.c_str (), "%80lg", &result);
137  return (result);
138 }
139 
140 bool
141 XMLCoreFactory::get_boolean (const XMLCoreNode& node, const std::string& name) {
142  bool result = false;
143  std::string s = get_token (node, name);
144  if (s == "TRUE") result = true;
145  return (result);
146 }
147 
148 std::string XMLCoreFactory::get_ID (const XMLCoreNode& node, const std::string& name) {
149  std::string result = get_value (node, name);
150  return (result);
151 }
152 
153 std::string
154 XMLCoreFactory::get_value (const XMLCoreNode& node, const std::string& name) {
156  std::cout << "XMLCoreFactory::get_value> name=" << name << std::endl;
157  }
158  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
159  CoreParser::DOMNamedNodeMap::const_iterator it = attrs.find (name);
160  if (it == attrs.end ()) return ("");
161  std::string result = (*it).second;
163  std::cout << "XMLCoreFactory::get_value>2 value=" << result << std::endl;
164  }
165  return (result);
166 }
167 
168 std::string
170  return node.get_node().get_name ();
171 }
172 
173 int
175  return node.get_node ().sibling_number ();
176 }
177 
178 std::string
180  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
181  CoreParser::DOMNamedNodeMap::const_iterator it;
182  for (it = attrs.begin (); (index > 0) && (it != attrs.end ()); ++it){
183  --index;
184  }
185  if (it == attrs.end ()) return ("");
186  return it->first;
187 }
188 
189 std::string
190 XMLCoreFactory::get_token (const XMLCoreNode& node, const std::string& name) {
191  std::string result = get_value (node, name);
192  // trim the value
193  while ((result.length () > 0) &&
194  (result.at(0) == ' ')) result.erase (0, 1);
195 
196  while ((result.length () > 0) &&
197  (result.at(result.length () - 1) == ' ')) result.erase (result.length () - 1, 1);
198  // Convert to upper case
199  for (std::string::size_type i = 0; i < result.length (); ++i){
200  result[i] = std::toupper (result[i]);
201  }
202  return (result);
203 }
204 
205 
206 bool
207 XMLCoreFactory::check_int (const int n, const XMLCoreNode& node, const std::string& name) {
208  std::string checkstring = get_value (node, name);
209  int counter = 0;
210  //
211  // concatenate two times same string to
212  // check the last number of the string
213  // explicitly!
214  //
215  std::string t = checkstring + " " + checkstring;
216  std::istringstream tmpstr (t.c_str());
217  while (tmpstr.good ()) {
218  int ii;
219  counter++;
220  tmpstr >> ii;
221  }
222  if (counter/2 != n) {
223  std::cerr << "XMLCoreFactory::check_int error: no " << n
224  << " ints in \"" << checkstring << "\" for attribute " <<
225  name << ". exit." << std::endl;
226 
227  std::string nodename = get_value (node, "name");
228  std::string volume = get_value (node, "volume");
229 
230  if (nodename != "" ) std::cerr << "for name=" << nodename << std::endl;
231  if (volume != "" ) std::cerr << "for volume=" << volume << std::endl;
232 
233  std::abort();
234  }
235  return true;
236 }
237 
238 bool
239 XMLCoreFactory::check_double (const int n, const XMLCoreNode& node, const std::string& name) {
240  std::string checkstring = get_value (node, name);
241  int counter = 0;
242  //
243  // concatenate two times same string to
244  // check the last number of the string
245  // explicitly!
246  //
247  std::string t = checkstring + " " + checkstring;
248  std::istringstream tmpstr (t.c_str());
249  while (tmpstr.good ()) {
250  double ii{};
251  counter++;
252  tmpstr >> ii;
253  }
254  if (counter/2 != n) {
255  std::cerr << "XMLCoreFactory::check_double error: (" << counter << ") no " << n
256  << " doubles in \"" << checkstring << "\" for attribute " <<
257  name << ". exit." << std::endl;
258  std::string name1 = get_value (node, "name");
259  std::string volume = get_value (node, "volume");
260  if (name1 != "" ) std::cerr << "for name=" << name << std::endl;
261  if (volume != "" ) std::cerr << "for volume=" << volume << std::endl;
262  std::abort();
263  }
264  return true;
265 }
266 
268 };
269 
270 
272 XMLCoreParser::parse (const std::string& file_name) {
273  m_level = 0;
274  std::unique_ptr<CoreParser::DOMNode> doc = ExpatCoreParser::parse (file_name);
276  if (doc != nullptr) doc->print ("============ ALL =============");
277  }
278  if (not doc){
279  throw std::runtime_error("XMLCoreParser: no such file ["+file_name+"]");
280  }
281  return XMLCoreNode (std::move (doc));
282 }
283 
284 void
285 XMLCoreParser::visit (const std::string& file_name) {
287  std::cout << "XMLCoreParser::visit file_name "
288  << file_name << std::endl;
289  }
292  const CoreParser::DOMNode& node = n.get_node();
293  const CoreParser::DOMNode* nptr = &node;
294  std::cout << "XMLCoreParser::visit node=" << nptr << std::endl;
295  }
296  visit (n);
297 }
298 
299 void
300 XMLCoreParser::visit (const XMLCoreNode& core_node){
301  // Get the name and value out for convenience
302  const CoreParser::DOMNode& node = core_node.get_node ();
303  const CoreParser::DOMNode* nptr = &node;
304  const std::string& nodeName = node.get_name();
305  const std::string& nodeValue = node.get_value();
307  std::cout << "XMLCoreParser::visit node(" << nptr << ") " << nodeName << std::endl;
308  }
309  XMLCoreFactory* factory = find_factory (nodeName);
311  std::cout << "XMLCoreParser::visit factory " << factory << std::endl;
312  }
313 
314  switch (node.get_type()) {
316  const CoreParser::DOMSiblings& siblings = node.get_siblings ();
317  for (const CoreParser::DOMNode* child : siblings) {
318  XMLCoreNode n (child);
319  visit (n);
320  }
321  break;
322  }
325  std::cout << "XMLCoreParser::visit ELEMENT_NODE "
326  << " factory=" << factory
327  << std::endl;
328  }
329  if (factory != 0){
330  factory->start (*this, core_node);
331  } else {
332  std::cerr << "XMLCoreParser> Cannot find factory for element "
333  << nodeName << std::endl;
334  register_factory (nodeName, std::make_unique<DummyFactory>());
335  }
336  const CoreParser::DOMSiblings& siblings = node.get_siblings ();
337  for (const CoreParser::DOMNode* child : siblings) {
338  XMLCoreNode n (child);
339  visit (n);
340  }
341  if (factory != 0) factory->end (*this, core_node);
342  break;
343  }
345  if (factory != 0) factory->comment (*this, nodeValue);
346  break;
347  }
349  std::cout << "ENTITY_NODE " << nodeValue << std::endl;
350  break;
351  }
353  std::cout << "ENTITY_REFERENCE_NODE " << nodeValue << std::endl;
354  break;
355  }
356  default: {
357  std::cerr << "Unrecognized node type = " << (long) node.get_type() << std::endl;
358  break;
359  }
360  }
362  std::cout << "XMLCoreParser::visit-2" << std::endl;
363  }
364 }
365 
366 void
367 XMLCoreParser::register_default_factory (std::unique_ptr<XMLCoreFactory> factory) {
368  m_default_factory = std::move (factory);
369 }
370 
371 void
373  std::unique_ptr<XMLCoreFactory> factory) {
375  std::cout << "XMLCoreFactory::register_factory> name=" << name
376  << " factory=" << factory.get() << std::endl;
377  }
378  m_factories[name] = std::move (factory);
379 }
380 
381 void
382 XMLCoreParser::register_external_entity (const std::string& name, const std::string& file_name){
384  std::cout << "XMLCoreParser::register_external_entity> name=" << name
385  << " file_name=" << file_name << std::endl;
386  }
388 }
389 
390 void
391 XMLCoreParser::register_text_entity (const std::string& name, const std::string& text){
393  std::cout << "XMLCoreParser::register_text_entity> name=" << name
394  << std::endl;
395  }
397 }
398 
399 
401 XMLCoreParser::find_factory (const std::string& name) {
403  if (it != m_factories.end ()) {
404  return (*it).second.get();
405  }
406  return m_default_factory.get();
407 }
408 
409 
410 void
412  m_level += 1;
413 }
414 
415 
416 void
418  m_level -= 1;
419 }
420 
421 
422 int
424  return m_level;
425 }
XMLCoreParser::find_factory
XMLCoreFactory * find_factory(const std::string &name)
Definition: XMLCoreParser.cxx:401
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
XMLCoreParser::parse
XMLCoreNode parse(const std::string &file_name)
Definition: XMLCoreParser.cxx:272
XMLCoreFactory::do_comment
virtual void do_comment(XMLCoreParser &parser, const std::string &comment)
Definition: XMLCoreParser.cxx:104
python.PoolAttributeHelper.attrs
list attrs
Definition: PoolAttributeHelper.py:89
ReadCellNoiseFromCool.name1
name1
Definition: ReadCellNoiseFromCool.py:233
get_generator_info.result
result
Definition: get_generator_info.py:21
XMLCoreNode::operator=
XMLCoreNode & operator=(const XMLCoreNode &other)
Definition: XMLCoreParser.cxx:28
CoreParser::DOMSiblings
std::vector< DOMNode * > DOMSiblings
Definition: DOMNode.h:17
index
Definition: index.py:1
XMLCoreFactory::has_attribute
static bool has_attribute(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:117
XMLCoreFactory::start
void start(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:66
CoreParser::DOMNamedNodeMap
std::map< std::string, std::string > DOMNamedNodeMap
Definition: DOMNode.h:14
skel.it
it
Definition: skel.GENtoEVGEN.py:407
CoreParser::DOMNode
Definition: DOMNode.h:20
XMLCoreFactory::get_int
static int get_int(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:125
XMLCoreFactory::get_boolean
static bool get_boolean(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:141
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
XMLCoreParser.h
CoreParser::DOMNode::ENTITY_NODE
@ ENTITY_NODE
Definition: DOMNode.h:27
XMLCoreFactory::check_int
static bool check_int(const int n, const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:207
XMLCoreNode
Definition: XMLCoreParser.h:19
XMLCoreParser::register_text_entity
void register_text_entity(const std::string &name, const std::string &text)
Definition: XMLCoreParser.cxx:391
XMLCoreParser::m_level
int m_level
Definition: XMLCoreParser.h:123
XMLCoreParser::down
void down()
Definition: XMLCoreParser.cxx:417
ExpatCoreParser.h
CoreParser::DOMNode::DOCUMENT_NODE
@ DOCUMENT_NODE
Definition: DOMNode.h:24
python.CaloAddPedShiftConfig.parser
parser
Definition: CaloAddPedShiftConfig.py:41
physics_parameters.file_name
string file_name
Definition: physics_parameters.py:32
XMLCoreParser
Definition: XMLCoreParser.h:97
XMLCoreParser::m_default_factory
std::unique_ptr< XMLCoreFactory > m_default_factory
Definition: XMLCoreParser.h:122
CoreParser::DOMNode::COMMENT_NODE
@ COMMENT_NODE
Definition: DOMNode.h:26
lumiFormat.i
int i
Definition: lumiFormat.py:85
ExpatCoreParser::register_text_entity
static void register_text_entity(const std::string &name, const std::string &text)
Definition: ExpatCoreParser.cxx:92
XMLCoreNode::m_owns
bool m_owns
Definition: XMLCoreParser.h:59
beamspotman.n
n
Definition: beamspotman.py:729
DummyFactory
Definition: XMLCoreParser.cxx:267
CoreParser::DOMNode::ELEMENT_NODE
@ ELEMENT_NODE
Definition: DOMNode.h:25
XMLCoreFactory::get_value
static std::string get_value(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:154
XMLCoreFactory::attribute_number
static int attribute_number(const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:111
XMLCoreFactory::do_start
virtual void do_start(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:90
XMLCoreParser::register_factory
void register_factory(const std::string &name, std::unique_ptr< XMLCoreFactory > factory)
Definition: XMLCoreParser.cxx:372
XMLCoreFactory::end
void end(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:74
XMLCoreFactory
Definition: XMLCoreParser.h:62
PyPoolBrowser.node
node
Definition: PyPoolBrowser.py:131
XMLCoreNode::m_node
const CoreParser::DOMNode * m_node
Definition: XMLCoreParser.h:58
CaloCondBlobAlgs_fillNoiseFromASCII.comment
string comment
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:26
XMLCoreFactory::check_double
static bool check_double(const int n, const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:239
merge_scale_histograms.doc
string doc
Definition: merge_scale_histograms.py:9
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
CoreParser::DOMNode::ENTITY_REFERENCE_NODE
@ ENTITY_REFERENCE_NODE
Definition: DOMNode.h:28
XMLCoreParser::m_factories
FactoryMap m_factories
Definition: XMLCoreParser.h:121
XMLCoreFactory::get_ID
static std::string get_ID(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:148
XMLCoreParser::register_default_factory
void register_default_factory(std::unique_ptr< XMLCoreFactory > factory)
Definition: XMLCoreParser.cxx:367
DeMoScan.index
string index
Definition: DeMoScan.py:362
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
ExpatCoreParser::parse
static std::unique_ptr< CoreParser::DOMNode > parse(const std::string &file_name)
Definition: ExpatCoreParser.cxx:407
XMLCoreFactory::~XMLCoreFactory
virtual ~XMLCoreFactory()
Definition: XMLCoreParser.cxx:59
XMLCoreNode::get_node
const CoreParser::DOMNode & get_node() const
Definition: XMLCoreParser.h:53
XMLCoreParser::register_external_entity
void register_external_entity(const std::string &name, const std::string &file_name)
Definition: XMLCoreParser.cxx:382
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
XMLCoreNode::~XMLCoreNode
~XMLCoreNode()
Definition: XMLCoreParser.cxx:48
makeTransCanvas.text
text
Definition: makeTransCanvas.py:11
ExpatCoreParser::register_external_entity
static void register_external_entity(const std::string &name, const std::string &file_name)
Definition: ExpatCoreParser.cxx:82
XMLCoreFactory::get_token
static std::string get_token(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:190
XMLCoreFactory::sibling_number
int sibling_number(const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:174
XMLCoreParser::level
int level() const
Definition: XMLCoreParser.cxx:423
XMLCoreFactory::get_name
std::string get_name(const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:169
test_pyathena.counter
counter
Definition: test_pyathena.py:15
XMLCoreFactory::comment
void comment(XMLCoreParser &parser, const std::string &comment)
Definition: XMLCoreParser.cxx:82
XMLCoreFactory::get_double
static double get_double(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:133
XMLCoreParser::up
void up()
Definition: XMLCoreParser.cxx:411
XMLCoreParser::visit
void visit(const std::string &file_name)
Definition: XMLCoreParser.cxx:285
node
Definition: node.h:21
XMLCoreFactory::do_end
virtual void do_end(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:97