XMLParser.hpp 3.3 KB

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