XmlParser.cpp 12 KB

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