My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
#include "Core.h"

NAMESPACE_UPP

#define LLOG(x) // LOG(x);

static inline void sDeXmlChar(StringBuffer& result, char chr, byte charset, bool escapelf)
{
/**/ if(chr == '<') result.Cat("&lt;");
else if(chr == '>') result.Cat("&gt;");
else if(chr == '&') result.Cat("&amp;");
else if(chr == '\'') result.Cat("&apos;");
else if(chr == '\"') result.Cat("&quot;");
else if((byte)chr < ' ' && (escapelf || chr != '\n' && chr != '\t' && chr != '\r'))
result.Cat(NFormat("&#x%02x;", (byte)chr));
else if(!(chr & 0x80) || charset == CHARSET_UTF8) result.Cat(chr);
else result.Cat(ToUtf8(ToUnicode(chr, charset)));
}

String DeXml(const char *s, byte charset, bool escapelf)
{
if(charset == CHARSET_DEFAULT)
charset = GetDefaultCharset();
StringBuffer result;
for(; *s; s++)
sDeXmlChar(result, *s, charset, escapelf);
return result;
}

String DeXml(const char *s, const char *end, byte charset, bool escapelf)
{
if(charset == CHARSET_DEFAULT)
charset = GetDefaultCharset();
StringBuffer result;
for(; s < end; s++)
sDeXmlChar(result, *s, charset, escapelf);
return result;
}

String DeXml(const String& s, byte charset, bool escapelf)
{
return DeXml(~s, s.End(), charset, escapelf);
}

String XmlHeader(const char *encoding, const char *version, const char *standalone)
{
StringBuffer r;
r << "<?xml version=\"" << version << "\" encoding=\"" << encoding << "\" standalone=\""
<< standalone << "\" ?>\r\n";
return r;
}

String XmlPI(const char *text)
{
StringBuffer r;
r << "<?" << text << "?>\r\n";
return r;
}

String XmlDecl(const char *text)
{
StringBuffer r;
r << "<!" << text << ">\r\n";
return r;
}

String XmlDocType(const char *text)
{
return XmlDecl("DOCTYPE " + String(text));
}

String XmlDoc(const char *name, const char *xmlbody)
{
return XmlHeader() + XmlDocType(name) + XmlTag(name)(xmlbody);
}

String XmlComment(const char *text)
{
StringBuffer out;
out << "<!-- " << text << " -->\r\n";
return out;
}

XmlTag& XmlTag::Tag(const char *s)
{
tag.Clear();
end.Clear();
tag << '<' << s;
end << "</" << s << ">\r\n";
return *this;
}

String XmlTag::operator()()
{
return tag + "/>\r\n";
}

String XmlTag::operator()(const char* text)
{
StringBuffer r;
r << tag << '>';
if(strchr(text, '\n') && strchr(text, '<')) {
r << "\r\n";
bool was = true;
for(;;) {
const char *s = text;
while(*s == ' ' || *s == '\t')
s++;
if(*s == '<') {
if(was)
r.Cat('\t');
was = true;
}
else
was = false;
r.Cat(text, s);
text = s;
while(*s && *s != '\n')
s++;
r.Cat(text, s);
if(*s == '\0')
break;
r.Cat('\n');
text = s + 1;
}
}
else
r << text;
r << end;
return r;
}

String XmlTag::Text(const char *text, byte charset)
{
StringBuffer r;
return r << tag << '>' << DeXml(text, charset) << end;
}

String XmlTag::PreservedText(const char *text, byte charset)
{
StringBuffer r;
return r << tag << " xml:spaces=\"preserve\">" << DeXml(text, charset) << end;
}

XmlTag& XmlTag::operator()(const char *attr, const char *val)
{
tag << ' ' << attr << "=\"" << DeXml(val, CHARSET_DEFAULT, true) << "\"";
return *this;
}

XmlTag& XmlTag::operator()(const char *attr, int q)
{
return operator()(attr, AsString(q));
}

XmlTag& XmlTag::operator()(const char *attr, double q)
{
return operator()(attr, AsString(q));
}

