XMLReader.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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-19
  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::_mergeNodesOnCurrentLevel() {
  155. auto iterator = this->parseData.begin();
  156. uint8_t parentLevel {};
  157. while(iterator != this->parseData.end()) {
  158. parentLevel = this->maxLevel - 1;
  159. if(iterator->level == parentLevel) {
  160. std::shared_ptr<ls_std::XMLNode> parent = iterator->node;
  161. do {
  162. iterator++;
  163. if(iterator != this->parseData.end() && iterator->level == this->maxLevel) {
  164. parent->addChildToEnd(iterator->node);
  165. }
  166. }
  167. while(iterator->level == this->maxLevel);
  168. }
  169. else {
  170. iterator++;
  171. }
  172. }
  173. }
  174. void ls_std::XMLReader::_parse(const ls_std::byte_field &_data)
  175. {
  176. for(std::string::size_type index = 0 ; index < _data.size() ; index++) {
  177. switch(this->mode) {
  178. case XML_PARSE_MODE_ANALYZE:
  179. {
  180. this->_analyze(_data, index);
  181. } break;
  182. case XML_PARSE_MODE_DECLARATION:
  183. {
  184. --index;
  185. index = this->_parseDeclaration(_data, index);
  186. this->mode = XML_PARSE_MODE_ANALYZE;
  187. } break;
  188. case XML_PARSE_MODE_OPENING_TAG:
  189. {
  190. --index;
  191. index = ls_std::XMLReader::_parseOpeningTag(_data, index);
  192. this->mode = XML_PARSE_MODE_ANALYZE;
  193. } break;
  194. case XML_PARSE_MODE_CLOSING_TAG:
  195. {
  196. --index;
  197. index = ls_std::XMLReader::_parseClosingTag(_data, index);
  198. this->mode = XML_PARSE_MODE_ANALYZE;
  199. } break;
  200. }
  201. }
  202. }
  203. std::pair<std::string, std::string> ls_std::XMLReader::_parseAttribute(const ls_std::byte_field &_data)
  204. {
  205. std::pair<std::string, std::string> parsedAttribute {};
  206. parsedAttribute.first = _data.substr(0, _data.find('='));
  207. parsedAttribute.second = _data.substr(_data.find('"') + 1);
  208. parsedAttribute.second.pop_back();
  209. return parsedAttribute;
  210. }
  211. std::list<std::pair<std::string, std::string>> ls_std::XMLReader::_parseAttributes(ls_std::byte_field _data)
  212. {
  213. std::list<std::pair<std::string, std::string>> attributes {};
  214. size_t position = _data.find(' ');
  215. _data = position == std::string::npos ? "" : _data.substr(position);
  216. while(!_data.empty()) {
  217. do {
  218. position = _data.find(' ') + 1;
  219. }
  220. while(_data[position] == ' ');
  221. if(_data.size() <= 3 && ls_std::String {_data}.endsWith(">")) {
  222. break;
  223. }
  224. std::string attributeString = _data.substr(position, ls_std::XMLReader::_findAttributeEndPosition(_data) + 1);
  225. attributes.push_back(ls_std::XMLReader::_parseAttribute(attributeString));
  226. _data = _data.substr(position + attributeString.size());
  227. }
  228. return attributes;
  229. }
  230. size_t ls_std::XMLReader::_parseClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
  231. {
  232. std::string tagString = ls_std::XMLReader::_getNextTagString(_data, _index);
  233. this->currentLevel -= 1;
  234. return tagString.empty() ? _index : _index + tagString.size();
  235. }
  236. size_t ls_std::XMLReader::_parseDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
  237. {
  238. std::string tagString = ls_std::XMLReader::_getNextTagString(_data, _index);
  239. bool isValidTagString = !tagString.empty();
  240. if(isValidTagString) {
  241. std::shared_ptr<ls_std::XMLDeclaration> declaration = this->_createDeclaration(ls_std::XMLReader::_parseAttributes(tagString));
  242. this->document->setDeclaration(declaration);
  243. }
  244. return !isValidTagString ? _index : _index + tagString.size();
  245. }
  246. size_t ls_std::XMLReader::_parseOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
  247. {
  248. ls_std::String tagString {ls_std::XMLReader::_getNextTagString(_data, _index)};
  249. bool isValidTagString = !tagString.toString().empty();
  250. ls_std::XMLParseData singleParseData {};
  251. if(isValidTagString) {
  252. std::shared_ptr<ls_std::XMLNode> node = ls_std::XMLReader::_createNode(ls_std::XMLReader::_parseAttributes(tagString), ls_std::XMLReader::_parseTagName(tagString));
  253. singleParseData.level = this->currentLevel;
  254. singleParseData.node = node;
  255. this->parseData.push_back(singleParseData);
  256. if(!tagString.endsWith("/>")) {
  257. this->currentLevel += 1;
  258. this->_setMaxLevel();
  259. }
  260. }
  261. return !isValidTagString ? _index : _index + tagString.toString().size();
  262. }
  263. ls_std::byte_field ls_std::XMLReader::_parseTagName(const ls_std::byte_field &_data)
  264. {
  265. std::string::size_type position = _data.find(' ');
  266. if(position == std::string::npos) {
  267. position = _data.find('>');
  268. }
  269. return _data.substr(1, position - 1);
  270. }
  271. void ls_std::XMLReader::_reset()
  272. {
  273. this->currentLevel = 1;
  274. this->maxLevel = 1;
  275. this->mode = XML_PARSE_MODE_ANALYZE;
  276. this->parseData.clear();
  277. }
  278. void ls_std::XMLReader::_setMaxLevel()
  279. {
  280. if(this->currentLevel > this->maxLevel) {
  281. this->maxLevel = this->currentLevel;
  282. }
  283. }