123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-09-24
- * Changed: 2023-02-23
- *
- * */
- #include <algorithm>
- #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
- #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
- #include <ls-std/io/xml/XmlNode.hpp>
- using ls::std::core::Class;
- using ls::std::core::EmptyStringArgumentEvaluator;
- using ls::std::core::NullPointerArgumentEvaluator;
- using ls::std::io::XmlAttribute;
- using ls::std::io::XmlNode;
- using std::find;
- using std::list;
- using std::move;
- using std::shared_ptr;
- using std::string;
- XmlNode::XmlNode(string _name) : Class("XmlNode"), name(::move(_name))
- {}
- XmlNode::~XmlNode() noexcept = default;
- bool XmlNode::addAttributeAfter(const shared_ptr<XmlAttribute> &_attribute, const string &_name)
- {
- bool added{};
- auto iterator = this->attributes.begin();
- NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
- EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
- if (!this->_hasAttribute(_attribute->getName()))
- {
- while (iterator != this->attributes.end())
- {
- if ((*iterator)->getName() == _name)
- {
- iterator++;
- this->attributes.insert(iterator, _attribute);
- added = true;
- break;
- }
- iterator++;
- }
- }
- return added;
- }
- bool XmlNode::addAttributeBefore(const shared_ptr<XmlAttribute> &_attribute, const string &_name)
- {
- bool added{};
- auto iterator = this->attributes.begin();
- NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
- EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
- if (!this->_hasAttribute(_attribute->getName()))
- {
- while (iterator != this->attributes.end())
- {
- if ((*iterator)->getName() == _name)
- {
- this->attributes.insert(iterator, _attribute);
- added = true;
- break;
- }
- iterator++;
- }
- }
- return added;
- }
- bool XmlNode::addAttributeToBeginning(const shared_ptr<XmlAttribute> &_attribute)
- {
- bool added{};
- NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
- ;
- if (!_hasAttribute(_attribute->getName()))
- {
- this->attributes.push_front(_attribute);
- added = true;
- }
- return added;
- }
- bool XmlNode::addAttributeToEnd(const shared_ptr<XmlAttribute> &_attribute)
- {
- bool added{};
- NullPointerArgumentEvaluator{_attribute, "passed attribute reference for add attempt is null!"}.evaluate();
- if (!_hasAttribute(_attribute->getName()))
- {
- this->attributes.push_back(_attribute);
- added = true;
- }
- return added;
- }
- bool XmlNode::addChildAfter(const shared_ptr<XmlNode> &_child, const shared_ptr<XmlNode> &_search)
- {
- bool added{};
- auto iterator = this->children.begin();
- NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
- NullPointerArgumentEvaluator{_search, "passed search node reference for add attempt is null!"}.evaluate();
- if (!this->_hasChild(_child))
- {
- while (iterator != this->children.end())
- {
- if (*iterator == _search)
- {
- iterator++;
- this->children.insert(iterator, _child);
- added = true;
- break;
- }
- iterator++;
- }
- }
- return added;
- }
- bool XmlNode::addChildBefore(const shared_ptr<XmlNode> &_child, const shared_ptr<XmlNode> &_search)
- {
- bool added{};
- auto iterator = this->children.begin();
- NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
- NullPointerArgumentEvaluator{_search, "passed search node reference for add attempt is null!"}.evaluate();
- if (!this->_hasChild(_child))
- {
- while (iterator != this->children.end())
- {
- if (*iterator == _search)
- {
- this->children.insert(iterator, _child);
- added = true;
- break;
- }
- iterator++;
- }
- }
- return added;
- }
- bool XmlNode::addChildToBeginning(const shared_ptr<XmlNode> &_child)
- {
- bool added{};
- NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
- if (!this->_hasChild(_child))
- {
- this->children.push_front(_child);
- added = true;
- }
- return added;
- }
- bool XmlNode::addChildToEnd(const shared_ptr<XmlNode> &_child)
- {
- bool added{};
- NullPointerArgumentEvaluator{_child, "passed child node reference for add attempt is null!"}.evaluate();
- if (!this->_hasChild(_child))
- {
- this->children.push_back(_child);
- added = true;
- }
- return added;
- }
- void XmlNode::clearValue()
- {
- this->value.clear();
- }
- list<shared_ptr<XmlAttribute>> XmlNode::getAttributes()
- {
- return this->attributes;
- }
- list<shared_ptr<XmlNode>> XmlNode::getChildren()
- {
- return this->children;
- }
- list<shared_ptr<XmlNode>> XmlNode::getChildren(const string &_name)
- {
- list<shared_ptr<XmlNode>> childrenWithName{};
- for (const auto &child : this->children)
- {
- if (child->getName() == _name)
- {
- childrenWithName.push_back(child);
- }
- }
- return childrenWithName;
- }
- string XmlNode::getName()
- {
- return this->name;
- }
- string XmlNode::getValue()
- {
- return this->value;
- }
- bool XmlNode::hasAttribute(const string &_name)
- {
- return this->_hasAttribute(_name);
- }
- bool XmlNode::hasChild(const string &_name)
- {
- return this->_hasChild(_name);
- }
- bool XmlNode::hasChild(const shared_ptr<XmlNode> &_child)
- {
- return this->_hasChild(_child);
- }
- bool XmlNode::removeFirstAttribute()
- {
- bool isValidOperation = !this->attributes.empty();
- if (isValidOperation)
- {
- this->attributes.pop_front();
- }
- return isValidOperation;
- }
- bool XmlNode::removeLastAttribute()
- {
- bool isValidOperation = !this->attributes.empty();
- if (isValidOperation)
- {
- this->attributes.pop_back();
- }
- return isValidOperation;
- }
- bool XmlNode::removeFirstChild()
- {
- bool isValidOperation = !this->children.empty();
- if (isValidOperation)
- {
- this->children.pop_front();
- }
- return isValidOperation;
- }
- bool XmlNode::removeLastChild()
- {
- bool isValidOperation = !this->children.empty();
- if (isValidOperation)
- {
- this->children.pop_back();
- }
- return isValidOperation;
- }
- void XmlNode::setName(const string &_name)
- {
- this->_assignName(_name);
- }
- void XmlNode::setValue(const string &_value)
- {
- this->_assignValue(_value);
- }
- string XmlNode::toXml()
- {
- return this->_toXml_(0);
- }
- string XmlNode::_toXml_(uint8_t _tabSize)
- {
- string xmlStream{};
- xmlStream += XmlNode::_getTab(_tabSize);
- xmlStream += this->_toXmlOpenTag();
- xmlStream += this->_toXmlAttributes();
- xmlStream += this->_toXmlOpenTagClose();
- xmlStream += this->_toXmlValue();
- xmlStream += this->_toXmlChildren(_tabSize + TAB_SIZE);
- xmlStream += this->value.empty() ? XmlNode::_getTab(_tabSize) : "";
- xmlStream += this->_toXmlCloseTag() + "\n";
- return xmlStream;
- }
- void XmlNode::_assignName(const string &_name)
- {
- EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
- this->name = _name;
- }
- void XmlNode::_assignValue(const string &_value)
- {
- EmptyStringArgumentEvaluator{_value, "xml node value is empty!"}.evaluate();
- this->value = _value;
- }
- string XmlNode::_getTab(uint8_t _tabSize)
- {
- string tab{};
- for (uint8_t index = 0; index < _tabSize; index++)
- {
- tab += " ";
- }
- return tab;
- }
- bool XmlNode::_hasAttribute(const string &_name)
- {
- bool exists{};
- EmptyStringArgumentEvaluator{_name, "xml attribute name is empty!"}.evaluate();
- for (const auto &attribute : this->attributes)
- {
- if (attribute->getName() == _name)
- {
- exists = true;
- break;
- }
- }
- return exists;
- }
- bool XmlNode::_hasChild(const shared_ptr<XmlNode> &_child)
- {
- NullPointerArgumentEvaluator{_child, "passed child node reference for check attempt is null!"}.evaluate();
- return find(this->children.begin(), this->children.end(), _child) != this->children.end();
- }
- bool XmlNode::_hasChild(const string &_name)
- {
- bool exists{};
- EmptyStringArgumentEvaluator{_name, "xml child node name is empty!"}.evaluate();
- for (const auto &attribute : this->children)
- {
- if (attribute->getName() == _name)
- {
- exists = true;
- break;
- }
- }
- return exists;
- }
- string XmlNode::_toXmlAttributes()
- {
- string stream{};
- for (const auto &_attribute : this->attributes)
- {
- stream += " " + _attribute->toXml();
- }
- return stream;
- }
- string XmlNode::_toXmlChildren(uint8_t _tabSize)
- {
- string stream{};
- if (this->value.empty())
- {
- for (const auto &_child : this->children)
- {
- stream += _child->_toXml_(_tabSize);
- }
- }
- return stream;
- }
- string XmlNode::_toXmlCloseTag()
- {
- string stream{};
- if (!this->children.empty() || !this->value.empty())
- {
- stream = "</" + this->name + ">";
- }
- return stream;
- }
- string XmlNode::_toXmlOpenTag()
- {
- return "<" + this->name;
- }
- string XmlNode::_toXmlOpenTagClose()
- {
- string stream{};
- if (this->children.empty() && this->value.empty())
- {
- stream = " />";
- }
- else
- {
- stream = ">";
- }
- return stream;
- }
- string XmlNode::_toXmlValue()
- {
- return this->value.empty() ? "\n" : this->value;
- }
|