XmlParser.hpp 4.0 KB

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