XmlNode.cpp 10 KB

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