XmlParser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-26
  6. * Changed: 2023-02-22
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/io/xml/XmlParser.hpp>
  11. ls::std::io::XmlParser::XmlParser(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document) : ls::std::core::Class("XmlParser")
  12. {
  13. this->_assignDocument(_document);
  14. this->_reset();
  15. }
  16. ls::std::io::XmlParser::~XmlParser() noexcept = default;
  17. ::std::shared_ptr<ls::std::io::XmlDocument> ls::std::io::XmlParser::getDocument()
  18. {
  19. return this->document;
  20. }
  21. void ls::std::io::XmlParser::parse(const ls::std::core::type::byte_field &_data)
  22. {
  23. this->_parse(_data);
  24. this->_mergeNodes();
  25. this->_reset();
  26. }
  27. void ls::std::io::XmlParser::setDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document)
  28. {
  29. this->_assignDocument(_document);
  30. }
  31. ::std::pair<::std::string, ::std::string> ls::std::io::XmlParser::_readAttribute_(const ls::std::core::type::byte_field &_data)
  32. {
  33. return ls::std::io::XmlParser::_parseAttribute(_data);
  34. }
  35. ::std::list<::std::pair<::std::string, ::std::string>> ls::std::io::XmlParser::_readAttributes_(ls::std::core::type::byte_field _data)
  36. {
  37. return ls::std::io::XmlParser::_parseAttributes(::std::move(_data));
  38. }
  39. void ls::std::io::XmlParser::_analyze(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  40. {
  41. this->_isDeclaration(_data, _index);
  42. this->_isClosingTag(_data, _index);
  43. this->_isOpeningTag(_data, _index);
  44. this->_isValue(_data, _index);
  45. }
  46. void ls::std::io::XmlParser::_assignDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document)
  47. {
  48. ls::std::core::NullPointerArgumentEvaluator{_document, "passed document reference is null!"}.evaluate();
  49. this->document = _document;
  50. }
  51. bool ls::std::io::XmlParser::_contains(const ::std::string &_text, const ::std::string &_searchText)
  52. {
  53. return _text.find(_searchText) != ::std::string::npos;
  54. }
  55. ::std::shared_ptr<ls::std::io::XmlDeclaration> ls::std::io::XmlParser::_createDeclaration(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes)
  56. {
  57. ::std::shared_ptr<ls::std::io::XmlDeclaration> declaration = ::std::make_shared<ls::std::io::XmlDeclaration>("1.0");
  58. ::std::pair<::std::string, ::std::string> attribute = ls::std::io::XmlParser::_findAttribute(_attributes, "version");
  59. if (!attribute.first.empty())
  60. {
  61. declaration->setVersion(attribute.second);
  62. }
  63. attribute = ls::std::io::XmlParser::_findAttribute(_attributes, "encoding");
  64. if (!attribute.first.empty())
  65. {
  66. declaration->setEncoding(attribute.second);
  67. }
  68. attribute = ls::std::io::XmlParser::_findAttribute(_attributes, "standalone");
  69. if (!attribute.first.empty())
  70. {
  71. declaration->setStandalone(attribute.second);
  72. }
  73. return declaration;
  74. }
  75. ::std::shared_ptr<ls::std::io::XmlNode> ls::std::io::XmlParser::_createNode(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, const ::std::string &_name)
  76. {
  77. ::std::shared_ptr<ls::std::io::XmlNode> node = ::std::make_shared<ls::std::io::XmlNode>(_name);
  78. ::std::shared_ptr<ls::std::io::XmlAttribute> attribute{};
  79. for (const auto &parsedAttribute : _attributes)
  80. {
  81. attribute = ::std::make_shared<ls::std::io::XmlAttribute>(parsedAttribute.first);
  82. attribute->setValue(parsedAttribute.second);
  83. node->addAttributeToEnd(attribute);
  84. }
  85. return node;
  86. }
  87. bool ls::std::io::XmlParser::_endsWith(const ::std::string &_text, const ::std::string &_ending)
  88. {
  89. return _text.rfind(_ending) == (_text.size() - _ending.size());
  90. }
  91. ::std::pair<::std::string, ::std::string> ls::std::io::XmlParser::_findAttribute(const ::std::list<::std::pair<::std::string, ::std::string>> &_attributes, const ::std::string &_name)
  92. {
  93. ::std::pair<::std::string, ::std::string> attribute{};
  94. for (const auto &currentAttribute : _attributes)
  95. {
  96. if (currentAttribute.first == _name)
  97. {
  98. attribute = currentAttribute;
  99. break;
  100. }
  101. }
  102. return attribute;
  103. }
  104. size_t ls::std::io::XmlParser::_findAttributeEndPosition(const ls::std::core::type::byte_field &_data)
  105. {
  106. ::std::string::size_type position = ::std::string::npos;
  107. ::std::string::size_type counter{};
  108. for (char letter : _data)
  109. {
  110. if (letter == '"')
  111. {
  112. counter++;
  113. }
  114. if (counter == 2)
  115. {
  116. break;
  117. }
  118. position++;
  119. }
  120. return position;
  121. }
  122. ls::std::core::type::byte_field ls::std::io::XmlParser::_getNextTagString(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  123. {
  124. ls::std::core::type::byte_field tag{};
  125. size_t closingCharacterPosition = _index + _data.substr(_index).find('>');
  126. if (closingCharacterPosition != ::std::string::npos)
  127. {
  128. tag = _data.substr(_index, (closingCharacterPosition - _index) + 1);
  129. }
  130. return tag;
  131. }
  132. void ls::std::io::XmlParser::_isClosingTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  133. {
  134. if (this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 2) == "</")
  135. {
  136. this->mode = XML_PARSE_MODE_CLOSING_TAG;
  137. }
  138. }
  139. void ls::std::io::XmlParser::_isDeclaration(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  140. {
  141. if (_data.substr(_index, 5) == "<?xml")
  142. {
  143. this->mode = XML_PARSE_MODE_DECLARATION;
  144. }
  145. }
  146. void ls::std::io::XmlParser::_isOpeningTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  147. {
  148. if (this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 1) == "<")
  149. {
  150. this->mode = XML_PARSE_MODE_OPENING_TAG;
  151. }
  152. }
  153. void ls::std::io::XmlParser::_isValue(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  154. {
  155. if (this->mode == XML_PARSE_MODE_ANALYZE)
  156. {
  157. ::std::string::size_type end = _data.substr(_index).find('<');
  158. bool isValue = _data[_index - 1] == '>' && end != ::std::string::npos && end > 0;
  159. if (isValue)
  160. {
  161. ::std::string value{_data.substr(_index, end)};
  162. if (!ls::std::io::XmlParser::_contains(value, "\n") && !ls::std::io::XmlParser::_contains(value, "\r\n"))
  163. {
  164. this->mode = XML_PARSE_MODE_VALUE;
  165. }
  166. }
  167. }
  168. }
  169. void ls::std::io::XmlParser::_mergeNodes()
  170. {
  171. while (this->maxLevel > 1)
  172. {
  173. this->_mergeNodesOnCurrentLevel();
  174. this->maxLevel -= 1;
  175. }
  176. this->document->setRootElement(this->parseParameters.front().getNode());
  177. }
  178. void ls::std::io::XmlParser::_mergeChildrenToParentNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_parent, ::std::list<ls::std::io::XmlParseParameter>::iterator &_iterator, uint8_t _parentLevel)
  179. {
  180. do
  181. {
  182. _iterator++;
  183. if (_iterator == this->parseParameters.end())
  184. {
  185. break;
  186. }
  187. else
  188. {
  189. if (_iterator->getLevel() == this->maxLevel)
  190. {
  191. _parent->addChildToEnd(_iterator->getNode());
  192. }
  193. }
  194. } while (_iterator->getLevel() > _parentLevel);
  195. }
  196. void ls::std::io::XmlParser::_mergeNodesOnCurrentLevel()
  197. {
  198. auto iterator = this->parseParameters.begin();
  199. uint8_t parentLevel = this->maxLevel - 1;
  200. while (iterator != this->parseParameters.end())
  201. {
  202. if (iterator->getLevel() == parentLevel)
  203. {
  204. this->_mergeChildrenToParentNode(iterator->getNode(), iterator, parentLevel);
  205. }
  206. else
  207. {
  208. iterator++;
  209. }
  210. }
  211. }
  212. void ls::std::io::XmlParser::_parse(const ls::std::core::type::byte_field &_data)
  213. {
  214. for (::std::string::size_type index = 0; index < _data.size(); index++)
  215. {
  216. switch (this->mode)
  217. {
  218. case XML_PARSE_MODE_ANALYZE:
  219. {
  220. this->_analyze(_data, index);
  221. }
  222. break;
  223. case XML_PARSE_MODE_DECLARATION:
  224. {
  225. --index;
  226. index = this->_parseDeclaration(_data, index);
  227. this->mode = XML_PARSE_MODE_ANALYZE;
  228. }
  229. break;
  230. case XML_PARSE_MODE_OPENING_TAG:
  231. {
  232. --index;
  233. index = ls::std::io::XmlParser::_parseOpeningTag(_data, index);
  234. this->mode = XML_PARSE_MODE_ANALYZE;
  235. }
  236. break;
  237. case XML_PARSE_MODE_VALUE:
  238. {
  239. --index;
  240. index = ls::std::io::XmlParser::_parseValue(_data, index);
  241. this->mode = XML_PARSE_MODE_ANALYZE;
  242. }
  243. break;
  244. case XML_PARSE_MODE_CLOSING_TAG:
  245. {
  246. --index;
  247. index = ls::std::io::XmlParser::_parseClosingTag(_data, index);
  248. this->mode = XML_PARSE_MODE_ANALYZE;
  249. }
  250. break;
  251. }
  252. }
  253. }
  254. ::std::pair<::std::string, ::std::string> ls::std::io::XmlParser::_parseAttribute(const ls::std::core::type::byte_field &_data)
  255. {
  256. ::std::pair<::std::string, ::std::string> parsedAttribute{};
  257. parsedAttribute.first = _data.substr(0, _data.find('='));
  258. parsedAttribute.second = _data.substr(_data.find('"') + 1);
  259. parsedAttribute.second.pop_back();
  260. return parsedAttribute;
  261. }
  262. ::std::list<::std::pair<::std::string, ::std::string>> ls::std::io::XmlParser::_parseAttributes(ls::std::core::type::byte_field _data)
  263. {
  264. ::std::list<::std::pair<::std::string, ::std::string>> attributes{};
  265. size_t position = _data.find(' ');
  266. _data = position == ::std::string::npos ? "" : _data.substr(position);
  267. while (!_data.empty())
  268. {
  269. do
  270. {
  271. position = _data.find(' ') + 1;
  272. } while (_data[position] == ' ');
  273. if (_data.size() <= 3 && ls::std::io::XmlParser::_endsWith(::std::string{_data}, ">"))
  274. {
  275. break;
  276. }
  277. ::std::string attributeString = _data.substr(position, ls::std::io::XmlParser::_findAttributeEndPosition(_data) + 1);
  278. attributes.push_back(ls::std::io::XmlParser::_parseAttribute(attributeString));
  279. _data = _data.substr(position + attributeString.size());
  280. }
  281. return attributes;
  282. }
  283. size_t ls::std::io::XmlParser::_parseClosingTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  284. {
  285. ::std::string tagString = ls::std::io::XmlParser::_getNextTagString(_data, _index);
  286. this->currentLevel -= 1;
  287. return tagString.empty() ? _index : _index + (tagString.size() - 1);
  288. }
  289. size_t ls::std::io::XmlParser::_parseDeclaration(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  290. {
  291. ::std::string tagString = ls::std::io::XmlParser::_getNextTagString(_data, _index);
  292. bool isValidTagString = !tagString.empty();
  293. if (isValidTagString)
  294. {
  295. ::std::shared_ptr<ls::std::io::XmlDeclaration> declaration = this->_createDeclaration(ls::std::io::XmlParser::_parseAttributes(tagString));
  296. this->document->setDeclaration(declaration);
  297. }
  298. return !isValidTagString ? _index : _index + (tagString.size() - 1);
  299. }
  300. size_t ls::std::io::XmlParser::_parseOpeningTag(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  301. {
  302. ::std::string tagString{ls::std::io::XmlParser::_getNextTagString(_data, _index)};
  303. bool isValidTagString = !tagString.empty();
  304. ls::std::io::XmlParseParameter singleParseParameter{};
  305. if (isValidTagString)
  306. {
  307. ::std::shared_ptr<ls::std::io::XmlNode> node = ls::std::io::XmlParser::_createNode(ls::std::io::XmlParser::_parseAttributes(tagString), ls::std::io::XmlParser::_parseTagName(tagString));
  308. singleParseParameter.setLevel(this->currentLevel);
  309. singleParseParameter.setNode(node);
  310. this->parseParameters.push_back(singleParseParameter);
  311. if (!ls::std::io::XmlParser::_endsWith(tagString, "/>"))
  312. {
  313. this->currentLevel += 1;
  314. this->_setMaxLevel();
  315. }
  316. }
  317. return !isValidTagString ? _index : _index + (tagString.size() - 1);
  318. }
  319. ls::std::core::type::byte_field ls::std::io::XmlParser::_parseTagName(const ls::std::core::type::byte_field &_data)
  320. {
  321. ::std::string::size_type position = _data.find(' ');
  322. if (position == ::std::string::npos)
  323. {
  324. position = _data.find('>');
  325. }
  326. return _data.substr(1, position - 1);
  327. }
  328. size_t ls::std::io::XmlParser::_parseValue(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  329. {
  330. ls::std::core::type::byte_field value = _data.substr(_index, _data.substr(_index).find('<'));
  331. this->parseParameters.back().getNode()->setValue(value);
  332. return _index + (value.size() - 1);
  333. }
  334. void ls::std::io::XmlParser::_reset()
  335. {
  336. this->currentLevel = 1;
  337. this->maxLevel = 1;
  338. this->mode = ls::std::io::XML_PARSE_MODE_ANALYZE;
  339. this->parseParameters.clear();
  340. }
  341. void ls::std::io::XmlParser::_setMaxLevel()
  342. {
  343. if (this->currentLevel > this->maxLevel)
  344. {
  345. this->maxLevel = this->currentLevel;
  346. }
  347. }