XMLReader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-10-10
  6. * Changed: 2020-11-06
  7. *
  8. * */
  9. #include "../../../../include/ls_std/io/xml/XMLReader.hpp"
  10. #include "../../../../include/ls_std/exception/IllegalArgumentException.hpp"
  11. #include "../../../../include/ls_std/io/FileReader.hpp"
  12. #include "../../../../include/ls_std/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. this->_isValue(_data, _index);
  53. }
  54. void ls_std::XMLReader::_checkDocumentExistence(const std::shared_ptr<ls_std::XMLDocument>& _document)
  55. {
  56. if(_document == nullptr) {
  57. throw ls_std::IllegalArgumentException {};
  58. }
  59. }
  60. void ls_std::XMLReader::_checkFileExistence(ls_std::File _xmlFile)
  61. {
  62. if(!_xmlFile.exists()) {
  63. throw ls_std::IllegalArgumentException {};
  64. }
  65. }
  66. std::shared_ptr<ls_std::XMLDeclaration> ls_std::XMLReader::_createDeclaration(const std::list<std::pair<std::string, std::string>>& _attributes)
  67. {
  68. std::shared_ptr<ls_std::XMLDeclaration> declaration = std::make_shared<ls_std::XMLDeclaration>("1.0");
  69. std::pair<std::string, std::string> attribute = ls_std::XMLReader::_findAttribute(_attributes, "version");
  70. if(!attribute.first.empty()) {
  71. declaration->setVersion(attribute.second);
  72. }
  73. attribute = ls_std::XMLReader::_findAttribute(_attributes, "encoding");
  74. if(!attribute.first.empty()) {
  75. declaration->setEncoding(attribute.second);
  76. }
  77. attribute = ls_std::XMLReader::_findAttribute(_attributes, "standalone");
  78. if(!attribute.first.empty()) {
  79. declaration->setStandalone(attribute.second);
  80. }
  81. return declaration;
  82. }
  83. std::shared_ptr<ls_std::XMLNode> ls_std::XMLReader::_createNode(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string& _name)
  84. {
  85. std::shared_ptr<ls_std::XMLNode> node = std::make_shared<ls_std::XMLNode>(_name);
  86. std::shared_ptr<ls_std::XMLAttribute> attribute {};
  87. for(const auto& parsedAttribute : _attributes) {
  88. attribute = std::make_shared<ls_std::XMLAttribute>(parsedAttribute.first);
  89. attribute->setValue(parsedAttribute.second);
  90. node->addAttributeToEnd(attribute);
  91. }
  92. return node;
  93. }
  94. std::pair<std::string, std::string> ls_std::XMLReader::_findAttribute(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string &_name)
  95. {
  96. std::pair<std::string, std::string> attribute {};
  97. for(const auto& currentAttribute : _attributes) {
  98. if(currentAttribute.first == _name) {
  99. attribute = currentAttribute;
  100. break;
  101. }
  102. }
  103. return attribute;
  104. }
  105. size_t ls_std::XMLReader::_findAttributeEndPosition(const ls_std::byte_field &_data)
  106. {
  107. std::string::size_type position = std::string::npos;
  108. std::string::size_type counter {};
  109. for(char letter : _data) {
  110. if(letter == '"') {
  111. counter++;
  112. }
  113. if(counter == 2) {
  114. break;
  115. }
  116. position++;
  117. }
  118. return position;
  119. }
  120. ls_std::byte_field ls_std::XMLReader::_getNextTagString(const ls_std::byte_field &_data, std::string::size_type _index)
  121. {
  122. ls_std::byte_field tag {};
  123. size_t closingCharacterPosition = _index + _data.substr(_index).find('>');
  124. if(closingCharacterPosition != std::string::npos) {
  125. tag = _data.substr(_index, (closingCharacterPosition - _index) + 1);
  126. }
  127. return tag;
  128. }
  129. void ls_std::XMLReader::_isClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
  130. {
  131. if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 2) == "</") {
  132. this->mode = XML_PARSE_MODE_CLOSING_TAG;
  133. }
  134. }
  135. void ls_std::XMLReader::_isDeclaration(const ls_std::byte_field& _data, std::string::size_type _index)
  136. {
  137. if(_data.substr(_index, 5) == "<?xml") {
  138. this->mode = XML_PARSE_MODE_DECLARATION;
  139. }
  140. }
  141. void ls_std::XMLReader::_isOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
  142. {
  143. if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 1) == "<") {
  144. this->mode = XML_PARSE_MODE_OPENING_TAG;
  145. }
  146. }
  147. void ls_std::XMLReader::_isValue(const ls_std::byte_field &_data, std::string::size_type _index)
  148. {
  149. if(this->mode == XML_PARSE_MODE_ANALYZE) {
  150. std::string::size_type end = _data.substr(_index).find('<');
  151. bool isValue = _data[_index - 1] == '>' && end != std::string::npos && end > 0;
  152. if(isValue) {
  153. ls_std::String value {_data.substr(_index, end)};
  154. if(!value.contains("\n") && !value.contains("\r\n") ) {
  155. this->mode = XML_PARSE_MODE_VALUE;
  156. }
  157. }
  158. }
  159. }
  160. void ls_std::XMLReader::_mergeNodes()
  161. {
  162. while(this->maxLevel > 1) {
  163. this->_mergeNodesOnCurrentLevel();
  164. this->maxLevel -= 1;
  165. }
  166. this->document->setRootElement(this->parseData.front().node);
  167. }
  168. void ls_std::XMLReader::_mergeChildrenToParentNode(const std::shared_ptr<ls_std::XMLNode>& _parent, std::list<ls_std::XMLParseData>::iterator& _iterator, uint8_t _parentLevel)
  169. {
  170. do {
  171. _iterator++;
  172. if(_iterator == this->parseData.end()) {
  173. break;
  174. }
  175. else {
  176. if(_iterator->level == this->maxLevel) {
  177. _parent->addChildToEnd(_iterator->node);
  178. }
  179. }
  180. }
  181. while(_iterator->level > _parentLevel);
  182. }
  183. void ls_std::XMLReader::_mergeNodesOnCurrentLevel() {
  184. auto iterator = this->parseData.begin();
  185. uint8_t parentLevel = this->maxLevel - 1;
  186. while(iterator != this->parseData.end()) {
  187. if(iterator->level == parentLevel) {
  188. this->_mergeChildrenToParentNode(iterator->node, iterator, parentLevel);
  189. }
  190. else {
  191. iterator++;
  192. }
  193. }
  194. }
  195. void ls_std::XMLReader::_parse(const ls_std::byte_field &_data)
  196. {
  197. for(std::string::size_type index = 0 ; index < _data.size() ; index++) {
  198. switch(this->mode) {
  199. case XML_PARSE_MODE_ANALYZE:
  200. {
  201. this->_analyze(_data, index);
  202. } break;
  203. case XML_PARSE_MODE_DECLARATION:
  204. {
  205. --index;
  206. index = this->_parseDeclaration(_data, index);
  207. this->mode = XML_PARSE_MODE_ANALYZE;
  208. } break;
  209. case XML_PARSE_MODE_OPENING_TAG:
  210. {
  211. --index;
  212. index = ls_std::XMLReader::_parseOpeningTag(_data, index);
  213. this->mode = XML_PARSE_MODE_ANALYZE;
  214. } break;
  215. case XML_PARSE_MODE_VALUE:
  216. {
  217. --index;
  218. index = ls_std::XMLReader::_parseValue(_data, index);
  219. this->mode = XML_PARSE_MODE_ANALYZE;
  220. } break;
  221. case XML_PARSE_MODE_CLOSING_TAG:
  222. {
  223. --index;
  224. index = ls_std::XMLReader::_parseClosingTag(_data, index);
  225. this->mode = XML_PARSE_MODE_ANALYZE;
  226. } break;
  227. }
  228. }
  229. }
  230. std::pair<std::string, std::string> ls_std::XMLReader::_parseAttribute(const ls_std::byte_field &_data)
  231. {
  232. std::pair<std::string, std::string> parsedAttribute {};
  233. parsedAttribute.first = _data.substr(0, _data.find('='));
  234. parsedAttribute.second = _data.substr(_data.find('"') + 1);
  235. parsedAttribute.second.pop_back();
  236. return parsedAttribute;
  237. }
  238. std::list<std::pair<std::string, std::string>> ls_std::XMLReader::_parseAttributes(ls_std::byte_field _data)
  239. {
  240. std::list<std::pair<std::string, std::string>> attributes {};
  241. size_t position = _data.find(' ');
  242. _data = position == std::string::npos ? "" : _data.substr(position);
  243. while(!_data.empty()) {
  244. do {
  245. position = _data.find(' ') + 1;
  246. }
  247. while(_data[position] == ' ');
  248. if(_data.size() <= 3 && ls_std::String {_data}.endsWith(">")) {
  249. break;
  250. }
  251. std::string attributeString = _data.substr(position, ls_std::XMLReader::_findAttributeEndPosition(_data) + 1);
  252. attributes.push_back(ls_std::XMLReader::_parseAttribute(attributeString));
  253. _data = _data.substr(position + attributeString.size());
  254. }
  255. return attributes;
  256. }
  257. size_t ls_std::XMLReader::_parseClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
  258. {
  259. std::string tagString = ls_std::XMLReader::_getNextTagString(_data, _index);
  260. this->currentLevel -= 1;
  261. return tagString.empty() ? _index : _index + (tagString.size() - 1);
  262. }
  263. size_t ls_std::XMLReader::_parseDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
  264. {
  265. std::string tagString = ls_std::XMLReader::_getNextTagString(_data, _index);
  266. bool isValidTagString = !tagString.empty();
  267. if(isValidTagString) {
  268. std::shared_ptr<ls_std::XMLDeclaration> declaration = this->_createDeclaration(ls_std::XMLReader::_parseAttributes(tagString));
  269. this->document->setDeclaration(declaration);
  270. }
  271. return !isValidTagString ? _index : _index + (tagString.size() - 1);
  272. }
  273. size_t ls_std::XMLReader::_parseOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
  274. {
  275. ls_std::String tagString {ls_std::XMLReader::_getNextTagString(_data, _index)};
  276. bool isValidTagString = !tagString.toString().empty();
  277. ls_std::XMLParseData singleParseData {};
  278. if(isValidTagString) {
  279. std::shared_ptr<ls_std::XMLNode> node = ls_std::XMLReader::_createNode(ls_std::XMLReader::_parseAttributes(tagString), ls_std::XMLReader::_parseTagName(tagString));
  280. singleParseData.level = this->currentLevel;
  281. singleParseData.node = node;
  282. this->parseData.push_back(singleParseData);
  283. if(!tagString.endsWith("/>")) {
  284. this->currentLevel += 1;
  285. this->_setMaxLevel();
  286. }
  287. }
  288. return !isValidTagString ? _index : _index + (tagString.toString().size() - 1);
  289. }
  290. ls_std::byte_field ls_std::XMLReader::_parseTagName(const ls_std::byte_field &_data)
  291. {
  292. std::string::size_type position = _data.find(' ');
  293. if(position == std::string::npos) {
  294. position = _data.find('>');
  295. }
  296. return _data.substr(1, position - 1);
  297. }
  298. size_t ls_std::XMLReader::_parseValue(const ls_std::byte_field &_data, std::string::size_type _index)
  299. {
  300. ls_std::byte_field value = _data.substr(_index, _data.substr(_index).find('<'));
  301. this->parseData.back().node->setValue(value);
  302. return _index + (value.size() - 1);
  303. }
  304. void ls_std::XMLReader::_reset()
  305. {
  306. this->currentLevel = 1;
  307. this->maxLevel = 1;
  308. this->mode = XML_PARSE_MODE_ANALYZE;
  309. this->parseData.clear();
  310. }
  311. void ls_std::XMLReader::_setMaxLevel()
  312. {
  313. if(this->currentLevel > this->maxLevel) {
  314. this->maxLevel = this->currentLevel;
  315. }
  316. }