XmlNode.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2020-09-24
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <algorithm>
  11. #include <iterator>
  12. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  13. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  14. #include <ls-std/io/xml/XmlNode.hpp>
  15. #include <numeric>
  16. using ls::standard::core::Class;
  17. using ls::standard::core::EmptyStringArgumentEvaluator;
  18. using ls::standard::core::NullPointerArgumentEvaluator;
  19. using ls::standard::io::XmlAttribute;
  20. using ls::standard::io::XmlNode;
  21. using std::accumulate;
  22. using std::any_of;
  23. using std::back_inserter;
  24. using std::copy_if;
  25. using std::find;
  26. using std::list;
  27. using std::move;
  28. using std::shared_ptr;
  29. using std::string;
  30. XmlNode::XmlNode(string _name) : Class("XmlNode"), name(::move(_name))
  31. {}
  32. XmlNode::~XmlNode() noexcept = default;
  33. bool XmlNode::addAttributeAfter(const shared_ptr<XmlAttribute> &_attribute, const string &_name)
  34. {
  35. bool added{};
  36. auto iterator = this->attributes.begin();
  37. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  38. EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
  39. if (!this->_hasAttribute(_attribute->getName()))
  40. {
  41. while (iterator != this->attributes.end())
  42. {
  43. if ((*iterator)->getName() == _name)
  44. {
  45. ++iterator;
  46. this->attributes.insert(iterator, _attribute);
  47. added = true;
  48. break;
  49. }
  50. ++iterator;
  51. }
  52. }
  53. return added;
  54. }
  55. bool XmlNode::addAttributeBefore(const shared_ptr<XmlAttribute> &_attribute, const string &_name)
  56. {
  57. bool added{};
  58. auto iterator = this->attributes.begin();
  59. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  60. EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
  61. if (!this->_hasAttribute(_attribute->getName()))
  62. {
  63. while (iterator != this->attributes.end())
  64. {
  65. if ((*iterator)->getName() == _name)
  66. {
  67. this->attributes.insert(iterator, _attribute);
  68. added = true;
  69. break;
  70. }
  71. ++iterator;
  72. }
  73. }
  74. return added;
  75. }
  76. bool XmlNode::addAttributeToBeginning(const shared_ptr<XmlAttribute> &_attribute)
  77. {
  78. bool added{};
  79. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  80. if (!_hasAttribute(_attribute->getName()))
  81. {
  82. this->attributes.push_front(_attribute);
  83. added = true;
  84. }
  85. return added;
  86. }
  87. bool XmlNode::addAttributeToEnd(const shared_ptr<XmlAttribute> &_attribute)
  88. {
  89. bool added{};
  90. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  91. if (!_hasAttribute(_attribute->getName()))
  92. {
  93. this->attributes.push_back(_attribute);
  94. added = true;
  95. }
  96. return added;
  97. }
  98. bool XmlNode::addChildAfter(const shared_ptr<XmlNode> &_child, const shared_ptr<XmlNode> &_search)
  99. {
  100. bool added{};
  101. auto iterator = this->children.begin();
  102. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  103. NullPointerArgumentEvaluator{_search, "passed search node reference for add attempt is null!"}.evaluate();
  104. if (!this->_hasChild(_child))
  105. {
  106. while (iterator != this->children.end())
  107. {
  108. if (*iterator == _search)
  109. {
  110. ++iterator;
  111. this->children.insert(iterator, _child);
  112. added = true;
  113. break;
  114. }
  115. ++iterator;
  116. }
  117. }
  118. return added;
  119. }
  120. bool XmlNode::addChildBefore(const shared_ptr<XmlNode> &_child, const shared_ptr<XmlNode> &_search)
  121. {
  122. bool added{};
  123. auto iterator = this->children.begin();
  124. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  125. NullPointerArgumentEvaluator{_search, "passed search node reference for add attempt is null!"}.evaluate();
  126. if (!this->_hasChild(_child))
  127. {
  128. while (iterator != this->children.end())
  129. {
  130. if (*iterator == _search)
  131. {
  132. this->children.insert(iterator, _child);
  133. added = true;
  134. break;
  135. }
  136. ++iterator;
  137. }
  138. }
  139. return added;
  140. }
  141. bool XmlNode::addChildToBeginning(const shared_ptr<XmlNode> &_child)
  142. {
  143. bool added{};
  144. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  145. if (!this->_hasChild(_child))
  146. {
  147. this->children.push_front(_child);
  148. added = true;
  149. }
  150. return added;
  151. }
  152. bool XmlNode::addChildToEnd(const shared_ptr<XmlNode> &_child)
  153. {
  154. bool added{};
  155. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  156. if (!this->_hasChild(_child))
  157. {
  158. this->children.push_back(_child);
  159. added = true;
  160. }
  161. return added;
  162. }
  163. void XmlNode::clearValue()
  164. {
  165. this->value.clear();
  166. }
  167. list<shared_ptr<XmlAttribute>> XmlNode::getAttributes() const
  168. {
  169. return this->attributes;
  170. }
  171. list<shared_ptr<XmlNode>> XmlNode::getChildren() const
  172. {
  173. return this->children;
  174. }
  175. list<shared_ptr<XmlNode>> XmlNode::getChildren(const string &_name)
  176. {
  177. list<shared_ptr<XmlNode>> childrenWithName{};
  178. copy_if(this->children.begin(), this->children.end(), back_inserter(childrenWithName), [&_name](const shared_ptr<XmlNode> &_node) { return _node->getName() == _name; });
  179. return childrenWithName;
  180. }
  181. string XmlNode::getName() const
  182. {
  183. return this->name;
  184. }
  185. string XmlNode::getValue() const
  186. {
  187. return this->value;
  188. }
  189. bool XmlNode::hasAttribute(const string &_name)
  190. {
  191. return this->_hasAttribute(_name);
  192. }
  193. bool XmlNode::hasChild(const string &_name)
  194. {
  195. return this->_hasChild(_name);
  196. }
  197. bool XmlNode::hasChild(const shared_ptr<XmlNode> &_child)
  198. {
  199. return this->_hasChild(_child);
  200. }
  201. bool XmlNode::removeFirstAttribute()
  202. {
  203. const bool isValidOperation = !this->attributes.empty();
  204. if (isValidOperation)
  205. {
  206. this->attributes.pop_front();
  207. }
  208. return isValidOperation;
  209. }
  210. bool XmlNode::removeLastAttribute()
  211. {
  212. const bool isValidOperation = !this->attributes.empty();
  213. if (isValidOperation)
  214. {
  215. this->attributes.pop_back();
  216. }
  217. return isValidOperation;
  218. }
  219. bool XmlNode::removeFirstChild()
  220. {
  221. const bool isValidOperation = !this->children.empty();
  222. if (isValidOperation)
  223. {
  224. this->children.pop_front();
  225. }
  226. return isValidOperation;
  227. }
  228. bool XmlNode::removeLastChild()
  229. {
  230. const bool isValidOperation = !this->children.empty();
  231. if (isValidOperation)
  232. {
  233. this->children.pop_back();
  234. }
  235. return isValidOperation;
  236. }
  237. void XmlNode::setName(const string &_name)
  238. {
  239. this->_assignName(_name);
  240. }
  241. void XmlNode::setValue(const string &_value)
  242. {
  243. this->_assignValue(_value);
  244. }
  245. string XmlNode::toXml()
  246. {
  247. return this->_toXml_(0);
  248. }
  249. string XmlNode::_toXml_(uint8_t _tabSize)
  250. {
  251. string xmlStream{};
  252. xmlStream += XmlNode::_getTab(_tabSize);
  253. xmlStream += this->_toXmlOpenTag();
  254. xmlStream += this->_toXmlAttributes();
  255. xmlStream += this->_toXmlOpenTagClose();
  256. xmlStream += this->_toXmlValue();
  257. xmlStream += this->_toXmlChildren(_tabSize + TAB_SIZE);
  258. xmlStream += this->value.empty() ? XmlNode::_getTab(_tabSize) : "";
  259. xmlStream += this->_toXmlCloseTag() + "\n";
  260. return xmlStream;
  261. }
  262. void XmlNode::_assignName(const string &_name)
  263. {
  264. EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
  265. this->name = _name;
  266. }
  267. void XmlNode::_assignValue(const string &_value)
  268. {
  269. EmptyStringArgumentEvaluator{_value, "xml node value is empty!"}.evaluate();
  270. this->value = _value;
  271. }
  272. string XmlNode::_getTab(const uint8_t _tabSize)
  273. {
  274. string tab{};
  275. for (uint8_t index = 0; index < _tabSize; index++)
  276. {
  277. tab += " ";
  278. }
  279. return tab;
  280. }
  281. bool XmlNode::_hasAttribute(const string &_name)
  282. {
  283. EmptyStringArgumentEvaluator{_name, "xml attribute name is empty!"}.evaluate();
  284. return any_of(this->attributes.begin(), this->attributes.end(), [&_name](const shared_ptr<XmlAttribute> &_attribute) { return _attribute->getName() == _name; });
  285. }
  286. bool XmlNode::_hasChild(const shared_ptr<XmlNode> &_child)
  287. {
  288. NullPointerArgumentEvaluator{_child, "passed child node reference for check attempt is null!"}.evaluate();
  289. return find(this->children.begin(), this->children.end(), _child) != this->children.end();
  290. }
  291. bool XmlNode::_hasChild(const string &_name)
  292. {
  293. EmptyStringArgumentEvaluator{_name, "xml child node name is empty!"}.evaluate();
  294. return any_of(this->children.begin(), this->children.end(), [&_name](const shared_ptr<XmlNode> &_node) { return _node->getName() == _name; });
  295. }
  296. string XmlNode::_toXmlAttributes()
  297. {
  298. return accumulate(this->attributes.begin(), this->attributes.end(), string{}, [](string stream, const shared_ptr<XmlAttribute> &_attribute) { return ::move(stream) + " " + _attribute->toXml(); });
  299. }
  300. string XmlNode::_toXmlChildren(uint8_t _tabSize)
  301. {
  302. return accumulate(this->children.begin(), this->children.end(), string{}, [_tabSize](string stream, const shared_ptr<XmlNode> &_child) { return ::move(stream) + _child->_toXml_(_tabSize); });
  303. }
  304. string XmlNode::_toXmlCloseTag() const
  305. {
  306. string stream{};
  307. if (!this->children.empty() || !this->value.empty())
  308. {
  309. stream = "</" + this->name + ">";
  310. }
  311. return stream;
  312. }
  313. string XmlNode::_toXmlOpenTag() const
  314. {
  315. return "<" + this->name;
  316. }
  317. string XmlNode::_toXmlOpenTagClose() const
  318. {
  319. string stream{};
  320. if (this->children.empty() && this->value.empty())
  321. {
  322. stream = " />";
  323. }
  324. else
  325. {
  326. stream = ">";
  327. }
  328. return stream;
  329. }
  330. string XmlNode::_toXmlValue() const
  331. {
  332. return this->value.empty() ? "\n" : this->value;
  333. }