forked from nlp-and-learning/SimpleSaxParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaxParser.cpp
1572 lines (1397 loc) · 31.9 KB
/
SaxParser.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
// SaxParser.cpp: implementation of the SaxParser class.
//
//////////////////////////////////////////////////////////////////////
//Some parts of encoding conversion code were copied out from 'tinyxml' component
#include "SaxParser.h"
#include <list>
#include <cstring>
#include <cassert>
using namespace std;
const int utf8ByteTable[256] =
{
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70 End of ASCII range
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x80 0x80 to 0xc1 invalid
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x90
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xa0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xb0
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xc0 0xc2 to 0xdf 2 byte
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xd0
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xe0 0xe0 to 0xef 3 byte
4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 0xf0 0xf0 to 0xf4 4 byte, 0xf5 and higher invalid
};
void ConvertUTF32ToUTF8( unsigned long input, string& output)
{
const unsigned long BYTE_MASK = 0xBF;
const unsigned long BYTE_MARK = 0x80;
const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
int length;
if (input < 0x80)
length = 1;
else if ( input < 0x800 )
length = 2;
else if ( input < 0x10000 )
length = 3;
else if ( input < 0x200000 )
length = 4;
else
{ length = 0; return; } // This code won't covert this correctly anyway.
output.resize(length);
// Scary scary fall throughs.
switch (length)
{
case 4:
//--output;
output[3] = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
case 3:
//--output;
output[2] = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
case 2:
//--output;
output[1] = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
case 1:
//--output;
output[0] = (char)(input | FIRST_BYTE_MARK[length]);
}
}
struct SAttribute
{
string m_name;
string m_value;
void Clear() {m_name.erase(); m_value.erase();}
};
static const char* g_cszErrorList[]=
{
"No error", //0
"Document is empty", //1
"Invalid document format", //2
"Invalid instance definition", //3
"Value is too long", //4
"Invalid processing instruction name", //5
"Name 'xml' is reserved and must content only lower-case symbols", //6
"Missing closing tag", //7
"Invalid declaration description", //8
"Invalid version number", //9
"Encoding not supported", //10
"Invalid element name", //11
"Closing element does not match to open one",//12
"Invalid comment declaration", //13
"Invalid CDATA declaration", //14
"Empty entity", //15
"Invalid symbol within entity", //16
"Invalid attribute name", //17
"Invalid attribute description", //18
"Document must not content data after root node",//19
"Unknown entity", //20
"Root node is not closed", //21
"DTD is not supported", //22
"End of file encountered", //23
"Section CDATA is not closed", //24
"Missing semicolumn", //25
"Missing quote", //26
"The text before root node is illegal", //27
"The text after root node is illegal", //28
"Whitespace after element symbol open is illegal",//29
"Whitespace after element symbol close is illegal",//30
"Whitespace after processing instruction close is illegal", //31
"Missing closing tag or commentary does not closed",//32
"Two-byte encodings are not supported", //33
"Entity before document open root is illegal", //34
"CDATA section before document open root is illegal",//35
"Declaration was not closed or invalid syntax", //36
"Processing instruction was not closed", //37
"Duplicate attribute name", //38
"Input data error" //39
};
const int g_ciCountError = std::extent <decltype(g_cszErrorList)>::value;
const char* SaxParserException::what() const noexcept
{
return g_cszErrorList[m_nCode];
}
struct SEntity
{
string m_szEntity;
char m_value;
};
const SEntity g_ListEntity[]=
{
{ "amp", '&' },
{ "lt", '<' },
{ "gt", '>' },
{ "quot",'\"' },
//{ "apos",'\'' }
};
const unsigned int g_ciCountEntity = std::extent <decltype(g_ListEntity)>::value;
int findEntity(char c) {
for (int i=0; i<g_ciCountEntity; i++)
if (g_ListEntity[i].m_value==c) return i;
return -1;
}
string ToXML(string str) {
int lenOut = str.length();
for (int i=0; i<str.length(); i++) {
int k = findEntity(str[i]);
if (k>=0) lenOut+= g_ListEntity[k].m_szEntity.length()+1;
}
string outStr;
outStr.resize(lenOut);
int i2=0;
for (int i=0; i<str.length(); i++) {
int k = findEntity(str[i]);
if (k>=0) {
outStr[i2] = '&';
i2++;
for (int j=0; j<g_ListEntity[k].m_szEntity.length(); j++)
outStr[i2+j] = g_ListEntity[k].m_szEntity[j];
i2 += g_ListEntity[k].m_szEntity.length();
outStr[i2] = ';';
i2++;
}
else {
outStr[i2] = str[i];
i2++;
}
}
return outStr;
}
inline bool IsLetter(char c)
{
if (((unsigned char)c)<127)
return isalpha(c) ? true : false;
return
true;
}
inline bool IsDigit(char c)
{
return isdigit((unsigned char)c) ? true : false;
}
inline bool IsHexDigit(char c)
{
return isxdigit((unsigned char)c) ? true : false;
}
inline bool IsPrintable(char c)
{
return isprint((unsigned char)c) ? true : false;
}
inline bool IsControl(char c)
{
return iscntrl((unsigned char)c) ? true : false;
}
inline bool IsSpace(char c)
{
return (isspace((unsigned char)c) || c == '\n' || c == '\r' || c=='\t') ? true : false;
}
inline unsigned int GetIncreaseColumn(char c)
{
switch(c)
{
case ' ':
case '\t':
return 1;
break;
default:
if (IsPrintable(c))
return 1;
}
return 0;
}
bool IsFirstNameValid(char c)
{
if (c=='_') return true;
if (IsLetter(c)) return true;
return false;
}
bool IsCharNameValid(char c)
{
if (c=='.' || c=='_' || c=='-' || c==':') return true;
if (IsLetter(c) || IsDigit(c)) return true;
return false;
}
int strcmpi(char const *a, char const *b)
{
for (;; a++, b++) {
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a)
return d;
}
}
bool IsXML(const string& str, bool& bError)
{
bError = false;
if (str.size()<3) return false;
if (strcmpi(str.c_str(),"xml")==0)
{
if (str[0] == 'X' || str[1] == 'M' || str[2] == 'L')
{
bError = true;
return false;
}
return true;
}
return false;
}
void TrimRight(string& str, XSPHandler* pHandler)
{
unsigned int pos=(unsigned int )str.size();
if (pos==0) return;
while (true)
{
if (!(IsSpace(str[pos-1]) || IsControl(str[pos-1])))
break;
else
pHandler->OnNotLeadingChar(str[pos-1]);
pos--;
if (pos==0)
break;
}
str.resize(pos);
}
//////////////////////////////////////////////////////////////////////////
//SPBuffer class
//////////////////////////////////////////////////////////////////////////
SaxParser::SPBuffer::SPBuffer()
{
m_Data[0]='0'; m_Data[1]='\0'; m_Data[2]='0'; m_Data[3]='\0';
m_nCount=0; m_nPos=0;
}
inline void SaxParser::SPBuffer::Put(char c)
{
assert(m_nCount<4);
m_Data[m_nCount]=c;
m_nCount++;
}
char SaxParser::SPBuffer::Extract()
{
assert(m_nCount>0);
m_nPos++;
char c=m_Data[m_nPos-1];
if (m_nPos==m_nCount)
{
m_nPos=0; m_nCount=0;
}
return c;
}
inline char SaxParser::SPBuffer::Get(unsigned int pos) const
{
assert(pos<m_nCount);
return m_Data[pos];
}
inline void SaxParser::SPBuffer::PutBack(char c)
{
assert(m_nCount<4);
if (IsEmpty())
Put(c);
else
{
if (m_nPos>0)
{
m_nPos--;
m_Data[m_nPos]=c;
}
else
{
for (int i=2; i>=0; i--)
m_Data[i+1]=m_Data[i];
m_Data[0]=c;
m_nCount++;
}
}
}
inline unsigned char SaxParser::SPBuffer::Current() const
{
assert(!IsEmpty());
return m_Data[m_nPos];
}
inline bool SaxParser::SPBuffer::IsEmpty() const
{
return m_nCount==0 ? true : false;
}
char SaxParser::SPBuffer::operator[] (unsigned int i) const
{
return Get(i);
}
inline const string SaxParser::SPBuffer::GetData() const
{
assert(m_nCount>0);
return m_Data;
}
inline void SaxParser::SPBuffer::Clear()
{
m_nCount=0; m_nPos=0;
}
//////////////////////////////////////////////////////////////////////
// SaxParser class
//////////////////////////////////////////////////////////////////////
SaxParser::SaxParser()
{
m_pHandler = NULL; m_pStream = NULL; m_nLimit = 0;
m_bReadHeader=false; m_nEncoding=SPENC_UNKNOWN;
}
char SaxParser::ReadChar()
{
char c; m_pStream->get(c);
if (m_pStream->bad())
ThrowException(SPE_INPUT_DATA_ERROR, "Can't read stream");
return c;
}
void SaxParser::Parse(std::istream* pStream, XSPHandler* pHandler, int encoding)
{
m_pStream=pStream; m_pHandler=pHandler; m_nEncoding=encoding;
m_nLine = 0; m_nColumn = 0; m_bReadHeader = false; m_bEmpty = true;
m_buffer.Clear();
enum TEState
{
st_begin=0, // initial state
st_ready, // is ready to enter
st_text, // enter of text
st_processing, // XML processing instruction
st_open, // enter of open tag
st_analys, // analysis of type after '!'
st_element, // enter of element
st_close, // Close of element
st_remark, // enter of remark
st_cdata, // enter of CDATA
st_dtd, // enter of DTD definition
st_entity, // enter of entity (value after the ampersand)
st_finish, // enter finished
st_finish_extra, //enter of additional data that could appear after the document has been closed
};
TEState state=st_begin;
string temp;
string accum;
bool bOpen=false;
pHandler->OnDocumentBegin();
ReadSignature();
while (!IsEOF())
{
char c=GetChar();
switch(state)
{
case st_begin:
{
if (::IsSpace(c))
{
m_pHandler->OnNotLeadingChar(c);
continue;
}
switch(c)
{
case '<':
m_pHandler->OnOpenTag();
state=st_open;
break;
default:
ThrowException(SPE_INVALID_FORMAT, "not < in st_begin");
break;
}
}
break;
case st_open:
{
if (::IsSpace(c))
ThrowException(SPE_WHITESPASE_OPEN, "space");
switch(c)
{
case '?':
state=st_processing;
EnterProcessing();
state=st_ready;
break;
case '!':
state=st_analys;
break;
case '/':
state=st_close;
EnterClosingElement();
if (m_StackItems.size() == 0)
state=st_finish;
else
state=st_ready;
break;
default:
state=st_element;
EnterOpenElement(c);
bOpen=true;
if (m_StackItems.size() == 0)
state=st_finish;
else
state=st_ready;
break;
}
}
break;
case st_analys:
{
switch(c)
{
case '-':
state=st_remark;
EnterComment();
state=st_ready;
break;
case '[':
if (!bOpen)
ThrowException(SPE_CDATA_DOC_OPEN, "[");
state=st_cdata;
EnterCDATA();
state=st_ready;
break;
default:
if (c)
{
state=st_dtd;
EnterDTD(c);
state=st_ready;
}
else
ThrowException(SPE_INVALID_INSTANCE);
break;
}
}
break;
case st_ready:
{
if (::IsSpace(c) || ::IsControl(c))
{
m_pHandler->OnNotLeadingChar(c);
continue;
}
switch(c)
{
case '<':
m_pHandler->OnOpenTag();
state=st_open;
break;
case '&':
if (!bOpen)
ThrowException(SPE_ENTITY_DOC_OPEN);
state=st_entity;
EnterEntity(&temp);
accum=accum+temp;
state=st_text;
break;
default:
if (!bOpen)
ThrowException(SPE_TEXT_BEFORE_ROOT);
accum+=c;
state=st_text;
break;
}
}
break;
case st_text:
switch(c)
{
case '<':
TrimRight(accum,m_pHandler);
m_pHandler->OnText(accum);
m_pHandler->OnOpenTag();
accum.erase();
state=st_open;
break;
case '&':
state=st_entity;
EnterEntity(&temp);
accum=accum+temp;
state=st_text;
break;
default:
accum+=c;
if (m_nLimit!=0 && accum.size()>m_nLimit)
ThrowException(SPE_TOO_BIG_VALUE,accum);
}
break;
case st_finish:
if (::IsSpace(c))
{
m_pHandler->OnNotLeadingChar(c);
continue;
}
if (c!='<')
ThrowException(SPE_TEXT_AFTER_ROOT);
m_pHandler->OnOpenTag();
state=st_finish_extra;
break;
case st_finish_extra:
switch(c)
{
case '?':
EnterProcessing();
state=st_finish;
break;
case'!':
EnterComment();
state=st_finish;
break;
default:
ThrowException(SPE_TEXT_AFTER_ROOT);
}
}
}
switch(state)
{
case st_begin:
ThrowException(SPE_EMPTY);
break;
case st_ready:
if (m_StackItems.size() != 0)
ThrowException(SPE_ROOT_CLOSE);
break;
case st_text:
ThrowException(SPE_TEXT_AFTER_ROOT);
break;
}
m_pHandler->OnDocumentEnd();
}
void SaxParser::EnterProcessing()
{
string accum; char store;
enum TEState {st_begin,st_first_part,st_enter,st_check_end,st_end};
TEState state=st_begin;
try
{
while (true)
{
unsigned char c=GetChar();
switch(state)
{
case st_begin:
if (IsFirstNameValid(c))
{
accum+=c;
state=st_first_part;
}
else
ThrowException(SPE_PROCESSING_NAME);
break;
case st_first_part:
if (::IsSpace(c))
{
bool bError;
if (IsXML(accum,bError))
{
EnterDeclaration();
return;
}
else
{
if (bError)
ThrowException(SPE_RESERVED_NAME);
accum+=c;
state=st_enter;
}
break;
}
switch(c)
{
case '?':
m_pHandler->OnProcessing(accum);
state=st_end;
break;
default:
if (IsCharNameValid(c))
accum+=c;
else
ThrowException(SPE_PROCESSING_NAME);
}
break;
case st_enter:
switch(c)
{
case '?':
store=c;
state=st_check_end;
break;
case '>':
ThrowException(SPE_PROCESSING_CLOSE);
break;
default:
accum+=c;
break;
}
break;
case st_end:
if (::IsSpace(c))
ThrowException(SPE_WHITESPACE_PROCESS);
if (c!='>')
ThrowException(SPE_MISSING_CLOSING);
m_pHandler->OnCloseTag();
break;
case st_check_end:
if (c=='>')
{
m_pHandler->OnProcessing(accum);
m_pHandler->OnCloseTag();
return;
}
else
{
accum+=store; accum+=c;
state=st_enter;
}
break;
}
}
}
catch (SaxParserException& e)
{
RethrowException(e, SPE_EOF, SPE_MISSING_CLOSING);
}
}
void SaxParser::EnterDeclaration()
{
SAttribute version,encoding,sdecl;
typedef enum {st_version,st_end_version,st_end_encoding,st_standalone,st_end,st_finish} TEState;
TEState state=st_version;
bool bProcess=true;
try
{
while (bProcess)
{
char c=GetChar();
switch(state)
{
case st_version:
if (::IsSpace(c))
continue;
EnterAttribute(&version,c);
if (version.m_name!="version")
ThrowException(SPE_INVALID_DECL);
if (version.m_value!="1.0")
ThrowException(SPE_VERSION);
state=st_end_version;
SkipWhiteSpace();
break;
case st_end_version:
switch(c)
{
case 'e':
EnterAttribute(&encoding,c);
if (encoding.m_name!="encoding")
ThrowException(SPE_INVALID_DECL);
if (encoding.m_value!="")
ThrowException(SPE_ENCODING);
SkipWhiteSpace();
state=st_end_encoding;
break;
case 's':
EnterAttribute(&sdecl,c);
if (sdecl.m_name!="standalone")
ThrowException(SPE_INVALID_DECL);
if (!(sdecl.m_value!="yes" || sdecl.m_value!="no"))
ThrowException(SPE_INVALID_DECL);
SkipWhiteSpace();
state=st_end;
break;
case '?':
state=st_finish;
break;
case '>':
ThrowException(SPE_PROCESSING_CLOSE);
default:
ThrowException(SPE_INVALID_DECL);
}
break;
case st_end_encoding:
switch (c)
{
case 's':
EnterAttribute(&sdecl,c);
if (sdecl.m_name!="standalone")
ThrowException(SPE_INVALID_DECL);
if (!(sdecl.m_value!="yes" || sdecl.m_value!="no"))
ThrowException(SPE_INVALID_DECL);
SkipWhiteSpace();
state=st_end;
break;
case '?':
state=st_finish;
break;
case '>':
ThrowException(SPE_PROCESSING_CLOSE);
break;
default:
ThrowException(SPE_INVALID_DECL);
}
break;
case st_end:
switch(c)
{
case '?':
state=st_finish;
break;
case '>':
ThrowException(SPE_PROCESSING_CLOSE);
default:
ThrowException(SPE_INVALID_DECL);
}
break;
case st_finish:
if (::IsSpace(c))
ThrowException(SPE_WHITESPACE_PROCESS);
if (c!='>')
ThrowException(SPE_MISSING_CLOSING);
bProcess=false;
break;
}
}
string szEncoding; string szStandalone;
if (encoding.m_value.size()!=0) szEncoding=encoding.m_value;
if (sdecl.m_value.size()!=0) szStandalone=sdecl.m_value;
m_pHandler->OnDeclaration(version.m_value,szEncoding,szStandalone);
m_pHandler->OnCloseTag();
if (strcmpi(encoding.m_value.c_str(),"UTF-8")==0)
m_nEncoding=SPENC_UTF_8;
}
catch (SaxParserException& e)
{
RethrowException(e, SPE_EOF, SPE_MISSING_CLOSING);
}
}
void SaxParser::EnterOpenElement(char c)
{
if (!::IsFirstNameValid(c))
ThrowException(SPE_ELEMENT_NAME);
std::list<std::string> listAttrNames;
string accum; accum=c;
enum TEState {st_name=0,st_end_name,st_end_attr,st_single};
TEState state=st_name;
try
{
while (true)
{
char c=GetChar();
switch(state)
{
case st_name:
if (IsSpace(c))
{
SkipWhiteSpace();
m_pHandler->OnElementBegin(accum);
m_StackItems.push_back(accum);
state=st_end_name;
}
else
{
switch(c)
{
case '>':
m_pHandler->OnElementBegin(accum);
m_pHandler->OnCloseTag();
m_StackItems.push_back(accum);
return;
break;
case '/':
m_pHandler->OnElementBegin(accum);
m_pHandler->OnCloseSingleElement(accum);
state=st_single;
break;
default:
if (!::IsCharNameValid(c))
ThrowException(SPE_ELEMENT_NAME);
else
accum+=c;
}
}
break;
case st_end_name:
if (c=='/')
{
m_pHandler->OnCloseSingleElement(accum);
m_StackItems.pop_back();
state=st_single;
}
else
{
SAttribute attr;
EnterAttribute(&attr,c);
for (auto i=listAttrNames.begin(); i!=listAttrNames.end(); i++)
{
if ((*i) == attr.m_name)
ThrowException(SPE_DUBLICATE_ATTRIBUTE);
}
listAttrNames.push_back(attr.m_name);
m_pHandler->OnAttribute(attr.m_name,attr.m_value);
SkipWhiteSpace();
state=st_end_attr;
}
break;
case st_end_attr:
switch(c)
{
case '/':
m_pHandler->OnCloseSingleElement(accum);
m_StackItems.pop_back();
state=st_single;
break;
case '>':
m_pHandler->OnCloseTag();
return;
break;
default:
SAttribute attr;
EnterAttribute(&attr,c);
for(auto i = listAttrNames.begin(); i != listAttrNames.end(); i++)
{
if ((*i)==attr.m_name)
ThrowException(SPE_DUBLICATE_ATTRIBUTE);
}
listAttrNames.push_back(attr.m_name);
m_pHandler->OnAttribute(attr.m_name,attr.m_value);
SkipWhiteSpace();
state=st_end_attr;
}
break;
case st_single:
if (::IsSpace(c))
ThrowException(SPE_WHITESPASE_CLOSE);
if (c!='>')
ThrowException(SPE_MISSING_CLOSING);
m_pHandler->OnCloseTag();
return;
break;
}
}
}
catch (SaxParserException& e)
{
RethrowException(e, SPE_EOF, SPE_MISSING_CLOSING);
}
}
void SaxParser::EnterClosingElement()
{
if (m_StackItems.size() == 0)
ThrowException(SPE_MATCH);
typedef enum {st_begin,st_name,st_end} TEState;
string accum;
TEState state=st_begin;
try
{
while (true)
{
char c=GetChar();
switch(state)
{
case st_begin:
if (::IsSpace(c))
ThrowException(SPE_WHITESPASE_CLOSE);
if (!::IsFirstNameValid(c))
ThrowException(SPE_ELEMENT_NAME);
accum+=c;
state=st_name;
break;
case st_name:
if (::IsSpace(c))
{
SkipWhiteSpace();
state=st_end;
continue;
}
switch(c)
{
case '>':