ATLAS Offline Software
Loading...
Searching...
No Matches
XMLCoreNode Class Reference

Simple DOM-like node structure to hold the result of XML parsing. More...

#include <XMLCoreNode.h>

Collaboration diagram for XMLCoreNode:

Public Types

enum  NodeType {
  DOCUMENT_NODE , ELEMENT_NODE , COMMENT_NODE , TEXT_NODE ,
  ENTITY_NODE , ENTITY_REFERENCE_NODE
}
 Classify node types. More...

Public Member Functions

 XMLCoreNode (NodeType type, const std::string &name="", const std::string &value="")
 Constructor.
void set_attrib (const std::string &name, const std::string &value)
 Set the value of an attribute for this node.
XMLCoreNodeadd_child (std::unique_ptr< XMLCoreNode > child)
 Add a new child to this node.
XMLCoreNodeget_parent ()
 Get the parent of this node, or nullptr for a top-level node.
const XMLCoreNodeget_parent () const
 Get the parent of this node, or nullptr for a top-level node.
NodeType get_type () const
 Return the type of this node.
const std::string & get_name () const
 Return the name of this node, or an empty string if no name.
const std::string & get_value () const
 Return the value of this node, or an empty string if no value.
size_t n_attribs () const
 Return the number of attributes for this node.
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::optional< int > try_int_attrib (std::string_view name) const
 Try to retrieve an integer attribute.
std::optional< double > try_double_attrib (std::string_view name) const
 Try to retrieve an double attribute.
const std::string & get_attrib (std::string_view name) const
 Retrieve the value of an attribute.
int get_int_attrib (std::string_view name) const
 Retrieve the value of an attribute as an integer.
double get_double_attrib (std::string_view name) const
 Retrieve the value of an attribute as a double.
const XMLCoreNodeget_child (const std::string &path) const
 Return the first child matching a pattern, or nullptr.
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
void print (std::ostream &os, const std::string &header, int depth=0) const

Private Member Functions

void set_parent (XMLCoreNode *parent)
 Set the parent for this node.
