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 | 1017608 | auto _child_find_lambda(std::string_view name) { | |
35 | 2641679 | 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 | 56836 | virtual ~Content() {} | |
50 | |||
51 | 28418 | 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 | 56836 | explicit ContentImpl(C&& c) | |
62 | 56836 | : _c(std::forward<C>(c)) | |
63 | 56836 | {} | |
64 | |||
65 | 56834 | void stream(std::ostream& os) const override { | |
66 | 56834 | os << _c; | |
67 | 56834 | } | |
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 | 261280 | explicit XMLElement(std::string name) | |
82 | 261280 | : XMLTag(std::move(name)) | |
83 | 261280 | {} | |
84 | |||
85 | 262362 | 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 | 248893 | XMLElement& add_child(std::string name) { | |
104 | 248893 | XMLElement child(std::move(name)); | |
105 | 248893 | child._parent = this; | |
106 |
1/2✓ Branch 2 taken 248893 times.
✗ Branch 3 not taken.
|
248893 | _children.emplace_back(std::move(child)); |
107 | 497786 | return _children.back(); | |
108 | 248893 | } | |
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 | 431206 | bool has_child(std::string_view child_name) const { | |
118 | 431206 | return std::ranges::any_of( | |
119 | 431206 | _children, | |
120 | Detail::_child_find_lambda<XMLElement>(child_name) | ||
121 | 431206 | ); | |
122 | } | ||
123 | |||
124 | 423567 | XMLElement& get_child(std::string_view child_name) { | |
125 | 423567 | auto it = std::ranges::find_if( | |
126 |
1/2✓ Branch 1 taken 423567 times.
✗ Branch 2 not taken.
|
423567 | _children, |
127 | Detail::_child_find_lambda<XMLElement>(child_name) | ||
128 | ); | ||
129 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 423567 times.
|
423567 | if (it == _children.end()) |
130 | ✗ | throw ValueError("XMLElement has no child '" + std::string{child_name} + "'"); | |
131 | 847134 | return *it; | |
132 | } | ||
133 | |||
134 | 162834 | const XMLElement& get_child(std::string_view child_name) const { | |
135 | 162834 | auto it = std::ranges::find_if( | |
136 |
1/2✓ Branch 1 taken 162834 times.
✗ Branch 2 not taken.
|
162834 | _children, |
137 | Detail::_child_find_lambda<XMLElement>(child_name) | ||
138 | ); | ||
139 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 162834 times.
|
162834 | if (it == _children.end()) |
140 | ✗ | throw ValueError("XMLElement has no child '" + std::string{child_name} + "'"); | |
141 | 325668 | return *it; | |
142 | } | ||
143 | |||
144 | 191225 | std::size_t number_of_children() const { | |
145 | 191225 | return _children.size(); | |
146 | } | ||
147 | |||
148 | template<Concepts::StreamableWith<std::ostream> C> | ||
149 | requires(!std::is_lvalue_reference_v<C>) | ||
150 | 56836 | void set_content(C&& content) { | |
151 |
1/2✓ Branch 2 taken 28418 times.
✗ Branch 3 not taken.
|
56836 | _content = std::make_unique<ContentImpl<C>>(std::forward<C>(content)); |
152 | 56836 | } | |
153 | |||
154 | 28417 | void stream_content(std::ostream& s) const { | |
155 | 28417 | _content->stream(s); | |
156 | 28417 | } | |
157 | |||
158 | 308726 | bool has_content() const { | |
159 | 308726 | 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 | 103969 | friend const ChildStorage& children(const XMLElement& e) { | |
174 | 103969 | 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 | 239396 | OptionalReference<Element> access_at(std::string_view path, Element& element, char delimiter = '/') { | |
201 |
2/2✓ Branch 2 taken 47041 times.
✓ Branch 3 taken 124607 times.
|
239396 | if (path == "") |
202 | 94082 | return OptionalReference<Element>{element}; | |
203 | 145314 | Element* result = &element; | |
204 |
11/16✓ Branch 1 taken 124607 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 124607 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 124607 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 200214 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 146055 times.
✓ Branch 14 taken 54159 times.
✓ Branch 16 taken 146055 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 200914 times.
✓ Branch 20 taken 69748 times.
✓ Branch 21 taken 2100 times.
✓ Branch 22 taken 700 times.
|
911626 | for (const auto& name : Path::elements_of(path, delimiter)) { |
205 |
3/4✓ Branch 2 taken 200214 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54159 times.
✓ Branch 5 taken 146055 times.
|
244578 | if (!result->has_child(name)) |
206 | 56368 | return {}; | |
207 |
1/2✓ Branch 2 taken 146055 times.
✗ Branch 3 not taken.
|
188210 | result = &result->get_child(name); |
208 | } | ||
209 | 88946 | 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 | 122467 | XMLElement& access_or_create_at(std::string_view path, XMLElement& element, char delimiter = '/') { | |
223 |
2/2✓ Branch 2 taken 15185 times.
✓ Branch 3 taken 107282 times.
|
122467 | if (path == "") |
224 | 15185 | return element; | |
225 | 107282 | XMLElement* result = &element; | |
226 |
2/4✓ Branch 1 taken 107282 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 107282 times.
✗ Branch 5 not taken.
|
107282 | std::ranges::for_each(Path::elements_of(path, delimiter), [&] (const auto& name) { |
227 |
3/4✓ Branch 2 taken 175027 times.
✓ Branch 3 taken 29756 times.
✓ Branch 6 taken 175027 times.
✗ Branch 7 not taken.
|
204783 | result = result->has_child(name) ? &result->get_child(name) |
228 |
4/8✓ Branch 1 taken 29756 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29756 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29756 times.
✓ Branch 7 taken 175027 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
204783 | : &result->add_child(name); |
229 | 204783 | }); | |
230 | 107282 | return *result; | |
231 | } | ||
232 | |||
233 | |||
234 | #ifndef DOXYGEN | ||
235 | namespace XML::Detail { | ||
236 | |||
237 | 219275 | void write_xml_tag_open(const XMLElement& e, | |
238 | std::ostream& s, | ||
239 | std::string_view close_char) { | ||
240 | 219275 | s << "<" << e.name(); | |
241 |
2/4✓ Branch 1 taken 219275 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 219275 times.
✗ Branch 5 not taken.
|
219275 | std::ranges::for_each(attributes(e), |
242 | 728120 | [&] (const std::string& attr_name) { | |
243 | s << " " << attr_name | ||
244 | << "=\"" | ||
245 |
1/2✓ Branch 1 taken 728120 times.
✗ Branch 2 not taken.
|
1456240 | << e.get_attribute(attr_name) |
246 |
2/4✓ Branch 5 taken 728120 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 728120 times.
✗ Branch 9 not taken.
|
1456240 | << "\""; |
247 | 728120 | }); | |
248 | 219275 | s << close_char; | |
249 | 219275 | } | |
250 | |||
251 | 89449 | void write_xml_tag_open(const XMLElement& e, | |
252 | std::ostream& s) { | ||
253 |
1/2✓ Branch 2 taken 89449 times.
✗ Branch 3 not taken.
|
89449 | write_xml_tag_open(e, s, ">"); |
254 | 89449 | } | |
255 | |||
256 | 33276 | void write_empty_xml_tag(const XMLElement& e, | |
257 | std::ostream& s) { | ||
258 |
1/2✓ Branch 2 taken 33276 times.
✗ Branch 3 not taken.
|
33276 | write_xml_tag_open(e, s, "/>"); |
259 | 33276 | } | |
260 | |||
261 | 89449 | void write_xml_tag_close(const XMLElement& e, | |
262 | std::ostream& s) { | ||
263 | 89449 | s << "</" << e.name() << ">"; | |
264 | 89449 | } | |
265 | |||
266 | 75192 | void write_xml_element(const XMLElement& e, | |
267 | std::ostream& s, | ||
268 | Indentation& ind) { | ||
269 |
6/6✓ Branch 1 taken 52930 times.
✓ Branch 2 taken 22262 times.
✓ Branch 4 taken 33276 times.
✓ Branch 5 taken 19654 times.
✓ Branch 6 taken 33276 times.
✓ Branch 7 taken 41916 times.
|
75192 | if (!e.has_content() && e.number_of_children() == 0) { |
270 | 33276 | s << ind; | |
271 | 33276 | write_empty_xml_tag(e, s); | |
272 | } else { | ||
273 | 41916 | s << ind; | |
274 | 41916 | write_xml_tag_open(e, s); | |
275 | 41916 | s << "\n"; | |
276 | |||
277 |
2/2✓ Branch 1 taken 22262 times.
✓ Branch 2 taken 19654 times.
|
41916 | if (e.has_content()) { |
278 | 22262 | e.stream_content(s); | |
279 | 22262 | s << "\n"; | |
280 | } | ||
281 | |||
282 | 41916 | ++ind; | |
283 |
2/2✓ Branch 6 taken 71336 times.
✓ Branch 7 taken 41916 times.
|
113252 | for (const auto& c : children(e)) { |
284 |
1/2✓ Branch 1 taken 71336 times.
✗ Branch 2 not taken.
|
71336 | write_xml_element(c, s, ind); |
285 |
1/2✓ Branch 1 taken 71336 times.
✗ Branch 2 not taken.
|
71336 | s << "\n"; |
286 | } | ||
287 | 41916 | --ind; | |
288 | |||
289 | 41916 | s << ind; | |
290 | 41916 | write_xml_tag_close(e, s); | |
291 | } | ||
292 | 75192 | } | |
293 | |||
294 | } // end namespace XML::Detail | ||
295 | #endif // DOXYGEN | ||
296 | |||
297 | 3856 | inline void write_xml(const XMLElement& e, | |
298 | std::ostream& s, | ||
299 | Indentation ind = {}) { | ||
300 | 3856 | XML::Detail::write_xml_element(e, s, ind); | |
301 | 3856 | } | |
302 | |||
303 | 3852 | inline void write_xml_with_version_header(const XMLElement& e, | |
304 | std::ostream& s, | ||
305 | Indentation ind = {}) { | ||
306 | 3852 | s << "<?xml version=\"1.0\"?>\n"; | |
307 |
2/4✓ Branch 1 taken 3852 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3852 times.
✗ Branch 5 not taken.
|
3852 | write_xml(e, s, ind); |
308 | 3852 | } | |
309 | |||
310 | } // end namespace GridFormat | ||
311 | |||
312 | #endif // GRIDFORMAT_XML_ELEMENT_HPP_ | ||
313 |