XmlNode.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-24
  6. * Changed: 2021-07-16
  7. *
  8. * */
  9. #include <ls_std/io/xml/XmlNode.hpp>
  10. #include <ls_std/utils/STLUtils.hpp>
  11. #include <ls_std/exception/IllegalArgumentException.hpp>
  12. ls_std::XmlNode::XmlNode(std::string _name)
  13. : ls_std::Class("XmlNode"),
  14. name(std::move(_name))
  15. {}
  16. bool ls_std::XmlNode::addAttributeAfter(const std::shared_ptr<ls_std::XmlAttribute> &_attribute, const std::string &_name)
  17. {
  18. bool added{};
  19. auto iterator = this->attributes.begin();
  20. _checkIfAttributeReferenceIsValid(_attribute);
  21. _checkIfNameIsNotEmpty(_name);
  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::XmlNode::addAttributeBefore(const std::shared_ptr<ls_std::XmlAttribute> &_attribute, const std::string &_name)
  39. {
  40. bool added{};
  41. auto iterator = this->attributes.begin();
  42. _checkIfAttributeReferenceIsValid(_attribute);
  43. _checkIfNameIsNotEmpty(_name);
  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::XmlNode::addAttributeToBeginning(const std::shared_ptr<ls_std::XmlAttribute> &_attribute)
  60. {
  61. bool added{};
  62. _checkIfAttributeReferenceIsValid(_attribute);
  63. if (!_hasAttribute(_attribute->getName()))
  64. {
  65. this->attributes.push_front(_attribute);
  66. added = true;
  67. }
  68. return added;
  69. }
  70. bool ls_std::XmlNode::addAttributeToEnd(const std::shared_ptr<ls_std::XmlAttribute> &_attribute)
  71. {
  72. bool added{};
  73. _checkIfAttributeReferenceIsValid(_attribute);
  74. if (!_hasAttribute(_attribute->getName()))
  75. {
  76. this->attributes.push_back(_attribute);
  77. added = true;
  78. }
  79. return added;
  80. }
  81. bool ls_std::XmlNode::addChildAfter(const std::shared_ptr<ls_std::XmlNode> &_child, const std::shared_ptr<ls_std::XmlNode> &_search)
  82. {
  83. bool added{};
  84. auto iterator = this->children.begin();
  85. _checkIfNodeReferenceIsValid(_child);
  86. _checkIfNodeReferenceIsValid(_search);
  87. if (!this->_hasChild(_child))
  88. {
  89. while (iterator != this->children.end())
  90. {
  91. if (*iterator == _search)
  92. {
  93. iterator++;
  94. this->children.insert(iterator, _child);
  95. added = true;
  96. break;
  97. }
  98. iterator++;
  99. }
  100. }
  101. return added;
  102. }
  103. bool ls_std::XmlNode::addChildBefore(const std::shared_ptr<ls_std::XmlNode> &_child, const std::shared_ptr<ls_std::XmlNode> &_search)
  104. {
  105. bool added{};
  106. auto iterator = this->children.begin();
  107. _checkIfNodeReferenceIsValid(_child);
  108. _checkIfNodeReferenceIsValid(_search);
  109. if (!this->_hasChild(_child))
  110. {
  111. while (iterator != this->children.end())
  112. {
  113. if (*iterator == _search)
  114. {
  115. this->children.insert(iterator, _child);
  116. added = true;
  117. break;
  118. }
  119. iterator++;
  120. }
  121. }
  122. return added;
  123. }
  124. bool ls_std::XmlNode::addChildToBeginning(const std::shared_ptr<ls_std::XmlNode> &_child)
  125. {
  126. bool added{};
  127. _checkIfNodeReferenceIsValid(_child);
  128. if (!this->_hasChild(_child))
  129. {
  130. this->children.push_front(_child);
  131. added = true;
  132. }
  133. return added;
  134. }
  135. bool ls_std::XmlNode::addChildToEnd(const std::shared_ptr<ls_std::XmlNode> &_child)
  136. {
  137. bool added{};
  138. _checkIfNodeReferenceIsValid(_child);
  139. if (!this->_hasChild(_child))
  140. {
  141. this->children.push_back(_child);
  142. added = true;
  143. }
  144. return added;
  145. }
  146. void ls_std::XmlNode::clearValue()
  147. {
  148. this->value.clear();
  149. }
  150. std::list<std::shared_ptr<ls_std::XmlAttribute>> ls_std::XmlNode::getAttributes()
  151. {
  152. return this->attributes;
  153. }
  154. std::list<std::shared_ptr<ls_std::XmlNode>> ls_std::XmlNode::getChildren()
  155. {
  156. return this->children;
  157. }
  158. std::list<std::shared_ptr<ls_std::XmlNode>> ls_std::XmlNode::getChildren(const std::string &_name)
  159. {
  160. std::list<std::shared_ptr<ls_std::XmlNode>> childrenWithName{};
  161. for (const auto &child : this->children)
  162. {
  163. if (child->getName() == _name)
  164. {
  165. childrenWithName.push_back(child);
  166. }
  167. }
  168. return childrenWithName;
  169. }
  170. std::string ls_std::XmlNode::getName()
  171. {
  172. return this->name;
  173. }
  174. std::string ls_std::XmlNode::getValue()
  175. {
  176. return this->value;
  177. }
  178. bool ls_std::XmlNode::hasAttribute(const std::string &_name)
  179. {
  180. return this->_hasAttribute(_name);
  181. }
  182. bool ls_std::XmlNode::hasChild(const std::string &_name)
  183. {
  184. return this->_hasChild(_name);
  185. }
  186. bool ls_std::XmlNode::hasChild(const std::shared_ptr<ls_std::XmlNode> &_child)
  187. {
  188. return this->_hasChild(_child);
  189. }
  190. bool ls_std::XmlNode::removeFirstAttribute()
  191. {
  192. bool isValidOperation = !this->attributes.empty();
  193. if (isValidOperation)
  194. {
  195. this->attributes.pop_front();
  196. }
  197. return isValidOperation;
  198. }
  199. bool ls_std::XmlNode::removeLastAttribute()
  200. {
  201. bool isValidOperation = !this->attributes.empty();
  202. if (isValidOperation)
  203. {
  204. this->attributes.pop_back();
  205. }
  206. return isValidOperation;
  207. }
  208. bool ls_std::XmlNode::removeFirstChild()
  209. {
  210. bool isValidOperation = !this->children.empty();
  211. if (isValidOperation)
  212. {
  213. this->children.pop_front();
  214. }
  215. return isValidOperation;
  216. }
  217. bool ls_std::XmlNode::removeLastChild()
  218. {
  219. bool isValidOperation = !this->children.empty();
  220. if (isValidOperation)
  221. {
  222. this->children.pop_back();
  223. }
  224. return isValidOperation;
  225. }
  226. void ls_std::XmlNode::setName(const std::string &_name)
  227. {
  228. this->_assignName(_name);
  229. }
  230. void ls_std::XmlNode::setValue(const std::string &_value)
  231. {
  232. this->_assignValue(_value);
  233. }
  234. std::string ls_std::XmlNode::toXml()
  235. {
  236. return this->_toXml_(0);
  237. }
  238. std::string ls_std::XmlNode::_toXml_(uint8_t _tabSize)
  239. {
  240. std::string xmlStream{};
  241. xmlStream += ls_std::XmlNode::_getTab(_tabSize);
  242. xmlStream += this->_toXmlOpenTag();
  243. xmlStream += this->_toXmlAttributes();
  244. xmlStream += this->_toXmlOpenTagClose();
  245. xmlStream += this->_toXmlValue();
  246. xmlStream += this->_toXmlChildren(_tabSize + TAB_SIZE);
  247. xmlStream += this->value.empty() ? ls_std::XmlNode::_getTab(_tabSize) : "";
  248. xmlStream += this->_toXmlCloseTag() + "\n";
  249. return xmlStream;
  250. }
  251. void ls_std::XmlNode::_assignName(const std::string &_name)
  252. {
  253. if (_name.empty())
  254. {
  255. throw ls_std::IllegalArgumentException{};
  256. }
  257. this->name = _name;
  258. }
  259. void ls_std::XmlNode::_assignValue(const std::string &_value)
  260. {
  261. if (_value.empty())
  262. {
  263. throw ls_std::IllegalArgumentException{};
  264. }
  265. this->value = _value;
  266. }
  267. void ls_std::XmlNode::_checkIfAttributeReferenceIsValid(const std::shared_ptr<ls_std::XmlAttribute> &_attribute)
  268. {
  269. if (_attribute == nullptr)
  270. {
  271. throw ls_std::IllegalArgumentException{};
  272. }
  273. }
  274. void ls_std::XmlNode::_checkIfNameIsNotEmpty(const std::string &_name)
  275. {
  276. if (_name.empty())
  277. {
  278. throw ls_std::IllegalArgumentException{};
  279. }
  280. }
  281. void ls_std::XmlNode::_checkIfNodeReferenceIsValid(const std::shared_ptr<ls_std::XmlNode> &_child)
  282. {
  283. if (_child == nullptr)
  284. {
  285. throw ls_std::IllegalArgumentException{};
  286. }
  287. }
  288. std::string ls_std::XmlNode::_getTab(uint8_t _tabSize)
  289. {
  290. std::string tab{};
  291. for (uint8_t index = 0; index < _tabSize; index++)
  292. {
  293. tab += " ";
  294. }
  295. return tab;
  296. }
  297. bool ls_std::XmlNode::_hasAttribute(const std::string &_name)
  298. {
  299. bool exists{};
  300. if (_name.empty())
  301. {
  302. throw ls_std::IllegalArgumentException{};
  303. }
  304. else
  305. {
  306. for (const auto &attribute : this->attributes)
  307. {
  308. if (attribute->getName() == _name)
  309. {
  310. exists = true;
  311. break;
  312. }
  313. }
  314. }
  315. return exists;
  316. }
  317. bool ls_std::XmlNode::_hasChild(const std::shared_ptr<ls_std::XmlNode> &_child)
  318. {
  319. _checkIfNodeReferenceIsValid(_child);
  320. return ls_std::STLUtils::contains(this->children, _child);
  321. }
  322. bool ls_std::XmlNode::_hasChild(const std::string &_name)
  323. {
  324. bool exists{};
  325. if (_name.empty())
  326. {
  327. throw ls_std::IllegalArgumentException{};
  328. }
  329. else
  330. {
  331. for (const auto &attribute : this->children)
  332. {
  333. if (attribute->getName() == _name)
  334. {
  335. exists = true;
  336. break;
  337. }
  338. }
  339. }
  340. return exists;
  341. }
  342. std::string ls_std::XmlNode::_toXmlAttributes()
  343. {
  344. std::string stream{};
  345. for (const auto &_attribute : this->attributes)
  346. {
  347. stream += " " + _attribute->toXml();
  348. }
  349. return stream;
  350. }
  351. std::string ls_std::XmlNode::_toXmlChildren(uint8_t _tabSize)
  352. {
  353. std::string stream{};
  354. if (this->value.empty())
  355. {
  356. for (const auto &_child : this->children)
  357. {
  358. stream += _child->_toXml_(_tabSize);
  359. }
  360. }
  361. return stream;
  362. }
  363. std::string ls_std::XmlNode::_toXmlCloseTag()
  364. {
  365. std::string stream{};
  366. if (!this->children.empty() || !this->value.empty())
  367. {
  368. stream = "</" + this->name + ">";
  369. }
  370. return stream;
  371. }
  372. std::string ls_std::XmlNode::_toXmlOpenTag()
  373. {
  374. return "<" + this->name;
  375. }
  376. std::string ls_std::XmlNode::_toXmlOpenTagClose()
  377. {
  378. std::string stream{};
  379. if (this->children.empty() && this->value.empty())
  380. {
  381. stream = " />";
  382. }
  383. else
  384. {
  385. stream = ">";
  386. }
  387. return stream;
  388. }
  389. std::string ls_std::XmlNode::_toXmlValue()
  390. {
  391. return this->value.empty() ? "\n" : this->value;
  392. }