void XmlParser::Ent(StringBuffer& out)
{
int outconst = 0;
const char *t = ++term;
if(*t == '#') {
if(*++t == 'X' || *t == 'x') {
for(byte c; (c = ctoi(*++t)) < 16; outconst = 16 * outconst + c)
;
}
else {
while(IsDigit(*t))
outconst = 10 * outconst + *t++ - '0';
}
out.Cat(ToUtf8(outconst));
if(*t == ';')
t++;
term = t;
return;
}
if(t[0] == 'l' && t[1] == 't' && t[2] == ';') {
t += 3;
out.Cat('<');
}
else
if(t[0] == 'g' && t[1] == 't' && t[2] == ';') {
t += 3;
out.Cat('>');
}
else
if(t[0] == 'a')
if(t[1] == 'm' && t[2] == 'p' && t[3] == ';') {
t += 4;
out.Cat('&');
}
else
if(t[1] == 'p' && t[2] == 'o' && t[3] == 's' && t[4] == ';') {
t += 5;
out.Cat('\'');
}
else
out.Cat('&');
else
if(t[0] == 'q' && t[1] == 'u' && t[2] == 'o' && t[3] == 't' && t[4] == ';') {
t += 5;
out.Cat('\"');
}
else
out.Cat('&');
term = t;
}

inline static bool IsXmlNameChar(int c)
{
return IsAlNum(c) || c == '.' || c == '-' || c == '_' || c == ':';
}

void XmlParser::SkipWhites()
{
while(*term && (byte)*term <= ' ') {
if(*term == '\n')
line++;
term++;
}
}

void XmlParser::ReadAttr(StringBuffer& attrval, int c)
{
term++;
while(*term && *term != c)
if(*term == '&')
Ent(attrval);
else {
const char *e = term;
while(*++e && *e != '&' && *e != c)
;
attrval.Cat(term, (int)(e - term));
term = e;
}
if(*term == c)
term++;
}

void XmlParser::Next()
{
nattr.Clear();
nattr1 = nattrval1 = Null;
if(empty_tag) {
empty_tag = false;
type = XML_END;
return;
}

type = Null;
StringBuffer raw_text;
for(;;) {
if(*term == '\0') {
type = XML_EOF;
return;
}
if(*term == '<') {
if(term[1] == '!' && term[2] == '[' &&
term[3] == 'C' && term[4] == 'D' && term[5] == 'A' && term[6] == 'T' && term[7] == 'A' &&
term[8] == '[') { // ![CDATA[
term += 9;
for(;;) {
if(*term == '\0')
throw XmlError("Unterminated CDATA");
if(term[0] == ']' && term[1] == ']' && term[2] == '>') { // ]]>
term += 3;
break;
}
if(*term == '\n')
line++;
raw_text.Cat(*term++);
}
type = XML_TEXT;
continue;
}
else
break;
}
if(*term == '\n')
line++;
if(*term == '&') {
Ent(raw_text);
type = XML_TEXT;
}
else {
if((byte)*term > ' ')
type = XML_TEXT;
raw_text.Cat(*term++);
}
}
cdata = FromUtf8(String(raw_text)).ToString();

if(cdata.GetCount() && (npreserve || preserveall))
type = XML_TEXT;

if(type == XML_TEXT)
return;

term++;
if(*term == '!') {
type = XML_DECL;
term++;
if(term[0] == '-' && term[1] == '-') {
type = XML_COMMENT;
term += 2;
for(;;) {
if(term[0] == '-' && term[1] == '-' && term[2] == '>')
break;
if(*term == '\0')
throw XmlError("Unterminated comment");
if(*term == '\n')
line++;
tagtext.Cat(*term++);
}
term += 3;
return;
}
bool intdt = false;
for(;;) {
if (*term == '[')
intdt = true;
if(*term == '>' && intdt == false) {
term++;
break;
}
if(intdt == true && term[0] == ']' && term[1] == '>') {
tagtext.Cat(*term++);
term++;
break;
}
if(*term == '\0')
throw XmlError("Unterminated declaration");
if(*term == '\n')
line++;
tagtext.Cat(*term++);
}
}
else
if(*term == '?') {
type = XML_PI;
term++;
for(;;) {
if(term[0] == '?' && term[1] == '>') {
term += 2;
return;
}
if(*term == '\0')
throw XmlError("Unterminated processing info");
if(*term == '\n')
line++;
tagtext.Cat(*term++);
}
}
else
if(*term == '/') {
type = XML_END;
term++;
const char *t = term;
while(IsXmlNameChar(*term))
term++;
tagtext = String(t, term);
if(*term != '>')
throw XmlError("Unterminated end-tag");
term++;
}
else {
type = XML_TAG;
const char *t = term;
while(IsXmlNameChar(*term))
term++;
tagtext = String(t, term);
for(;;) {
SkipWhites();
if(*term == '>') {
term++;
break;
}
if(term[0] == '/' && term[1] == '>') {
cdata.Clear();
empty_tag = true;
term += 2;
break;
}
if(*term == '\0')
throw XmlError("Unterminated tag");
const char *t = term++;
while((byte)*term > ' ' && *term != '=' && *term != '>')
term++;
String attr(t, term);
SkipWhites();
if(*term == '=') {
term++;
SkipWhites();
StringBuffer attrval;
if(*term == '\"')
ReadAttr(attrval, '\"');
else
if(*term == '\'')
ReadAttr(attrval, '\'');
else
while((byte)*term > ' ' && *term != '>' && *term != '/')
if(*term == '&')
Ent(attrval);
else {
const char *e = term;
while((byte)*++e > ' ' && *e != '>' && *e != '&')
;
attrval.Cat(term,(int) (e - term));
term = e;
}
if(attr == "xml:space" && attrval.GetLength() == 8 && !memcmp(~attrval, "preserve", 8))
npreserve = true;
String aval = FromUtf8(~attrval, attrval.GetLength()).ToString();
if(IsNull(nattr1)) {
nattr1 = attr;
nattrval1 = aval;
}
else
nattr.Add(attr, aval);
}
}
}
}

