XMLReader.cpp 8.4 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::_readAttribute(_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::_readAttributes(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. void ls_std::XMLReader::_isClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
  118. {
  119. if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 2) == "</") {
  120. this->mode = XML_PARSE_MODE_CLOSING_TAG;
  121. }
  122. }
  123. void ls_std::XMLReader::_isDeclaration(const ls_std::byte_field& _data, std::string::size_type _index)
  124. {
  125. if(_data.substr(_index, 5) == "<?xml") {
  126. this->mode = XML_PARSE_MODE_DECLARATION;
  127. }
  128. }
  129. void ls_std::XMLReader::_isOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
  130. {
  131. if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 1) == "<") {
  132. this->mode = XML_PARSE_MODE_OPENING_TAG;
  133. }
  134. }
  135. void ls_std::XMLReader::_read(const ls_std::byte_field &_data)
  136. {
  137. for(std::string::size_type index = 0 ; index < _data.size() ; index++) {
  138. switch(this->mode) {
  139. case XML_PARSE_MODE_ANALYZE:
  140. {
  141. this->_analyze(_data, index);
  142. } break;
  143. case XML_PARSE_MODE_DECLARATION:
  144. {
  145. --index;
  146. index = this->_readDeclaration(_data, index);
  147. this->mode = XML_PARSE_MODE_ANALYZE;
  148. } break;
  149. case XML_PARSE_MODE_OPENING_TAG:
  150. {
  151. --index;
  152. index = ls_std::XMLReader::_readOpeningTag(_data, index);
  153. this->mode = XML_PARSE_MODE_ANALYZE;
  154. } break;
  155. case XML_PARSE_MODE_CLOSING_TAG:
  156. {
  157. --index;
  158. index = ls_std::XMLReader::_readClosingTag(_data, index);
  159. this->mode = XML_PARSE_MODE_ANALYZE;
  160. } break;
  161. }
  162. }
  163. }
  164. std::pair<std::string, std::string> ls_std::XMLReader::_readAttribute(const ls_std::byte_field &_data)
  165. {
  166. std::pair<std::string, std::string> parsedAttribute {};
  167. parsedAttribute.first = _data.substr(0, _data.find('='));
  168. parsedAttribute.second = _data.substr(_data.find('"') + 1);
  169. parsedAttribute.second.pop_back();
  170. return parsedAttribute;
  171. }
  172. std::list<std::pair<std::string, std::string>> ls_std::XMLReader::_readAttributes(ls_std::byte_field _data)
  173. {
  174. std::list<std::pair<std::string, std::string>> attributes {};
  175. size_t position = _data.find(' ');
  176. _data = position == std::string::npos ? "" : _data.substr(position);
  177. while(!_data.empty()) {
  178. do {
  179. position = _data.find(' ') + 1;
  180. }
  181. while(_data[position] == ' ');
  182. if(_data.size() <= 3 && ls_std::String {_data}.endsWith(">")) {
  183. break;
  184. }
  185. std::string attributeString = _data.substr(position, ls_std::XMLReader::_findAttributeEndPosition(_data) + 1);
  186. attributes.push_back(ls_std::XMLReader::_readAttribute(attributeString));
  187. _data = _data.substr(position + attributeString.size());
  188. }
  189. return attributes;
  190. }
  191. size_t ls_std::XMLReader::_readClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
  192. {
  193. std::string tagString = ls_std::XMLReader::_readTag(_data, _index);
  194. this->currentLevel -= 1;
  195. return tagString.empty() ? _index : _index + tagString.size();
  196. }
  197. size_t ls_std::XMLReader::_readDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
  198. {
  199. std::string tagString = ls_std::XMLReader::_readTag(_data, _index);
  200. bool isValidTagString = !tagString.empty();
  201. if(isValidTagString) {
  202. std::shared_ptr<ls_std::XMLDeclaration> declaration = this->_createDeclaration(ls_std::XMLReader::_readAttributes(tagString));
  203. this->document->setDeclaration(declaration);
  204. }
  205. return !isValidTagString ? _index : _index + tagString.size();
  206. }
  207. size_t ls_std::XMLReader::_readOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
  208. {
  209. ls_std::String tagString {ls_std::XMLReader::_readTag(_data, _index)};
  210. bool isValidTagString = !tagString.toString().empty();
  211. ls_std::XMLParseData singleParseData {};
  212. if(isValidTagString) {
  213. std::shared_ptr<ls_std::XMLNode> node = ls_std::XMLReader::_createNode(ls_std::XMLReader::_readAttributes(tagString));
  214. if(!tagString.endsWith("/>")) {
  215. this->currentLevel += 1;
  216. }
  217. singleParseData.level = this->currentLevel;
  218. singleParseData.node = node;
  219. this->parseData.push_back(singleParseData);
  220. }
  221. return !isValidTagString ? _index : _index + tagString.toString().size();
  222. }
  223. ls_std::byte_field ls_std::XMLReader::_readTag(const ls_std::byte_field &_data, std::string::size_type _index)
  224. {
  225. ls_std::byte_field tag {};
  226. size_t closingCharacterPosition = _index + _data.substr(_index).find('>');
  227. if(closingCharacterPosition != std::string::npos) {
  228. tag = _data.substr(_index, (closingCharacterPosition - _index) + 1);
  229. }
  230. return tag;
  231. }
  232. void ls_std::XMLReader::_reset()
  233. {
  234. this->currentLevel = 0;
  235. this->mode = XML_PARSE_MODE_ANALYZE;
  236. this->parseData.clear();
  237. }