aboutsummaryrefslogtreecommitdiff
path: root/src/xml/XmlNode.cpp
blob: 00437f9b4a80e859706d5d95dd578806918eb2b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "cru/xml/XmlNode.hpp"
#include <algorithm>

namespace cru::xml {

XmlElementNode* XmlNode::AsElement() {
  return static_cast<XmlElementNode*>(this);
}

XmlTextNode* XmlNode::AsText() { return static_cast<XmlTextNode*>(this); }

const XmlElementNode* XmlNode::AsElement() const {
  return static_cast<const XmlElementNode*>(this);
}

const XmlTextNode* XmlNode::AsText() const {
  return static_cast<const XmlTextNode*>(this);
}

XmlElementNode::~XmlElementNode() {
  for (auto child : children_) {
    delete child;
  }
}

void XmlElementNode::AddAttribute(String key, 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;
}
}  // namespace cru::xml