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 (const std::string &name) const
 Try to retrieve an integer attribute.
std::optional< double > try_double_attrib (const std::string &name) const
 Try to retrieve an double attribute.
const std::string & get_attrib (const std::string &name) const
 Retrieve the value of an attribute.
int get_int_attrib (const std::string &name) const
 Retrieve the value of an attribute as an integer.
double get_double_attrib (const std::string &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 (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.

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 > 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 44 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 50 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 36 of file XMLCoreNode.cxx.

39 : m_type (type),
40 m_name (name),
41 m_value (value)
42{
43}
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 63 of file XMLCoreNode.cxx.

64{
65 child->set_parent (this);
66 m_children.push_back (std::move (child));
67 return m_children.back().get();
68}
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 371 of file XMLCoreNode.cxx.

374{
375 std::string::size_type pos = path.find ('/');
376 std::string name = path.substr (0, pos);
377 std::string tail;
378 if (pos != std::string::npos) {
379 tail = path.substr (pos+1);
380 }
381
382 for (const auto& child : m_children) {
383 if (name == "*" || child->get_name() == name) {
384 if (tail.empty()) {
385 children.push_back (child.get());
386 }
387 else {
388 child->collect_children (tail, children, only_one);
389 }
390 }
391 if (only_one && !children.empty()) break;
392 }
393}
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 ( const std::string & 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 213 of file XMLCoreNode.cxx.

214{
215 auto it = m_attribs.find (name);
216 if (it == m_attribs.end()) {
217 throw ExcXMLCore ("Cannot find attribute " + name + " in " + m_name);
218 }
219 return it->second;
220}
std::map< std::string, std::string > 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 132 of file XMLCoreNode.cxx.

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

◆ 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 266 of file XMLCoreNode.cxx.

267{
268 std::vector<const XMLCoreNode*> children;
269 collect_children (path, children, true);
270 if (!children.empty()) {
271 return children[0];
272 }
273 return nullptr;
274}
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 287 of file XMLCoreNode.cxx.

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

◆ get_double_attrib()

double XMLCoreNode::get_double_attrib ( const std::string & 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 247 of file XMLCoreNode.cxx.

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

◆ get_int_attrib()

int XMLCoreNode::get_int_attrib ( const std::string & 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 230 of file XMLCoreNode.cxx.

231{
232 std::optional<int> val = try_int_attrib (name);
233 if (!val) {
234 throw ExcXMLCore ("Bad integer attribute " + name + " in " + m_name);
235 }
236 return val.value();
237}
std::optional< int > try_int_attrib(const std::string &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 101 of file XMLCoreNode.cxx.

102{
103 return m_name;
104}

◆ get_parent() [1/2]

XMLCoreNode * XMLCoreNode::get_parent ( )

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

Definition at line 74 of file XMLCoreNode.cxx.

75{
76 return m_parent;
77}
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 83 of file XMLCoreNode.cxx.

84{
85 return m_parent;
86}

◆ get_type()

XMLCoreNode::NodeType XMLCoreNode::get_type ( ) const

Return the type of this node.

Definition at line 92 of file XMLCoreNode.cxx.

93{
94 return m_type;
95}

◆ 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 110 of file XMLCoreNode.cxx.

111{
112 return m_value;
113}

◆ 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 148 of file XMLCoreNode.cxx.

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

◆ n_attribs()

size_t XMLCoreNode::n_attribs ( ) const

Return the number of attributes for this node.

Definition at line 119 of file XMLCoreNode.cxx.

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

◆ print() [1/2]

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

Definition at line 300 of file XMLCoreNode.cxx.

302{
303 print (std::cout, header, depth);
304}
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 313 of file XMLCoreNode.cxx.

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

52{
54}

◆ set_parent()

void XMLCoreNode::set_parent ( XMLCoreNode * parent)
private

Set the parent for this node.

Parameters
Pointerto the parent node.

Definition at line 357 of file XMLCoreNode.cxx.

358{
360}

◆ try_attrib()

template<class T>
std::optional< T > XMLCoreNode::try_attrib ( const std::string & 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 162 of file XMLCoreNode.cxx.

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

◆ try_double_attrib()

std::optional< double > XMLCoreNode::try_double_attrib ( const std::string & 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 201 of file XMLCoreNode.cxx.

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

◆ try_int_attrib()

std::optional< int > XMLCoreNode::try_int_attrib ( const std::string & 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 188 of file XMLCoreNode.cxx.

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

Member Data Documentation

◆ m_attribs

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

Attributes of this node.

Definition at line 277 of file XMLCoreNode.h.

◆ m_children

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

Children of this node.

Definition at line 280 of file XMLCoreNode.h.

◆ m_name

std::string XMLCoreNode::m_name
private

The name of this node.

Definition at line 271 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 265 of file XMLCoreNode.h.

◆ m_type

NodeType XMLCoreNode::m_type
private

The type of this node.

Definition at line 268 of file XMLCoreNode.h.

◆ m_value

std::string XMLCoreNode::m_value
private

The value fo this node.

Definition at line 274 of file XMLCoreNode.h.


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