aboutsummaryrefslogtreecommitdiff
path: root/src/xml/XmlNode.cpp
blob: 41bbed4d95f97a2b389ee27d4cb3c667d2a94b3e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "cru/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(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;
}

XmlCommentNode::~XmlCommentNode() {}

XmlNode* XmlCommentNode::Clone() const {
  XmlCommentNode* node = new XmlCommentNode(text_);

  return node;
}
}  // namespace cru::xml