← Documentation
ADS-IDAC Engine 5.0.0
xml_lite.hpp
Go to the documentation of this file.
1#pragma once
10//
11// A deliberately small XML reader covering exactly the subset the `.fta`
12// files use: a single root element, nested elements, double/single quoted
13// attributes, self-closing tags, comments, an optional XML declaration, the
14// five predefined entities, and ASCII numeric character references.
15//
16// We do not reuse src/HCL/xmlParser: it is only built when ENABLE_HCL=ON (the
17// production macOS and HPC builds set it OFF) and it is LGPL, which the
18// open-source packaging would rather not inherit for a new component.
19//
20// Anything outside the subset (DOCTYPE, CDATA, processing instructions beyond
21// the declaration) is rejected with a parse error rather than silently
22// mis-parsed. Namespace prefixes are not interpreted: a colon is an ordinary
23// name character. Text between elements is skipped.
24//
25#include <stdexcept>
26#include <string>
27#include <vector>
28
29namespace faulttree::xml {
30
36class XmlError : public std::runtime_error {
37public:
39 explicit XmlError(const std::string &what) : std::runtime_error(what) {}
40};
41
43struct Attribute {
44 std::string name;
45 std::string value;
46};
47
53struct Element {
54 std::string name;
55 std::vector<Attribute> attributes;
56 std::vector<Element> children;
57
61 [[nodiscard]] const std::string *attribute(const std::string &name) const;
65 [[nodiscard]] std::string attributeOr(const std::string &name,
66 const std::string &fallback) const;
70 [[nodiscard]] const Element *child(const std::string &name) const;
75 [[nodiscard]] std::vector<const Element *>
76 childrenNamed(const std::string &name) const;
77};
78
94Element parse(const std::string &text);
95
103Element parseFile(const std::string &path);
104
105} // namespace faulttree::xml
Exception thrown by parse() and parseFile().
Definition: xml_lite.hpp:36
XmlError(const std::string &what)
Definition: xml_lite.hpp:39
Element parseFile(const std::string &path)
Definition: xml_lite.cpp:261
Element parse(const std::string &text)
Definition: xml_lite.cpp:259
One attribute of an element.
Definition: xml_lite.hpp:43
std::string name
attribute name as written, including any prefix
Definition: xml_lite.hpp:44
std::string value
value with entity and character references decoded
Definition: xml_lite.hpp:45
One XML element with its attributes and child elements.
Definition: xml_lite.hpp:53
std::vector< Attribute > attributes
in document order
Definition: xml_lite.hpp:55
std::string name
tag name as written
Definition: xml_lite.hpp:54
const Element * child(const std::string &name) const
Definition: xml_lite.cpp:22
const std::string * attribute(const std::string &name) const
Definition: xml_lite.cpp:9
std::vector< Element > children
child elements in document order
Definition: xml_lite.hpp:56
std::string attributeOr(const std::string &name, const std::string &fallback) const
Definition: xml_lite.cpp:16
std::vector< const Element * > childrenNamed(const std::string &name) const
Definition: xml_lite.cpp:30