XMLNode.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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-24
  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::addAttributeToBeginning(const std::shared_ptr<ls_std::XMLAttribute> &_attribute)
  15. {
  16. bool added {};
  17. if(_attribute != nullptr && !_hasAttribute(_attribute->getName())) {
  18. this->attributes.push_front(_attribute);
  19. added = true;
  20. }
  21. return added;
  22. }
  23. bool ls_std::XMLNode::addAttributeToEnd(const std::shared_ptr<ls_std::XMLAttribute> &_attribute)
  24. {
  25. bool added {};
  26. if(_attribute != nullptr && !_hasAttribute(_attribute->getName())) {
  27. this->attributes.push_back(_attribute);
  28. added = true;
  29. }
  30. return added;
  31. }
  32. bool ls_std::XMLNode::addChildAfter(const std::shared_ptr<ls_std::XMLNode>& _child, const std::shared_ptr<ls_std::XMLNode>& _search)
  33. {
  34. bool added {};
  35. auto iterator = this->children.begin();
  36. if(_child != nullptr && !this->_hasChild(_child)) {
  37. while(iterator != this->children.end()) {
  38. if(*iterator == _search) {
  39. iterator++;
  40. this->children.insert(iterator, _child);
  41. added = true;
  42. break;
  43. }
  44. iterator++;
  45. }
  46. }
  47. return added;
  48. }
  49. bool ls_std::XMLNode::addChildBefore(const std::shared_ptr<ls_std::XMLNode>& _child, const std::shared_ptr<ls_std::XMLNode>& _search)
  50. {
  51. bool added {};
  52. auto iterator = this->children.begin();
  53. if(_child != nullptr && !this->_hasChild(_child)) {
  54. while(iterator != this->children.end()) {
  55. if(*iterator == _search) {
  56. this->children.insert(iterator, _child);
  57. added = true;
  58. break;
  59. }
  60. iterator++;
  61. }
  62. }
  63. return added;
  64. }
  65. bool ls_std::XMLNode::addChildToBeginning(const std::shared_ptr<XMLNode> &_child)
  66. {
  67. bool added {};
  68. if(_child != nullptr && !this->_hasChild(_child)) {
  69. this->children.push_front(_child);
  70. added = true;
  71. }
  72. return added;
  73. }
  74. bool ls_std::XMLNode::addChildToEnd(const std::shared_ptr<XMLNode>& _child)
  75. {
  76. bool added {};
  77. if(_child != nullptr && !this->_hasChild(_child)) {
  78. this->children.push_back(_child);
  79. added = true;
  80. }
  81. return added;
  82. }
  83. std::list<std::shared_ptr<ls_std::XMLAttribute>> ls_std::XMLNode::getAttributes()
  84. {
  85. return this->attributes;
  86. }
  87. std::list<std::shared_ptr<ls_std::XMLNode>> ls_std::XMLNode::getChildren()
  88. {
  89. return this->children;
  90. }
  91. std::list<std::shared_ptr<ls_std::XMLNode>> ls_std::XMLNode::getChildren(const std::string &_name)
  92. {
  93. std::list<std::shared_ptr<ls_std::XMLNode>> childrenWithName {};
  94. for(const auto& child : this->children) {
  95. if(child->getName() == _name) {
  96. childrenWithName.push_back(child);
  97. }
  98. }
  99. return childrenWithName;
  100. }
  101. std::string ls_std::XMLNode::getName()
  102. {
  103. return this->name;
  104. }
  105. bool ls_std::XMLNode::hasAttribute(const std::string &_name)
  106. {
  107. return this->_hasAttribute(_name);
  108. }
  109. bool ls_std::XMLNode::hasChild(const std::string &_name)
  110. {
  111. return this->_hasChild(_name);
  112. }
  113. bool ls_std::XMLNode::hasChild(const std::shared_ptr<XMLNode> &_child)
  114. {
  115. return this->_hasChild(_child);
  116. }
  117. void ls_std::XMLNode::removeFirstAttribute()
  118. {
  119. this->attributes.pop_front();
  120. }
  121. void ls_std::XMLNode::removeLastAttribute()
  122. {
  123. this->attributes.pop_back();
  124. }
  125. void ls_std::XMLNode::removeFirstChild()
  126. {
  127. this->children.pop_front();
  128. }
  129. void ls_std::XMLNode::removeLastChild()
  130. {
  131. this->children.pop_back();
  132. }
  133. void ls_std::XMLNode::setName(std::string _name)
  134. {
  135. this->name = std::move(_name);
  136. }
  137. bool ls_std::XMLNode::_hasAttribute(const std::string &_name)
  138. {
  139. bool exists {};
  140. for(const auto& attribute : this->attributes) {
  141. if(attribute->getName() == _name) {
  142. exists = true;
  143. break;
  144. }
  145. }
  146. return exists;
  147. }
  148. bool ls_std::XMLNode::_hasChild(const std::shared_ptr<ls_std::XMLNode> &_child)
  149. {
  150. return ls_std::STLUtils<std::list<std::shared_ptr<ls_std::XMLNode>>, std::shared_ptr<ls_std::XMLNode>>::contains(this->children, _child);
  151. }
  152. bool ls_std::XMLNode::_hasChild(const std::string &_name)
  153. {
  154. bool exists {};
  155. for(const auto& attribute : this->children) {
  156. if(attribute->getName() == _name) {
  157. exists = true;
  158. break;
  159. }
  160. }
  161. return exists;
  162. }