ATLAS Offline Software
Loading...
Searching...
No Matches
XMLCoreNode.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
4 */
11
12
13#ifndef XMLCOREPARSER_XMLCORENODE_H
14#define XMLCOREPARSER_XMLCORENODE_H
15
16
17#include <string>
18#include <map>
19#include <vector>
20#include <memory>
21#include <optional>
22#include <iosfwd>
23#include <stdexcept>
24
25
30 : public std::runtime_error
31{
32public:
37 ExcXMLCore (const std::string& what);
38};
39
40
45{
46public:
59
60
68 const std::string& name="",
69 const std::string& value="");
70
71
77 void set_attrib (const std::string& name, const std::string& value);
78
79
86 XMLCoreNode* add_child (std::unique_ptr<XMLCoreNode> child);
87
88
93
94
98 const XMLCoreNode* get_parent() const;
99
100
104 NodeType get_type () const;
105
106
110 const std::string& get_name () const;
111
112
116 const std::string& get_value () const;
117
118
122 size_t n_attribs() const;
123
124
132 std::string get_attrib_name (size_t i) const;
133
134
139 bool has_attrib (const std::string& name) const;
140
141
149 std::optional<int> try_int_attrib (const std::string& name) const;
150
151
159 std::optional<double> try_double_attrib (const std::string& name) const;
160
161
168 const std::string& get_attrib (const std::string& name) const;
169
170
178 int get_int_attrib (const std::string& name) const;
179
180
188 double get_double_attrib (const std::string& name) const;
189
190
200 const XMLCoreNode* get_child (const std::string& path) const;
201
202
212 std::vector<const XMLCoreNode*> get_children (const std::string& path ="*") const;
213
214
215 /*
216 * @brief Print the node structure in XML format (to cout).
217 * @param header String to print on hte first line.
218 * @param depth Initial nesting depth.
219 */
220 void print (const std::string& header, int depth = 0) const;
221
222
223 /*
224 * @brief Print the node structure in XML format.
225 * @param os Stream to which to print.
226 * @param header String to print on hte first line.
227 * @param depth Initial nesting depth.
228 */
229 void print (std::ostream& os, const std::string& header, int depth = 0) const;
230
231
232private:
237 void set_parent (XMLCoreNode* parent);
238
239
247 template <class T>
248 std::optional<T> try_attrib (const std::string& name) const;
249
250
259 void collect_children (const std::string& path,
260 std::vector<const XMLCoreNode*>& children,
261 bool only_one) const;
262
263
266
269
271 std::string m_name;
272
274 std::string m_value;
275
277 std::map<std::string, std::string> m_attribs;
278
280 std::vector<std::unique_ptr<XMLCoreNode> > m_children;
281};
282
283
284#endif // not XMLCOREPARSER_XMLCORENODE_H
ExcXMLCore(const std::string &what)
Constructor.
Simple DOM-like node structure to hold the result of XML parsing.
Definition XMLCoreNode.h:45
std::string m_value
The value fo this node.
NodeType
Classify node types.
Definition XMLCoreNode.h:51
@ ENTITY_REFERENCE_NODE
Definition XMLCoreNode.h:57
double get_double_attrib(const std::string &name) const
Retrieve the value of an attribute as a double.
std::string m_name
The name of this node.
const std::string & get_attrib(const std::string &name) const
Retrieve the value of an attribute.
XMLCoreNode * m_parent
The parent of this node, or null for a top-level node.
NodeType m_type
The type of this node.
XMLCoreNode(NodeType type, const std::string &name="", const std::string &value="")
Constructor.
std::string get_attrib_name(size_t i) const
Return the name of the i'th attribute.
bool has_attrib(const std::string &name) const
Test for presence of an attribute with a given name.
std::vector< const XMLCoreNode * > get_children(const std::string &path="*") const
Return all children matching a pattern.
void print(const std::string &header, int depth=0) const
XMLCoreNode * add_child(std::unique_ptr< XMLCoreNode > child)
Add a new child to this node.
void set_parent(XMLCoreNode *parent)
Set the parent for this node.
NodeType get_type() const
Return the type of this node.
std::optional< int > try_int_attrib(const std::string &name) const
Try to retrieve an integer attribute.
size_t n_attribs() const
Return the number of attributes for this node.
const std::string & get_value() const
Return the value of this node, or an empty string if no value.
XMLCoreNode * get_parent()
Get the parent of this node, or nullptr for a top-level node.
void set_attrib(const std::string &name, const std::string &value)
Set the value of an attribute for this node.
std::optional< double > try_double_attrib(const std::string &name) const
Try to retrieve an double attribute.
std::optional< T > try_attrib(const std::string &name) const
Try to retrieve an attribute of type T.
void collect_children(const std::string &path, std::vector< const XMLCoreNode * > &children, bool only_one) const
Collect children of this node matching a path.
const std::string & get_name() const
Return the name of this node, or an empty string if no name.
int get_int_attrib(const std::string &name) const
Retrieve the value of an attribute as an integer.
std::map< std::string, std::string > m_attribs
Attributes of this node.
std::vector< std::unique_ptr< XMLCoreNode > > m_children
Children of this node.
const XMLCoreNode * get_child(const std::string &path) const
Return the first child matching a pattern, or nullptr.
std::string depth
tag string for intendation
Definition fastadd.cxx:46