XMLNode.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-24
  6. * Changed: 2020-09-26
  7. *
  8. * */
  9. #include "XMLNode.hpp"
  10. #include "../../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. std::list<std::shared_ptr<ls_std::XMLAttribute>> ls_std::XMLNode::getAttributes()
  115. {
  116. return this->attributes;
  117. }
  118. std::list<std::shared_ptr<ls_std::XMLNode>> ls_std::XMLNode::getChildren()
  119. {
  120. return this->children;
  121. }
  122. std::list<std::shared_ptr<ls_std::XMLNode>> ls_std::XMLNode::getChildren(const std::string &_name)
  123. {
  124. std::list<std::shared_ptr<ls_std::XMLNode>> childrenWithName {};
  125. for(const auto& child : this->children) {
  126. if(child->getName() == _name) {
  127. childrenWithName.push_back(child);
  128. }
  129. }
  130. return childrenWithName;
  131. }
  132. std::string ls_std::XMLNode::getName()
  133. {
  134. return this->name;
  135. }
  136. bool ls_std::XMLNode::hasAttribute(const std::string &_name)
  137. {
  138. return this->_hasAttribute(_name);
  139. }
  140. bool ls_std::XMLNode::hasChild(const std::string &_name)
  141. {
  142. return this->_hasChild(_name);
  143. }
  144. bool ls_std::XMLNode::hasChild(const std::shared_ptr<XMLNode> &_child)
  145. {
  146. return this->_hasChild(_child);
  147. }
  148. void ls_std::XMLNode::removeFirstAttribute()
  149. {
  150. if(!this->attributes.empty()) {
  151. this->attributes.pop_front();
  152. }
  153. }
  154. void ls_std::XMLNode::removeLastAttribute()
  155. {
  156. if(!this->attributes.empty()) {
  157. this->attributes.pop_back();
  158. }
  159. }
  160. void ls_std::XMLNode::removeFirstChild()
  161. {
  162. if(!this->children.empty()) {
  163. this->children.pop_front();
  164. }
  165. }
  166. void ls_std::XMLNode::removeLastChild()
  167. {
  168. if(!this->children.empty()) {
  169. this->children.pop_back();
  170. }
  171. }
  172. void ls_std::XMLNode::setName(std::string _name)
  173. {
  174. this->name = std::move(_name);
  175. }
  176. bool ls_std::XMLNode::_hasAttribute(const std::string &_name)
  177. {
  178. bool exists {};
  179. for(const auto& attribute : this->attributes) {
  180. if(attribute->getName() == _name) {
  181. exists = true;
  182. break;
  183. }
  184. }
  185. return exists;
  186. }
  187. bool ls_std::XMLNode::_hasChild(const std::shared_ptr<ls_std::XMLNode> &_child)
  188. {
  189. return ls_std::STLUtils<std::list<std::shared_ptr<ls_std::XMLNode>>, std::shared_ptr<ls_std::XMLNode>>::contains(this->children, _child);
  190. }
  191. bool ls_std::XMLNode::_hasChild(const std::string &_name)
  192. {
  193. bool exists {};
  194. for(const auto& attribute : this->children) {
  195. if(attribute->getName() == _name) {
  196. exists = true;
  197. break;
  198. }
  199. }
  200. return exists;
  201. }