XmlNode.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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-04
  7. *
  8. * */
  9. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  10. #include <ls-std/core/utils/StlUtils.hpp>
  11. #include <ls-std/io/xml/XmlNode.hpp>
  12. ls::std::io::XmlNode::XmlNode(::std::string _name)
  13. : ls::std::core::Class("XmlNode"),
  14. name(::std::move(_name))
  15. {}
  16. ls::std::io::XmlNode::~XmlNode() = default;
  17. bool ls::std::io::XmlNode::addAttributeAfter(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name)
  18. {
  19. bool added{};
  20. auto iterator = this->attributes.begin();
  21. _checkIfAttributeReferenceIsValid(_attribute);
  22. _checkIfNameIsNotEmpty(_name);
  23. if (!this->_hasAttribute(_attribute->getName()))
  24. {
  25. while (iterator != this->attributes.end())
  26. {
  27. if ((*iterator)->getName() == _name)
  28. {
  29. iterator++;
  30. this->attributes.insert(iterator, _attribute);
  31. added = true;
  32. break;
  33. }
  34. iterator++;
  35. }
  36. }
  37. return added;
  38. }
  39. bool ls::std::io::XmlNode::addAttributeBefore(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name)
  40. {
  41. bool added{};
  42. auto iterator = this->attributes.begin();
  43. _checkIfAttributeReferenceIsValid(_attribute);
  44. _checkIfNameIsNotEmpty(_name);
  45. if (!this->_hasAttribute(_attribute->getName()))
  46. {
  47. while (iterator != this->attributes.end())
  48. {
  49. if ((*iterator)->getName() == _name)
  50. {
  51. this->attributes.insert(iterator, _attribute);
  52. added = true;
  53. break;
  54. }
  55. iterator++;
  56. }
  57. }
  58. return added;
  59. }
  60. bool ls::std::io::XmlNode::addAttributeToBeginning(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute)
  61. {
  62. bool added{};
  63. _checkIfAttributeReferenceIsValid(_attribute);
  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. _checkIfAttributeReferenceIsValid(_attribute);
  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. _checkIfNodeReferenceIsValid(_child);
  87. _checkIfNodeReferenceIsValid(_search);
  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. _checkIfNodeReferenceIsValid(_child);
  109. _checkIfNodeReferenceIsValid(_search);
  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. _checkIfNodeReferenceIsValid(_child);
  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. _checkIfNodeReferenceIsValid(_child);
  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. if (_name.empty())
  255. {
  256. throw ls::std::core::IllegalArgumentException{};
  257. }
  258. this->name = _name;
  259. }
  260. void ls::std::io::XmlNode::_assignValue(const ::std::string &_value)
  261. {
  262. if (_value.empty())
  263. {
  264. throw ls::std::core::IllegalArgumentException{};
  265. }
  266. this->value = _value;
  267. }
  268. void ls::std::io::XmlNode::_checkIfAttributeReferenceIsValid(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute)
  269. {
  270. if (_attribute == nullptr)
  271. {
  272. throw ls::std::core::IllegalArgumentException{};
  273. }
  274. }
  275. void ls::std::io::XmlNode::_checkIfNameIsNotEmpty(const ::std::string &_name)
  276. {
  277. if (_name.empty())
  278. {
  279. throw ls::std::core::IllegalArgumentException{};
  280. }
  281. }
  282. void ls::std::io::XmlNode::_checkIfNodeReferenceIsValid(const ::std::shared_ptr<ls::std::io::XmlNode> &_child)
  283. {
  284. if (_child == nullptr)
  285. {
  286. throw ls::std::core::IllegalArgumentException{};
  287. }
  288. }
  289. std::string ls::std::io::XmlNode::_getTab(uint8_t _tabSize)
  290. {
  291. ::std::string tab{};
  292. for (uint8_t index = 0; index < _tabSize; index++)
  293. {
  294. tab += " ";
  295. }
  296. return tab;
  297. }
  298. bool ls::std::io::XmlNode::_hasAttribute(const ::std::string &_name)
  299. {
  300. bool exists{};
  301. if (_name.empty())
  302. {
  303. throw ls::std::core::IllegalArgumentException{};
  304. }
  305. else
  306. {
  307. for (const auto &attribute : this->attributes)
  308. {
  309. if (attribute->getName() == _name)
  310. {
  311. exists = true;
  312. break;
  313. }
  314. }
  315. }
  316. return exists;
  317. }
  318. bool ls::std::io::XmlNode::_hasChild(const ::std::shared_ptr<ls::std::io::XmlNode> &_child)
  319. {
  320. _checkIfNodeReferenceIsValid(_child);
  321. return ls::std::core::StlUtils::contains(this->children, _child);
  322. }
  323. bool ls::std::io::XmlNode::_hasChild(const ::std::string &_name)
  324. {
  325. bool exists{};
  326. if (_name.empty())
  327. {
  328. throw ls::std::core::IllegalArgumentException{};
  329. }
  330. else
  331. {
  332. for (const auto &attribute : this->children)
  333. {
  334. if (attribute->getName() == _name)
  335. {
  336. exists = true;
  337. break;
  338. }
  339. }
  340. }
  341. return exists;
  342. }
  343. std::string ls::std::io::XmlNode::_toXmlAttributes()
  344. {
  345. ::std::string stream{};
  346. for (const auto &_attribute : this->attributes)
  347. {
  348. stream += " " + _attribute->toXml();
  349. }
  350. return stream;
  351. }
  352. std::string ls::std::io::XmlNode::_toXmlChildren(uint8_t _tabSize)
  353. {
  354. ::std::string stream{};
  355. if (this->value.empty())
  356. {
  357. for (const auto &_child : this->children)
  358. {
  359. stream += _child->_toXml_(_tabSize);
  360. }
  361. }
  362. return stream;
  363. }
  364. std::string ls::std::io::XmlNode::_toXmlCloseTag()
  365. {
  366. ::std::string stream{};
  367. if (!this->children.empty() || !this->value.empty())
  368. {
  369. stream = "</" + this->name + ">";
  370. }
  371. return stream;
  372. }
  373. std::string ls::std::io::XmlNode::_toXmlOpenTag()
  374. {
  375. return "<" + this->name;
  376. }
  377. std::string ls::std::io::XmlNode::_toXmlOpenTagClose()
  378. {
  379. ::std::string stream{};
  380. if (this->children.empty() && this->value.empty())
  381. {
  382. stream = " />";
  383. }
  384. else
  385. {
  386. stream = ">";
  387. }
  388. return stream;
  389. }
  390. std::string ls::std::io::XmlNode::_toXmlValue()
  391. {
  392. return this->value.empty() ? "\n" : this->value;
  393. }