Line | Branch | Exec | Source |
---|---|---|---|
1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
2 | // SPDX-License-Identifier: MIT | ||
3 | /*! | ||
4 | * \ingroup XML | ||
5 | * \copydoc GridFormat::XMLElement | ||
6 | */ | ||
7 | #ifndef GRIDFORMAT_XML_ELEMENT_HPP_ | ||
8 | #define GRIDFORMAT_XML_ELEMENT_HPP_ | ||
9 | |||
10 | #include <ranges> | ||
11 | #include <utility> | ||
12 | #include <string> | ||
13 | #include <ostream> | ||
14 | #include <iterator> | ||
15 | #include <string_view> | ||
16 | #include <type_traits> | ||
17 | #include <memory> | ||
18 | #include <list> | ||
19 | #include <any> | ||
20 | |||
21 | #include <gridformat/common/path.hpp> | ||
22 | #include <gridformat/common/concepts.hpp> | ||
23 | #include <gridformat/common/indentation.hpp> | ||
24 | #include <gridformat/common/exceptions.hpp> | ||
25 | #include <gridformat/common/optional_reference.hpp> | ||
26 | #include <gridformat/xml/tag.hpp> | ||
27 | |||
28 | namespace GridFormat { | ||
29 | |||
30 | #ifndef DOXYGEN | ||
31 | namespace Detail { | ||
32 | |||
33 | template<typename Child> | ||
34 | 1018085 | auto _child_find_lambda(std::string_view name) { | |
35 | 2643058 | return [name] (const Child& child) { return child.name() == name; }; | |
36 | } | ||
37 | |||
38 | } // end namespace Detail | ||
39 | #endif // DOXYGEN | ||
40 | |||
41 | /*! | ||
42 | * \ingroup XML | ||
43 | * \brief Class to represent an XML element, i.e. an XML tag with a data body. | ||
44 | */ | ||
45 | class XMLElement : public XMLTag { | ||
46 | using ChildStorage = std::list<XMLElement>; | ||
47 | |||
48 | struct Content { | ||
49 | 56140 | virtual ~Content() {} | |
50 | |||
51 | 28070 | Content() = default; | |
52 | Content(const Content&) = delete; | ||
53 | Content& operator=(const Content&) = delete; | ||
54 | |||
55 | virtual void stream(std::ostream& os) const = 0; | ||
56 | virtual std::any get_as_any() const = 0; | ||
57 | }; | ||
58 | |||
59 | template<Concepts::StreamableWith<std::ostream> C> | ||
60 | struct ContentImpl : public Content { | ||
61 | 56140 | explicit ContentImpl(C&& c) | |
62 | 56140 | : _c(std::forward<C>(c)) | |
63 | 56140 | {} | |
64 | |||
65 | 56138 | void stream(std::ostream& os) const override { | |
66 | 56138 | os << _c; | |
67 | 56138 | } | |
68 | |||
69 | 4 | std::any get_as_any() const override { | |
70 | if constexpr (std::is_constructible_v<std::any, C>) | ||
71 | 4 | return {_c}; | |
72 | else | ||
73 | throw InvalidState("Cannot parse content"); | ||
74 | } | ||
75 | |||
76 | private: | ||
77 | std::remove_cvref_t<C> _c; | ||
78 | }; | ||
79 | |||
80 | public: | ||
81 | 260335 | explicit XMLElement(std::string name) | |
82 | 260335 | : XMLTag(std::move(name)) | |
83 | 260335 | {} | |
84 | |||
85 | 261417 | XMLElement(XMLElement&&) = default; | |
86 | XMLElement(const XMLElement&) = delete; | ||
87 | XMLElement() = delete; | ||
88 | |||
89 | 1 | XMLElement& parent() { | |
90 | 1 | _check_parent(); | |
91 | 1 | return *_parent; | |
92 | } | ||
93 | |||
94 | const XMLElement& parent() const { | ||
95 | _check_parent(); | ||
96 | return *_parent; | ||
97 | } | ||
98 | |||
99 | bool has_parent() const { | ||
100 | return _parent; | ||
101 | } | ||
102 | |||
103 | 248050 | XMLElement& add_child(std::string name) { | |
104 | 248050 | XMLElement child(std::move(name)); | |
105 | 248050 | child._parent = this; | |
106 |
1/2✓ Branch 2 taken 248050 times.
✗ Branch 3 not taken.
|
248050 | _children.emplace_back(std::move(child)); |
107 | 496100 | return _children.back(); | |
108 | 248050 | } | |
109 | |||
110 | 1 | bool remove_child(std::string_view child_name) { | |
111 | 1 | return std::erase_if( | |
112 | 1 | _children, | |
113 | Detail::_child_find_lambda<XMLElement>(child_name) | ||
114 | 1 | ); | |
115 | } | ||
116 | |||
117 | 427324 | bool has_child(std::string_view child_name) const { | |
118 | 427324 | return std::ranges::any_of( | |
119 | 427324 | _children, | |
120 | Detail::_child_find_lambda<XMLElement>(child_name) | ||
121 | 427324 | ); | |
122 | } | ||
123 | |||
124 | 427831 | XMLElement& get_child(std::string_view child_name) { | |
125 | 427831 | auto it = std::ranges::find_if( | |
126 |
1/2✓ Branch 1 taken 427831 times.
✗ Branch 2 not taken.
|
427831 | _children, |
127 | Detail::_child_find_lambda<XMLElement>(child_name) | ||
128 | ); | ||
129 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 427831 times.
|
427831 | if (it == _children.end()) |
130 | ✗ | throw ValueError("XMLElement has no child '" + std::string{child_name} + "'"); | |
131 | 855662 | return *it; | |
132 | } | ||
133 | |||
134 | 162929 | const XMLElement& get_child(std::string_view child_name) const { | |
135 | 162929 | auto it = std::ranges::find_if( | |
136 |
1/2✓ Branch 1 taken 162929 times.
✗ Branch 2 not taken.
|
162929 | _children, |
137 | Detail::_child_find_lambda<XMLElement>(child_name) | ||
138 | ); | ||
139 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 162929 times.
|
162929 | if (it == _children.end()) |
140 | ✗ | throw ValueError("XMLElement has no child '" + std::string{child_name} + "'"); | |
141 | 325858 | return *it; | |
142 | } | ||
143 | |||
144 | 189939 | std::size_t number_of_children() const { | |
145 | 189939 | return _children.size(); | |
146 | } | ||
147 | |||
148 | template<Concepts::StreamableWith<std::ostream> C> | ||
149 | requires(!std::is_lvalue_reference_v<C>) | ||
150 | 56140 | void set_content(C&& content) { | |
151 |
1/2✓ Branch 2 taken 28070 times.
✗ Branch 3 not taken.
|
56140 | _content = std::make_unique<ContentImpl<C>>(std::forward<C>(content)); |
152 | 56140 | } | |
153 | |||
154 | 28069 | void stream_content(std::ostream& s) const { | |
155 | 28069 | _content->stream(s); | |
156 | 28069 | } | |
157 | |||
158 | 306180 | bool has_content() const { | |
159 | 306180 | return static_cast<bool>(_content); | |
160 | } | ||
161 | |||
162 | template<typename T> | ||
163 | 4 | T get_content() const { | |
164 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
4 | if (!has_content()) |
165 | ✗ | throw ValueError("XMLElement has no content"); | |
166 |
3/4✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
|
4 | return std::any_cast<T>(_content->get_as_any()); |
167 | } | ||
168 | |||
169 | 4 | friend ChildStorage& children(XMLElement& e) { | |
170 | 4 | return e._children; | |
171 | } | ||
172 | |||
173 | 103152 | friend const ChildStorage& children(const XMLElement& e) { | |
174 | 103152 | return e._children; | |
175 | } | ||
176 | |||
177 | private: | ||
178 | 1 | void _check_parent() const { | |
179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!_parent) |
180 | ✗ | throw InvalidState("This xml element has no parent"); | |
181 | 1 | } | |
182 | |||
183 | XMLElement* _parent{nullptr}; | ||
184 | ChildStorage _children; | ||
185 | std::unique_ptr<Content> _content{nullptr}; | ||
186 | }; | ||
187 | |||
188 | |||
189 | /*! | ||
190 | * \ingroup XML | ||
191 | * \brief Return a reference to the element resulting from successively | ||
192 | * accessing the child elements as given by the provided path. | ||
193 | * \param path The relative path starting from the given element | ||
194 | * \param element The element at which to start traversing | ||
195 | * \param delimiter The delimiter used to separate path entries. | ||
196 | * \note If an element has multiple children with the same (matching) name, | ||
197 | * the first one will be selected. | ||
198 | */ | ||
199 | template<typename Element> requires(std::same_as<XMLElement, std::remove_cvref_t<Element>>) | ||
200 | 238316 | OptionalReference<Element> access_at(std::string_view path, Element& element, char delimiter = '/') { | |
201 |
2/2✓ Branch 2 taken 47041 times.
✓ Branch 3 taken 123527 times.
|
238316 | if (path == "") |
202 | 94082 | return OptionalReference<Element>{element}; | |
203 | 144234 | Element* result = &element; | |
204 |
11/16✓ Branch 1 taken 123527 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 123527 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 123527 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 198594 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 144975 times.
✓ Branch 14 taken 53619 times.
✓ Branch 16 taken 144975 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 199294 times.
✓ Branch 20 taken 69208 times.
✓ Branch 21 taken 2100 times.
✓ Branch 22 taken 700 times.
|
905686 | for (const auto& name : Path::elements_of(path, delimiter)) { |
205 |
3/4✓ Branch 2 taken 198594 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53619 times.
✓ Branch 5 taken 144975 times.
|
242958 | if (!result->has_child(name)) |
206 | 55828 | return {}; | |
207 |
1/2✓ Branch 2 taken 144975 times.
✗ Branch 3 not taken.
|
187130 | result = &result->get_child(name); |
208 | } | ||
209 | 88406 | return OptionalReference<Element>{*result}; | |
210 | } | ||
211 | |||
212 | /*! | ||
213 | * \ingroup XML | ||
214 | * \brief Return a reference to the element resulting from successively | ||
215 | * accessing or creating the child elements as given by the provided path. | ||
216 | * \param path The relative path starting from the given element | ||
217 | * \param element The element at which to start traversing | ||
218 | * \param delimiter The delimiter used to separate path entries. | ||
219 | * \note If an element has multiple children with the same (matching) name, | ||
220 | * the first one will be selected. | ||
221 | */ | ||
222 | 120877 | XMLElement& access_or_create_at(std::string_view path, XMLElement& element, char delimiter = '/') { | |
223 |
2/2✓ Branch 2 taken 14777 times.
✓ Branch 3 taken 106100 times.
|
120877 | if (path == "") |
224 | 14777 | return element; | |
225 | 106100 | XMLElement* result = &element; | |
226 |
2/4✓ Branch 1 taken 106100 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106100 times.
✗ Branch 5 not taken.
|
106100 | std::ranges::for_each(Path::elements_of(path, delimiter), [&] (const auto& name) { |
227 |
3/4✓ Branch 2 taken 173047 times.
✓ Branch 3 taken 29474 times.
✓ Branch 6 taken 173047 times.
✗ Branch 7 not taken.
|
202521 | result = result->has_child(name) ? &result->get_child(name) |
228 |
4/8✓ Branch 1 taken 29474 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29474 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29474 times.
✓ Branch 7 taken 173047 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
202521 | : &result->add_child(name); |
229 | 202521 | }); | |
230 | 106100 | return *result; | |
231 | } | ||
232 | |||
233 | |||
234 | #ifndef DOXYGEN | ||
235 | namespace XML::Detail { | ||
236 | |||
237 | 217641 | void write_xml_tag_open(const XMLElement& e, | |
238 | std::ostream& s, | ||
239 | std::string_view close_char) { | ||
240 | 217641 | s << "<" << e.name(); | |
241 |
2/4✓ Branch 1 taken 217641 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 217641 times.
✗ Branch 5 not taken.
|
217641 | std::ranges::for_each(attributes(e), |
242 | 721028 | [&] (const std::string& attr_name) { | |
243 | s << " " << attr_name | ||
244 | << "=\"" | ||
245 |
1/2✓ Branch 1 taken 721028 times.
✗ Branch 2 not taken.
|
1442056 | << e.get_attribute(attr_name) |
246 |
2/4✓ Branch 5 taken 721028 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 721028 times.
✗ Branch 9 not taken.
|
1442056 | << "\""; |
247 | 721028 | }); | |
248 | 217641 | s << close_char; | |
249 | 217641 | } | |
250 | |||
251 | 88537 | void write_xml_tag_open(const XMLElement& e, | |
252 | std::ostream& s) { | ||
253 |
1/2✓ Branch 2 taken 88537 times.
✗ Branch 3 not taken.
|
88537 | write_xml_tag_open(e, s, ">"); |
254 | 88537 | } | |
255 | |||
256 | 33410 | void write_empty_xml_tag(const XMLElement& e, | |
257 | std::ostream& s) { | ||
258 |
1/2✓ Branch 2 taken 33410 times.
✗ Branch 3 not taken.
|
33410 | write_xml_tag_open(e, s, "/>"); |
259 | 33410 | } | |
260 | |||
261 | 88537 | void write_xml_tag_close(const XMLElement& e, | |
262 | std::ostream& s) { | ||
263 | 88537 | s << "</" << e.name() << ">"; | |
264 | 88537 | } | |
265 | |||
266 | 74936 | void write_xml_element(const XMLElement& e, | |
267 | std::ostream& s, | ||
268 | Indentation& ind) { | ||
269 |
6/6✓ Branch 1 taken 52944 times.
✓ Branch 2 taken 21992 times.
✓ Branch 4 taken 33410 times.
✓ Branch 5 taken 19534 times.
✓ Branch 6 taken 33410 times.
✓ Branch 7 taken 41526 times.
|
74936 | if (!e.has_content() && e.number_of_children() == 0) { |
270 | 33410 | s << ind; | |
271 | 33410 | write_empty_xml_tag(e, s); | |
272 | } else { | ||
273 | 41526 | s << ind; | |
274 | 41526 | write_xml_tag_open(e, s); | |
275 | 41526 | s << "\n"; | |
276 | |||
277 |
2/2✓ Branch 1 taken 21992 times.
✓ Branch 2 taken 19534 times.
|
41526 | if (e.has_content()) { |
278 | 21992 | e.stream_content(s); | |
279 | 21992 | s << "\n"; | |
280 | } | ||
281 | |||
282 | 41526 | ++ind; | |
283 |
2/2✓ Branch 6 taken 71104 times.
✓ Branch 7 taken 41526 times.
|
112630 | for (const auto& c : children(e)) { |
284 |
1/2✓ Branch 1 taken 71104 times.
✗ Branch 2 not taken.
|
71104 | write_xml_element(c, s, ind); |
285 |
1/2✓ Branch 1 taken 71104 times.
✗ Branch 2 not taken.
|
71104 | s << "\n"; |
286 | } | ||
287 | 41526 | --ind; | |
288 | |||
289 | 41526 | s << ind; | |
290 | 41526 | write_xml_tag_close(e, s); | |
291 | } | ||
292 | 74936 | } | |
293 | |||
294 | } // end namespace XML::Detail | ||
295 | #endif // DOXYGEN | ||
296 | |||
297 | 3832 | inline void write_xml(const XMLElement& e, | |
298 | std::ostream& s, | ||
299 | Indentation ind = {}) { | ||
300 | 3832 | XML::Detail::write_xml_element(e, s, ind); | |
301 | 3832 | } | |
302 | |||
303 | 3828 | inline void write_xml_with_version_header(const XMLElement& e, | |
304 | std::ostream& s, | ||
305 | Indentation ind = {}) { | ||
306 | 3828 | s << "<?xml version=\"1.0\"?>\n"; | |
307 |
2/4✓ Branch 1 taken 3828 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3828 times.
✗ Branch 5 not taken.
|
3828 | write_xml(e, s, ind); |
308 | 3828 | } | |
309 | |||
310 | } // end namespace GridFormat | ||
311 | |||
312 | #endif // GRIDFORMAT_XML_ELEMENT_HPP_ | ||
313 |