XmlParser.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-25
  6. * Changed: 2023-05-16
  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. namespace ls::std::io
  20. {
  21. class LS_STD_DYNAMIC_GOAL XmlParser : public ls::std::core::Class
  22. {
  23. public:
  24. explicit XmlParser(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document);
  25. ~XmlParser() noexcept override;
  26. [[nodiscard]] ::std::shared_ptr<ls::std::io::XmlDocument> getDocument() const;
  27. void parse(const ls::std::core::type::byte_field &_data);
  28. void setDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document);
  29. protected:
  30. [[nodiscard]] static ::std::pair<::std::string, ::std::string> _readAttribute_(const ls::std::core::type::byte_field &_data);
  31. [[nodiscard]] static ::std::list<::std::pair<::std::string, ::std::string>> _readAttributes_(ls::std::core::type::byte_field _data);
  32. private:
  33. uint8_t currentLevel{};
  34. ::std::shared_ptr<ls::std::io::XmlDocument> document{};
  35. uint8_t maxLevel{};
  36. ls::std::io::XmlParseMode mode{};
  37. ::std::list<ls::std::io::XmlParseParameter> parseParameters{};
  38. void _analyze(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  39. void _assignDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document);
  40. [[nodiscard]] static bool _contains(::std::string_view _text, ::std::string_view _searchText);
  41. [[nodiscard]] static ::std::shared_ptr<ls::std::io::XmlDeclaration> _createDeclaration(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes);
  42. [[nodiscard]] static ::std::shared_ptr<ls::std::io::XmlNode> _createNode(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, ::std::string_view _name);
  43. [[nodiscard]] static bool _endsWith(::std::string_view _text, ::std::string_view _ending);
  44. [[nodiscard]] static ::std::pair<::std::string, ::std::string> _findAttribute(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, const ::std::string &_name);
  45. [[nodiscard]] static size_t _findAttributeEndPosition(const ls::std::core::type::byte_field &_data);
  46. [[nodiscard]] static ls::std::core::type::byte_field _getNextTagString(::std::string_view _data, ::std::string::size_type _index);
  47. void _isClosingTag(::std::string_view _data, ::std::string::size_type _index);
  48. void _isDeclaration(::std::string_view _data, ::std::string::size_type _index);
  49. void _isOpeningTag(::std::string_view _data, ::std::string::size_type _index);
  50. void _isValue(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  51. void _mergeNodes();
  52. void _mergeChildrenToParentNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_parent, ::std::list<ls::std::io::XmlParseParameter>::iterator &_iterator, uint8_t _parentLevel);
  53. void _mergeNodesOnCurrentLevel();
  54. void _parse(const ls::std::core::type::byte_field &_data);
  55. [[nodiscard]] static ::std::pair<::std::string, ::std::string> _parseAttribute(::std::string_view _data);
  56. [[nodiscard]] static ::std::list<::std::pair<::std::string, ::std::string>> _parseAttributes(ls::std::core::type::byte_field _data);
  57. [[nodiscard]] size_t _parseClosingTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  58. [[nodiscard]] size_t _parseDeclaration(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index) const;
  59. [[nodiscard]] ::std::string::size_type _parseMode(::std::string::size_type _index, const ls::std::core::type::byte_field &_data);
  60. [[nodiscard]] size_t _parseOpeningTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  61. [[nodiscard]] static ::std::string_view _parseTagName(::std::string_view _data);
  62. [[nodiscard]] size_t _parseValue(::std::string_view _data, ::std::string::size_type _index);
  63. void _reset();
  64. void _setMaxLevel();
  65. };
  66. }
  67. #endif