XmlParser.hpp 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-25
  6. * Changed: 2024-09-13
  7. *
  8. * */
  9. #ifndef LS_STD_XML_PARSER_HPP
  10. #define LS_STD_XML_PARSER_HPP
  11. #include "XmlDocument.hpp"
  12. #include "XmlParseMode.hpp"
  13. #include "XmlParseParameter.hpp"
  14. #include <list>
  15. #include <ls-std/core/Class.hpp>
  16. #include <ls-std/core/type/Types.hpp>
  17. #include <ls-std/os/dynamic-goal.hpp>
  18. #include <string_view>
  19. /*
  20. * @doc: class(name: 'XmlParser', package: 'io')
  21. * @doc: io.XmlParser.description('This class parses a whole XML tree in form of an XML document.')
  22. * */
  23. namespace ls::std::io
  24. {
  25. class LS_STD_DYNAMIC_GOAL XmlParser : public ls::std::core::Class
  26. {
  27. public:
  28. explicit XmlParser(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document);
  29. ~XmlParser() noexcept override;
  30. [[nodiscard]] ::std::shared_ptr<ls::std::io::XmlDocument> getDocument() const;
  31. void parse(const ls::std::core::type::byte_field &_data);
  32. void setDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document);
  33. protected:
  34. [[nodiscard]] static ::std::pair<::std::string, ::std::string> _readAttribute_(const ls::std::core::type::byte_field &_data);
  35. [[nodiscard]] static ::std::list<::std::pair<::std::string, ::std::string>> _readAttributes_(ls::std::core::type::byte_field _data);
  36. private:
  37. uint8_t currentLevel{};
  38. ::std::shared_ptr<ls::std::io::XmlDocument> document{};
  39. uint8_t maxLevel{};
  40. ls::std::io::XmlParseMode mode{};
  41. ::std::list<ls::std::io::XmlParseParameter> parseParameters{};
  42. void _analyze(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  43. void _assignDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document);
  44. [[nodiscard]] static bool _contains(::std::string_view _text, ::std::string_view _searchText);
  45. [[nodiscard]] static ::std::shared_ptr<ls::std::io::XmlDeclaration> _createDeclaration(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes);
  46. [[nodiscard]] static ::std::shared_ptr<ls::std::io::XmlNode> _createNode(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, ::std::string_view _name);
  47. [[nodiscard]] static bool _endsWith(::std::string_view _text, ::std::string_view _ending);
  48. [[nodiscard]] static ::std::pair<::std::string, ::std::string> _findAttribute(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, const ::std::string &_name);
  49. [[nodiscard]] static size_t _findAttributeEndPosition(const ls::std::core::type::byte_field &_data);
  50. [[nodiscard]] static ls::std::core::type::byte_field _getNextTagString(::std::string_view _data, ::std::string::size_type _index);
  51. void _isClosingTag(::std::string_view _data, ::std::string::size_type _index);
  52. void _isDeclaration(::std::string_view _data, ::std::string::size_type _index);
  53. void _isOpeningTag(::std::string_view _data, ::std::string::size_type _index);
  54. void _isValue(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  55. void _mergeNodes();
  56. void _mergeChildrenToParentNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_parent, ::std::list<ls::std::io::XmlParseParameter>::iterator &_iterator, uint8_t _parentLevel);
  57. void _mergeNodesOnCurrentLevel();
  58. void _parse(const ls::std::core::type::byte_field &_data);
  59. [[nodiscard]] static ::std::pair<::std::string, ::std::string> _parseAttribute(::std::string_view _data);
  60. [[nodiscard]] static ::std::list<::std::pair<::std::string, ::std::string>> _parseAttributes(ls::std::core::type::byte_field _data);
  61. [[nodiscard]] size_t _parseClosingTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  62. [[nodiscard]] size_t _parseDeclaration(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index) const;
  63. [[nodiscard]] ::std::string::size_type _parseMode(::std::string::size_type _index, const ls::std::core::type::byte_field &_data);
  64. [[nodiscard]] size_t _parseOpeningTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  65. [[nodiscard]] static ::std::string_view _parseTagName(::std::string_view _data);
  66. [[nodiscard]] size_t _parseValue(::std::string_view _data, ::std::string::size_type _index);
  67. void _reset();
  68. void _setMaxLevel();
  69. };
  70. }
  71. #endif