diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-04 21:59:42 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-04 21:59:42 +0800 |
| commit | 1a6111e3f02b0a9cff0f81fb524b4dfb7d69854b (patch) | |
| tree | 3a92e1bf61fc165c2148f38ca6602f5066011f06 /src/base/xml/XmlNode.cpp | |
| parent | f48505c96a70e2f1d1982fea30f3015e42fcd49d (diff) | |
| download | cru-1a6111e3f02b0a9cff0f81fb524b4dfb7d69854b.tar.gz cru-1a6111e3f02b0a9cff0f81fb524b4dfb7d69854b.tar.bz2 cru-1a6111e3f02b0a9cff0f81fb524b4dfb7d69854b.zip | |
Move xml to base.
Diffstat (limited to 'src/base/xml/XmlNode.cpp')
| -rw-r--r-- | src/base/xml/XmlNode.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/base/xml/XmlNode.cpp b/src/base/xml/XmlNode.cpp new file mode 100644 index 00000000..d3e7369b --- /dev/null +++ b/src/base/xml/XmlNode.cpp @@ -0,0 +1,75 @@ +#include "cru/base/xml/XmlNode.h" + +#include <algorithm> + +namespace cru::xml { + +XmlElementNode* XmlNode::AsElement() { + return static_cast<XmlElementNode*>(this); +} + +XmlTextNode* XmlNode::AsText() { return static_cast<XmlTextNode*>(this); } + +XmlCommentNode* XmlNode::AsComment() { + return static_cast<XmlCommentNode*>(this); +} + +const XmlElementNode* XmlNode::AsElement() const { + return static_cast<const XmlElementNode*>(this); +} + +const XmlTextNode* XmlNode::AsText() const { + return static_cast<const XmlTextNode*>(this); +} + +const XmlCommentNode* XmlNode::AsComment() const { + return static_cast<const XmlCommentNode*>(this); +} + +XmlElementNode::~XmlElementNode() { + for (auto child : children_) { + delete child; + } +} + +void XmlElementNode::AddAttribute(std::string key, std::string value) { + attributes_[std::move(key)] = std::move(value); +} + +void XmlElementNode::AddChild(XmlNode* child) { + Expects(child->GetParent() == nullptr); + children_.push_back(child); + child->parent_ = this; +} + +Index XmlElementNode::GetChildElementCount() const { + return std::count_if( + children_.cbegin(), children_.cend(), + [](xml::XmlNode* node) { return node->IsElementNode(); }); +} + +XmlElementNode* XmlElementNode::GetFirstChildElement() const { + for (auto child : children_) { + if (child->GetType() == XmlNode::Type::Element) { + return child->AsElement(); + } + } + return nullptr; +} + +XmlNode* XmlElementNode::Clone() const { + XmlElementNode* node = new XmlElementNode(tag_, attributes_); + + for (auto child : children_) { + node->AddChild(child->Clone()); + } + + return node; +} + +XmlNode* XmlCommentNode::Clone() const { + XmlCommentNode* node = new XmlCommentNode(text_); + + return node; +} +} // namespace cru::xml |
