XMLReader.hpp 3.5 KB

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