-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_document.cpp
254 lines (221 loc) · 6.7 KB
/
xml_document.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "xml_document.h"
#include "utility.h"
#include "translator.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>
XMLNode::XMLNode(
const std::string& name, XMLNode* parent
) : name_(name), parent_(parent) {
if( parent != nullptr ) {
parent->addNode(this);
}
}
XMLNode::XMLNode(
const std::string& name, const std::string& value, XMLNode* parent
) : name_(name), value_(value), parent_(parent) {
if( parent != nullptr ) {
parent->addNode(this);
}
}
XMLNode::~XMLNode() {
nodes_.erase(nodes_.begin(), nodes_.end());
attributes_.erase(attributes_.begin(), attributes_.end());
}
void XMLNode::addNode(XMLNode* node) {
nodes_.push_back(node);
}
int XMLNode::nodeCount() const {
return nodes_.size();
}
const XMLNode* XMLNode::node(int index) const {
if( index < 0 || index >= nodeCount() ) {
return nullptr;
}
return nodes_.at(index);
}
XMLNode* XMLNode::node(int index) {
if( index < 0 || index >= nodeCount() ) {
return nullptr;
}
return nodes_.at(index);
}
XMLNode* XMLNode::getNodeFromName(const std::string& node_name) {
for( XMLNode* node : nodes() ) {
XMLAttr* attr = node->getAttrFromName("name");
if( attr == nullptr ) continue;
if( attr->value() == node_name ) {
return node;
}
}
return nullptr;
}
XMLNode* XMLNode::getNodeFromTag(const std::string& tag_name) {
for( XMLNode* node : nodes() ) {
if( node->name() == tag_name ) {
return node;
}
}
return nullptr;
}
XMLNode* XMLNode::addValuedNode(const std::string& name, const std::string& value) {
return new XMLNode(name, value, this);
}
void XMLNode::addAttr(XMLAttr* attr) {
attributes_.push_back(attr);
}
XMLAttr* XMLNode::addValuedAttr(const std::string& label, const std::string& value) {
return new XMLAttr(label, value, this);
}
int XMLNode::attrCount() const {
return attributes_.size();
}
const XMLAttr* XMLNode::attr(int index) const {
if( index < 0 || index >= attrCount() ) {
return nullptr;
}
return attributes_.at(index);
}
XMLAttr* XMLNode::attr(int index) {
if( index < 0 || index >= attrCount() ) {
return nullptr;
}
return attributes_.at(index);
}
XMLAttr* XMLNode::getAttrFromName(const std::string& attr_name) {
for( XMLAttr* attr : attributes() ) {
if( attr->label() == attr_name ) {
return attr;
}
}
return nullptr;
}
const std::string& XMLNode::name() const {
return name_;
}
bool XMLNode::hasValue() const {
return !value_.empty();
}
const std::string& XMLNode::value() const {
return value_;
}
/******************************************************************/
XMLAttr::XMLAttr(
const std::string& label,
const std::string& value,
XMLNode* parent
) : label_(label), value_(value) {
if( parent != nullptr ) {
parent->addAttr(this);
}
}
XMLAttr::~XMLAttr() {
}
const std::string& XMLAttr::label() const {
return label_;
}
const std::string& XMLAttr::value() const {
return value_;
}
/******************************************************************/
namespace {
/*!
* result can be:
* <nodename>...</nodename>
* or
* <nodename attr="" ...>...</nodename>
* or
* <nodename attr="" .../>
*/
void save_node(std::ofstream& file, const XMLNode* node, std::string offset) {
file << offset << "<" << node->name();
if( node->attrCount() > 0 ) {
for( int i = 0; i < node->attrCount(); i++ ) {
const XMLAttr* attr = node->attr(i);
file << " " << attr->label() << "=\"" << attr->value() << "\"";
}
}
if( !node->hasValue() && !node->nodeCount() > 0 ) {
file << "/>" << std::endl;
return;
}
if( node->hasValue() ) {
file << ">" << node->value() << "</" << node->name() << ">" << std::endl;
} else {
file << ">" << std::endl;
for( int i = 0; i < node->nodeCount(); i++ ) {
const XMLNode* child = node->node(i);
save_node(file, child, offset.append(" "));
offset = offset.substr(2);
}
file << offset << "</" << node->name() << ">" << std::endl;
}
}
XMLNode* read_node(std::string& str, XMLNode* parent) {
// starting with a node
// 1. get name
int endtag = str.find_first_of(" />");
std::string name = str.substr(0, endtag);
XMLNode* node = new XMLNode(name, parent);
// read potential attributes
str = str.substr(name.length(), str.length()-name.length()+1);
str = Utility::trim(str);
// 2. get attributes if any
int attrtag = str.find_first_of("=");
while( attrtag >= 0 ) {
endtag = str.find_first_of(" />");
std::string label = str.substr(0, attrtag);
std::string value = str.substr(attrtag+1, endtag-attrtag-1);
value = Utility::trim(value, '"');
node->addValuedAttr(label, value);
str = str.substr(endtag, str.length()-endtag+1);
str = Utility::trim(str);
attrtag = str.find_first_of("=");
}
// read potential value or nodes
return node;
}
}
XMLNode* XMLDocument::read_doc(const std::string& filename) {
std::ifstream file(filename);
if (!file) {
Logger::error() << tr("unable to open file for load: ") << filename << Logger::endl;
return nullptr;
}
XMLNode* doc = new XMLNode("XMLDocument");
XMLNode* parent = doc;
XMLNode* cur_node = parent;
std::string str;
while (std::getline(file, str)) {
if( Utility::startsWith(str, "</") ) {
// end tag
parent = cur_node->parent();
} else {
int begintag = str.find_first_of("<");
if( begintag >= 0 ) {
str = str.substr(begintag+1,str.length() - begintag + 1);
bool is_complete = (str.find_first_of("/") != std::string::npos);
XMLNode* node = read_node(str, cur_node);
if( !is_complete ) {
parent = node;
cur_node = node;
} else {
parent = node->parent();
}
}
}
}
if( doc->nodeCount() == 1 ) {
return doc->node(0);
}
return doc;
}
bool XMLDocument::write_doc(const XMLNode* node, const std::string& filename) {
std::ofstream file(filename);
if (!file) {
Logger::error() << tr("unable to open file for save: ") << filename << Logger::endl;
return false;
}
save_node(file, node, "");
return true;
}