XMLReader.cpp 8.5 KB

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