bool XmlParser::IsEof()
{
return type == XML_EOF;
}

bool XmlParser::IsTag()
{
return type == XML_TAG;
}

String XmlParser::ReadTag(bool next)
{
if(type != XML_TAG)
throw XmlError("Expected tag");
LLOG("ReadTag " << text);
String h = tagtext;
if(next) {
stack.Add(Nesting(h, npreserve));
attr = nattr;
attr1 = nattr1;
attrval1 = nattrval1;
Next();
}
return h;
}

bool XmlParser::Tag(const char *tag)
{
if(IsTag() && tagtext == tag) {
LLOG("Tag " << tagtext);
stack.Add(Nesting(tagtext, npreserve));
attr = nattr;
attr1 = nattr1;
attrval1 = nattrval1;
Next();
return true;
}
return false;
}

bool XmlParser::Tag(const String& tag)
{
if(IsTag() && tagtext == tag) {
LLOG("Tag " << tagtext);
stack.Add(Nesting(tagtext, npreserve));
attr = nattr;
attr1 = nattr1;
attrval1 = nattrval1;
Next();
return true;
}
return false;
}

void XmlParser::PassTag(const char *tag)
{
if(!Tag(tag))
throw XmlError(String().Cat() << '\'' << tag << "'\' tag expected");
}

void XmlParser::PassTag(const String& tag)
{
if(!Tag(tag))
throw XmlError(String().Cat() << '\'' << tag << "'\' tag expected");
}

bool XmlParser::IsEnd()
{
return type == XML_END;
}

bool XmlParser::End()
{
if(IsEof())
throw XmlError("Unexpected end of file");
if(IsEnd()) {
LLOG("EndTag " << text);
if(stack.IsEmpty())
throw XmlError(NFormat("Unexpected end-tag: </%s>", tagtext));
if(stack.Top().tag != tagtext && !relaxed) {
LLOG("Tag/end-tag mismatch: <" << stack.Top().tag << "> </" << tagtext << ">");
throw XmlError(NFormat("Tag/end-tag mismatch: <%s> </%s>", stack.Top().tag, tagtext));
}
stack.Drop();
npreserve = (!stack.IsEmpty() && stack.Top().preserve_blanks);
Next();
return true;
}
return false;
}

void XmlParser::PassEnd()
{
if(!End())
throw XmlError(String().Cat() << "Expected \'" << (stack.GetCount() ? stack.Top().tag : String()) << "\' end-tag");
}

bool XmlParser::TagE(const char *tag)
{
if(Tag(tag)) {
SkipEnd();
return true;
}
return false;
}

