XmlParser.hpp 4.3 KB

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