XMLParser.cpp 11 KB

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