aboutsummaryrefslogtreecommitdiff
path: root/src/xml/XmlParser.cpp
blob: f24a7f68c6b11bbd9a7152892bf4f95b9e89d2aa (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "cru/xml/XmlParser.hpp"
#include "cru/xml/XmlNode.hpp"

namespace cru::xml {
XmlElementNode* XmlParser::Parse() {
  if (!cache_) {
    cache_ = DoParse();
  }
  return static_cast<XmlElementNode*>(cache_->Clone());
}

char16_t XmlParser::Read1() {
  if (current_position_ >= xml_.size()) {
    throw XmlParsingException(u"Unexpected end of xml");
  }
  return xml_[current_position_++];
}

void XmlParser::ReadSpacesAndDiscard() {
  while (current_position_ < xml_.size() &&
         (xml_[current_position_] == ' ' || xml_[current_position_] == '\t' ||
          xml_[current_position_] == '\n' || xml_[current_position_] == '\r')) {
    ++current_position_;
  }
}

String XmlParser::ReadSpaces() {
  String spaces;
  while (current_position_ < xml_.size() &&
         (xml_[current_position_] == ' ' || xml_[current_position_] == '\t' ||
          xml_[current_position_] == '\n' || xml_[current_position_] == '\r')) {
    spaces += xml_[current_position_];
    ++current_position_;
  }
  return spaces;
}

String XmlParser::ReadIdenitifier() {
  String identifier;
  while (current_position_ < xml_.size() &&
         (xml_[current_position_] >= 'a' && xml_[current_position_] <= 'z' ||
          xml_[current_position_] >= 'A' && xml_[current_position_] <= 'Z' ||
          xml_[current_position_] >= '0' && xml_[current_position_] <= '9' ||
          xml_[current_position_] == '_')) {
    identifier += xml_[current_position_];
    ++current_position_;
  }
  return identifier;
}

String XmlParser::ReadAttributeString() {
  if (Read1() != '"') {
    throw XmlParsingException(u"Expected \".");
  }

  String string;

  while (true) {
    char16_t c = Read1();

    if (c == '"') {
      break;
    }

    string += c;
  }

  return string;
}

XmlElementNode* XmlParser::DoParse() {
  while (current_position_ < xml_.size()) {
    switch (xml_[current_position_]) {
      case '<': {
        ++current_position_;

        if (Read1() == '/') {
        } else {
          ReadSpacesAndDiscard();

          String tag = ReadIdenitifier();

          XmlElementNode* node = new XmlElementNode(tag);

          while (true) {
            ReadSpacesAndDiscard();
            if (Read1() == '>') {
              break;
            } else {
              String attribute_name = ReadIdenitifier();

              ReadSpacesAndDiscard();

              if (Read1() != '=') {
                throw XmlParsingException(u"Expected '='");
              }

              ReadSpacesAndDiscard();

              String attribute_value = ReadAttributeString();

              node->AddAttribute(attribute_name, attribute_value);
            }
          }

          current_->AddChild(node);
          current_ = node;
        }

        break;
      }
    }
  }

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