XmlParser.cpp 11 KB

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