sptk2 logo
SPTK Home Page
Node.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/Variant.h>
30#include <sptk5/xdoc/Attributes.h>
31
32namespace sptk::xdoc {
33
34enum class DataFormat : uint8_t
35{
36 JSON,
37 XML
38};
39
40enum class SearchMode : uint8_t
41{
42 ImmediateChild,
43 Recursive
44};
45
46class SP_EXPORT Node
47 : public std::enable_shared_from_this<Node>
48{
49public:
50 using SNode = std::shared_ptr<Node>;
51 using Nodes = std::vector<SNode>;
52 using Vector = std::vector<SNode>;
53 using iterator = Nodes::iterator;
54 using const_iterator = Nodes::const_iterator;
55
56 enum class Type : uint8_t
57 {
58 DocumentRoot,
59 Null,
60 Text,
61 Number,
62 Boolean,
63 Array,
64 Object,
65 CData,
66 Comment,
67 ProcessingInstruction
68 };
69
70 Node(const String& nodeName = "", Type type = Type::Null);
71
72 virtual ~Node() = default;
73
74 virtual void clear();
75
76 virtual void clearChildren();
77
78 String name() const
79 {
80 return m_name;
81 }
82
83 void name(const String& name)
84 {
85 m_name = name;
86 }
87
88 Type type() const
89 {
90 return m_type;
91 }
92
93 void type(Type type)
94 {
95 m_type = type;
96 }
97
98 SNode& pushNode(const String& name, Type type = Type::Null);
99
108 SNode& pushValue(const String& name, const Variant& value, Node::Type type = Node::Type::Null);
109
117 SNode& pushValue(const Variant& value, Node::Type type = Node::Type::Null);
118
119 Attributes& attributes();
120
121 const Attributes& attributes() const;
122
123 String getString(const String& name = "") const;
124
125 String getText(const String& name = "") const;
126
127 double getNumber(const String& name = "") const;
128
129 bool getBoolean(const String& name = "") const;
130
131 const Nodes& nodes(const String& name = "") const;
132
133 const Variant& getValue() const
134 {
135 return m_value;
136 }
137
138 template<typename T>
139 void set(const T& value)
140 {
141 m_value = value;
142 }
143
144 template<typename T>
145 SNode& set(const String& name, const T& value)
146 {
147 auto& node = findOrCreate(name);
148 node->m_value = value;
149 node->type(variantTypeToNodeType(node->m_value.dataType()));
150
151 return node;
152 }
153
154 bool remove(const String& name);
155
156 bool remove(const SNode& node);
157
158 SNode& findOrCreate(const String& name);
159
160 SNode findFirst(const String& name, SearchMode searchMode = SearchMode::Recursive) const;
161
162 SNode& parent()
163 {
164 return m_parent;
165 }
166
167 const SNode& parent() const
168 {
169 return m_parent;
170 }
171
178 static void importJson(const SNode& jsonElement, const sptk::Buffer& jsonStr);
179
180 void load(DataFormat dataFormat, const Buffer& data, bool xmlKeepFormatting = false);
181
182 void load(DataFormat dataFormat, const String& data, bool xmlKeepFormatting = false);
183
184 void exportTo(DataFormat dataFormat, Buffer& data, bool formatted) const;
185
186 void exportTo(DataFormat dataFormat, std::ostream& stream, bool formatted) const;
187
193 [[nodiscard]] Node::Vector select(const String& xpath);
194
200 static void clone(const SNode& destination, const SNode& source);
201
202private:
203 SNode m_parent {nullptr};
204 String m_name;
205 Type m_type {Type::Null};
206 Variant m_value;
207 Attributes m_attributes;
208 Nodes m_nodes;
209
210 static Type variantTypeToNodeType(VariantDataType type);
211};
212
213using Element = Node;
214using SNode = Node::SNode;
215
220bool isFloat(const String& str);
221
226bool isInteger(const String& str);
227
232bool isBoolean(const String& str);
233
234} // namespace sptk::xdoc
Definition: Buffer.h:51
Definition: String.h:49
Definition: Variant.h:372
Definition: Attributes.h:35
Definition: Node.h:48
SNode & pushValue(const String &name, const Variant &value, Node::Type type=Node::Type::Null)
Push named property to object.
SNode & pushValue(const Variant &value, Node::Type type=Node::Type::Null)
Push value to array.
static void importJson(const SNode &jsonElement, const sptk::Buffer &jsonStr)
Node::Vector select(const String &xpath)
Select a list of sub-nodes matching xpath.
static void clone(const SNode &destination, const SNode &source)
Perform a deep copy of the source to destination.
VariantDataType
Definition: VariantData.h:44

Fri Oct 14 2022 09:58:32: SPTK 5.4.1