XMLReader.cpp 9.9 KB

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