XmlNode.cpp 9.3 KB

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