ATLAS Offline Software
XMLCoreParser.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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  static const std::string uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
201  static const std::string lc = "abcdefghijklmnopqrstuvwxyz";
202  char c = result[i];
203  std::string::size_type p = lc.find (c);
204  if (p != std::string::npos) result[i] = lc[p];
205  }
206  return (result);
207 }
208 
209 
210 bool
211 XMLCoreFactory::check_int (const int n, const XMLCoreNode& node, const std::string& name) {
212  std::string checkstring = get_value (node, name);
213  int counter = 0;
214  //
215  // concatenate two times same string to
216  // check the last number of the string
217  // explicitly!
218  //
219  std::string t = checkstring + " " + checkstring;
220  std::istringstream tmpstr (t.c_str());
221  while (tmpstr.good ()) {
222  int ii;
223  counter++;
224  tmpstr >> ii;
225  }
226  if (counter/2 != n) {
227  std::cerr << "XMLCoreFactory::check_int error: no " << n
228  << " ints in \"" << checkstring << "\" for attribute " <<
229  name << ". exit." << std::endl;
230 
231  std::string nodename = get_value (node, "name");
232  std::string volume = get_value (node, "volume");
233 
234  if (nodename != "" ) std::cerr << "for name=" << nodename << std::endl;
235  if (volume != "" ) std::cerr << "for volume=" << volume << std::endl;
236 
237  std::abort();
238  }
239  return true;
240 }
241 
242 bool
243 XMLCoreFactory::check_double (const int n, const XMLCoreNode& node, const std::string& name) {
244  std::string checkstring = get_value (node, name);
245  int counter = 0;
246  //
247  // concatenate two times same string to
248  // check the last number of the string
249  // explicitly!
250  //
251  std::string t = checkstring + " " + checkstring;
252  std::istringstream tmpstr (t.c_str());
253  while (tmpstr.good ()) {
254  double ii{};
255  counter++;
256  tmpstr >> ii;
257  }
258  if (counter/2 != n) {
259  std::cerr << "XMLCoreFactory::check_double error: (" << counter << ") no " << n
260  << " doubles in \"" << checkstring << "\" for attribute " <<
261  name << ". exit." << std::endl;
262  std::string name1 = get_value (node, "name");
263  std::string volume = get_value (node, "volume");
264  if (name1 != "" ) std::cerr << "for name=" << name << std::endl;
265  if (volume != "" ) std::cerr << "for volume=" << volume << std::endl;
266  std::abort();
267  }
268  return true;
269 }
270 
272 };
273 
274 
276 XMLCoreParser::parse (const std::string& file_name) {
277  m_level = 0;
278  std::unique_ptr<CoreParser::DOMNode> doc = ExpatCoreParser::parse (file_name);
280  if (doc != nullptr) doc->print ("============ ALL =============");
281  }
282  if (not doc){
283  throw std::runtime_error("XMLCoreParser: no such file ["+file_name+"]");
284  }
285  return XMLCoreNode (std::move (doc));
286 }
287 
288 void
289 XMLCoreParser::visit (const std::string& file_name) {
291  std::cout << "XMLCoreParser::visit file_name "
292  << file_name << std::endl;
293  }
296  const CoreParser::DOMNode& node = n.get_node();
297  const CoreParser::DOMNode* nptr = &node;
298  std::cout << "XMLCoreParser::visit node=" << nptr << std::endl;
299  }
300  visit (n);
301 }
302 
303 void
304 XMLCoreParser::visit (const XMLCoreNode& core_node){
305  // Get the name and value out for convenience
306  const CoreParser::DOMNode& node = core_node.get_node ();
307  const CoreParser::DOMNode* nptr = &node;
308  const std::string& nodeName = node.get_name();
309  const std::string& nodeValue = node.get_value();
311  std::cout << "XMLCoreParser::visit node(" << nptr << ") " << nodeName << std::endl;
312  }
313  XMLCoreFactory* factory = find_factory (nodeName);
315  std::cout << "XMLCoreParser::visit factory " << factory << std::endl;
316  }
317 
318  switch (node.get_type()) {
320  const CoreParser::DOMSiblings& siblings = node.get_siblings ();
321  for (const CoreParser::DOMNode* child : siblings) {
322  XMLCoreNode n (child);
323  visit (n);
324  }
325  break;
326  }
329  std::cout << "XMLCoreParser::visit ELEMENT_NODE "
330  << " factory=" << factory
331  << std::endl;
332  }
333  if (factory != 0){
334  factory->start (*this, core_node);
335  } else {
336  std::cerr << "XMLCoreParser> Cannot find factory for element "
337  << nodeName << std::endl;
338  register_factory (nodeName, std::make_unique<DummyFactory>());
339  }
340  const CoreParser::DOMSiblings& siblings = node.get_siblings ();
341  for (const CoreParser::DOMNode* child : siblings) {
342  XMLCoreNode n (child);
343  visit (n);
344  }
345  if (factory != 0) factory->end (*this, core_node);
346  break;
347  }
349  if (factory != 0) factory->comment (*this, nodeValue);
350  break;
351  }
353  std::cout << "ENTITY_NODE " << nodeValue << std::endl;
354  break;
355  }
357  std::cout << "ENTITY_REFERENCE_NODE " << nodeValue << std::endl;
358  break;
359  }
360  default: {
361  std::cerr << "Unrecognized node type = " << (long) node.get_type() << std::endl;
362  break;
363  }
364  }
366  std::cout << "XMLCoreParser::visit-2" << std::endl;
367  }
368 }
369 
370 void
371 XMLCoreParser::register_default_factory (std::unique_ptr<XMLCoreFactory> factory) {
372  m_default_factory = std::move (factory);
373 }
374 
375 void
377  std::unique_ptr<XMLCoreFactory> factory) {
379  std::cout << "XMLCoreFactory::register_factory> name=" << name
380  << " factory=" << factory.get() << std::endl;
381  }
382  m_factories[name] = std::move (factory);
383 }
384 
385 void
386 XMLCoreParser::register_external_entity (const std::string& name, const std::string& file_name){
388  std::cout << "XMLCoreParser::register_external_entity> name=" << name
389  << " file_name=" << file_name << std::endl;
390  }
392 }
393 
394 void
395 XMLCoreParser::register_text_entity (const std::string& name, const std::string& text){
397  std::cout << "XMLCoreParser::register_text_entity> name=" << name
398  << std::endl;
399  }
401 }
402 
403 
405 XMLCoreParser::find_factory (const std::string& name) {
407  if (it != m_factories.end ()) {
408  return (*it).second.get();
409  }
410  return m_default_factory.get();
411 }
412 
413 
414 void
416  m_level += 1;
417 }
418 
419 
420 void
422  m_level -= 1;
423 }
424 
425 
426 int
428  return m_level;
429 }
XMLCoreParser::find_factory
XMLCoreFactory * find_factory(const std::string &name)
Definition: XMLCoreParser.cxx:405
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
XMLCoreParser::parse
XMLCoreNode parse(const std::string &file_name)
Definition: XMLCoreParser.cxx:276
XMLCoreFactory::do_comment
virtual void do_comment(XMLCoreParser &parser, const std::string &comment)
Definition: XMLCoreParser.cxx:104
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
XMLCoreNode::operator=
XMLCoreNode & operator=(const XMLCoreNode &other)
Definition: XMLCoreParser.cxx:28
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: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:15
skel.it
it
Definition: skel.GENtoEVGEN.py:396
CoreParser::DOMNode
Definition: DOMNode.h:21
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
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:211
XMLCoreNode
Definition: XMLCoreParser.h:19
XMLCoreParser::register_text_entity
void register_text_entity(const std::string &name, const std::string &text)
Definition: XMLCoreParser.cxx:395
XMLCoreParser::m_level
int m_level
Definition: XMLCoreParser.h:123
XMLCoreParser::down
void down()
Definition: XMLCoreParser.cxx:421
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:97
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
XMLCoreParser::m_default_factory
std::unique_ptr< XMLCoreFactory > m_default_factory
Definition: XMLCoreParser.h:122
CoreParser::DOMNode::COMMENT_NODE
@ COMMENT_NODE
Definition: DOMNode.h:27
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:731
DummyFactory
Definition: XMLCoreParser.cxx:271
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: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:376
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:27
XMLCoreFactory::check_double
static bool check_double(const int n, const XMLCoreNode &node, const std::string &name)
Definition: XMLCoreParser.cxx:243
merge_scale_histograms.doc
string doc
Definition: merge_scale_histograms.py:9
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
CoreParser::DOMNode::ENTITY_REFERENCE_NODE
@ ENTITY_REFERENCE_NODE
Definition: DOMNode.h:29
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:371
DeMoScan.index
string index
Definition: DeMoScan.py:364
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:417
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:386
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:427
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
python.compressB64.c
def c
Definition: compressB64.py:93
XMLCoreParser::up
void up()
Definition: XMLCoreParser.cxx:415
XMLCoreParser::visit
void visit(const std::string &file_name)
Definition: XMLCoreParser.cxx:289
node
Definition: memory_hooks-stdcmalloc.h:74
XMLCoreFactory::do_end
virtual void do_end(XMLCoreParser &parser, const XMLCoreNode &node)
Definition: XMLCoreParser.cxx:97