XmlParser.cpp 12 KB

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