XmlParser.hpp 4.2 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: 2022-07-03
  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. #include <ls_std/os/dynamic_goal.hpp>
  18. namespace ls
  19. {
  20. namespace std
  21. {
  22. namespace 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() override = default;
  29. ::std::shared_ptr<ls::std::io::XmlDocument> getDocument();
  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. static ::std::pair<::std::string, ::std::string> _readAttribute_(const ls::std::core::type::byte_field &_data);
  34. 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. static bool _contains(const ::std::string &_text, const ::std::string &_searchText);
  44. static ::std::shared_ptr<ls::std::io::XmlDeclaration> _createDeclaration(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes);
  45. static ::std::shared_ptr<ls::std::io::XmlNode> _createNode(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, const ::std::string &_name);
  46. static bool _endsWith(const ::std::string &_text, const ::std::string &_ending);
  47. static ::std::pair<::std::string, ::std::string> _findAttribute(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, const ::std::string &_name);
  48. static size_t _findAttributeEndPosition(const ls::std::core::type::byte_field &_data);
  49. static ls::std::core::type::byte_field _getNextTagString(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  50. void _isClosingTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  51. void _isDeclaration(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  52. void _isOpeningTag(const ls::std::core::type::byte_field &_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. static ::std::pair<::std::string, ::std::string> _parseAttribute(const ls::std::core::type::byte_field &_data);
  59. static ::std::list<::std::pair<::std::string, ::std::string>> _parseAttributes(ls::std::core::type::byte_field _data);
  60. size_t _parseClosingTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  61. size_t _parseDeclaration(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  62. size_t _parseOpeningTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  63. static ls::std::core::type::byte_field _parseTagName(const ls::std::core::type::byte_field &_data);
  64. size_t _parseValue(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
  65. void _reset();
  66. void _setMaxLevel();
  67. };
  68. }
  69. }
  70. }
  71. #endif