-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml3.cpp
213 lines (209 loc) · 7.4 KB
/
xml3.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <string_view>
#include <regex>
#include <exception>
#include <algorithm>
#include <memory>
#include <utility>
#include <cctype>
class XMLError : public std::exception {
std::string str;
public:
XMLError(std::string_view s) : str{ s } {}
const char *what() const noexcept override { return str.c_str(); }
};
class XMLElement;
class XMLElement {
std::string name, value;
std::vector<std::pair<std::string,std::string>> attributes;
std::vector<std::unique_ptr<XMLElement>> children;
const std::string_view indent_with = " ";
static std::string read_until(std::istream &is, char c, bool eat, bool skipws) {
std::string str;
auto ch = is.get();
while (!is.eof() && (static_cast<char>(ch) != c)) {
if (!skipws || ch > ' ') {
str += static_cast<char>(ch);
}
ch = is.get();
}
if (eat) {
if (!skipws || ch > ' ') {
str += static_cast<char>(ch);
}
}
else {
is.putback(ch);
}
return str;
}
public:
XMLElement(std::string_view name, std::string_view value)
: name{ name }, value{ value } {}
XMLElement(
std::string_view name,
std::vector<std::pair<std::string,std::string>>&& attributes = {}
) : name{ name }, attributes{ std::move(attributes) } {}
XMLElement(std::istream& is) {
std::string str;
str = read_until(is, '<', false, true);
if (str.empty()) {
str = read_until(is, '>', true, false);
const std::regex begin_elem{ R"(<([A-Za-z_][A-Za-z0-9_]*)((\n|\r|.)*)>)" },
end_elem{ R"((/>)|(</([A-Za-z_][A-Za-z0-9_]*)>))" },
attr{ R"*(^\s+([A-Za-z_][A-Za-z0-9_]*)="([^"]*)")*" }, end_attrs{ R"(\s*/?$)" };
std::smatch matches;
if (std::regex_match(str, matches, begin_elem)) {
name = matches[1].str();
str = matches[2].str();
if (!str.empty()) {
std::smatch attrs;
while (std::regex_search(str, attrs, attr)) {
attributes.emplace_back(attrs[1].str(), attrs[2].str());
str = str.substr(attrs[0].str().size());
}
if (!std::regex_match(str, end_attrs)) {
throw XMLError("Expected attribute instead of: " + str);
}
}
if (str.back() == '/') {
return;
}
str = read_until(is, '<', false, false);
if (std::find_if(str.cbegin(), str.cend(), [](char c){ return c > ' '; }) != str.cend()) {
value = str;
str = read_until(is, '>', true, false);
if (std::regex_match(str, matches, end_elem)) {
if (matches[3].str() == name) {
return;
}
}
}
else {
for (;;) {
children.push_back(std::make_unique<XMLElement>(is));
str = read_until(is, '<', true, true);
if (str != "<") {
break;
}
char c;
is >> c;
if (c == '/') {
str = "</" + read_until(is, '>', true, false);
if (std::regex_match(str, matches, end_elem)) {
if (matches[3].str() == name) {
return;
}
}
break;
}
is.putback(c);
is.putback('<');
}
}
}
}
throw XMLError("Bad input: " + str);
}
void addChild(std::unique_ptr<XMLElement> child) {
children.emplace_back(std::move(child));
}
void serialize(std::ostream& os, int indent = 0) const {
for (int i = 0; i != indent; ++i) {
os << indent_with;
}
os << '<' << name;
for (const auto& a : attributes) {
os << ' ' << a.first << "=\"" << a.second << "\"";
}
if (children.empty()) {
if (value.empty()) {
os << " />\n";
}
else {
os << '>' << value << "</" << name << ">\n";
}
}
else {
os << ">\n";
for (const auto& c : children) {
c->serialize(os, indent + 1);
}
for (int i = 0; i != indent; ++i) {
os << indent_with;
}
os << "</" << name << ">\n";
}
}
const XMLElement& operator[](size_t idx) const {
if (idx >= children.size()) {
throw XMLError("Index " + std::to_string(idx)
+ " out of range for element: " + name);
}
return *children[idx];
}
const std::string& operator[](std::string_view idx) const {
auto iter1 = std::find_if(attributes.cbegin(), attributes.cend(),
[&idx](const auto& attr){ return idx == attr.first;
});
if (iter1 != attributes.cend()) {
return iter1->second;
}
auto iter2 = std::find_if(children.cbegin(), children.cend(),
[&idx](const auto& child){
return idx == child->name && !child->value.empty();
});
if (iter2 != children.cend()) {
return (*iter2)->value;
}
throw XMLError("No such attribute or leaf child name "
+ std::string{ idx } + " for element: " + name);
}
const std::string& elementName() const {
return name;
}
size_t numberOfAttributes() const {
return attributes.size();
}
size_t numberOfChildren() const {
return children.size();
}
const std::vector<std::string> getAttributeNames() const {
std::vector<std::string> attrs;
std::for_each(attributes.cbegin(), attributes.cend(),
[&attrs](const auto& attr){ attrs.push_back(attr.first);
});
return attrs;
}
const std::vector<std::string> getChildNames() const {
std::vector<std::string> childNames;
std::for_each(children.cbegin(), children.cend(),
[&childNames](const auto& child){
childNames.push_back(child->name);
});
return childNames;
}
};
int main() {
try {
XMLElement xml_doc(std::cin);
auto children = xml_doc.numberOfChildren();
std::cout << "Root element has " << children << " children\n";
for (size_t c = 0; c != children; ++c) {
std::cout << c << ": " << xml_doc[c].elementName() << ":\nAttributes:\n";
for (const auto& n : xml_doc[c].getAttributeNames()) {
std::cout << " " << n << ": " << xml_doc[c][n] << '\n';
}
std::cout << "Children:\n";
for (const auto& n : xml_doc[c].getChildNames()) {
std::cout << " " << n << ": " << xml_doc[c][n] << '\n';
}
}
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
}
}