void XmlParser::PassTagE(const char *tag)
{
PassTag(tag);
SkipEnd();
}

VectorMap<String, String> XmlParser::PickAttrs() pick_
{
if(!IsNull(attr1))
const_cast<VectorMap<String, String>&>(attr).Insert(0, attr1, attrval1);
return attr;
}

int XmlParser::Int(const char *id, int def) const
{
if(id == attr1) return ScanInt(attrval1);
int q = attr.Find(id);
return q < 0 ? def : ScanInt(attr[q]);
}

double XmlParser::Double(const char *id, double def) const
{
if(id == attr1) return ScanDouble(attrval1);
int q = attr.Find(id);
return q < 0 ? def : ScanDouble(attr[q]);
}

bool XmlParser::IsText()
{
if(npreserve || preserveall)
return cdata.GetCount();
const char *e = cdata.End();
for(const char *s = cdata.Begin(); s < e; s++)
if((byte)*s > ' ')
return true;
return false;
}

String XmlParser::ReadText()
{
String h = cdata;
cdata.Clear();
if(type == XML_TEXT)
Next();
return h;
}

String XmlParser::ReadTextE()
{
StringBuffer out;
for(;;) {
String t = ReadText();
if(!IsNull(t))
out.Cat(t);
else if(IsEnd()) {
PassEnd();
return out;
}
else
Skip();
}
}

bool XmlParser::IsDecl()
{
return type == XML_DECL;
}

String XmlParser::ReadDecl(bool next)
{
if(!IsDecl())
throw XmlError("Declaration expected");
String h = tagtext;
if(next)
Next();
return h;
}

bool XmlParser::IsPI()
{
return type == XML_PI;
}

String XmlParser::ReadPI(bool next)
{
if(!IsPI())
throw XmlError("Processing info expected");
String h = tagtext;
if(next)
Next();
return h;
}

bool XmlParser::IsComment()
{
return type == XML_COMMENT;
}

String XmlParser::ReadComment(bool next)
{
if(!IsComment())
throw XmlError("Comment expected");
String h = tagtext;
if(next)
Next();
return h;
}

void XmlParser::Skip()
{
if(IsEof())
throw XmlError("Unexpected end of file");
if(cdata.GetCount() && type != XML_TEXT)
cdata.Clear();
else
if(IsTag()) {
String n = ReadTag();
while(!End()) {
if(IsEof())
throw XmlError("Unexpected end of file expected when skipping tag \'" + n + "\'");
Skip();
}
}
else
Next();
}

void XmlParser::SkipEnd()
{
while(!IsEnd()) Skip();
PassEnd();
}

int XmlParser::GetColumn() const
{
const char *s = term;
int n = 1;
while(s > begin && *(s - 1) != '\n') {
s--;
n++;
}
return n;
}

XmlParser::XmlParser(const char *s)
{
relaxed = false;
empty_tag = false;
npreserve = false;
begin = term = s;
line = 1;
preserveall = false;
Next();
}

int XmlNode::FindTag(const char *_tag) const
{
String tag = _tag;
for(int i = 0; i < node.GetCount(); i++)
if(node[i].type == XML_TAG && node[i].text == tag)
return i;
return -1;
}

XmlNode& XmlNode::Add(const char *tag)
{
XmlNode& m = node.Add();
m.CreateTag(tag);
return m;
}

XmlNode& XmlNode::GetAdd(const char *tag)
{
int q = FindTag(tag);
return q >= 0 ? node[q] : Add(tag);
}

const XmlNode& XmlNode::Void()
{
static XmlNode *h;
ONCELOCK {
static XmlNode empty;
h = &empty;
}
return *h;
}

const XmlNode& XmlNode::operator[](const char *tag) const
{
int q = FindTag(tag);
return q < 0 ? Void() : node[q];
}

void XmlNode::Remove(const char *tag)
{
int q = FindTag(tag);
if(q >= 0)
node.Remove(q);
}

String XmlNode::GatherText() const
{
String r;
for(int i = 0; i < GetCount(); i++)
if(node[i].IsText())
r << node[i].GetText();
return r;
}

int XmlNode::AttrInt(const char *id, int def) const
{
String x = Attr(id);
CParser p(x);
return p.IsInt() ? p.ReadInt() : def;
}

