XMLReader.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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-18
  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->_reset();
  27. return data;
  28. }
  29. std::shared_ptr<ls_std::XMLDocument> ls_std::XMLReader::getDocument()
  30. {
  31. return this->document;
  32. }
  33. void ls_std::XMLReader::setFile(const ls_std::File &_xmlFile)
  34. {
  35. XMLReader::_checkFileExistence(_xmlFile);
  36. this->xmlFile = _xmlFile;
  37. }
  38. std::pair<std::string, std::string> ls_std::XMLReader::_readAttribute_(const ls_std::byte_field &_data)
  39. {
  40. return ls_std::XMLReader::_parseAttribute(_data);
  41. }
  42. std::list<std::pair<std::string, std::string>> ls_std::XMLReader::_readAttributes_(ls_std::byte_field _data)
  43. {
  44. return ls_std::XMLReader::_parseAttributes(std::move(_data));
  45. }
  46. void ls_std::XMLReader::_analyze(const ls_std::byte_field &_data, std::string::size_type _index)
  47. {
  48. this->_isDeclaration(_data, _index);
  49. this->_isClosingTag(_data, _index);
  50. this->_isOpeningTag(_data, _index);
  51. }
  52. void ls_std::XMLReader::_checkDocumentExistence(const std::shared_ptr<ls_std::XMLDocument>& _document)
  53. {
  54. if(_document == nullptr) {
  55. throw ls_std::IllegalArgumentException {};
  56. }
  57. }
  58. void ls_std::XMLReader::_checkFileExistence(ls_std::File _xmlFile)
  59. {
  60. if(!_xmlFile.exists()) {
  61. throw ls_std::IllegalArgumentException {};
  62. }
  63. }
  64. std::shared_ptr<ls_std::XMLDeclaration> ls_std::XMLReader::_createDeclaration(const std::list<std::pair<std::string, std::string>>& _attributes)
  65. {
  66. std::shared_ptr<ls_std::XMLDeclaration> declaration = std::make_shared<ls_std::XMLDeclaration>("1.0");
  67. std::pair<std::string, std::string> attribute = ls_std::XMLReader::_findAttribute(_attributes, "version");
  68. if(!attribute.first.empty()) {
  69. declaration->setVersion(attribute.second);
  70. }
  71. attribute = ls_std::XMLReader::_findAttribute(_attributes, "encoding");
  72. if(!attribute.first.empty()) {
  73. declaration->setEncoding(attribute.second);
  74. }
  75. attribute = ls_std::XMLReader::_findAttribute(_attributes, "standalone");
  76. if(!attribute.first.empty()) {
  77. declaration->setStandalone(attribute.second);
  78. }
  79. return declaration;
  80. }
  81. std::shared_ptr<ls_std::XMLNode> ls_std::XMLReader::_createNode(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string& _name)
  82. {
  83. std::shared_ptr<ls_std::XMLNode> node = std::make_shared<ls_std::XMLNode>(_name);
  84. std::shared_ptr<ls_std::XMLAttribute> attribute {};
  85. for(const auto& parsedAttribute : _attributes) {
  86. attribute = std::make_shared<ls_std::XMLAttribute>(parsedAttribute.first);
  87. attribute->setValue(parsedAttribute.second);
  88. node->addAttributeToEnd(attribute);
  89. }
  90. return node;
  91. }
  92. std::pair<std::string, std::string> ls_std::XMLReader::_findAttribute(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string &_name)
  93. {
  94. std::pair<std::string, std::string> attribute {};
  95. for(const auto& currentAttribute : _attributes) {
  96. if(currentAttribute.first == _name) {
  97. attribute = currentAttribute;
  98. break;
  99. }
  100. }
  101. return attribute;
  102. }
  103. size_t ls_std::XMLReader::_findAttributeEndPosition(const ls_std::byte_field &_data)
  104. {
  105. std::string::size_type position = std::string::npos;
  106. std::string::size_type counter {};
  107. for(char letter : _data) {
  108. if(letter == '"') {
  109. counter++;
  110. }
  111. if(counter == 2) {
  112. break;
  113. }
  114. position++;
  115. }
  116. return position;
  117. }
  118. ls_std::byte_field ls_std::XMLReader::_getNextTagString(const ls_std::byte_field &_data, std::string::size_type _index)
  119. {
  120. ls_std::byte_field tag {};
  121. size_t closingCharacterPosition = _index + _data.substr(_index).find('>');
  122. if(closingCharacterPosition != std::string::npos) {
  123. tag = _data.substr(_index, (closingCharacterPosition - _index) + 1);
  124. }
  125. return tag;
  126. }
  127. void ls_std::XMLReader::_isClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
  128. {
  129. if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 2) == "</") {
  130. this->mode = XML_PARSE_MODE_CLOSING_TAG;
  131. }
  132. }
  133. void ls_std::XMLReader::_isDeclaration(const ls_std::byte_field& _data, std::string::size_type _index)
  134. {
  135. if(_data.substr(_index, 5) == "<?xml") {
  136. this->mode = XML_PARSE_MODE_DECLARATION;
  137. }
  138. }
  139. void ls_std::XMLReader::_isOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
  140. {
  141. if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 1) == "<") {
  142. this->mode = XML_PARSE_MODE_OPENING_TAG;
  143. }
  144. }
  145. void ls_std::XMLReader::_parse(const ls_std::byte_field &_data)
  146. {
  147. for(std::string::size_type index = 0 ; index < _data.size() ; index++) {
  148. switch(this->mode) {
  149. case XML_PARSE_MODE_ANALYZE:
  150. {
  151. this->_analyze(_data, index);
  152. } break;
  153. case XML_PARSE_MODE_DECLARATION:
  154. {
  155. --index;
  156. index = this->_parseDeclaration(_data, index);
  157. this->mode = XML_PARSE_MODE_ANALYZE;
  158. } break;
  159. case XML_PARSE_MODE_OPENING_TAG:
  160. {
  161. --index;
  162. index = ls_std::XMLReader::_parseOpeningTag(_data, index);
  163. this->mode = XML_PARSE_MODE_ANALYZE;
  164. } break;
  165. case XML_PARSE_MODE_CLOSING_TAG:
  166. {
  167. --index;
  168. index = ls_std::XMLReader::_parseClosingTag(_data, index);
  169. this->mode = XML_PARSE_MODE_ANALYZE;
  170. } break;
  171. }
  172. }
  173. }
  174. std::pair<std::string, std::string> ls_std::XMLReader::_parseAttribute(const ls_std::byte_field &_data)
  175. {
  176. std::pair<std::string, std::string> parsedAttribute {};
  177. parsedAttribute.first = _data.substr(0, _data.find('='));
  178. parsedAttribute.second = _data.substr(_data.find('"') + 1);
  179. parsedAttribute.second.pop_back();
  180. return parsedAttribute;
  181. }
  182. std::list<std::pair<std::string, std::string>> ls_std::XMLReader::_parseAttributes(ls_std::byte_field _data)
  183. {
  184. std::list<std::pair<std::string, std::string>> attributes {};
  185. size_t position = _data.find(' ');
  186. _data = position == std::string::npos ? "" : _data.substr(position);
  187. while(!_data.empty()) {
  188. do {
  189. position = _data.find(' ') + 1;
  190. }
  191. while(_data[position] == ' ');
  192. if(_data.size() <= 3 && ls_std::String {_data}.endsWith(">")) {
  193. break;
  194. }
  195. std::string attributeString = _data.substr(position, ls_std::XMLReader::_findAttributeEndPosition(_data) + 1);
  196. attributes.push_back(ls_std::XMLReader::_parseAttribute(attributeString));
  197. _data = _data.substr(position + attributeString.size());
  198. }
  199. return attributes;
  200. }
  201. size_t ls_std::XMLReader::_parseClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
  202. {
  203. std::string tagString = ls_std::XMLReader::_getNextTagString(_data, _index);
  204. this->currentLevel -= 1;
  205. return tagString.empty() ? _index : _index + tagString.size();
  206. }
  207. size_t ls_std::XMLReader::_parseDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
  208. {
  209. std::string tagString = ls_std::XMLReader::_getNextTagString(_data, _index);
  210. bool isValidTagString = !tagString.empty();
  211. if(isValidTagString) {
  212. std::shared_ptr<ls_std::XMLDeclaration> declaration = this->_createDeclaration(ls_std::XMLReader::_parseAttributes(tagString));
  213. this->document->setDeclaration(declaration);
  214. }
  215. return !isValidTagString ? _index : _index + tagString.size();
  216. }
  217. size_t ls_std::XMLReader::_parseOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
  218. {
  219. ls_std::String tagString {ls_std::XMLReader::_getNextTagString(_data, _index)};
  220. bool isValidTagString = !tagString.toString().empty();
  221. ls_std::XMLParseData singleParseData {};
  222. if(isValidTagString) {
  223. std::shared_ptr<ls_std::XMLNode> node = ls_std::XMLReader::_createNode(ls_std::XMLReader::_parseAttributes(tagString), ls_std::XMLReader::_parseTagName(tagString));
  224. singleParseData.level = this->currentLevel;
  225. singleParseData.node = node;
  226. this->parseData.push_back(singleParseData);
  227. if(!tagString.endsWith("/>")) {
  228. this->currentLevel += 1;
  229. this->_setMaxLevel();
  230. }
  231. }
  232. return !isValidTagString ? _index : _index + tagString.toString().size();
  233. }
  234. ls_std::byte_field ls_std::XMLReader::_parseTagName(const ls_std::byte_field &_data)
  235. {
  236. std::string::size_type position = _data.find(' ');
  237. if(position == std::string::npos) {
  238. position = _data.find('>');
  239. }
  240. return _data.substr(1, position - 1);
  241. }
  242. void ls_std::XMLReader::_reset()
  243. {
  244. this->currentLevel = 1;
  245. this->maxLevel = 1;
  246. this->mode = XML_PARSE_MODE_ANALYZE;
  247. this->parseData.clear();
  248. }
  249. void ls_std::XMLReader::_setMaxLevel()
  250. {
  251. if(this->currentLevel > this->maxLevel) {
  252. this->maxLevel = this->currentLevel;
  253. }
  254. }