XMLParser.hpp 3.3 KB

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