template<class T>
std::optional< T > try_attrib (std::string_view 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.

Private Attributes

XMLCoreNodem_parent = nullptr
 The parent of this node, or null for a top-level node.
NodeType m_type
 The type of this node.
std::string m_name
 The name of this node.
std::string m_value
 The value fo this node.
std::map< std::string, std::string, std::less<> > m_attribs
 Attributes of this node.
std::vector< std::unique_ptr< XMLCoreNode > > m_children
 Children of this node.

Detailed Description

Simple DOM-like node structure to hold the result of XML parsing.

Definition at line 45 of file XMLCoreNode.h.

Member Enumeration Documentation

◆ NodeType

Classify node types.

Enumerator
DOCUMENT_NODE 
ELEMENT_NODE 
COMMENT_NODE 
TEXT_NODE 
ENTITY_NODE 
ENTITY_REFERENCE_NODE 

Definition at line 51 of file XMLCoreNode.h.

Constructor & Destructor Documentation

◆ XMLCoreNode()

XMLCoreNode::XMLCoreNode ( NodeType type,
const std::string & name = "",
const std::string & value = "" )

Constructor.

Parameters
typeThe node type.
nameNode name (for elements). @apram value Node value (for text/comments).

Definition at line 37 of file XMLCoreNode.cxx.

40 : m_type (type),
41 m_name (name),
42 m_value (value)
43{
44}
std::string m_value
The value fo this node.
std::string m_name
The name of this node.
NodeType m_type
The type of this node.

Member Function Documentation

◆ add_child()

XMLCoreNode * XMLCoreNode::add_child ( std::unique_ptr< XMLCoreNode > child)

Add a new child to this node.

Parameters
childThe child to add.

Children are ordered according to the order of add_child calls.

Definition at line 64 of file XMLCoreNode.cxx.

65{
66 child->set_parent (this);
67 m_children.push_back (std::move (child));
68 return m_children.back().get();
69}
void set_parent(XMLCoreNode *parent)
Set the parent for this node.
std::vector< std::unique_ptr< XMLCoreNode > > m_children
Children of this node.

◆ collect_children()

void XMLCoreNode::collect_children ( const std::string & path,
std::vector< const XMLCoreNode * > & children,
bool only_one ) const
private

Collect children of this node matching a path.

Parameters
pathPattern of the child path for which to search.
children[out]Vector in which to collect matching children.
only_oneIf true, stop after finding the first child.

See get_child above for a description of path.

Definition at line 372 of file XMLCoreNode.cxx.

375{
376 std::string::size_type pos = path.find ('/');
377 std::string name = path.substr (0, pos);
378 std::string tail;
379 if (pos != std::string::npos) {
380 tail = path.substr (pos+1);
381 }
382
383 for (const auto& child : m_children) {
384 if (name == "*" || child->get_name() == name) {
385 if (tail.empty()) {
386 children.push_back (child.get());
387 }
388 else {
389 child->collect_children (tail, children, only_one);
390 }
391 }
392 if (only_one && !children.empty()) break;
393 }
394}
std::string tail(std::string s, const std::string &pattern)
tail of a string
path
python interpreter configuration --------------------------------------—
Definition athena.py:130

◆ get_attrib()

const std::string & XMLCoreNode::get_attrib ( std::string_view name) const

Retrieve the value of an attribute.

Parameters
nameName of the attribute.

Throws an exception if the attribute does not exist.

Definition at line 214 of file XMLCoreNode.cxx.

215{
216 auto it = m_attribs.find (name);
217 if (it == m_attribs.end()) {
218 throw ExcXMLCore (std::format("Cannot find attribute {} in {}", name, m_name));
219 }
220 return it->second;
221}
std::map< std::string, std::string, std::less<> > m_attribs
Attributes of this node.

◆ get_attrib_name()

std::string XMLCoreNode::get_attrib_name ( size_t i) const

Return the name of the i'th attribute.

Parameters
iThe index of the attribute.

Indexes count attributes in order sorted by name. Throws an exception if the index is out of range.

Definition at line 133 of file XMLCoreNode.cxx.

134{
135 if (i >= m_attribs.size()) {
136 throw ExcXMLCore ("Out-of-range attribute index " + std::to_string(i) + " in " + m_name);
137 }
138
139 auto it = m_attribs.begin();
140 std::advance (it, i);
141 return it->first;
142}

◆ get_child()

const XMLCoreNode * XMLCoreNode::get_child ( const std::string & path) const

Return the first child matching a pattern, or nullptr.

Parameters
pathPattern of the child path for which to search.

The @path argument is a set of nested child node names, separated by dots. For example, path node1.node2 means to look for a child of the current node named node1, and then look for a child of that one named node2. A node name may also be * to match any child.

Parameters
pathPattern of the child path for which to search.

The @path argument is a set of nested child node names, separated by slashes. For example, path node1/node2 means to look for a child of the current node named node1, and then look for a child of that one named node2. A node name may also be * to match any child.

Definition at line 267 of file XMLCoreNode.cxx.

268{
269 std::vector<const XMLCoreNode*> children;
270 collect_children (path, children, true);
271 if (!children.empty()) {
272 return children[0];
273 }
274 return nullptr;
275}
void collect_children(const std::string &path, std::vector< const XMLCoreNode * > &children, bool only_one) const
Collect children of this node matching a path.

◆ get_children()

std::vector< const XMLCoreNode * > XMLCoreNode::get_children ( const std::string & path = "*") const

Return all children matching a pattern.

Parameters
pathPattern of the child path for which to search.

The @path argument is a set of nested child node names, separated by dots. For example, path node1.node2 means to look for a child of the current node named node1, and then look for a child of that one named node2. A node name may also be * to match any child.

Parameters
pathPattern of the child path for which to search.

For example, path node1/node2 means to look for a child of the current node named node1, and then look for a child of that one named node2. node named node1, and then look for a child of that one named node2. A node name may also be * to match any child.

Definition at line 288 of file XMLCoreNode.cxx.

289{
290 std::vector<const XMLCoreNode*> children;
291 collect_children (path, children, false);
292 return children;
293}

◆ get_double_attrib()

double XMLCoreNode::get_double_attrib ( std::string_view name) const

Retrieve the value of an attribute as a double.

Parameters
nameName of the attribute.

Throws an exception if the attribute does not exist or cannot be converted to a double.

Definition at line 248 of file XMLCoreNode.cxx.

249{
250 std::optional<double> val = try_double_attrib (name);
251 if (!val) {
252 throw ExcXMLCore (std::format("Bad double attribute {} in {}", name, m_name));
253 }
254 return val.value();
255}
std::optional< double > try_double_attrib(std::string_view name) const
Try to retrieve an double attribute.

◆ get_int_attrib()

int XMLCoreNode::get_int_attrib ( std::string_view name) const

Retrieve the value of an attribute as an integer.

Parameters
nameName of the attribute.

Throws an exception if the attribute does not exist or cannot be converted to an int.

Definition at line 231 of file XMLCoreNode.cxx.

232{
233 std::optional<int> val = try_int_attrib (name);
234 if (!val) {
235 throw ExcXMLCore (std::format("Bad integer attribute {} in {}", name, m_name));
236 }
237 return val.value();
238}
std::optional< int > try_int_attrib(std::string_view name) const
Try to retrieve an integer attribute.

◆ get_name()

const std::string & XMLCoreNode::get_name ( ) const

Return the name of this node, or an empty string if no name.

Definition at line 102 of file XMLCoreNode.cxx.

103{
104 return m_name;
105}

◆ get_parent() [1/2]

XMLCoreNode * XMLCoreNode::get_parent ( )

Get the parent of this node, or nullptr for a top-level node.

Definition at line 75 of file XMLCoreNode.cxx.

76{
77 return m_parent;
78}
XMLCoreNode * m_parent
The parent of this node, or null for a top-level node.

◆ get_parent() [2/2]

const XMLCoreNode * XMLCoreNode::get_parent ( ) const

Get the parent of this node, or nullptr for a top-level node.

Definition at line 84 of file XMLCoreNode.cxx.

85{
86 return m_parent;
87}

◆ get_type()

XMLCoreNode::NodeType XMLCoreNode::get_type ( ) const

Return the type of this node.

Definition at line 93 of file XMLCoreNode.cxx.

94{
95 return m_type;
96}

◆ get_value()

const std::string & XMLCoreNode::get_value ( ) const

Return the value of this node, or an empty string if no value.

Definition at line 111 of file XMLCoreNode.cxx.

112{
113 return m_value;
114}

◆ has_attrib()

bool XMLCoreNode::has_attrib ( const std::string & name) const

Test for presence of an attribute with a given name.

Parameters
nameAttribute name to test.

Definition at line 149 of file XMLCoreNode.cxx.

150{
151 return m_attribs.contains (name);
152}

◆ n_attribs()

size_t XMLCoreNode::n_attribs ( ) const

Return the number of attributes for this node.

Definition at line 120 of file XMLCoreNode.cxx.

121{
122 return m_attribs.size();
123}

◆ print() [1/2]

void XMLCoreNode::print ( const std::string & header,
int depth = 0 ) const

Definition at line 301 of file XMLCoreNode.cxx.

303{
304 print (std::cout, header, depth);
305}
void print(const std::string &header, int depth=0) const
std::string depth
tag string for intendation
Definition fastadd.cxx:46

◆ print() [2/2]

void XMLCoreNode::print ( std::ostream & os,
const std::string & header,
int depth = 0 ) const

Definition at line 314 of file XMLCoreNode.cxx.

317{
318 if (!header.empty()) {
319 os << header << std::endl;
320 }
321
322 if (m_type == TEXT_NODE) {
323 bool allspace = m_value.find_last_of(SPACE) == std::string::npos;
324 if (!allspace)
325 os << m_value << std::endl;
326 return;
327 }
328
329 for (int i = 0; i < depth; i++) os << " ";
330
331 if (m_type == COMMENT_NODE) {
332 os << "<!--" << m_value << "-->" << std::endl;
333 return;
334 }
335
336 os << "<" << m_name;
337
338 for (const auto& p : m_attribs) {
339 os << " " << p.first << "='" << p.second << "'";
340 }
341
342 os << ">" << std::endl;
343
344 for (const auto& child : m_children) {
345 child->print (os, "", depth+1);
346 }
347
348 for (int i = 0; i < depth; i++) os << " ";
349 os << "</" << m_name << ">" << std::endl;
350 return;
351}
static const char *const SPACE

◆ set_attrib()

void XMLCoreNode::set_attrib ( const std::string & name,
const std::string & value )

Set the value of an attribute for this node.

Parameters
nameAttribute name.
valueAttribute value (as a string).

Definition at line 52 of file XMLCoreNode.cxx.

53{
55}

◆ set_parent()

void XMLCoreNode::set_parent ( XMLCoreNode * parent)
private

Set the parent for this node.

Parameters
Pointerto the parent node.

Definition at line 358 of file XMLCoreNode.cxx.

359{
361}

◆ try_attrib()

template<class T>
std::optional< T > XMLCoreNode::try_attrib ( std::string_view name) const
private

Try to retrieve an attribute of type T.

param name Name of the attribute to retrieve.

Returns an invalid optional if the attribute does not exist or cannot be converted to T.

Definition at line 163 of file XMLCoreNode.cxx.

164{
165 std::optional<T> ret;
166 auto it = m_attribs.find (name);
167 if (it == m_attribs.end()) return ret;
168
169 T val = 0;
170 std::string::size_type beg = it->second.find_first_not_of (SPACE);
171 std::string::size_type end = it->second.find_last_not_of (SPACE);
172 if (beg == std::string::npos || end == std::string::npos) return ret;
173 const char* begp = it->second.c_str()+beg;
174 const char* endp = it->second.c_str()+end+1;
175 auto [ptr, ec] = std::from_chars (begp, endp, val);
176 if (ec == std::errc() && ptr == endp)
177 ret.emplace (val);
178 return ret;
179}
unsigned long long T
void * ptr(T *p)
Definition SGImplSvc.cxx:74

◆ try_double_attrib()

std::optional< double > XMLCoreNode::try_double_attrib ( std::string_view name) const

Try to retrieve an double attribute.

Parameters
nameName of the attribute.

Returns an invalid optional if the attribute does not exist or cannot be converted to a double.

Definition at line 202 of file XMLCoreNode.cxx.

203{
204 return try_attrib<double> (name);
205}
std::optional< T > try_attrib(std::string_view name) const
Try to retrieve an attribute of type T.

◆ try_int_attrib()

std::optional< int > XMLCoreNode::try_int_attrib ( std::string_view name) const

Try to retrieve an integer attribute.

Parameters
nameName of the attribute.

Returns an invalid optional if the attribute does not exist or cannot be converted to an integer.

Definition at line 189 of file XMLCoreNode.cxx.

190{
191 return try_attrib<int> (name);
192}

Member Data Documentation

◆ m_attribs

std::map<std::string, std::string, std::less<> > XMLCoreNode::m_attribs
private

Attributes of this node.

Definition at line 278 of file XMLCoreNode.h.

◆ m_children

std::vector<std::unique_ptr<XMLCoreNode> > XMLCoreNode::m_children
private

Children of this node.

Definition at line 281 of file XMLCoreNode.h.

◆ m_name

std::string XMLCoreNode::m_name
private

The name of this node.

Definition at line 272 of file XMLCoreNode.h.

◆ m_parent

XMLCoreNode* XMLCoreNode::m_parent = nullptr
private

The parent of this node, or null for a top-level node.

Definition at line 266 of file XMLCoreNode.h.

◆ m_type

NodeType XMLCoreNode::m_type
private

The type of this node.

Definition at line 269 of file XMLCoreNode.h.

◆ m_value

std::string XMLCoreNode::m_value
private

The value fo this node.

Definition at line 275 of file XMLCoreNode.h.


The documentation for this class was generated from the following files: