XMLNode.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-24
  6. * Changed: 2020-11-06
  7. *
  8. * */
  9. #include "../../../../include/ls_std/io/xml/XMLNode.hpp"
  10. #include "../../../../include/ls_std/utils/STLUtils.hpp"
  11. ls_std::XMLNode::XMLNode(std::string _name) : Class("XMLNode"),
  12. name(std::move(_name))
  13. {}
  14. bool ls_std::XMLNode::addAttributeAfter(const std::shared_ptr<ls_std::XMLAttribute> &_attribute, const std::string &_name) {
  15. bool added {};
  16. auto iterator = this->attributes.begin();
  17. if(!this->_hasAttribute(_attribute->getName())) {
  18. while(iterator != this->attributes.end()) {
  19. if((*iterator)->getName() == _name) {
  20. iterator++;
  21. this->attributes.insert(iterator, _attribute);
  22. added = true;
  23. break;
  24. }
  25. iterator++;
  26. }
  27. }
  28. return added;
  29. }
  30. bool ls_std::XMLNode::addAttributeBefore(const std::shared_ptr<ls_std::XMLAttribute> &_attribute, const std::string &_name) {
  31. bool added {};
  32. auto iterator = this->attributes.begin();
  33. if(!this->_hasAttribute(_attribute->getName())) {
  34. while(iterator != this->attributes.end()) {
  35. if((*iterator)->getName() == _name) {
  36. this->attributes.insert(iterator, _attribute);
  37. added = true;
  38. break;
  39. }
  40. iterator++;
  41. }
  42. }
  43. return added;
  44. }
  45. bool ls_std::XMLNode::addAttributeToBeginning(const std::shared_ptr<ls_std::XMLAttribute> &_attribute)
  46. {
  47. bool added {};
  48. if(_attribute != nullptr && !_hasAttribute(_attribute->getName())) {
  49. this->attributes.push_front(_attribute);
  50. added = true;
  51. }
  52. return added;
  53. }
  54. bool ls_std::XMLNode::addAttributeToEnd(const std::shared_ptr<ls_std::XMLAttribute> &_attribute)
  55. {
  56. bool added {};
  57. if(_attribute != nullptr && !_hasAttribute(_attribute->getName())) {
  58. this->attributes.push_back(_attribute);
  59. added = true;
  60. }
  61. return added;
  62. }
  63. bool ls_std::XMLNode::addChildAfter(const std::shared_ptr<ls_std::XMLNode>& _child, const std::shared_ptr<ls_std::XMLNode>& _search)
  64. {
  65. bool added {};
  66. auto iterator = this->children.begin();
  67. if(_child != nullptr && !this->_hasChild(_child)) {
  68. while(iterator != this->children.end()) {
  69. if(*iterator == _search) {
  70. iterator++;
  71. this->children.insert(iterator, _child);
  72. added = true;
  73. break;
  74. }
  75. iterator++;
  76. }
  77. }
  78. return added;
  79. }
  80. bool ls_std::XMLNode::addChildBefore(const std::shared_ptr<ls_std::XMLNode>& _child, const std::shared_ptr<ls_std::XMLNode>& _search)
  81. {
  82. bool added {};
  83. auto iterator = this->children.begin();
  84. if(_child != nullptr && !this->_hasChild(_child)) {
  85. while(iterator != this->children.end()) {
  86. if(*iterator == _search) {
  87. this->children.insert(iterator, _child);
  88. added = true;
  89. break;
  90. }
  91. iterator++;
  92. }
  93. }
  94. return added;
  95. }
  96. bool ls_std::XMLNode::addChildToBeginning(const std::shared_ptr<XMLNode> &_child)
  97. {
  98. bool added {};
  99. if(_child != nullptr && !this->_hasChild(_child)) {
  100. this->children.push_front(_child);
  101. added = true;
  102. }
  103. return added;
  104. }
  105. bool ls_std::XMLNode::addChildToEnd(const std::shared_ptr<XMLNode>& _child)
  106. {
  107. bool added {};
  108. if(_child != nullptr && !this->_hasChild(_child)) {
  109. this->children.push_back(_child);
  110. added = true;
  111. }
  112. return added;
  113. }
  114. void ls_std::XMLNode::clearValue()
  115. {
  116. this->value.clear();
  117. }
  118. std::list<std::shared_ptr<ls_std::XMLAttribute>> ls_std::XMLNode::getAttributes()
  119. {
  120. return this->attributes;
  121. }
  122. std::list<std::shared_ptr<ls_std::XMLNode>> ls_std::XMLNode::getChildren()
  123. {
  124. return this->children;
  125. }
  126. std::list<std::shared_ptr<ls_std::XMLNode>> ls_std::XMLNode::getChildren(const std::string &_name)
  127. {
  128. std::list<std::shared_ptr<ls_std::XMLNode>> childrenWithName {};
  129. for(const auto& child : this->children) {
  130. if(child->getName() == _name) {
  131. childrenWithName.push_back(child);
  132. }
  133. }
  134. return childrenWithName;
  135. }
  136. std::string ls_std::XMLNode::getName()
  137. {
  138. return this->name;
  139. }
  140. std::string ls_std::XMLNode::getValue()
  141. {
  142. return this->value;
  143. }
  144. bool ls_std::XMLNode::hasAttribute(const std::string &_name)
  145. {
  146. return this->_hasAttribute(_name);
  147. }
  148. bool ls_std::XMLNode::hasChild(const std::string &_name)
  149. {
  150. return this->_hasChild(_name);
  151. }
  152. bool ls_std::XMLNode::hasChild(const std::shared_ptr<XMLNode> &_child)
  153. {
  154. return this->_hasChild(_child);
  155. }
  156. void ls_std::XMLNode::removeFirstAttribute()
  157. {
  158. if(!this->attributes.empty()) {
  159. this->attributes.pop_front();
  160. }
  161. }
  162. void ls_std::XMLNode::removeLastAttribute()
  163. {
  164. if(!this->attributes.empty()) {
  165. this->attributes.pop_back();
  166. }
  167. }
  168. void ls_std::XMLNode::removeFirstChild()
  169. {
  170. if(!this->children.empty()) {
  171. this->children.pop_front();
  172. }
  173. }
  174. void ls_std::XMLNode::removeLastChild()
  175. {
  176. if(!this->children.empty()) {
  177. this->children.pop_back();
  178. }
  179. }
  180. void ls_std::XMLNode::setName(std::string _name)
  181. {
  182. this->name = std::move(_name);
  183. }
  184. void ls_std::XMLNode::setValue(std::string _value)
  185. {
  186. this->value = std::move(_value);
  187. }
  188. std::string ls_std::XMLNode::toXML()
  189. {
  190. return this->_toXML_(0);
  191. }
  192. std::string ls_std::XMLNode::_toXML_(uint8_t _tabSize)
  193. {
  194. std::string xmlStream {};
  195. xmlStream += ls_std::XMLNode::_getTab(_tabSize);
  196. xmlStream += this->_toXMLOpenTag();
  197. xmlStream += this->_toXMLAttributes();
  198. xmlStream += this->_toXMLOpenTagClose();
  199. xmlStream += this->_toXMLValue();
  200. xmlStream += this->_toXMLChildren(_tabSize + TAB_SIZE);
  201. xmlStream += this->value.empty() ? ls_std::XMLNode::_getTab(_tabSize) : "";
  202. xmlStream += this->_toXMLCloseTag() + "\n";
  203. return xmlStream;
  204. }
  205. std::string ls_std::XMLNode::_getTab(uint8_t _tabSize)
  206. {
  207. std::string tab {};
  208. for(uint8_t index = 0 ; index < _tabSize ; index++) {
  209. tab += " ";
  210. }
  211. return tab;
  212. }
  213. bool ls_std::XMLNode::_hasAttribute(const std::string &_name)
  214. {
  215. bool exists {};
  216. for(const auto& attribute : this->attributes) {
  217. if(attribute->getName() == _name) {
  218. exists = true;
  219. break;
  220. }
  221. }
  222. return exists;
  223. }
  224. bool ls_std::XMLNode::_hasChild(const std::shared_ptr<ls_std::XMLNode> &_child)
  225. {
  226. return ls_std::STLUtils::contains(this->children, _child);
  227. }
  228. bool ls_std::XMLNode::_hasChild(const std::string &_name)
  229. {
  230. bool exists {};
  231. for(const auto& attribute : this->children) {
  232. if(attribute->getName() == _name) {
  233. exists = true;
  234. break;
  235. }
  236. }
  237. return exists;
  238. }
  239. std::string ls_std::XMLNode::_toXMLAttributes()
  240. {
  241. std::string stream {};
  242. for(const auto& _attribute : this->attributes) {
  243. stream += " " + _attribute->toXML();
  244. }
  245. return stream;
  246. }
  247. std::string ls_std::XMLNode::_toXMLChildren(uint8_t _tabSize)
  248. {
  249. std::string stream {};
  250. if(this->value.empty()) {
  251. for(const auto& _child : this->children) {
  252. stream += _child->_toXML_(_tabSize);
  253. }
  254. }
  255. return stream;
  256. }
  257. std::string ls_std::XMLNode::_toXMLCloseTag()
  258. {
  259. std::string stream {};
  260. if(!this->children.empty() || !this->value.empty()) {
  261. stream = "</" + this->name + ">";
  262. }
  263. return stream;
  264. }
  265. std::string ls_std::XMLNode::_toXMLOpenTag()
  266. {
  267. return "<" + this->name;
  268. }
  269. std::string ls_std::XMLNode::_toXMLOpenTagClose()
  270. {
  271. std::string stream {};
  272. if(this->children.empty() && this->value.empty()) {
  273. stream = "/>";
  274. } else {
  275. stream = ">";
  276. }
  277. return stream;
  278. }
  279. std::string ls_std::XMLNode::_toXMLValue()
  280. {
  281. return this->value.empty() ? "\n" : this->value;
  282. }