XmlNode.cpp 9.2 KB

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