ATLAS Offline Software
XMLCoreParser.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #include <stdio.h>
7 #include <cstdlib>
8 
10 #include <sstream>
11 
13 {
14 public:
15  static bool get_debug_state()
16  {
17  return ::getenv ("XMLDEBUG") != 0;
18  }
19  static bool debug ()
20  {
21  static const bool debug_state = get_debug_state();
22  return debug_state;
23  }
24 };
25 
26 #include "ExpatCoreParser.h"
27 
28 
30 {
31  if (this != &other) {
32  if (m_owns) delete m_node;
33  m_node = other.m_node;
34  m_owns = false;
35  }
36  return *this;
37 }
38 
40 {
41  if (this != &other) {
42  if (m_owns) delete m_node;
43  m_node = other.m_node;
44  m_owns = other.m_owns;
45  other.m_node = nullptr;
46  other.m_owns = false;
47  }
48  return *this;
49 }
50 
52 {
53  if (m_owns) delete m_node;
54 }
55 
56 
57 /*
58  *
59  * XMLCoreFactory implementation
60  *
61  */
62 
64 {
66  {
67  std::cout << "XMLCoreFactory::~XMLCoreFactory> factory=" << this << std::endl;
68  }
69 }
70 
72 {
74  {
75  std::cout << "XMLCoreFactory::start> factory=" << this << std::endl;
76  }
77 
78  do_start (parser, node);
79 }
80 
82 {
84  {
85  std::cout << "XMLCoreFactory::end>" << std::endl;
86  }
87 
88  do_end (parser, node);
89 }
90 
92 {
94  {
95  std::cout << "XMLCoreFactory::comment>" << std::endl;
96  }
97 
99 }
100 
101 void XMLCoreFactory::do_start (XMLCoreParser& /*parser*/, const XMLCoreNode& /*node*/)
102 {
104  {
105  std::cout << "XMLCoreFactory::do_start>" << std::endl;
106  }
107 }
108 
109 void XMLCoreFactory::do_end (XMLCoreParser& /*parser*/, const XMLCoreNode& /*node*/)
110 {
112  {
113  std::cout << "XMLCoreFactory::do_end>" << std::endl;
114  }
115 }
116 
117 void XMLCoreFactory::do_comment (XMLCoreParser& /*parser*/, const std::string& /*comment*/)
118 {
120  {
121  std::cout << "XMLCoreFactory::do_comment>" << std::endl;
122  }
123 }
124 
126 {
127  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
128 
129  return (attrs.size ());
130 }
131 
132 bool XMLCoreFactory::has_attribute (const XMLCoreNode& node, const std::string& name)
133 {
134  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
135 
136  CoreParser::DOMNamedNodeMap::const_iterator it = attrs.find (name);
137 
138  if (it == attrs.end ()) return (false);
139  return (true);
140 }
141 
142 int XMLCoreFactory::get_int (const XMLCoreNode& node, const std::string& name)
143 {
144  int result = 0;
145 
146  std::string s = get_value (node, name);
147  sscanf (s.c_str (), "%80d", &result);
148 
149  return (result);
150 }
151 
152 double XMLCoreFactory::get_double (const XMLCoreNode& node, const std::string& name)
153 {
154  double result = 0;
155 
156  std::string s = get_value (node, name);
157  sscanf (s.c_str (), "%80lg", &result);
158 
159  return (result);
160 }
161 
162 bool XMLCoreFactory::get_boolean (const XMLCoreNode& node, const std::string& name)
163 {
164  bool result = false;
165 
166  std::string s = get_token (node, name);
167 
168  if (s == "TRUE") result = true;
169 
170  return (result);
171 }
172 
173 std::string XMLCoreFactory::get_ID (const XMLCoreNode& node, const std::string& name)
174 {
175  std::string result = get_value (node, name);
176 
177  return (result);
178 }
179 
180 std::string XMLCoreFactory::get_value (const XMLCoreNode& node, const std::string& name)
181 {
183  {
184  std::cout << "XMLCoreFactory::get_value> name=" << name << std::endl;
185  }
186 
187  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
188 
189  CoreParser::DOMNamedNodeMap::const_iterator it = attrs.find (name);
190 
191  if (it == attrs.end ()) return ("");
192 
193  std::string result = (*it).second;
194 
196  {
197  std::cout << "XMLCoreFactory::get_value>2 value=" << result << std::endl;
198  }
199 
200  return (result);
201 }
202 
204 {
205  std::string result = node.get_node ().get_name ();
206 
207  return (result);
208 }
209 
211 {
212  return (node.get_node ().sibling_number ());
213 }
214 
215 std::string XMLCoreFactory::get_name (const XMLCoreNode& node, int index)
216 {
217  const CoreParser::DOMNamedNodeMap& attrs = node.get_node ().get_attributes();
218 
219  CoreParser::DOMNamedNodeMap::const_iterator it;
220 
221  for (it = attrs.begin (); (index > 0) && (it != attrs.end ()); ++it)
222  {
223  --index;
224  }
225 
226  if (it == attrs.end ()) return ("");
227 
228  std::string result = (*it).first;
229 
230  return (result);
231 }
232 
233 std::string XMLCoreFactory::get_token (const XMLCoreNode& node, const std::string& name)
234 {
235  std::string result = get_value (node, name);
236 
237  // trim the value
238 
239  while ((result.length () > 0) &&
240  (result.at(0) == ' ')) result.erase (0, 1);
241 
242  while ((result.length () > 0) &&
243  (result.at(result.length () - 1) == ' ')) result.erase (result.length () - 1, 1);
244 
245  // Convert to upper case
246 
247  for (std::string::size_type i = 0; i < result.length (); ++i)
248  {
249  static const std::string uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
250  static const std::string lc = "abcdefghijklmnopqrstuvwxyz";
251  char c = result[i];
252  std::string::size_type p = lc.find (c);
253  if (p != std::string::npos) result[i] = lc[p];
254  }
255 
256  return (result);
257 }
258 
259 
260 bool XMLCoreFactory::check_int (const int n, const XMLCoreNode& node, const std::string& name)
261 {
262  std::string checkstring = get_value (node, name);
263  int counter = 0;
264 
265  //
266  // concatenate two times same string to
267  // check the last number of the string
268  // explicitly!
269  //
270  std::string t = checkstring + " " + checkstring;
271 
272  std::istringstream tmpstr (t.c_str());
273  while (tmpstr.good ())
274  {
275  int ii;
276  counter++;
277  tmpstr >> ii;
278  }
279  //counter--;
280 
281  if (counter/2 != n)
282  {
283  std::cerr << "XMLCoreFactory::check_int error: no " << n
284  << " ints in \"" << checkstring << "\" for attribute " <<
285  name << ". exit." << std::endl;
286 
287  std::string nodename = get_value (node, "name");
288  std::string volume = get_value (node, "volume");
289 
290  if (nodename != "" ) std::cerr << "for name=" << nodename << std::endl;
291  if (volume != "" ) std::cerr << "for volume=" << volume << std::endl;
292 
293  std::abort();
294  }
295 
296  return true;
297 }
298 
299 bool XMLCoreFactory::check_double (const int n, const XMLCoreNode& node, const std::string& name)
300 {
301  std::string checkstring = get_value (node, name);
302  int counter = 0;
303 
304  //
305  // concatenate two times same string to
306  // check the last number of the string
307  // explicitly!
308  //
309  std::string t = checkstring + " " + checkstring;
310 
311  std::istringstream tmpstr (t.c_str());
312  while (tmpstr.good ())
313  {
314  double ii;
315  counter++;
316  tmpstr >> ii;
317  }
318 
319  //counter--;
320 
321  if (counter/2 != n)
322  {
323  std::cerr << "XMLCoreFactory::check_double error: (" << counter << ") no " << n
324  << " doubles in \"" << checkstring << "\" for attribute " <<
325  name << ". exit." << std::endl;
326 
327  std::string name1 = get_value (node, "name");
328  std::string volume = get_value (node, "volume");
329 
330  if (name1 != "" ) std::cerr << "for name=" << name << std::endl;
331  if (volume != "" ) std::cerr << "for volume=" << volume << std::endl;
332 
333  std::abort();
334  }
335 
336  return true;
337 }
338 
340 {
341 };
342 
344 {
345 }
346 
348 {
349 }
350 
351 void XMLCoreParser::set_validation_scheme (const char* /*parm*/)
352 {
353 }
354 
356 {
357 }
358 
360 {
361 }
362 
364 {
365 }
366 
368 {
369 }
370 
372 {
373  m_level = 0;
374  std::unique_ptr<CoreParser::DOMNode> doc = ExpatCoreParser::parse (file_name);
376  {
377  if (doc != 0) doc->print ("============ ALL =============");
378  }
379  return XMLCoreNode (std::move (doc));
380 }
381 
382 void XMLCoreParser::visit (const std::string& file_name)
383 {
385  {
386  std::cout << "XMLCoreParser::visit file_name "
387  << file_name << std::endl;
388  }
389 
391 
393  {
394  const CoreParser::DOMNode& node = n.get_node();
395  const CoreParser::DOMNode* nptr = &node;
396  std::cout << "XMLCoreParser::visit node=" << nptr << std::endl;
397  }
398 
399  visit (n);
400 }
401 
402 void XMLCoreParser::visit (const XMLCoreNode& core_node)
403 {
404  // Get the name and value out for convenience
405 
406  const CoreParser::DOMNode& node = core_node.get_node ();
407  const CoreParser::DOMNode* nptr = &node;
408 
409  const std::string& nodeName = node.get_name();
410  const std::string& nodeValue = node.get_value();
411 
413  {
414  std::cout << "XMLCoreParser::visit node(" << nptr << ") " << nodeName << std::endl;
415  }
416 
417  XMLCoreFactory* factory = find_factory (nodeName);
418 
420  {
421  std::cout << "XMLCoreParser::visit factory " << factory << std::endl;
422  }
423 
424  switch (node.get_type())
425  {
427  {
428  const CoreParser::DOMSiblings& siblings = node.get_siblings ();
429  for (const CoreParser::DOMNode* child : siblings) {
430  XMLCoreNode n (child);
431  visit (n);
432  }
433 
434  break;
435  }
437  {
439  {
440  std::cout << "XMLCoreParser::visit ELEMENT_NODE "
441  << " factory=" << factory
442  << std::endl;
443  }
444 
445  if (factory != 0) factory->start (*this, core_node);
446  else
447  {
448  std::cerr << "XMLCoreParser> Cannot find factory for element "
449  << nodeName << std::endl;
450  register_factory (nodeName, std::make_unique<DummyFactory>());
451  }
452 
453  const CoreParser::DOMSiblings& siblings = node.get_siblings ();
454  for (const CoreParser::DOMNode* child : siblings) {
455  XMLCoreNode n (child);
456  visit (n);
457  }
458 
459  if (factory != 0) factory->end (*this, core_node);
460 
461  break;
462  }
464  {
465  if (factory != 0) factory->comment (*this, nodeValue);
466  break;
467  }
469  {
470  std::cout << "ENTITY_NODE " << nodeValue << std::endl;
471  break;
472  }
474  {
475  std::cout << "ENTITY_REFERENCE_NODE " << nodeValue << std::endl;
476  break;
477  }
478  default:
479  std::cerr << "Unrecognized node type = "
480  << (long) node.get_type() << std::endl;
481  break;
482  }
483 
485  {
486  std::cout << "XMLCoreParser::visit-2" << std::endl;
487  }
488 }
489 
490 void XMLCoreParser::register_default_factory (std::unique_ptr<XMLCoreFactory> factory)
491 {
492  m_default_factory = std::move (factory);
493 }
494 
495 void XMLCoreParser::register_factory (const std::string& name,
496  std::unique_ptr<XMLCoreFactory> factory)
497 {
499  {
500  std::cout << "XMLCoreFactory::register_factory> name=" << name
501  << " factory=" << factory.get() << std::endl;
502  }
503 
504  m_factories[name] = std::move (factory);
505 }
506 
507 void XMLCoreParser::register_external_entity (const std::string& name, const std::string& file_name)
508 {
510  {
511  std::cout << "XMLCoreParser::register_external_entity> name=" << name
512  << " file_name=" << file_name << std::endl;
513  }
514 
516 }
517 
518 void XMLCoreParser::register_text_entity (const std::string& name, const std::string& text)
519 {
521  {
522  std::cout << "XMLCoreParser::register_text_entity> name=" << name
523  << std::endl;
524  }
525 
527 }
528 
529 
531 {
533  if (it != m_factories.end ())
534  {
535  return (*it).second.get();
536  }
537  return m_default_factory.get();
538 }
539 
540 
542 {
543  m_level += 1;
544 }
545 
546 
548 {
549  m_level -= 1;
550 }
551 
552 
554 {
555  return m_level;
556 }
XMLCoreParser::find_factory
XMLCoreFactory * find_factory(const std::string &name)
Definition: XMLCoreParser.cxx:530
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
XMLCoreParser::parse
XMLCoreNode parse(const std::string &file_name)
Definition: XMLCoreParser.cxx:371
XMLCoreFactory::do_comment
virtual void do_comment(XMLCoreParser &parser, const std::string &comment)
Definition: XMLCoreParser.cxx:117
python.CaloScaleNoiseConfig.parser
parser
Definition: CaloScaleNoiseConfig.py:75
python.PoolAttributeHelper.attrs
list attrs
Definition: PoolAttributeHelper.py:89
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ReadCellNoiseFromCool.name1
name1
Definition: ReadCellNoiseFromCool.py:233
get_generator_info.result
result
Definition: get_generator_info.py:21
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
XMLCoreNode::operator=
XMLCoreNode & operator=(const XMLCoreNode &other)
Definition: XMLCoreParser.cxx:29
CoreParser::DOMSiblings
std::vector< DOMNode * > DOMSiblings
Definition: DOMNode.h:18
index
Definition: index.py:1
XMLCoreFactory::has_attribute
static bool has_attribute(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:132
XMLCoreFactory::start
void start(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:71
CoreParser::DOMNamedNodeMap
std::map< std::string, std::string > DOMNamedNodeMap
Definition: DOMNode.h:15
skel.it
it
Definition: skel.GENtoEVGEN.py:423
CoreParser::DOMNode
Definition: DOMNode.h:21
XMLCoreFactory::get_int
static int get_int(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:142
XMLCoreFactory::get_boolean
static bool get_boolean(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:162
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
XMLCoreParserDebugger::debug
static bool debug()
Definition: XMLCoreParser.cxx:19
XMLCoreParser.h
python.LumiCalcHtml.lc
lc
Definition: LumiCalcHtml.py:579
CoreParser::DOMNode::ENTITY_NODE
@ ENTITY_NODE
Definition: DOMNode.h:28
XMLCoreFactory::check_int
static bool check_int(const int n, const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:260
XMLCoreNode
Definition: XMLCoreParser.h:21
XMLCoreParser::register_text_entity
void register_text_entity(const std::string &name, const std::string &text)
Definition: XMLCoreParser.cxx:518
XMLCoreParser::m_level
int m_level
Definition: XMLCoreParser.h:141
XMLCoreParser::down
void down()
Definition: XMLCoreParser.cxx:547
ExpatCoreParser.h
CoreParser::DOMNode::DOCUMENT_NODE
@ DOCUMENT_NODE
Definition: DOMNode.h:25
physics_parameters.file_name
string file_name
Definition: physics_parameters.py:32
XMLCoreParser
Definition: XMLCoreParser.h:107
XMLCoreParser::set_validation_scheme
void set_validation_scheme(const char *parm)
Definition: XMLCoreParser.cxx:351
XMLCoreParser::m_default_factory
std::unique_ptr< XMLCoreFactory > m_default_factory
Definition: XMLCoreParser.h:140
CoreParser::DOMNode::COMMENT_NODE
@ COMMENT_NODE
Definition: DOMNode.h:27
lumiFormat.i
int i
Definition: lumiFormat.py:92
ExpatCoreParser::register_text_entity
static void register_text_entity(const std::string &name, const std::string &text)
Definition: ExpatCoreParser.cxx:96
XMLCoreNode::m_owns
bool m_owns
Definition: XMLCoreParser.h:67
beamspotman.n
n
Definition: beamspotman.py:731
DummyFactory
Definition: XMLCoreParser.cxx:340
CoreParser::DOMNode::ELEMENT_NODE
@ ELEMENT_NODE
Definition: DOMNode.h:26
XMLCoreFactory::get_value
static std::string get_value(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:180
XMLCoreFactory::attribute_number
static int attribute_number(const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:125
XMLCoreFactory::do_start
virtual void do_start(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:101
XMLCoreParser::register_factory
void register_factory(const std::string &name, std::unique_ptr< XMLCoreFactory > factory)
Definition: XMLCoreParser.cxx:495
XMLCoreFactory::end
void end(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:81
XMLCoreFactory
Definition: XMLCoreParser.h:71
PyPoolBrowser.node
node
Definition: PyPoolBrowser.py:131
XMLCoreNode::m_node
const CoreParser::DOMNode * m_node
Definition: XMLCoreParser.h:66
CaloCondBlobAlgs_fillNoiseFromASCII.comment
string comment
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:27
XMLCoreParser::set_do_namespaces
void set_do_namespaces()
Definition: XMLCoreParser.cxx:355
XMLCoreFactory::check_double
static bool check_double(const int n, const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:299
merge_scale_histograms.doc
string doc
Definition: merge_scale_histograms.py:9
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
CoreParser::DOMNode::ENTITY_REFERENCE_NODE
@ ENTITY_REFERENCE_NODE
Definition: DOMNode.h:29
XMLCoreParser::m_factories
FactoryMap m_factories
Definition: XMLCoreParser.h:139
XMLCoreFactory::get_ID
static std::string get_ID(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:173
XMLCoreParser::register_default_factory
void register_default_factory(std::unique_ptr< XMLCoreFactory > factory)
Definition: XMLCoreParser.cxx:490
XMLCoreParserDebugger
Definition: XMLCoreParser.cxx:13
XMLCoreParser::set_validation_schema_full_checking
void set_validation_schema_full_checking()
Definition: XMLCoreParser.cxx:363
DeMoScan.index
string index
Definition: DeMoScan.py:362
XMLCoreParserDebugger::get_debug_state
static bool get_debug_state()
Definition: XMLCoreParser.cxx:15
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
XMLCoreParser::set_do_schema
void set_do_schema()
Definition: XMLCoreParser.cxx:359
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:557
XMLCoreParser::set_create_entity_reference_nodes
void set_create_entity_reference_nodes()
Definition: XMLCoreParser.cxx:367
XMLCoreFactory::~XMLCoreFactory
virtual ~XMLCoreFactory()
Definition: XMLCoreParser.cxx:63
XMLCoreNode::get_node
const CoreParser::DOMNode & get_node() const
Definition: XMLCoreParser.h:60
XMLCoreParser::register_external_entity
void register_external_entity(const std::string &name, const std::string &file_name)
Definition: XMLCoreParser.cxx:507
XMLCoreNode::~XMLCoreNode
~XMLCoreNode()
Definition: XMLCoreParser.cxx:51
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:84
XMLCoreParser::XMLCoreParser
XMLCoreParser()
Definition: XMLCoreParser.cxx:343
XMLCoreFactory::get_token
static std::string get_token(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:233
XMLCoreFactory::sibling_number
int sibling_number(const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:210
XMLCoreParser::level
int level() const
Definition: XMLCoreParser.cxx:553
XMLCoreParser::~XMLCoreParser
~XMLCoreParser()
Definition: XMLCoreParser.cxx:347
XMLCoreFactory::get_name
std::string get_name(const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:203
test_pyathena.counter
counter
Definition: test_pyathena.py:15
XMLCoreFactory::comment
void comment(XMLCoreParser &parser, const std::string &comment)
Definition: XMLCoreParser.cxx:91
XMLCoreFactory::get_double
static double get_double(const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:152
python.compressB64.c
def c
Definition: compressB64.py:93
XMLCoreParser::up
void up()
Definition: XMLCoreParser.cxx:541
XMLCoreParser::visit
void visit(const std::string &file_name)
Definition: XMLCoreParser.cxx:382
node
Definition: memory_hooks-stdcmalloc.h:74
XMLCoreFactory::do_end
virtual void do_end(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:109