XmlNode.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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-23
  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::find;
  19. using std::list;
  20. using std::move;
  21. using std::shared_ptr;
  22. using std::string;
  23. XmlNode::XmlNode(string _name) : Class("XmlNode"), name(::move(_name))
  24. {}
  25. XmlNode::~XmlNode() noexcept = default;
  26. bool XmlNode::addAttributeAfter(const shared_ptr<XmlAttribute> &_attribute, const string &_name)
  27. {
  28. bool added{};
  29. auto iterator = this->attributes.begin();
  30. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  31. EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
  32. if (!this->_hasAttribute(_attribute->getName()))
  33. {
  34. while (iterator != this->attributes.end())
  35. {
  36. if ((*iterator)->getName() == _name)
  37. {
  38. iterator++;
  39. this->attributes.insert(iterator, _attribute);
  40. added = true;
  41. break;
  42. }
  43. iterator++;
  44. }
  45. }
  46. return added;
  47. }
  48. bool XmlNode::addAttributeBefore(const shared_ptr<XmlAttribute> &_attribute, const string &_name)
  49. {
  50. bool added{};
  51. auto iterator = this->attributes.begin();
  52. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  53. EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
  54. if (!this->_hasAttribute(_attribute->getName()))
  55. {
  56. while (iterator != this->attributes.end())
  57. {
  58. if ((*iterator)->getName() == _name)
  59. {
  60. this->attributes.insert(iterator, _attribute);
  61. added = true;
  62. break;
  63. }
  64. iterator++;
  65. }
  66. }
  67. return added;
  68. }
  69. bool XmlNode::addAttributeToBeginning(const shared_ptr<XmlAttribute> &_attribute)
  70. {
  71. bool added{};
  72. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  73. ;
  74. if (!_hasAttribute(_attribute->getName()))
  75. {
  76. this->attributes.push_front(_attribute);
  77. added = true;
  78. }
  79. return added;
  80. }
  81. bool XmlNode::addAttributeToEnd(const shared_ptr<XmlAttribute> &_attribute)
  82. {
  83. bool added{};
  84. NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
  85. if (!_hasAttribute(_attribute->getName()))
  86. {
  87. this->attributes.push_back(_attribute);
  88. added = true;
  89. }
  90. return added;
  91. }
  92. bool XmlNode::addChildAfter(const shared_ptr<XmlNode> &_child, const shared_ptr<XmlNode> &_search)
  93. {
  94. bool added{};
  95. auto iterator = this->children.begin();
  96. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  97. NullPointerArgumentEvaluator{_search, "passed search node reference for add attempt is null!"}.evaluate();
  98. if (!this->_hasChild(_child))
  99. {
  100. while (iterator != this->children.end())
  101. {
  102. if (*iterator == _search)
  103. {
  104. iterator++;
  105. this->children.insert(iterator, _child);
  106. added = true;
  107. break;
  108. }
  109. iterator++;
  110. }
  111. }
  112. return added;
  113. }
  114. bool XmlNode::addChildBefore(const shared_ptr<XmlNode> &_child, const shared_ptr<XmlNode> &_search)
  115. {
  116. bool added{};
  117. auto iterator = this->children.begin();
  118. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  119. NullPointerArgumentEvaluator{_search, "passed search node reference for add attempt is null!"}.evaluate();
  120. if (!this->_hasChild(_child))
  121. {
  122. while (iterator != this->children.end())
  123. {
  124. if (*iterator == _search)
  125. {
  126. this->children.insert(iterator, _child);
  127. added = true;
  128. break;
  129. }
  130. iterator++;
  131. }
  132. }
  133. return added;
  134. }
  135. bool XmlNode::addChildToBeginning(const shared_ptr<XmlNode> &_child)
  136. {
  137. bool added{};
  138. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  139. if (!this->_hasChild(_child))
  140. {
  141. this->children.push_front(_child);
  142. added = true;
  143. }
  144. return added;
  145. }
  146. bool XmlNode::addChildToEnd(const shared_ptr<XmlNode> &_child)
  147. {
  148. bool added{};
  149. NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
  150. if (!this->_hasChild(_child))
  151. {
  152. this->children.push_back(_child);
  153. added = true;
  154. }
  155. return added;
  156. }
  157. void XmlNode::clearValue()
  158. {
  159. this->value.clear();
  160. }
  161. list<shared_ptr<XmlAttribute>> XmlNode::getAttributes()
  162. {
  163. return this->attributes;
  164. }
  165. list<shared_ptr<XmlNode>> XmlNode::getChildren()
  166. {
  167. return this->children;
  168. }
  169. list<shared_ptr<XmlNode>> XmlNode::getChildren(const string &_name)
  170. {
  171. list<shared_ptr<XmlNode>> childrenWithName{};
  172. for (const auto &child : this->children)
  173. {
  174. if (child->getName() == _name)
  175. {
  176. childrenWithName.push_back(child);
  177. }
  178. }
  179. return childrenWithName;
  180. }
  181. string XmlNode::getName()
  182. {
  183. return this->name;
  184. }
  185. string XmlNode::getValue()
  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. 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. 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. 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. 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(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. bool exists{};
  284. EmptyStringArgumentEvaluator{_name, "xml attribute name is empty!"}.evaluate();
  285. for (const auto &attribute : this->attributes)
  286. {
  287. if (attribute->getName() == _name)
  288. {
  289. exists = true;
  290. break;
  291. }
  292. }
  293. return exists;
  294. }
  295. bool XmlNode::_hasChild(const shared_ptr<XmlNode> &_child)
  296. {
  297. NullPointerArgumentEvaluator{_child, "passed child node reference for check attempt is null!"}.evaluate();
  298. return find(this->children.begin(), this->children.end(), _child) != this->children.end();
  299. }
  300. bool XmlNode::_hasChild(const string &_name)
  301. {
  302. bool exists{};
  303. EmptyStringArgumentEvaluator{_name, "xml child node name is empty!"}.evaluate();
  304. for (const auto &attribute : this->children)
  305. {
  306. if (attribute->getName() == _name)
  307. {
  308. exists = true;
  309. break;
  310. }
  311. }
  312. return exists;
  313. }
  314. string XmlNode::_toXmlAttributes()
  315. {
  316. string stream{};
  317. for (const auto &_attribute : this->attributes)
  318. {
  319. stream += " " + _attribute->toXml();
  320. }
  321. return stream;
  322. }
  323. string XmlNode::_toXmlChildren(uint8_t _tabSize)
  324. {
  325. string stream{};
  326. if (this->value.empty())
  327. {
  328. for (const auto &_child : this->children)
  329. {
  330. stream += _child->_toXml_(_tabSize);
  331. }
  332. }
  333. return stream;
  334. }
  335. string XmlNode::_toXmlCloseTag()
  336. {
  337. string stream{};
  338. if (!this->children.empty() || !this->value.empty())
  339. {
  340. stream = "</" + this->name + ">";
  341. }
  342. return stream;
  343. }
  344. string XmlNode::_toXmlOpenTag()
  345. {
  346. return "<" + this->name;
  347. }
  348. string XmlNode::_toXmlOpenTagClose()
  349. {
  350. string stream{};
  351. if (this->children.empty() && this->value.empty())
  352. {
  353. stream = " />";
  354. }
  355. else
  356. {
  357. stream = ">";
  358. }
  359. return stream;
  360. }
  361. string XmlNode::_toXmlValue()
  362. {
  363. return this->value.empty() ? "\n" : this->value;
  364. }