UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
ixmlnode.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
8#pragma once
9
10#include <memory>
11#include <sstream>
12#include <string>
13#include <unordered_map>
14#include <vector>
15
16#include "util/stringutil.h"
17
18namespace {
19template<typename T>
20T XValue(const std::string &value) {
21 std::string v(value);
23 std::istringstream temp(v);
24 T out = {};
25 temp >> out;
26 return out;
27}
28
29template<>
30bool XValue(const std::string &value) {
31 std::string v(value);
33 return util::string::IEquals(v, "1") || util::string::IEquals(v, "ON") ||
34 util::string::IEquals(v, "T", 1) // True/False
35 || util::string::IEquals(v, "Y", 1); // Yes/No
36}
37
38template<>
39std::string XValue(const std::string &value) {
40 std::string v(value);
42 return v;
43}
44
45} // end namespace
46
47namespace util::xml {
48
61class IXmlNode {
62 public:
63 explicit IXmlNode(const std::string &tag_name);
64 virtual ~IXmlNode() = default;
65
66 IXmlNode() = delete;
67
73 [[nodiscard]] std::string TagName() const { return tag_name_; }
74
80 void TagName(const std::string &name) { tag_name_ = name; }
81
89 [[nodiscard]] bool IsTagName(const std::string &tag) const;
90
98 [[nodiscard]] bool IsAttribute(const std::string &key,
99 const std::string &value) const;
100
109 template<typename T>
110 [[nodiscard]] T Attribute(const std::string &key, const T &def = {}) const {
111 for (const auto &p : attribute_list_) {
112 if (util::string::IEquals(p.first, key)) {
113 return XValue<T>(p.second);
114 }
115 }
116 return def;
117 }
118
125 template<typename T>
126 void SetAttribute(const std::string &key, const T &value);
127
134 template<typename T>
135 [[nodiscard]] T Value() const {
136 return XValue<T>(value_);
137 }
138
145 template<typename T>
146 void Value(const T &value);
147
157 template<typename T>
158 [[nodiscard]] T Property(const std::string &key, const T &def = {}) const {
159 const IXmlNode *node = GetNode(key);
160 return node != nullptr ? node->Value<T>() : def;
161 }
162
170 template<typename T>
171 void SetProperty(const std::string &key, const T &value) {
172 auto &node = AddUniqueNode(key);
173 node.Value(value);
174 }
175
179 using ChildList = std::vector<const IXmlNode *>;
180
181 virtual void GetChildList(
182 ChildList &child_list) const;
183 [[nodiscard]] virtual const IXmlNode *GetNode(
184 const std::string &tag) const;
185 [[nodiscard]] virtual const IXmlNode *GetNode(const std::string &tag,
186 const std::string &key,
187 const std::string &attr)
188 const;
189
197 [[nodiscard]] bool ExistProperty(const std::string &tag) const {
198 return GetNode(tag) != nullptr;
199 }
200
201 virtual IXmlNode &AddNode(const std::string &name);
203 const std::string &name);
205 const std::string &name, const std::string &key,
206 const std::string &attr);
207
208 virtual void AddNode(std::unique_ptr<IXmlNode> p);
209
210 void DeleteNode(const std::string &name);
211 void DeleteNode(const IXmlNode *node);
212
213 virtual void Write(std::ostream &dest,
214 size_t level);
215
220 [[nodiscard]] bool HasChildren() const {
221 return !node_list_.empty();
222 }
223
224 protected:
225 std::string tag_name_;
226 std::string value_;
227
231 using AttributeList = std::unordered_map<std::string, std::string>;
233
237 using NodeList = std::vector<std::unique_ptr<IXmlNode> >;
239
240 [[nodiscard]] virtual std::unique_ptr<IXmlNode> CreateNode(
241 const std::string &name) const;
242};
243
244template<typename T>
245void IXmlNode::Value(const T &value) {
246 // value_ = std::to_string(value);
247 std::ostringstream temp;
248 temp << value;
249 value_ = temp.str();
250}
251
256template<>
257void IXmlNode::Value(const bool &value);
258
263template<>
264void IXmlNode::Value(const std::string &value);
265
266template<typename T>
267void IXmlNode::SetAttribute(const std::string &key, const T &value) {
268 std::ostringstream temp;
269 temp << value;
270 attribute_list_.insert({key, temp.str()});
271}
272
278template<>
279void IXmlNode::SetAttribute(const std::string &key, const bool &value);
280
281} // namespace util::xml
Interface class against a XML tag (node) in a XML file.
Definition ixmlnode.h:61
T Attribute(const std::string &key, const T &def={}) const
Returns an attribute.
Definition ixmlnode.h:110
virtual const IXmlNode * GetNode(const std::string &tag) const
Returns a node if it exist.
virtual void Write(std::ostream &dest, size_t level)
Write the node to the stream.
T Property(const std::string &key, const T &def={}) const
Returns a specific tag value.
Definition ixmlnode.h:158
void SetProperty(const std::string &key, const T &value)
Sets a property.
Definition ixmlnode.h:171
IXmlNode(const std::string &tag_name)
Creates a XML tag.
std::string TagName() const
Returns the nodes tag name.
Definition ixmlnode.h:73
bool ExistProperty(const std::string &tag) const
Returns true if the named property exist.
Definition ixmlnode.h:197
virtual IXmlNode & AddUniqueNode(const std::string &name, const std::string &key, const std::string &attr)
Adds a unique tag with an attribute.
bool HasChildren() const
Returns true if the XML node has children.
Definition ixmlnode.h:220
virtual IXmlNode & AddUniqueNode(const std::string &name)
Adds a unique tag.
NodeList node_list_
List of nodes.
Definition ixmlnode.h:238
virtual ~IXmlNode()=default
Destructor.
void SetAttribute(const std::string &key, const T &value)
Sets an attribute.
Definition ixmlnode.h:267
std::vector< const IXmlNode * > ChildList
List of pointer to child nodes.
Definition ixmlnode.h:179
T Value() const
Returns a value.
Definition ixmlnode.h:135
virtual void AddNode(std::unique_ptr< IXmlNode > p)
Adds a node.
void DeleteNode(const std::string &name)
Deletes all nodes with name.
void DeleteNode(const IXmlNode *node)
Deletes a specific node.
bool IsAttribute(const std::string &key, const std::string &value) const
Checks if an attribute exist.
virtual const IXmlNode * GetNode(const std::string &tag, const std::string &key, const std::string &attr) const
Returns a node with a specific attribute.
std::string tag_name_
Name of this tag.
Definition ixmlnode.h:225
bool IsTagName(const std::string &tag) const
Returns true if the tag name match. Check if this is a specific tag name. The check ignore case and n...
virtual void GetChildList(ChildList &child_list) const
Returns the child nodes.
void TagName(const std::string &name)
Sets the tag name.
Definition ixmlnode.h:80
AttributeList attribute_list_
Indexed list of attribute.
Definition ixmlnode.h:232
std::vector< std::unique_ptr< IXmlNode > > NodeList
List of nodes.
Definition ixmlnode.h:237
std::unordered_map< std::string, std::string > AttributeList
Indexed list of to key, attribute value pair.
Definition ixmlnode.h:231
virtual IXmlNode & AddNode(const std::string &name)
Adds a new tag.
virtual std::unique_ptr< IXmlNode > CreateNode(const std::string &name) const
Create a node.
std::string value_
String value of this tag.
Definition ixmlnode.h:226
void Trim(std::string &text)
Remove white space from string.
bool IEquals(const std::string &s1, const std::string &s2, size_t nChar=0)
Compare strings by ignoring case.
The xml namespace is used for XML related classes and functions.
Definition isuperviseapplication.h:22