XmlParser.hpp 4.2 KB

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