XmlNode& XmlNode::SetAttr(const char *id, int i)
{
SetAttr(id, AsString(i));
return *this;
}

XmlNode& XmlNode::SetAttr(const char *id, const String& text)
{
attr.GetAdd(id) = text;
return *this;
}

bool Ignore(XmlParser& p, dword style)
{
if((XML_IGNORE_DECLS & style) && p.IsDecl() ||
(XML_IGNORE_PIS & style) && p.IsPI() ||
(XML_IGNORE_COMMENTS & style) && p.IsComment()) {
p.Skip();
return true;
}
return false;
}

static XmlNode sReadXmlNode(XmlParser& p, dword style)
{
XmlNode m;
if(p.IsTag()) {
m.CreateTag(p.ReadTag());
m.SetAttrsPick(p.PickAttrs());
while(!p.End())
if(!Ignore(p, style))
m.Add() = sReadXmlNode(p, style);
return m;
}
if(p.IsPI()) {
m.CreatePI(p.ReadPI());
return m;
}
if(p.IsDecl()) {
m.CreateDecl(p.ReadDecl());
return m;
}
if(p.IsComment()) {
m.CreateComment(p.ReadComment());
return m;
}
m.CreateText(p.ReadText());
return m;
}

XmlNode ParseXML(XmlParser& p, dword style)
{
XmlNode r;
while(!p.IsEof())
if(!Ignore(p, style))
r.Add() = sReadXmlNode(p, style);
return r;
}

XmlNode ParseXML(const char *s, dword style)
{
XmlParser p(s);
return ParseXML(p, style);
}

bool ShouldPreserve(const String& s)
{
if(*s == ' ' || *s == '\t' || *s == '\n')
return true;
const char *l = s.Last();
if(*l == ' ' || *l == '\t' || *l == '\n')
return true;
l = s.End();
for(const char *x = s; x < l; x++)
if(*x == '\t' || *x == '\n' || *x == ' ' && x[1] == ' ')
return true;
return false;
}

String AsXML(const XmlNode& node, dword style)
{
StringBuffer r;
if(style & XML_HEADER)
r << XmlHeader();
if(style & XML_DOCTYPE)
for(int i = 0; i < node.GetCount(); i++) {
const XmlNode& m = node.Node(i);
if(m.GetType() == XML_TAG) {
r << XmlDocType(m.GetText());
break;
}
}
style &= ~(XML_HEADER|XML_DOCTYPE);
switch(node.GetType()) {
case XML_PI:
r << "<?" << node.GetText() << "?>\r\n";
break;
case XML_DECL:
r << "<!" << node.GetText() << ">\r\n";
break;
case XML_COMMENT:
r << "<!--" << node.GetText() << "-->\r\n";
break;
case XML_DOC:
for(int i = 0; i < node.GetCount(); i++)
r << AsXML(node.Node(i), style);
break;
case XML_TEXT:
r << DeXml(node.GetText());
break;
case XML_TAG:
XmlTag tag(node.GetText());
for(int i = 0; i < node.GetAttrCount(); i++)
tag(node.AttrId(i), node.Attr(i));
if(node.GetCount()) {
StringBuffer body;
for(int i = 0; i < node.GetCount(); i++)
body << AsXML(node.Node(i), style);
r << tag(~body);
}
else
r << tag();
}
return r;
}

END_UPP_NAMESPACE

Change log

r4511 by rylek on Feb 2, 2012   Diff
.Core/XML.cpp: bug fix - cdata gets
cleared inside content-less tag to prevent
ReadText from returning nonexistent text
content
Go to: 
Project members, sign in to write a code review

Older revisions

r4151 by cxl on Nov 8, 2011   Diff
Core: Fixed ParseXML issue with
nonsignificant texts
r3777 by rylek on Aug 21, 2011   Diff
.plugin/sqlite: fixed conversion of
statement to UTF8
.Core/XML.cpp: ReadTextE updated to
reflect modified IsText / ReadText
logic
...
r2972 by cxl on Jan 8, 2011   Diff
*uppsrc: mr_ped's GCC warnings fix,
Core: now methods in Thread based on
tojocky's ideas
All revisions of this file

File info

Size: 16533 bytes, 876 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting