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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
|
#include "XMLHandler.h"
#include <algorithm>
#include <expat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define NS_SEPARATOR 1
#define MORE_INDENT " "
static string
xml_text_escape(const string& s)
{
string result;
const size_t N = s.length();
for (size_t i=0; i<N; i++) {
char c = s[i];
switch (c) {
case '<':
result += "<";
break;
case '>':
result += ">";
break;
case '&':
result += "&";
break;
default:
result += c;
break;
}
}
return result;
}
static string
xml_attr_escape(const string& s)
{
string result;
const size_t N = s.length();
for (size_t i=0; i<N; i++) {
char c = s[i];
switch (c) {
case '\"':
result += """;
break;
default:
result += c;
break;
}
}
return result;
}
XMLNamespaceMap::XMLNamespaceMap()
{
}
XMLNamespaceMap::XMLNamespaceMap(char const*const* nspaces)
{
while (*nspaces) {
m_map[nspaces[1]] = nspaces[0];
nspaces += 2;
}
}
string
XMLNamespaceMap::Get(const string& ns) const
{
if (ns == "xml") {
return ns;
}
map<string,string>::const_iterator it = m_map.find(ns);
if (it == m_map.end()) {
return "";
} else {
return it->second;
}
}
string
XMLNamespaceMap::GetPrefix(const string& ns) const
{
if (ns == "") {
return "";
}
map<string,string>::const_iterator it = m_map.find(ns);
if (it != m_map.end()) {
if (it->second == "") {
return "";
} else {
return it->second + ":";
}
} else {
return ":"; // invalid
}
}
void
XMLNamespaceMap::AddToAttributes(vector<XMLAttribute>* attrs) const
{
map<string,string>::const_iterator it;
for (it=m_map.begin(); it!=m_map.end(); it++) {
if (it->second == "xml") {
continue;
}
XMLAttribute attr;
if (it->second == "") {
attr.name = "xmlns";
} else {
attr.name = "xmlns:";
attr.name += it->second;
}
attr.value = it->first;
attrs->push_back(attr);
}
}
XMLAttribute::XMLAttribute()
{
}
XMLAttribute::XMLAttribute(const XMLAttribute& that)
:ns(that.ns),
name(that.name),
value(that.value)
{
}
XMLAttribute::XMLAttribute(string n, string na, string v)
:ns(n),
name(na),
value(v)
{
}
XMLAttribute::~XMLAttribute()
{
}
int
XMLAttribute::Compare(const XMLAttribute& that) const
{
if (ns != that.ns) {
return ns < that.ns ? -1 : 1;
}
if (name != that.name) {
return name < that.name ? -1 : 1;
}
return 0;
}
string
XMLAttribute::Find(const vector<XMLAttribute>& list, const string& ns, const string& name,
const string& def)
{
const size_t N = list.size();
for (size_t i=0; i<N; i++) {
const XMLAttribute& attr = list[i];
if (attr.ns == ns && attr.name == name) {
return attr.value;
}
}
return def;
}
struct xml_handler_data {
vector<XMLHandler*> stack;
XML_Parser parser;
vector<vector<XMLAttribute>*> attributes;
string filename;
};
XMLNode::XMLNode()
{
}
XMLNode::~XMLNode()
{
// for_each(m_children.begin(), m_children.end(), delete_object<XMLNode>);
}
XMLNode*
XMLNode::Clone() const
{
switch (m_type) {
case ELEMENT: {
XMLNode* e = XMLNode::NewElement(m_pos, m_ns, m_name, m_attrs, m_pretty);
const size_t N = m_children.size();
for (size_t i=0; i<N; i++) {
e->m_children.push_back(m_children[i]->Clone());
}
return e;
}
case TEXT: {
return XMLNode::NewText(m_pos, m_text, m_pretty);
}
default:
return NULL;
}
}
XMLNode*
XMLNode::NewElement(const SourcePos& pos, const string& ns, const string& name,
const vector<XMLAttribute>& attrs, int pretty)
{
XMLNode* node = new XMLNode();
node->m_type = ELEMENT;
node->m_pretty = pretty;
node->m_pos = pos;
node->m_ns = ns;
node->m_name = name;
node->m_attrs = attrs;
return node;
}
XMLNode*
XMLNode::NewText(const SourcePos& pos, const string& text, int pretty)
{
XMLNode* node = new XMLNode();
node->m_type = TEXT;
node->m_pretty = pretty;
node->m_pos = pos;
node->m_text = text;
return node;
}
void
XMLNode::SetPrettyRecursive(int value)
{
m_pretty = value;
const size_t N = m_children.size();
for (size_t i=0; i<N; i++) {
m_children[i]->SetPrettyRecursive(value);
}
}
string
XMLNode::ContentsToString(const XMLNamespaceMap& nspaces) const
{
return contents_to_string(nspaces, "");
}
string
XMLNode::ToString(const XMLNamespaceMap& nspaces) const
{
return to_string(nspaces, "");
}
string
XMLNode::OpenTagToString(const XMLNamespaceMap& nspaces, int pretty) const
{
return open_tag_to_string(nspaces, "", pretty);
}
string
XMLNode::contents_to_string(const XMLNamespaceMap& nspaces, const string& indent) const
{
string result;
const size_t N = m_children.size();
for (size_t i=0; i<N; i++) {
const XMLNode* child = m_children[i];
switch (child->Type()) {
case ELEMENT:
if (m_pretty == PRETTY) {
result += '\n';
result += indent;
}
case TEXT:
result += child->to_string(nspaces, indent);
break;
}
}
return result;
}
string
trim_string(const string& str)
{
const char* p = str.c_str();
while (*p && isspace(*p)) {
p++;
}
const char* q = str.c_str() + str.length() - 1;
while (q > p && isspace(*q)) {
q--;
}
q++;
return string(p, q-p);
}
string
XMLNode::open_tag_to_string(const XMLNamespaceMap& nspaces, const string& indent, int pretty) const
{
if (m_type != ELEMENT) {
return "";
}
string result = "<";
result += nspaces.GetPrefix(m_ns);
result += m_name;
vector<XMLAttribute> attrs = m_attrs;
sort(attrs.begin(), attrs.end());
const size_t N = attrs.size();
for (size_t i=0; i<N; i++) {
const XMLAttribute& attr = attrs[i];
if (i == 0 || m_pretty == EXACT || pretty == EXACT) {
result += ' ';
}
else {
result += "\n";
result += indent;
result += MORE_INDENT;
result += MORE_INDENT;
}
result += nspaces.GetPrefix(attr.ns);
result += attr.name;
result += "=\"";
result += xml_attr_escape(attr.value);
result += '\"';
}
if (m_children.size() > 0) {
result += '>';
} else {
result += " />";
}
return result;
}
string
XMLNode::to_string(const XMLNamespaceMap& nspaces, const string& indent) const
{
switch (m_type)
{
case TEXT: {
if (m_pretty == EXACT) {
return xml_text_escape(m_text);
} else {
return xml_text_escape(trim_string(m_text));
}
}
case ELEMENT: {
string result = open_tag_to_string(nspaces, indent, PRETTY);
if (m_children.size() > 0) {
result += contents_to_string(nspaces, indent + MORE_INDENT);
if (m_pretty == PRETTY && m_children.size() > 0) {
result += '\n';
result += indent;
}
result += "</";
result += nspaces.GetPrefix(m_ns);
result += m_name;
result += '>';
}
return result;
}
default:
return "";
}
}
string
XMLNode::CollapseTextContents() const
{
if (m_type == TEXT) {
return m_text;
}
else if (m_type == ELEMENT) {
string result;
const size_t N=m_children.size();
for (size_t i=0; i<N; i++) {
result += m_children[i]->CollapseTextContents();
}
return result;
}
else {
return "";
}
}
vector<XMLNode*>
XMLNode::GetElementsByName(const string& ns, const string& name) const
{
vector<XMLNode*> result;
const size_t N=m_children.size();
for (size_t i=0; i<N; i++) {
XMLNode* child = m_children[i];
if (child->m_type == ELEMENT && child->m_ns == ns && child->m_name == name) {
result.push_back(child);
}
}
return result;
}
XMLNode*
XMLNode::GetElementByNameAt(const string& ns, const string& name, size_t index) const
{
vector<XMLNode*> result;
const size_t N=m_children.size();
for (size_t i=0; i<N; i++) {
XMLNode* child = m_children[i];
if (child->m_type == ELEMENT && child->m_ns == ns && child->m_name == name) {
if (index == 0) {
return child;
} else {
index--;
}
}
}
return NULL;
}
size_t
XMLNode::CountElementsByName(const string& ns, const string& name) const
{
size_t result = 0;
const size_t N=m_children.size();
for (size_t i=0; i<N; i++) {
XMLNode* child = m_children[i];
if (child->m_type == ELEMENT && child->m_ns == ns && child->m_name == name) {
result++;
}
}
return result;
}
string
XMLNode::GetAttribute(const string& ns, const string& name, const string& def) const
{
return XMLAttribute::Find(m_attrs, ns, name, def);
}
static void
parse_namespace(const char* data, string* ns, string* name)
{
const char* p = strchr(data, NS_SEPARATOR);
if (p != NULL) {
ns->assign(data, p-data);
name->assign(p+1);
} else {
ns->assign("");
name->assign(data);
}
}
static void
convert_attrs(const char** in, vector<XMLAttribute>* out)
{
while (*in) {
XMLAttribute attr;
parse_namespace(in[0], &attr.ns, &attr.name);
attr.value = in[1];
out->push_back(attr);
in += 2;
}
}
static bool
list_contains(const vector<XMLHandler*>& stack, XMLHandler* handler)
{
const size_t N = stack.size();
for (size_t i=0; i<N; i++) {
if (stack[i] == handler) {
return true;
}
}
return false;
}
static void XMLCALL
start_element_handler(void *userData, const char *name, const char **attrs)
{
xml_handler_data* data = (xml_handler_data*)userData;
XMLHandler* handler = data->stack[data->stack.size()-1];
SourcePos pos(data->filename, (int)XML_GetCurrentLineNumber(data->parser));
string nsString;
string nameString;
XMLHandler* next = handler;
vector<XMLAttribute> attributes;
parse_namespace(name, &nsString, &nameString);
convert_attrs(attrs, &attributes);
handler->OnStartElement(pos, nsString, nameString, attributes, &next);
if (next == NULL) {
next = handler;
}
if (next != handler) {
next->elementPos = pos;
next->elementNamespace = nsString;
next->elementName = nameString;
next->elementAttributes = attributes;
}
data->stack.push_back(next);
}
static void XMLCALL
end_element_handler(void *userData, const char *name)
{
xml_handler_data* data = (xml_handler_data*)userData;
XMLHandler* handler = data->stack[data->stack.size()-1];
data->stack.pop_back();
SourcePos pos(data->filename, (int)XML_GetCurrentLineNumber(data->parser));
if (!list_contains(data->stack, handler)) {
handler->OnDone(pos);
if (data->stack.size() > 1) {
// not top one
delete handler;
}
}
handler = data->stack[data->stack.size()-1];
string nsString;
string nameString;
parse_namespace(name, &nsString, &nameString);
handler->OnEndElement(pos, nsString, nameString);
}
static void XMLCALL
text_handler(void *userData, const XML_Char *s, int len)
{
xml_handler_data* data = (xml_handler_data*)userData;
XMLHandler* handler = data->stack[data->stack.size()-1];
SourcePos pos(data->filename, (int)XML_GetCurrentLineNumber(data->parser));
handler->OnText(pos, string(s, len));
}
static void XMLCALL
comment_handler(void *userData, const char *comment)
{
xml_handler_data* data = (xml_handler_data*)userData;
XMLHandler* handler = data->stack[data->stack.size()-1];
SourcePos pos(data->filename, (int)XML_GetCurrentLineNumber(data->parser));
handler->OnComment(pos, string(comment));
}
bool
XMLHandler::ParseFile(const string& filename, XMLHandler* handler)
{
char buf[16384];
int fd = open(filename.c_str(), O_RDONLY);
if (fd < 0) {
SourcePos(filename, -1).Error("Unable to open file for read: %s", strerror(errno));
return false;
}
XML_Parser parser = XML_ParserCreateNS(NULL, NS_SEPARATOR);
xml_handler_data state;
state.stack.push_back(handler);
state.parser = parser;
state.filename = filename;
XML_SetUserData(parser, &state);
XML_SetElementHandler(parser, start_element_handler, end_element_handler);
XML_SetCharacterDataHandler(parser, text_handler);
XML_SetCommentHandler(parser, comment_handler);
ssize_t len;
bool done;
do {
len = read(fd, buf, sizeof(buf));
done = len < (ssize_t)sizeof(buf);
if (len < 0) {
SourcePos(filename, -1).Error("Error reading file: %s\n", strerror(errno));
close(fd);
return false;
}
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
SourcePos(filename, (int)XML_GetCurrentLineNumber(parser)).Error(
"Error parsing XML: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
close(fd);
return false;
}
} while (!done);
XML_ParserFree(parser);
close(fd);
return true;
}
bool
XMLHandler::ParseString(const string& filename, const string& text, XMLHandler* handler)
{
XML_Parser parser = XML_ParserCreateNS(NULL, NS_SEPARATOR);
xml_handler_data state;
state.stack.push_back(handler);
state.parser = parser;
state.filename = filename;
XML_SetUserData(parser, &state);
XML_SetElementHandler(parser, start_element_handler, end_element_handler);
XML_SetCharacterDataHandler(parser, text_handler);
XML_SetCommentHandler(parser, comment_handler);
if (XML_Parse(parser, text.c_str(), text.size(), true) == XML_STATUS_ERROR) {
SourcePos(filename, (int)XML_GetCurrentLineNumber(parser)).Error(
"Error parsing XML: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
return false;
}
XML_ParserFree(parser);
return true;
}
XMLHandler::XMLHandler()
{
}
XMLHandler::~XMLHandler()
{
}
int
XMLHandler::OnStartElement(const SourcePos& pos, const string& ns, const string& name,
const vector<XMLAttribute>& attrs, XMLHandler** next)
{
return 0;
}
int
XMLHandler::OnEndElement(const SourcePos& pos, const string& ns, const string& name)
{
return 0;
}
int
XMLHandler::OnText(const SourcePos& pos, const string& text)
{
return 0;
}
int
XMLHandler::OnComment(const SourcePos& pos, const string& text)
{
return 0;
}
int
XMLHandler::OnDone(const SourcePos& pos)
{
return 0;
}
TopElementHandler::TopElementHandler(const string& ns, const string& name, XMLHandler* next)
:m_ns(ns),
m_name(name),
m_next(next)
{
}
int
TopElementHandler::OnStartElement(const SourcePos& pos, const string& ns, const string& name,
const vector<XMLAttribute>& attrs, XMLHandler** next)
{
*next = m_next;
return 0;
}
int
TopElementHandler::OnEndElement(const SourcePos& pos, const string& ns, const string& name)
{
return 0;
}
int
TopElementHandler::OnText(const SourcePos& pos, const string& text)
{
return 0;
}
int
TopElementHandler::OnDone(const SourcePos& pos)
{
return 0;
}
NodeHandler::NodeHandler(XMLNode* root, int pretty)
:m_root(root),
m_pretty(pretty)
{
if (root != NULL) {
m_nodes.push_back(root);
}
}
NodeHandler::~NodeHandler()
{
}
int
NodeHandler::OnStartElement(const SourcePos& pos, const string& ns, const string& name,
const vector<XMLAttribute>& attrs, XMLHandler** next)
{
int pretty;
if (XMLAttribute::Find(attrs, XMLNS_XMLNS, "space", "") == "preserve") {
pretty = XMLNode::EXACT;
} else {
if (m_root == NULL) {
pretty = m_pretty;
} else {
pretty = m_nodes[m_nodes.size()-1]->Pretty();
}
}
XMLNode* n = XMLNode::NewElement(pos, ns, name, attrs, pretty);
if (m_root == NULL) {
m_root = n;
} else {
m_nodes[m_nodes.size()-1]->EditChildren().push_back(n);
}
m_nodes.push_back(n);
return 0;
}
int
NodeHandler::OnEndElement(const SourcePos& pos, const string& ns, const string& name)
{
m_nodes.pop_back();
return 0;
}
int
NodeHandler::OnText(const SourcePos& pos, const string& text)
{
if (m_root == NULL) {
return 1;
}
XMLNode* n = XMLNode::NewText(pos, text, m_nodes[m_nodes.size()-1]->Pretty());
m_nodes[m_nodes.size()-1]->EditChildren().push_back(n);
return 0;
}
int
NodeHandler::OnComment(const SourcePos& pos, const string& text)
{
return 0;
}
int
NodeHandler::OnDone(const SourcePos& pos)
{
return 0;
}
XMLNode*
NodeHandler::ParseFile(const string& filename, int pretty)
{
NodeHandler handler(NULL, pretty);
if (!XMLHandler::ParseFile(filename, &handler)) {
fprintf(stderr, "error parsing file: %s\n", filename.c_str());
return NULL;
}
return handler.Root();
}
XMLNode*
NodeHandler::ParseString(const string& filename, const string& text, int pretty)
{
NodeHandler handler(NULL, pretty);
if (!XMLHandler::ParseString(filename, text, &handler)) {
fprintf(stderr, "error parsing file: %s\n", filename.c_str());
return NULL;
}
return handler.Root();
}
|