-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.cpp
executable file
·1552 lines (1459 loc) · 46 KB
/
xml.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
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////
// Xml-Object-Saving
TStrStrH TXmlObjSer::TypeNmToTagNmH;
TStr TXmlObjSer::GetTagNm(const TStr& TypeNm){
TStr& XmlTagNm=TypeNmToTagNmH.AddDat(TypeNm);
if (XmlTagNm.Empty()){
TChA XmlTagChA=TypeNm;
for (int ChN=0; ChN<XmlTagChA.Len(); ChN++){
char Ch=XmlTagChA[ChN];
if (!(('A'<=Ch)&&(Ch<='Z')||('a'<=Ch)&&(Ch<='z')||('0'<=Ch)&&(Ch<='9'))){
XmlTagChA.PutCh(ChN, '_');
}
}
while ((XmlTagChA.Len()>0)&&(XmlTagChA.LastCh()=='_')){
XmlTagChA.Pop();}
XmlTagNm=XmlTagChA;
}
return XmlTagNm;
}
void TXmlObjSer::AssertXmlHd(
const PXmlTok& XmlTok, const TStr& Nm, const TStr& TypeNm){
// check if the token is full
EAssertR(!XmlTok.Empty(), "Xml-Token Empty");
// if name is empty then tag=type else tag=name
if (!Nm.Empty()){
// check if the token is tag
if (!XmlTok->IsTag()){
TStr ArgStr1="Expected: Tag";
TStr ArgStr2=TStr("Found: ")+XmlTok->GetSymStr();
TExcept::Throw("Invalid Xml-Token", ArgStr1, ArgStr2);
}
if (Nm!="-"){
// check if the tag is correct
if (!XmlTok->IsTag(Nm)){
TStr ArgStr1=TStr("Expected: ")+Nm;
TStr ArgStr2=TStr("Found: ")+XmlTok->GetStr();
TExcept::Throw("Invalid Xml-Tag", ArgStr1, ArgStr2);
}
// check if the type is correct
TStr TypeArgVal=XmlTok->GetStrArgVal("Type");
if (TypeArgVal!=TypeNm){
TStr ArgStr1=TStr("Expected: ")+TypeNm;
TStr ArgStr2=TStr("Found: ")+TypeArgVal;
TExcept::Throw("Invalid Xml-Type", ArgStr1, ArgStr2);
}
}
} else {
// check if the tag is correct
if (!XmlTok->IsTag(TypeNm)){
TStr ArgStr1=TStr("Expected: ")+TypeNm;
TStr ArgStr2=TStr("Found: ")+XmlTok->GetSymStr();
TExcept::Throw("Invalid Xml-Type-Tag", ArgStr1, ArgStr2);
}
}
}
bool TXmlObjSer::GetBoolArg(const PXmlTok& XmlTok, const TStr& Nm){
TStr ValStr;
if (XmlTok->IsArg(Nm, ValStr)){
bool Val;
if (ValStr.IsBool(Val)){
return Val;
} else {
TExcept::Throw("Invalid Xml-Argument Boolean-Value", Nm, ValStr);
}
} else {
TExcept::Throw("Xml-Argument Missing", Nm);
}
Fail; return 0;
}
int TXmlObjSer::GetIntArg(const PXmlTok& XmlTok, const TStr& Nm){
TStr ValStr;
if (XmlTok->IsArg(Nm, ValStr)){
int Val;
if (ValStr.IsInt(Val)){
return Val;
} else {
TExcept::Throw("Invalid Xml-Argument Integer-Value", Nm, ValStr);
}
} else {
TExcept::Throw("Xml-Argument Missing", Nm);
}
Fail; return 0;
}
int64 TXmlObjSer::GetInt64Arg(const PXmlTok& XmlTok, const TStr& Nm){
TStr ValStr;
if (XmlTok->IsArg(Nm, ValStr)){
int64 Val;
if (ValStr.IsInt64(Val)){
return Val;
} else {
TExcept::Throw("Invalid Xml-Argument Integer64-Value", Nm, ValStr);
}
} else {
TExcept::Throw("Xml-Argument Missing", Nm);
}
Fail; return 0;
}
double TXmlObjSer::GetFltArg(const PXmlTok& XmlTok, const TStr& Nm){
TStr ValStr;
if (XmlTok->IsArg(Nm, ValStr)){
double Val;
if (ValStr.IsFlt(Val)){
return Val;
} else {
TExcept::Throw("Invalid Xml-Argument Double-Value", Nm, ValStr);
}
} else {
TExcept::Throw("Xml-Argument Missing", Nm);
}
Fail; return 0;
}
/////////////////////////////////////////////////
// Xml-Object-Serialization-Tag-Name
TXmlObjSerTagNm::TXmlObjSerTagNm(
TSOut& _SOut, const bool& ETagP,
const TStr& Nm, const TStr& TypeNm,
const TStr& ArgNm, const TStr& ArgVal):
TagNm(), SOut(&_SOut){
if (Nm!="-"){
SOut->PutCh('<');
if (Nm.Empty()){
SOut->PutStr(TagNm=TypeNm);
} else {
SOut->PutStr(TagNm=Nm);
SOut->PutStr(" Type=\""); SOut->PutStr(TypeNm); SOut->PutCh('"');
}
if (!ArgNm.Empty()){
SOut->PutCh(' '); SOut->PutStr(ArgNm); SOut->PutCh('=');
SOut->PutCh('"'); SOut->PutStr(ArgVal); SOut->PutCh('"');
}
if (ETagP){
SOut->PutCh('/'); TagNm="";}
SOut->PutCh('>');
}
}
TXmlObjSerTagNm::TXmlObjSerTagNm(
TSOut& _SOut, const bool& ETagP,
const TStr& Nm, const TStr& TypeNm,
const TStr& ArgNm1, const TStr& ArgVal1,
const TStr& ArgNm2, const TStr& ArgVal2,
const TStr& ArgNm3, const TStr& ArgVal3,
const TStr& ArgNm4, const TStr& ArgVal4):
TagNm(), SOut(&_SOut){
if (Nm!="-"){
SOut->PutCh('<');
if (Nm.Empty()){
SOut->PutStr(TagNm=TypeNm);
} else {
SOut->PutStr(TagNm=Nm);
SOut->PutStr(" Type=\""); SOut->PutStr(TypeNm); SOut->PutCh('"');
}
if (!ArgNm1.Empty()){
SOut->PutCh(' '); SOut->PutStr(ArgNm1); SOut->PutCh('=');
SOut->PutCh('"'); SOut->PutStr(ArgVal1); SOut->PutCh('"');
}
if (!ArgNm2.Empty()){
SOut->PutCh(' '); SOut->PutStr(ArgNm2); SOut->PutCh('=');
SOut->PutCh('"'); SOut->PutStr(ArgVal2); SOut->PutCh('"');
}
if (!ArgNm3.Empty()){
SOut->PutCh(' '); SOut->PutStr(ArgNm3); SOut->PutCh('=');
SOut->PutCh('"'); SOut->PutStr(ArgVal3); SOut->PutCh('"');
}
if (!ArgNm4.Empty()){
SOut->PutCh(' '); SOut->PutStr(ArgNm4); SOut->PutCh('=');
SOut->PutCh('"'); SOut->PutStr(ArgVal4); SOut->PutCh('"');
}
if (ETagP){
SOut->PutCh('/'); TagNm="";}
SOut->PutCh('>');
}
}
TXmlObjSerTagNm::~TXmlObjSerTagNm(){
if (!TagNm.Empty()){
SOut->PutCh('<'); SOut->PutCh('/'); SOut->PutStr(TagNm); SOut->PutCh('>');
}
}
/////////////////////////////////////////////////
// Xml-Chars
void TXmlChDef::SetChTy(TBSet& ChSet, const int& MnCh, const int& MxCh){
IAssert((0<=MnCh)&&((MxCh==-1)||((MnCh<=MxCh)&&(MxCh<Chs))));
ChSet.Incl(MnCh);
for (int Ch=MnCh+1; Ch<=MxCh; Ch++){
ChSet.Incl(Ch);}
}
void TXmlChDef::SetChTy(TBSet& ChSet, const TStr& Str){
for (int ChN=0; ChN<Str.Len(); ChN++){
uchar Ch=Str[ChN];
ChSet.Incl(Ch);
}
}
void TXmlChDef::SetEntityVal(const TStr& Nm, const TStr& Val){
EntityNmToValH.AddDat(Nm, Val);
}
TXmlChDef::TXmlChDef():
Chs(TUCh::Vals),
CharChSet(), CombChSet(), ExtChSet(),
LetterChSet(), DigitChSet(), NameChSet(), PubidChSet(),
EntityNmToValH(100){
// Character-Sets
// Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | ...
CharChSet.Gen(Chs);
// ... because of DMoz (temporary patch)
SetChTy(CharChSet, 0x1); SetChTy(CharChSet, 0x3); SetChTy(CharChSet, 0x6);
SetChTy(CharChSet, 11); SetChTy(CharChSet, 24); SetChTy(CharChSet, 27);
// regular characters
SetChTy(CharChSet, 0x9); SetChTy(CharChSet, 0xA); SetChTy(CharChSet, 0xD);
SetChTy(CharChSet, 0x20, TUCh::Mx);
// BaseChar ::= [#x0041-#x005A] | [#x0061-#x007A] | [#x00C0-#x00D6] |
// [#x00D8-#x00F6] | [#x00F8-#x00FF] | ...
TBSet BaseChSet(Chs);
SetChTy(BaseChSet, 0x41, 0x5A); SetChTy(BaseChSet, 0x61, 0x7A);
SetChTy(BaseChSet, 0xC0, 0xD6); SetChTy(BaseChSet, 0xD8, 0xF6);
SetChTy(BaseChSet, 0xF8, 0xFF);
// Ideographic ::= ...
TBSet IdeoChSet(Chs);
// CombiningChar ::= ...
CombChSet.Gen(Chs);
// Extender ::= #x00B7 | ...
ExtChSet.Gen(Chs);
SetChTy(ExtChSet, 0xB7);
// Letter ::= BaseChar | Ideographic
LetterChSet=BaseChSet|IdeoChSet;
// Digit ::= [#x0030-#x0039] | ...
DigitChSet.Gen(Chs);
SetChTy(DigitChSet, 0x30, 0x39);
// NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar
NameChSet=LetterChSet|DigitChSet|
uchar('.')|uchar('-')|uchar('_')|uchar(':')|CombChSet;
// PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
PubidChSet.Gen(Chs);
SetChTy(PubidChSet, 0x20); SetChTy(PubidChSet, 0xD); SetChTy(PubidChSet, 0xA);
SetChTy(PubidChSet, 'a', 'z'); SetChTy(PubidChSet, 'A', 'Z');
SetChTy(PubidChSet, '0', '9'); SetChTy(PubidChSet, "-'()+,./:=?;!*#@$_%");
// Standard-Entity-Sequences
SetEntityVal("amp", "&");
SetEntityVal("lt", "<"); SetEntityVal("gt", ">");
SetEntityVal("apos", "'"); SetEntityVal("quot", "\"");
}
/////////////////////////////////////////////////
// Xml-Lexical
TXmlChDef TXmlLx::ChDef;
uchar TXmlLx::GetCh(){
EAssert(Ch!=TCh::EofCh);
PrevCh=Ch;
if (ChStack.Empty()){Ch=(RSIn.Eof()) ? TCh::EofCh : RSIn.GetCh();}
else {Ch=ChStack.Pop();}
ChN++; if (Ch==TCh::LfCh){LnN++; LnChN=0;} else {LnChN++;}
//putchar(Ch);
return Ch;
}
void TXmlLx::ToNrSpacing(){
if (Spacing==xspIntact){
} else
if (Spacing==xspPreserve){
int SrcChN=0; int DstChN=0;
while (SrcChN<TxtChA.Len()){
if (TxtChA[SrcChN]==TCh::CrCh){
TxtChA.PutCh(DstChN, TCh::LfCh); SrcChN++; DstChN++;
if ((SrcChN<TxtChA.Len())&&(TxtChA[SrcChN]==TCh::LfCh)){SrcChN++;}
} else {
if (SrcChN!=DstChN){
TxtChA.PutCh(DstChN, TxtChA[SrcChN]);}
SrcChN++; DstChN++;
}
}
TxtChA.Trunc(DstChN);
} else
if (Spacing==xspSeparate){
// squeeze series of white-spaces to single space
int SrcChN=0; int DstChN=0;
while (SrcChN<TxtChA.Len()){
if (ChDef.IsWs(TxtChA[SrcChN])){
if ((DstChN>0)&&(TxtChA[DstChN-1]==' ')){
SrcChN++;
} else {
TxtChA.PutCh(DstChN, ' ');
SrcChN++; DstChN++;
}
} else {
TxtChA.PutCh(DstChN, TxtChA[SrcChN]);
SrcChN++; DstChN++;
}
}
TxtChA.Trunc(DstChN);
} else
if (Spacing==xspTruncate){
// cut leading and trailing white-spaces and
// squeeze series of white-spaces to single space
int SrcChN=0; int DstChN=0;
while (SrcChN<TxtChA.Len()){
if (ChDef.IsWs(TxtChA[SrcChN])){
if ((DstChN>0)&&(TxtChA[DstChN-1]==' ')){
SrcChN++;
} else {
TxtChA.PutCh(DstChN, ' ');
SrcChN++; DstChN++;
}
} else {
TxtChA.PutCh(DstChN, TxtChA[SrcChN]);
SrcChN++; DstChN++;
}
}
TxtChA.Trunc(DstChN);
// delete trailing white-spaces
while ((TxtChA.Len()>0)&&(ChDef.IsWs(TxtChA.LastCh()))){
TxtChA.Trunc(TxtChA.Len()-1);}
} else {
Fail;
}
}
void TXmlLx::GetWs(const bool& IsRq){
// [3] S ::= (#x20 | #x9 | #xD | #xA)+
int WSpaces=0; TxtChA.Clr();
while (ChDef.IsWs(Ch)){
WSpaces++; TxtChA+=Ch; GetCh();}
if (IsRq&&(WSpaces==0)){
EThrow("White-space required.");}
}
TStr TXmlLx::GetReference(){
// [67] Reference ::= EntityRef | CharRef
if (Ch=='#'){
// [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
TChA RefChA; int RefCd=0;
if (GetCh()=='x'){
// hex-decimal character code
forever {
GetCh();
if (TCh::IsHex(Ch)){
RefChA+=Ch;
RefCd=RefCd*16+TCh::GetHex(Ch);
} else {
break;
}
}
} else {
// decimal character code
forever {
if (TCh::IsNum(Ch)){
RefChA+=Ch;
RefCd=RefCd*10+TCh::GetNum(Ch);
} else {
break;
}
GetCh();
}
}
if ((!RefChA.Empty())&&(Ch==';')){
GetCh();
uchar RefCh=uchar(RefCd);
return TStr(RefCh);
} else {
EThrow("Invalid Char-Reference."); Fail; return TStr();
}
} else {
// [68] EntityRef ::= '&' Name ';'
TStr EntityNm=GetName();
if ((!EntityNm.Empty())&&(Ch==';')){
GetCh();
TStr EntityVal;
if (IsEntityNm(EntityNm, EntityVal)){/*intentionaly empty*/}
else if (ChDef.IsEntityNm(EntityNm, EntityVal)){/*intentionaly empty*/}
else {EThrow(TStr("Entity-Reference (")+EntityNm+") does not exist.");}
return EntityVal;
} else {
EThrow("Invalid Entity-Reference."); Fail; return TStr();
}
}
}
TStr TXmlLx::GetPEReference(){
// [69] PEReference ::= '%' Name ';'
TStr EntityNm=GetName();
if ((EntityNm.Empty())||(Ch!=';')){EThrow("Invalid PEntity-Reference.");}
GetCh();
TStr EntityVal;
if (IsPEntityNm(EntityNm, EntityVal)){/*intentionaly empty*/}
else {EThrow(TStr("PEntity-Reference (")+EntityNm+") does not exist.");}
return EntityVal;
}
void TXmlLx::GetEq(){
// [25] Eq ::= S? '=' S?
GetWs(false);
if (Ch=='='){GetCh();}
else {EThrow("Equality ('=') character expected.");}
GetWs(false);
}
TStr TXmlLx::GetName(){
// [5] Name ::= (Letter | '_' | ':') (NameChar)*
TChA NmChA;
if (ChDef.IsFirstNameCh(Ch)){
do {NmChA+=Ch;} while (ChDef.IsName(GetCh()));
} else {
EThrow("Invalid first name character.");
// EThrow(TStr::Fmt("Invalid first name character [%u:'%c%c%c%c%c'].",
// uint(Ch), Ch, RSIn.GetCh(), RSIn.GetCh(), RSIn.GetCh(), RSIn.GetCh()));
}
return NmChA;
}
TStr TXmlLx::GetName(const TStr& RqNm){
TStr Nm=GetName();
// test if the name is equal to the required name
if (Nm==RqNm){return RqNm;}
else {EThrow(TStr("Name '")+RqNm+"' expected."); Fail; return TStr();}
}
void TXmlLx::GetComment(){
// [15] Comment ::= {{'<!-}}-' ((Char - '-') | ('-' (Char - '-')))* '-->'
if (GetCh()!='-'){EThrow("Invalid comment start.");}
TxtChA.Clr();
forever {
GetCh();
if (!ChDef.IsChar(Ch)){EThrow("Invalid comment character.");}
if (Ch=='-'){
if (GetCh()=='-'){
if (GetCh()=='>'){GetCh(); break;} // final bracket
else {EThrow("Invalid comment end.");}
} else {
if (!ChDef.IsChar(Ch)){EThrow("Invalid comment character.");}
TxtChA+='-'; TxtChA+=Ch; // special case if single '-'
}
} else {
TxtChA+=Ch; // usual char
}
}
}
TStr TXmlLx::GetAttValue(){
// [10] AttValue ::= '"' ([^<&"] | Reference)* '"'
// | "'" ([^<&'] | Reference)* "'"
uchar QCh=Ch;
if ((QCh!='"')&&(QCh!='\'')){EThrow("Invalid attribute-value start.");}
TChA ValChA; GetCh();
forever {
if ((Ch=='<')||(!ChDef.IsChar(Ch))){
EThrow("Invalid attribute-value character.");}
if (Ch==QCh){GetCh(); break;} // final quote
else if (Ch=='&'){GetCh(); ValChA+=GetReference();} // reference
else {ValChA+=Ch; GetCh();} // usual char
}
return ValChA;
}
TStr TXmlLx::GetVersionNum(){
// [24] VersionInfo ::= {{S 'version' Eq}} (' VersionNum ' | " VersionNum ")
// [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+
char QCh=Ch;
if ((Ch!='\'')&&(Ch!='"')){EThrow("Quote character (' or \") expected.");}
TChA VerNumChA;
GetCh();
do {
if ((('a'<=Ch)&&(Ch<='z'))||(('A'<=Ch)&&(Ch<='Z'))||
(('0'<=Ch)&&(Ch<='9'))||(Ch=='_')||(Ch=='.')||(Ch==':')||(Ch=='-')){
VerNumChA+=Ch;
} else {
EThrow("Invalid version-number character.");
}
GetCh();
} while (Ch!=QCh);
GetCh();
return VerNumChA;
}
TStr TXmlLx::GetEncName(){
// [80] EncodingDecl ::= {{S 'encoding' Eq}} ('"' EncName '"' | "'" EncName "'" )
// [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
char QCh=Ch;
if ((Ch!='\'')&&(Ch!='"')){EThrow("Quote character (' or \") expected.");}
TChA EncNmChA;
GetCh();
if ((('a'<=Ch)&&(Ch<='z'))||(('A'<=Ch)&&(Ch<='Z'))){EncNmChA+=Ch;}
else {EThrow("Invalid encoding-name character.");}
GetCh();
while (Ch!=QCh){
if ((('a'<=Ch)&&(Ch<='z'))||(('A'<=Ch)&&(Ch<='Z'))||
(('0'<=Ch)&&(Ch<='9'))||(Ch=='.')||(Ch=='_')||(Ch=='-')){EncNmChA+=Ch;}
else {EThrow("Invalid version-number character.");}
GetCh();
}
GetCh();
return EncNmChA;
}
TStr TXmlLx::GetStalVal(){
// [32] SDDecl ::= {{S 'standalone' Eq}}
// (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))
char QCh=Ch;
if ((Ch!='\'')&&(Ch!='"')){EThrow("Quote character (' or \") expected.");}
TChA StalChA;
GetCh();
while (Ch!=QCh){
if (('a'<=Ch)&&(Ch<='z')){StalChA+=Ch;}
else {EThrow("Invalid standalone-value character.");}
GetCh();
}
GetCh();
TStr StalVal=StalChA;
if ((StalVal=="yes")||(StalVal=="no")){return StalVal;}
else {EThrow("Invalid standalone-value."); Fail; return TStr();}
}
void TXmlLx::GetXmlDecl(){
// [23] XMLDecl ::= {{'<?xml'}}... VersionInfo EncodingDecl? SDDecl? S? '?>'
// [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
GetWs(true);
TStr VerNm=GetName("version"); GetEq(); TStr VerVal=GetVersionNum();
if (VerVal!="1.0"){EThrow("Invalid XML version.");}
AddArg(VerNm, VerVal);
GetWs(false);
if (Ch!='?'){
// EncodingDecl ::= {{S}} 'encoding' Eq
// ('"' EncName '"' | "'" EncName "'" )
TStr EncNm=GetName("encoding"); GetEq(); TStr EncVal=GetEncName();
AddArg(EncNm, EncVal);
}
GetWs(false);
if (Ch!='?'){
// SDDecl ::= {{S}} 'standalone' Eq
// (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))
TStr StalNm=GetName("standalone"); GetEq(); TStr StalVal=GetStalVal();
AddArg(StalNm, StalVal);
}
GetWs(false);
if (Ch=='?'){
GetCh();
if (Ch=='>'){GetCh();}
else {EThrow("Invalid end-of-tag in XML-declaration.");}
} else {
EThrow("Invalid end-of-tag in XML-declaration.");
}
}
void TXmlLx::GetPI(){
// [16] PI ::= {{'<?' PITarget}} (S (Char* - (Char* '?>' Char*)))? '?>'
// [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
GetWs(false);
TxtChA.Clr();
forever {
if (!ChDef.IsChar(Ch)){EThrow("Invalid PI character.");}
if (Ch=='?'){
if (GetCh()=='>'){
GetCh(); break;
} else {
if (!ChDef.IsChar(Ch)){EThrow("Invalid PI character.");}
TxtChA+='?'; TxtChA+=Ch; // special case if single '?'
}
} else {
TxtChA+=Ch; // usual char
}
GetCh();
}
}
TStr TXmlLx::GetSystemLiteral(){
// [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
char QCh=Ch;
if ((Ch!='\'')&&(Ch!='"')){EThrow("Quote character (' or \") expected.");}
TChA LitChA; GetCh();
while (Ch!=QCh){
if (!ChDef.IsChar(Ch)){EThrow("Invalid System-Literal character.");}
LitChA+=Ch; GetCh();
}
GetCh();
return LitChA;
}
TStr TXmlLx::GetPubidLiteral(){
// [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
char QCh=Ch;
if ((Ch!='\'')&&(Ch!='"')){EThrow("Quote character (' or \") expected.");}
TChA LitChA; GetCh();
while (Ch!=QCh){
if (!ChDef.IsPubid(Ch)){EThrow("Invalid Public-Id-Literal character.");}
LitChA+=Ch; GetCh();
}
GetCh();
return LitChA;
}
void TXmlLx::GetExternalId(){
// ExternalID ::= 'SYSTEM' S SystemLiteral
// | 'PUBLIC' S PubidLiteral S SystemLiteral
TStr ExtIdNm=GetName();
if (ExtIdNm=="SYSTEM"){
GetWs(true); GetSystemLiteral();
} else if (ExtIdNm=="PUBLIC"){
GetWs(true); GetPubidLiteral(); GetWs(true); GetSystemLiteral();
} else {
EThrow("Invalid external-id ('SYSTEM' or 'PUBLIC' expected).");
}
}
void TXmlLx::GetNData(){
// [76] NDataDecl ::= S 'NDATA' S Name
GetName("NDATA"); GetWs(true); GetName();
}
void TXmlLx::GetDocTypeDecl(){
// [28] doctypedecl ::= {{'<!DOCTYPE'}} S Name (S ExternalID)? S?
// ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
GetWs(true);
TStr DocTypeDeclNm=GetName();
GetWs(false);
if (Ch=='>'){GetCh(); return;}
if (Ch!='['){GetExternalId();}
GetWs(false);
if (Ch=='['){
GetCh();
// [28] (markupdecl | PEReference | S)*
GetWs(false);
while (Ch!=']'){
if (ChDef.IsWs(Ch)){GetWs(true);}
else if (Ch=='%'){GetPEReference();}
else {
GetSym();
}
}
GetCh();
}
GetWs(false);
// '>'
if (Ch=='>'){GetCh();}
else {EThrow("Invalid end-of-tag in document-type-declaration.");}
TagNm=DocTypeDeclNm;
}
void TXmlLx::GetElement(){
TxtChA.Clr();
while (Ch!='>'){
if (!ChDef.IsChar(Ch)){EThrow("Invalid Element character.");}
TxtChA+=Ch; GetCh();
}
GetCh();
}
void TXmlLx::GetAttList(){
TxtChA.Clr();
while (Ch!='>'){
if (!ChDef.IsChar(Ch)){EThrow("Invalid Element character.");}
TxtChA+=Ch; GetCh();
}
GetCh();
}
TStr TXmlLx::GetEntityValue(){
// [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"'
// | "'" ([^%&'] | PEReference | Reference)* "'"
uchar QCh=Ch;
if ((QCh!='"')&&(QCh!='\'')){EThrow("Invalid entity-value start.");}
TChA ValChA; GetCh();
forever {
if (!ChDef.IsChar(Ch)){EThrow("Invalid entity-value character.");}
if (Ch==QCh){GetCh(); break;} // final quote
else if (Ch=='&'){GetCh(); ValChA+=GetReference();} // reference
else if (Ch=='%'){GetCh(); ValChA+=GetPEReference();} // pereference
else {ValChA+=Ch; GetCh();} // usual char
}
return ValChA;
}
void TXmlLx::GetEntity(){
// [70] EntityDecl ::= GEDecl | PEDecl
// [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
// [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
GetWs(true); TStr EntityNm;
if (Ch=='%'){
GetCh(); GetWs(true); EntityNm=GetName(); GetWs(true);
// [74] PEDef ::= EntityValue | ExternalID
if ((Ch=='\"')||(Ch=='\'')){
TStr EntityVal=GetEntityValue();
PutPEntityVal(EntityNm, EntityVal);
} else {
GetExternalId();
GetWs(false);
if (Ch!='>'){GetNData();}
}
} else {
EntityNm=GetName(); GetWs(true);
// [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
if ((Ch=='\"')||(Ch=='\'')){
TStr EntityVal=GetEntityValue();
PutEntityVal(EntityNm, EntityVal);
} else {
GetExternalId();
}
}
GetWs(false);
if (Ch=='>'){GetCh();}
else {EThrow("Invalid end-of-tag in entity-declaration.");}
TagNm=EntityNm;
}
void TXmlLx::GetNotation(){
// [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
// [83] PublicID ::= 'PUBLIC' S PubidLiteral
TxtChA.Clr();
while (Ch!='>'){
if (!ChDef.IsChar(Ch)){EThrow("Invalid Element character.");}
TxtChA+=Ch; GetCh();
}
GetCh();
}
void TXmlLx::GetCDSect(){
// [18] CDSect ::= CDStart CData CDEnd
// [19] CDStart ::= '<![CDATA{{['}}
// [20] CData ::= (Char* - (Char* ']]>' Char*))
// [21] CDEnd ::= ']]>'
if (Ch=='['){GetCh();}
else {EThrow("Invalid start of CDATA section.");}
TxtChA.Clr();
forever {
if (!ChDef.IsChar(Ch)){EThrow("Invalid CDATA character.");}
if ((Ch=='>')&&(TxtChA.Len()>=2)&&
(TxtChA[TxtChA.Len()-2]==']')&&(TxtChA[TxtChA.Len()-1]==']')){
GetCh(); TxtChA.Trunc(TxtChA.Len()-2); break;
} else {
TxtChA+=Ch; GetCh();
}
}
}
void TXmlLx::SkipWs(){
// [3] S ::= (#x20 | #x9 | #xD | #xA)+
while (ChDef.IsWs(Ch)){GetCh();}
}
TXmlLxSym TXmlLx::GetSym(){
if (Ch=='<'){
GetCh(); ClrArgV();
if (Ch=='?'){
GetCh(); TagNm=GetName();
if (TagNm.GetLc()=="xml"){Sym=xsyXmlDecl; GetXmlDecl();}
else {Sym=xsyPI; GetPI();}
} else
if (Ch=='!'){
GetCh();
if (Ch=='['){
GetCh(); TagNm=GetName();
if (TagNm=="CDATA"){Sym=xsyQStr; GetCDSect();}
else {EThrow(TStr("Invalid tag after '<![' (")+TagNm+").");}
} else
if (Ch=='-'){
Sym=xsyComment; GetComment();
} else {
TagNm=GetName();
if (TagNm=="DOCTYPE"){GetDocTypeDecl(); Sym=xsyDocTypeDecl;}
else if (TagNm=="ELEMENT"){GetElement(); Sym=xsyElement;}
else if (TagNm=="ATTLIST"){GetAttList(); Sym=xsyAttList;}
else if (TagNm=="ENTITY"){GetEntity(); Sym=xsyEntity;}
else if (TagNm=="NOTATION"){GetNotation(); Sym=xsyNotation;}
else {EThrow(TStr("Invalid tag (")+TagNm+").");}
}
} else
if (Ch=='/'){
// xsyETag
GetCh(); Sym=xsyETag; TagNm=GetName(); GetWs(false);
if (Ch=='>'){GetCh();}
else {EThrow("Invalid End-Tag.");}
} else {
// xsySTag or xsySETag
TagNm=GetName(); GetWs(false);
while ((Ch!='>')&&(Ch!='/')){
TStr AttrNm=GetName();
GetEq();
TStr AttrVal=GetAttValue();
GetWs(false);
AddArg(AttrNm, AttrVal);
}
if (Ch=='/'){
if (GetCh()=='>'){Sym=xsySETag; GetCh();}
else {EThrow("Invalid Empty-Element-Tag.");}
} else {
Sym=xsySTag; GetCh();
}
}
if (Spacing==xspTruncate){SkipWs();}
} else
if (ChDef.IsWs(Ch)){
Sym=xsyWs; GetWs(true); ToNrSpacing();
if (Spacing==xspTruncate){GetSym();}
} else
if (Ch==TCh::EofCh){
Sym=xsyEof;
} else {
Sym=xsyStr; TxtChA.Clr();
// [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
forever {
if (!ChDef.IsChar(Ch)){
EThrow(TUInt::GetStr(Ch, "Invalid character (%d)."));}
// GetCh(); continue; // skip invalid characters
if (Ch=='<'){break;} // tag
if (Ch=='&'){GetCh(); TxtChA+=GetReference();} // reference
else {
if ((Ch=='>')&&(TxtChA.Len()>=2)&&
(TxtChA[TxtChA.Len()-2]==']')&&(TxtChA[TxtChA.Len()-1]==']')){
EThrow("Forbidden substring ']]>' in character data.");}
TxtChA+=Ch; GetCh(); // usual char
}
}
ToNrSpacing();
}
return Sym;
}
TStr TXmlLx::GetSymStr() const {
TChA SymChA;
switch (Sym){
case xsyUndef:
SymChA="{Undef}"; break;
case xsyWs:
SymChA+="{Space:'"; SymChA+=TStr(TxtChA).GetHex(); SymChA+="'}"; break;
case xsyComment:
SymChA+="<!--"; SymChA+=TxtChA; SymChA+="-->"; break;
case xsyXmlDecl:{
SymChA+="<?"; SymChA+=TagNm;
for (int ArgN=0; ArgN<GetArgs(); ArgN++){
TStr ArgNm; TStr ArgVal; GetArg(ArgN, ArgNm, ArgVal);
char ArgValQCh=GetArgValQCh(ArgVal);
SymChA+=' '; SymChA+=ArgNm; SymChA+='=';
SymChA+=ArgValQCh; SymChA+=ArgVal; SymChA+=ArgValQCh;
}
SymChA+="?>"; break;}
case xsyPI:
SymChA+="<?"; SymChA+=TagNm;
if (!TxtChA.Empty()){SymChA+=' '; SymChA+=TxtChA;}
SymChA+="?>"; break;
case xsyDocTypeDecl:
SymChA+="<!DOCTYPE "; SymChA+=TagNm; SymChA+=">"; break;
case xsySTag:
case xsySETag:{
SymChA+="<"; SymChA+=TagNm;
for (int ArgN=0; ArgN<GetArgs(); ArgN++){
TStr ArgNm; TStr ArgVal; GetArg(ArgN, ArgNm, ArgVal);
char ArgValQCh=GetArgValQCh(ArgVal);
SymChA+=' '; SymChA+=ArgNm; SymChA+='=';
SymChA+=ArgValQCh; SymChA+=ArgVal; SymChA+=ArgValQCh;
}
if (Sym==xsySTag){SymChA+=">";}
else if (Sym==xsySETag){SymChA+="/>";}
else {Fail;}
break;}
case xsyETag:
SymChA+="</"; SymChA+=TagNm; SymChA+=">"; break;
case xsyStr:
SymChA="{String:'"; SymChA+=TxtChA; SymChA+="'}"; break;
case xsyQStr:
SymChA="{QString:'"; SymChA+=TxtChA; SymChA+="'}"; break;
case xsyEof:
SymChA="{Eof}"; break;
default: Fail;
}
return SymChA;
}
void TXmlLx::EThrow(const TStr& MsgStr) const {
TChA FPosChA;
FPosChA+=" [File:"; FPosChA+=SIn->GetSNm();
FPosChA+=" Line:"; FPosChA+=TInt::GetStr(LnN);
FPosChA+=" Char:"; FPosChA+=TInt::GetStr(LnChN);
FPosChA+="]";
TStr FullMsgStr=MsgStr+FPosChA;
TExcept::Throw(FullMsgStr);
}
TStr TXmlLx::GetFPosStr() const {
TChA FPosChA;
FPosChA+=" [File:"; FPosChA+=SIn->GetSNm();
FPosChA+=" Line:"; FPosChA+=TInt::GetStr(LnN);
FPosChA+=" Char:"; FPosChA+=TInt::GetStr(LnChN);
FPosChA+="]";
return FPosChA;
}
TStr TXmlLx::GetXmlLxSymStr(const TXmlLxSym& XmlLxSym){
switch (XmlLxSym){
case xsyUndef: return "Undef";
case xsyWs: return "White-Space";
case xsyComment: return "Comment";
case xsyXmlDecl: return "Declaration";
case xsyPI: return "PI";
case xsyDocTypeDecl: return "Document-Type";
case xsyElement: return "Element";
case xsyAttList: return "Attribute-List";
case xsyEntity: return "Entity";
case xsyNotation: return "Notation";
case xsyTag: return "Tag";
case xsySTag: return "Start-Tag";
case xsyETag: return "End-Tag";
case xsySETag: return "Start-End-Tag";
case xsyStr: return "String";
case xsyQStr: return "Quoted-String";
case xsyEof: return "Eon-Of-File";
default: return "Undef";
}
}
bool TXmlLx::IsTagNm(const TStr& Str){
TChA ChA=Str;
if (ChA.Len()>0){
if (TXmlLx::ChDef.IsFirstNameCh(ChA[0])){
for (int ChN=1; ChN<ChA.Len(); ChN++){
if (!TXmlLx::ChDef.IsName(ChA[ChN])){
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}
TStr TXmlLx::GetXmlStrFromPlainMem(const TMem& PlainMem){
TChA XmlChA;
for (int ChN=0; ChN<PlainMem.Len(); ChN++){
uchar Ch=PlainMem[ChN];
if ((' '<=Ch)&&(Ch<='~')){
switch (Ch){
case '"': XmlChA+="""; break;
case '&': XmlChA+="&"; break;
case '\'': XmlChA+="'"; break;
case '<': XmlChA+="<"; break;
case '>': XmlChA+=">"; break;
default: XmlChA+=Ch;
}
} else
if ((Ch=='\r')||(Ch=='\n')){
XmlChA+=Ch;
} else {
XmlChA+='&'; XmlChA+='#'; XmlChA+=TUInt::GetStr(Ch); XmlChA+=';';
}
}
return XmlChA;
}
TStr TXmlLx::GetXmlStrFromPlainStr(const TChA& PlainChA){
TChA XmlChA;
for (int ChN=0; ChN<PlainChA.Len(); ChN++){
uchar Ch=PlainChA[ChN];
if ((' '<=Ch)&&(Ch<='~')){
switch (Ch){
case '"': XmlChA+="""; break;
case '&': XmlChA+="&"; break;
case '\'': XmlChA+="'"; break;
case '<': XmlChA+="<"; break;
case '>': XmlChA+=">"; break;
default: XmlChA+=Ch;
}
} else
if ((Ch=='\r')||(Ch=='\n')){
XmlChA+=Ch;
} else {
XmlChA+='&'; XmlChA+='#'; XmlChA+=TUInt::GetStr(Ch); XmlChA+=';';
}
}
return XmlChA;
}
TStr TXmlLx::GetPlainStrFromXmlStr(const TStr& XmlStr){
TChA PlainChA;
TChRet Ch(TStrIn::New(XmlStr));
Ch.GetCh();
while (!Ch.Eof()){
if (Ch()!='&'){
PlainChA+=Ch(); Ch.GetCh();
} else {
// [67] Reference ::= EntityRef | CharRef
if (Ch.GetCh()=='#'){
// [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
TChA RefChA; int RefCd=0;
if (Ch.GetCh()=='x'){
// hex-decimal character code
forever {
Ch.GetCh();