-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlRep.cpp
430 lines (379 loc) · 11.5 KB
/
xmlRep.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "xmlRep.h"
#ifdef TIMER
#include <chrono>
#endif
#include <iostream>
using namespace std;
#ifdef TIMER
double xmlNode::getNextTagTimer = 0.0;
double xmlNode::getNextTagGroupTimer = 0.0;
#endif
// note, will return only the first child index that matches!
int xmlNode::getChildIndex(const string& childName, int strict) const {
int index = -1;
for (int i = 0; i < children.size(); i++) {
if (children[i].name == childName) {
index = i;
}
}
if (strict != 0 && index < 0) {
cout << "In xmlNode with name: " << name << ", could not find index for child with name: " << childName << endl;
exit(1);
}
return index;
}
int xmlNode::getAttributeIndex(const string& attrName, int strict) const {
int index = -1;
for (int i = 0; i < attributes.size(); i++) {
if (attributes[i].name == attrName) {
index = i;
}
}
if (strict != 0 && index < 0) {
cout << "In xmlNode with name: " << name << ", could not find index for attribute with name: " << attrName << endl;
exit(1);
}
return index;
}
xmlNode::xmlNode(stringstream& ss) {
stripcomments(ss);
string tag = getNextTag(ss, 0);
string tagGroup = getNextTagGroup(ss, 0);
handleTagString(tag, *this);
valInline = 1;
value = getTextElement(tagGroup);
const int numElements = numSubGroups(tagGroup);
if (numElements > 0) {
valInline = 0;
getNextTag(ss, 1);
for (int i = 0; i < numElements; i++) {
string subElement = getNextTagGroup(ss, 1);
stringstream ss2(subElement);
children.push_back(xmlNode(ss2));
}
}
}
xmlNode::xmlNode(const xmlNode& c) {
isSelfClosing = c.isSelfClosing;
name = c.name;
for (int i = 0; i < c.attributes.size(); i++) {
attributes.push_back(c.attributes[i]);
}
valInline = c.valInline;
value = c.value;
for (int i = 0; i < c.children.size(); i++) {
children.push_back(c.children[i]);
}
}
xmlNode& xmlNode::operator=(const xmlNode& c) {
isSelfClosing = c.isSelfClosing;
name = c.name;
for (int i = 0; i < c.attributes.size(); i++) {
attributes.push_back(c.attributes[i]);
}
valInline = c.valInline;
value = c.value;
for (int i = 0; i < c.children.size(); i++) {
children.push_back(c.children[i]);
}
return *this;
}
void xmlNode::write(ostream& os, int indentLevel) const {
string str = getString(indentLevel);
os << str;
}
string xmlNode::getString(int indentLevel) const {
stringstream ss;
ss << getInStr(indentLevel);
ss << "<" << name;
for (int i = 0; i < attributes.size(); i++) {
ss << " " << attributes[i].name << "=\"" << attributes[i].value << "\"";
}
if (isSelfClosing == 1) {
ss << "/>" << endl;
return ss.str();
} else {
ss << ">";
}
if (valInline == 1) {
ss << value << "</" << name << ">" << endl;
} else {
ss << endl;
for (int i = 0; i < children.size(); i++) {
ss << children[i].getString(indentLevel+2);
}
if(value.size() > 0) ss << getInStr(indentLevel+2) << value << endl;
ss << getInStr(indentLevel) << "</" << name << ">" << endl;
}
return ss.str();
}
// this doesn't work at all
// what if there are no xml comments but there is an xml declaration (currently nothing happens)
//
void xmlNode::stripcomments(stringstream& ss) const {
stringstream ss2;
const string contents = stripXmlDeclaration(ss);
ss.str(contents);
int inComment = 0;
int commentStart = -1;
int commentEnd = 0;
while (contents.find("<!--",commentEnd) != string::npos) {
commentStart = contents.find("<!--",commentEnd);
ss2 << contents.substr(commentEnd, commentStart-commentEnd);
commentEnd = contents.find("-->",commentStart);
}
if (commentStart == -1) {
return;
} else {
ss2 << contents.substr(commentEnd+3, contents.size()-commentEnd-3);
}
ss.str(ss2.str());
}
string xmlNode::stripXmlDeclaration(stringstream& ss) const {
if (ss.str().find("<?") == 0) {
getNextTag(ss, 1);
}
return ss.str();
}
int xmlNode::checkSelfClosing(const string& tagstr) const {
int retval = 0;
if (tagstr[tagstr.size()-2] == '/') {
retval = 1;
}
return retval;
}
int xmlNode::isEndingTag(const string& tagstr) const {
int retval = 0;
if (tagstr.size() > 1 && tagstr[1] == '/') {
retval = 1;
}
return retval;
}
string xmlNode::trimWhitespace(const string& str) const {
size_t first = str.find_first_not_of(' ');
if (string::npos == first) {
return string("");
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
size_t xmlNode::getPosNextLiveChar(const std::string& str, char c) const {
size_t index = string::npos;
if (str[0] == c) {
index = 0;
} else {
for(int i = 1; i < str.size(); i++) {
if (str[i] == c && str[i-1] != '\\') {
index = i;
break;
}
}
}
return index;
}
int numQuotes(const string& str, int start, int stop) {
int counter = 0;
int quoteloc = str.find('\"', start);
while(quoteloc < stop && quoteloc != string::npos) {
if (quoteloc < 1 || str[quoteloc-1] != '\\') {
counter++;
}
quoteloc = str.find('\"', quoteloc+1);
}
return counter;
}
string xmlNode::getNextTag(stringstream& ss, int removeFromSS) const {
#ifdef TIMER
auto begin = std::chrono::high_resolution_clock::now();
#endif
char prev;
int start = 0;
int stop = 0;
int inquote = 0;
stringstream result;
int length = ss.str().size();
int closeCaretFound = 0;
int closeCaretLoc = -1;
int openCaretFound = 0;
int lastCaretLoc = 0;
int openCaretLoc = ss.str().find("<");
int quotesFound = 0;
while (openCaretFound == 0 && openCaretLoc != string::npos) {
quotesFound += numQuotes(ss.str(), lastCaretLoc, openCaretLoc);
// need number of quotes found before this caret to be even,
// otherwise we are in a quote and should skip this one
// also check that the previous character is not an \,
// otherwise the caret is escaped and should be skipped
if (quotesFound % 2 == 0 && (openCaretLoc == 0 || ss.str()[openCaretLoc-1] != '\\')) {
openCaretFound = 1;
lastCaretLoc = openCaretLoc;
closeCaretLoc = ss.str().find(">", lastCaretLoc+1);
// need total number of quotes found before this caret to be even,
// otherwise we are in a quote and should skip this one
// also check that the previous character is not an \,
// otherwise the caret is escaped and should be skipped
while (closeCaretFound == 0 && closeCaretLoc != string::npos) {
quotesFound += numQuotes(ss.str(), lastCaretLoc, closeCaretLoc);
if (quotesFound % 2 == 0 && ss.str()[closeCaretLoc-1] != '\\') {
closeCaretFound = 1;
} else {
lastCaretLoc = closeCaretLoc;
closeCaretLoc = ss.str().find(">", lastCaretLoc+1);
}
}
} else {
lastCaretLoc = openCaretLoc;
openCaretLoc = ss.str().find("<", lastCaretLoc+1);
}
}
string nextTag;
if (closeCaretFound && openCaretFound) {
nextTag = ss.str().substr(openCaretLoc, closeCaretLoc-openCaretLoc+1);
}
if (removeFromSS == 1) {
string temp = ss.str();
temp = temp.substr(closeCaretLoc+1);
ss.str(temp);
}
#ifdef TIMER
auto end = std::chrono::high_resolution_clock::now();
getNextTagTimer += std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count();
#endif
return nextTag;
}
// Gets the whole tag from starting to ending, including everything in between
string xmlNode::getNextTagGroup(stringstream& ss, int removeFromSS) const {
#ifdef TIMER
auto begin = std::chrono::high_resolution_clock::now();
#endif
string retstr;
string openingTag = getNextTag(ss, 0);
int startPos = ss.str().find(openingTag);
int charactersRemoved = 0;
stringstream remainder;
// if it's self closing, we're done
if (checkSelfClosing(openingTag)) {
retstr = openingTag;
charactersRemoved = retstr.size();
} else {
// search for the closing tag
string name = getTagName(openingTag);
int level = 1;
remainder.str(ss.str());
getNextTag(remainder, 1);
charactersRemoved += openingTag.size();
while(level > 0) {
string nextTag = getNextTag(remainder, 0);
charactersRemoved += remainder.str().find(nextTag) + nextTag.size();
getNextTag(remainder, 1);
if (checkSelfClosing(nextTag) == 0) {// && getTagName(nextTag) != name) {
if (isEndingTag(nextTag) == 1) {
level--;
} else {
level++;
}
}
}
retstr = ss.str().substr(startPos, charactersRemoved);
}
if (removeFromSS == 1) {
string temp = ss.str();
temp.erase(0, charactersRemoved+startPos);
ss.str(temp);
}
#ifdef TIMER
auto end = std::chrono::high_resolution_clock::now();
getNextTagGroupTimer += std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count();
#endif
return retstr;
}
string xmlNode::getTagName(const string& tagstr) const {
string tagName;
size_t spaceLoc = tagstr.find(" ");
size_t closeLoc = tagstr.find('>');
size_t slashLoc = string::npos;
if (checkSelfClosing(tagstr) == 1) {
slashLoc = tagstr.find("/");
}
int endChar = tagstr.size();
if (spaceLoc != string::npos) {
endChar = spaceLoc;
}
if (closeLoc < endChar && closeLoc != string::npos) {
endChar = closeLoc;
}
if (slashLoc < endChar && slashLoc != string::npos) {
endChar = slashLoc;
}
if (isEndingTag(tagstr) == 1) {
endChar -= 2;
tagName = tagstr.substr(2, endChar);
} else {
endChar -= 1;
tagName = tagstr.substr(1, endChar);
}
return tagName;
}
void xmlNode::getNextKeyVal(string& contentstr, string& key, string& val) const {
size_t breakone = getPosNextLiveChar(contentstr, '=');
key = contentstr.substr(0,breakone);
key = trimWhitespace(key);
contentstr = contentstr.substr(breakone+1);
size_t firstquote = getPosNextLiveChar(contentstr, '\"');
contentstr = contentstr.substr(firstquote+1);
size_t secondquote = getPosNextLiveChar(contentstr, '\"');
val = contentstr.substr(0,secondquote);
val = trimWhitespace(val);
contentstr = contentstr.substr(secondquote+1);
}
// creates all of the attributes
void xmlNode::handleTagString(const string& tagstr, xmlNode& node) const {
node.isSelfClosing = checkSelfClosing(tagstr);
node.name = getTagName(tagstr);
string decliningstring = tagstr.substr(tagstr.find(node.name)+node.name.size()+1);
int numToRemove = 1;
if (node.isSelfClosing == 1) {
numToRemove++;
}
decliningstring = decliningstring.substr(0,decliningstring.size()-numToRemove);
decliningstring = trimWhitespace(decliningstring);
while(decliningstring.size() > 1) {
attrpair att;
getNextKeyVal(decliningstring, att.name, att.value);
node.attributes.push_back(att);
}
}
int xmlNode::numSubGroups(const string& tagGroupStr) const {
stringstream ss(tagGroupStr);
getNextTag(ss, 1);
int numSubGroups = 0;
string nextTag = getNextTag(ss,0);
while(nextTag.size() != 0 && isEndingTag(nextTag) == 0) {
numSubGroups++;
getNextTagGroup(ss, 1);
nextTag = getNextTag(ss, 0);
}
return numSubGroups;
}
string xmlNode::getTextElement(const string& tagGroupStr) const {
string text("");
int nsg = numSubGroups(tagGroupStr);
stringstream ss(tagGroupStr);
getNextTag(ss,1);
string nextTag = getNextTag(ss,0);
string candidateText = ss.str().substr(0,ss.str().find(nextTag));
candidateText = trimWhitespace(candidateText);
if (candidateText.size() != 0) {
text = candidateText;
}
for (int i = 0; i < nsg; i++) {
getNextTagGroup(ss,1);
nextTag = getNextTag(ss,0);
candidateText = ss.str().substr(0,ss.str().find(nextTag));
candidateText = trimWhitespace(candidateText);
if (candidateText.size() != 0) {
text = candidateText;
}
}
return text;
}