XmlNode.hpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-24
  6. * Changed: 2024-09-13
  7. *
  8. * */
  9. #ifndef LS_STD_XML_NODE_HPP
  10. #define LS_STD_XML_NODE_HPP
  11. #include "XmlAttribute.hpp"
  12. #include <list>
  13. #include <ls-std/core/Class.hpp>
  14. #include <ls-std/os/dynamic-goal.hpp>
  15. #include <memory>
  16. /*
  17. * @doc: class(name: 'XmlNode', package: 'io')
  18. * @doc: io.XmlNode.description('This class represents an XML node and can be serialized to an XML tree.')
  19. * */
  20. namespace ls::std::io
  21. {
  22. class LS_STD_DYNAMIC_GOAL XmlNode : public ls::std::core::Class
  23. {
  24. public:
  25. explicit XmlNode(::std::string _name);
  26. ~XmlNode() noexcept override;
  27. bool addAttributeAfter(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name); // nodiscard is optional here
  28. bool addAttributeBefore(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name); // nodiscard is optional here
  29. bool addAttributeToBeginning(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute); // nodiscard is optional here
  30. bool addAttributeToEnd(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute); // nodiscard is optional here
  31. bool addChildAfter(const ::std::shared_ptr<ls::std::io::XmlNode> &_child, const ::std::shared_ptr<ls::std::io::XmlNode> &_search); // nodiscard is optional here
  32. bool addChildBefore(const ::std::shared_ptr<ls::std::io::XmlNode> &_child, const ::std::shared_ptr<ls::std::io::XmlNode> &_search); // nodiscard is optional here
  33. bool addChildToBeginning(const ::std::shared_ptr<ls::std::io::XmlNode> &_child); // nodiscard is optional here
  34. bool addChildToEnd(const ::std::shared_ptr<ls::std::io::XmlNode> &_child); // nodiscard is optional here
  35. void clearValue();
  36. [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlAttribute>> getAttributes() const;
  37. [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlNode>> getChildren() const;
  38. [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlNode>> getChildren(const ::std::string &_name);
  39. [[nodiscard]] ::std::string getName() const;
  40. [[nodiscard]] ::std::string getValue() const;
  41. [[nodiscard]] bool hasAttribute(const ::std::string &_name);
  42. [[nodiscard]] bool hasChild(const ::std::string &_name);
  43. [[nodiscard]] bool hasChild(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);
  44. bool removeFirstAttribute(); // nodiscard is optional here
  45. bool removeLastAttribute(); // nodiscard is optional here
  46. bool removeFirstChild(); // nodiscard is optional here
  47. bool removeLastChild(); // nodiscard is optional here
  48. void setName(const ::std::string &_name);
  49. void setValue(const ::std::string &_value);
  50. [[nodiscard]] ::std::string toXml();
  51. protected:
  52. ::std::string _toXml_(uint8_t _tabSize);
  53. private:
  54. ::std::list<::std::shared_ptr<ls::std::io::XmlAttribute>> attributes{};
  55. ::std::list<::std::shared_ptr<ls::std::io::XmlNode>> children{};
  56. ::std::string name{};
  57. const static uint8_t TAB_SIZE{4};
  58. ::std::string value{};
  59. void _assignName(const ::std::string &_name);
  60. void _assignValue(const ::std::string &_value);
  61. [[nodiscard]] static ::std::string _getTab(uint8_t _tabSize);
  62. [[nodiscard]] bool _hasAttribute(const ::std::string &_name);
  63. [[nodiscard]] bool _hasChild(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);
  64. [[nodiscard]] bool _hasChild(const ::std::string &_name);
  65. [[nodiscard]] ::std::string _toXmlAttributes();
  66. [[nodiscard]] ::std::string _toXmlChildren(uint8_t _tabSize);
  67. [[nodiscard]] ::std::string _toXmlCloseTag() const;
  68. [[nodiscard]] ::std::string _toXmlOpenTag() const;
  69. [[nodiscard]] ::std::string _toXmlOpenTagClose() const;
  70. [[nodiscard]] ::std::string _toXmlValue() const;
  71. };
  72. }
  73. #endif