commonlib
libxml_node.h
Go to the documentation of this file.
1 
11 #pragma once
12 #include "serializable_node.h"
13 #include "dx_node.h"
14 
15 BEGIN_NAMESPACE_SERIALIZATION
16 
17 class LibXmlNode;
18 using LibXmlNodePtr = std::shared_ptr<LibXmlNode>;
19 
26 {
27 public:
28  using DxNodePtr = Dewesoft::Utils::Serialization::DxNodePtr;
29 
33  LibXmlNode();
34 
38  explicit LibXmlNode(const std::string& name);
39 
43  LibXmlNode(const std::string& name, const std::string& value);
44 
48  LibXmlNode(const std::string& name, const std::string& value, bool write);
49 
53  explicit LibXmlNode(const NodePtr& node);
54 
58  ~LibXmlNode();
59 
65  NodePtr updateChild(const std::string& childName) override;
66 
72  NodePtr readChild(const std::string& childName) override;
73 
80  NodePtr createNode(std::string name, std::string value) override;
81 
87  void initializeNode(std::string name, std::string value) override;
88 
93  void initializeNode(NodePtr node) override;
94 
99  void setValue(std::string value) override;
100 
106  NodePtr addChild(std::string name) override;
107 
113  NodePtr addChild(const NodePtr& childNode) override;
114 
119  NodePtr clone() override;
120 
126  NodePtr addAttributeNode(const NodePtr& attributeNode) override;
127 
131  void clearChildNodes() override;
132 
136  void clearAttributeNodes() override;
137 
143  NodePtr findChildNode(const std::string& name) override;
144 
150  NodePtr findAttributeNode(const std::string& name) override;
151 
157  NodePtr getAttribute(const size_t attributeIndex) override;
158 
164  NodePtr getChild(const size_t childIndex) override;
165 
170  size_t getAttributeCount() override;
171 
176  size_t getChildCount() override;
177 
182  void removeAttribute(const std::string& attributeName) override;
183 
188  void removeAttribute(const size_t attributeIndex) override;
189 
194  void removeAttribute(NodePtr& attributeNode) override;
195 
200  void removeChild(const std::string& childName) override;
201 
206  void removeChild(const size_t childIndex) override;
207 
212  void removeChild(NodePtr& childNode) override;
213 
218  std::string getName() override;
219 
224  std::string getValue() override;
225 
230  NodePtr getNextSibling() override;
231 
237  void updateOperation(bool write) override;
238 
243  bool isWrite() override;
244 
249  bool isInitialized() override;
250 
255  bool isEmpty() override;
256 
260  void destroy() override;
261 
266  void saveToFile(const std::string& fileName) override;
267 
272  void readFromFile(const std::string& fileName) override;
273 
278  void saveToStream(std::ostream& outStream) override;
279 
284  void readFromStream(std::istream& inStream) override;
285 protected:
286  LibXmlNode(const DxNodePtr& node, bool xmlUpdateType);
287  LibXmlNode(const DxNodePtr& node, bool xmlUpdateType, bool isDummyNode);
288 
289 private:
290  DxNodePtr node = nullptr;
291  bool isWriteUpdateType = true;
292 
293  DxNodePtr getInternalNode() const;
294 
295  void internalWrite(const std::string& name, const std::string& value, const std::string& defaultValue) override;
296  void internalRead(const std::string& name, std::string& value, const std::string& defaultValue) override;
297  void internalAddAttribute(const std::string& name, const std::string& value) override;
298 
299  DxNodePtr convertNodeToDxNode(const NodePtr& inputNode);
300  void buildDxNodeTreeFromInputNode(const NodePtr& inputNode, const DxNodePtr& outputNode);
301  void copyInputNodeAttributesToDxNode(const NodePtr& inputNode, const DxNodePtr& outputNode);
302 
303  NodePtr addUniqueChild(const std::string& name) override;
304 };
305 
306 END_NAMESPACE_SERIALIZATION
307 
NodePtr findChildNode(const std::string &name) override
findChildNode finds a child node with a given name and returns a pointer to it or nullptr if not foun...
NodePtr readChild(const std::string &childName) override
readChild function finds a node child with a given name
SerializableNode is an abstract class that defines functions for writing/reading to/from a custom fil...
Definition: serializable_node.h:19
std::string getName() override
getName returns a name of a node.
bool isEmpty() override
isEmpty returns true if node does not have any children, attributes and if it does not have any value...
bool isInitialized() override
isInitialized returns true if node is initialized or false otherwise.
void clearChildNodes() override
clearChildNodes clears all child nodes.
std::string getValue() override
getValue returns a value of a node.
void updateOperation(bool write) override
updateOperation sets a node update operation (node can be used either for writing or reading)...
NodePtr getNextSibling() override
getNextSibling returns a sibling of a node or nullptr if it does not exist.
void removeAttribute(const std::string &attributeName) override
removeAttribute removes all attributes with a given name from a node.
NodePtr clone() override
clone function creates a clone of a node.
LibXmlNode is a class that inherits from Node and SerializableNode.
Definition: libxml_node.h:25
LibXmlNode()
LibXmlNode constructor that initializes a node with a &#39;RootNode&#39; default name.
NodePtr addChild(std::string name) override
addChild function creates and adds a child with a given name to a node and returns a pointer to it...
~LibXmlNode()
Destructor.
SerializableNode is an abstract class that defines functions for writing/reading to/from a custom fil...
NodePtr getChild(const size_t childIndex) override
getChild returns a child with a given index or nullptr if index is invalid.
void write(const std::string &name, const T &value)
write function creates a new child with a given name and adds a given value to it.
Definition: node.h:393
NodePtr getAttribute(const size_t attributeIndex) override
getAttribute returns an attribute with a given index or nullptr if index is invalid.
void destroy() override
destroy function releases internal node and sets it value to nullptr.
void setValue(std::string value) override
setValue function sets a given value to a node.
void saveToStream(std::ostream &outStream) override
saveToStream saves an internal node to a given ostream.
void readFromFile(const std::string &fileName) override
readFromFile reads and builds an internal node from a given file.
NodePtr addAttributeNode(const NodePtr &attributeNode) override
addAttributeNode adds a given attribute node to a node and returns a pointer to it.
void removeChild(const std::string &childName) override
removeChild removes all children with a given name from a node.
size_t getAttributeCount() override
getAttributeCount returns a number of attributes of a node.
size_t getChildCount() override
getChildCount returns a number of children of a node.
void initializeNode(std::string name, std::string value) override
initializeNode function initializes a Node with a given name and given value.
void readFromStream(std::istream &inStream) override
readFromStream reads and builds an internal node from a given istream.
bool isWrite() override
isWrite returns true if node should be used for writing or false otherwise.
NodePtr createNode(std::string name, std::string value) override
createNode function creates a new Node and returns a pointer to it.
void clearAttributeNodes() override
clearAttributeNodes clears all attribute nodes.
DxNode class defines an internal xml node and implements functions for creating, modifying and removi...
void saveToFile(const std::string &fileName) override
saveToFile saves an internal node to a given file.
NodePtr findAttributeNode(const std::string &name) override
findAttributeNode finds an attribute node with a given name and returns a pointer to it or nullptr if...
NodePtr updateChild(const std::string &childName) override
updateChild function creates or finds a node child with a given name based on update type ...