-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastXML.pas
More file actions
1381 lines (1263 loc) · 41.2 KB
/
Copy pathFastXML.pas
File metadata and controls
1381 lines (1263 loc) · 41.2 KB
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
unit FastXML;
interface
uses System.SysUtils, System.Classes, System.Generics.Collections,
Winapi.Windows;
type
TXMLPartialElement = class;
TXMLElementArray = array of TXMLPartialElement;
PXMLElementParserOptions = ^TXMLElementParserOptions;
TXMLNewElement = function (const ElementName: string; ParseOptions: PXMLElementParserOptions; Root: Pointer): Pointer of object;
TXMLNewAttribute = procedure(const AttributeName, AttributeValue: string; Element: Pointer) of object;
TXMLCloseElement = procedure (Element: Pointer) of object;
TXMLNewText = procedure (const Value: string; Element: Pointer) of object;
TXMLNewProcessingInstruction = procedure (const Name, Params: string) of object;
TXMLElementParserOptions = record
OnNewAttribute: TXMLNewAttribute;
OnText: TXMLNewText;
SkipSpaceText: Boolean;
OnCData: TXMLNewText;
OnComment: TXMLNewText;
OnNewProcessingInstruction: TXMLNewProcessingInstruction;
OnNewInnerElement: TXMLNewElement; //Open child element
OnCloseChildElement: TXMLCloseElement; //Close child element
OnCloseElement: TXMLCloseElement; //Close current element
end;
TXMLReader = class
private
FRoot: TXMLPartialElement;
FOwned: Boolean;
FStream: TStream;
FBufferOffset: Int64;
FEncodingOwner: Boolean;
FEncoding: TEncoding;
FSkipedPart: Integer;
FAfterReadedPosition: Int64;
FWrongXML: Boolean;
FXMLVersion: string;
FIsStandalone: Boolean;
protected
function GetRoot: TXMLPartialElement;
function Init: string;
function ReadEncoding(var Str: string; var Position: Integer; var enc: string): Boolean;
function ReadName(var Str: string; var Position: Integer; out LastIndex: Integer; var Name: string): Boolean;
function SkipName(var Str: string; var Position: Integer; out LastIndex: Integer): Boolean;
function ReadAttributeValue(var Str: string; var Position: Integer; out LastIndex: Integer; var Value: string): Boolean;
function SkipAttributeValue(var Str: string; var Position: Integer; out LastIndex: Integer): Boolean;
function ReadComment(var Str: string; var Position: Integer; out LastIndex: Integer; var Value: string): Boolean;
function SkipComment(var Str: string; var Position: Integer; out LastIndex: Integer): Boolean;
procedure ExtendStringFromBuffer(var str: string); //inline;
function ReadNextBufString: string; //inline;
function ExtendString(var Str: string; var Offset: Integer; out LastIndex: Integer): Boolean; //inline;
function ExtendStringWithSafe(var Str: string; var Offset, LastIndex, BeginPos: Integer): Boolean; //inline;
procedure SetBufferOffset(const Value: Int64); //inline;
function Skip(var Str: string; var Position: Integer; out LastIndex: Integer; MinLen: Integer): Boolean; overload;
function Skip(var Str: string; var Position: Integer; MinLen: Integer): Boolean; overload; inline;
function SkipByMark(var Str: string; var Position: Integer; const Mark: string): Boolean;
function SkipByMarkWithSave(var Str, Value: string; var Position: Integer; const Mark: string): Boolean;
procedure Parse(const BeginValue: string; const DefaultParserOptions: TXMLElementParserOptions);
public
property XMLVersion: string read FXMLVersion;
property IsStandalone: Boolean read FIsStandalone;
property Root: TXMLPartialElement read GetRoot;
function XPath(const Path: string; CreateInnerBlocks: Boolean = False): TXMLElementArray;
function XPathFirst(const Path: string): TXMLPartialElement;
procedure LoadFromStream(AStream: TStream; const DefaultParserOptions: TXMLElementParserOptions; AOwned: Boolean = False);
procedure LoadFromFile(const FileName: string; const DefaultParserOptions: TXMLElementParserOptions);
procedure ParseString(const Value: string; const DefaultParserOptions: TXMLElementParserOptions);
destructor Destroy; override;
end;
TXMLElementMonoParserOptions = record
ParseAttrib: Boolean;
ParseText: Boolean;
SkipSpaceText: Boolean;
ParseCData: Boolean;
ParseComments: Boolean;
ParseInnerElements: Boolean;
ParseProcessingInstructions: Boolean;
end;
PXMLElementMonoParserOptions = ^TXMLElementMonoParserOptions;
TXMLMonoNewElement = function (const ElementName: string; var ParseOptions: TXMLElementMonoParserOptions; Root: Pointer): Pointer of object;
TXMLMonoReader = class (TXMLReader)
private
FOnNewElement: TXMLMonoNewElement;
FOnNewAttribute: TXMLNewAttribute;
FOnCloseElement: TXMLCloseElement;
FOnNewComment: TXMLNewText;
FOnNewText: TXMLNewText;
FOnCData: TXMLNewText;
FOnNewProcessingInstruction: TXMLNewProcessingInstruction;
protected
function OnNewInnerElement(const ElementName: string; ParseOptions: PXMLElementParserOptions; Root: Pointer): Pointer;
function GenerateOptions(const ParserOptions: TXMLElementMonoParserOptions): TXMLElementParserOptions;
public
DefaultParserOptions: TXMLElementMonoParserOptions;
constructor Create;
procedure LoadFromStream(AStream: TStream; AOwned: Boolean = False);
procedure LoadFromFile(const FileName: string);
procedure ParseString(const Value: string);
property OnNewElement: TXMLMonoNewElement read FOnNewElement write FOnNewElement;
property OnNewAttribute: TXMLNewAttribute read FOnNewAttribute write FOnNewAttribute;
property OnCloseElement: TXMLCloseElement read FOnCloseElement write FOnCloseElement;
property OnNewComment: TXMLNewText read FOnNewComment write FOnNewComment;
property OnNewText: TXMLNewText read FOnNewText write FOnNewText;
property OnCData: TXMLNewText read FOnCData write FOnCData;
property OnNewProcessingInstructions: TXMLNewProcessingInstruction read FOnNewProcessingInstruction write FOnNewProcessingInstruction;
end;
TXMLNodeType = (ntText, ntProcessingInstruction, ntNode, ntCData, ntComment);
TXMLNode = class
private
protected
public
Value: string;
NodeType: TXMLNodeType;
constructor Create(AValue: string; ANodeType: TXMLNodeType);
end;
TXMLElement = class (TXMLNode)
private
FAttributes: TDictionary<string, string>;
FNodes: TList<TXMLNode>;
protected
public
property Attributes: TDictionary<string, string> read FAttributes;
property Nodes: TList<TXMLNode> read FNodes;
constructor Create(AName: string; ANodeType: TXMLNodeType = ntNode);
destructor Destroy; override;
end;
TXMLDocument = class
private
FFullDocument: TList<TXMLNode>;
FElementsById: TDictionary<string, TList<TXMLElement>>;
protected
FRoot: TXMLElement;
function OnNewElement(const ElementName: string; var ParseOptions: TXMLElementMonoParserOptions; Root: Pointer): Pointer; virtual;
procedure OnNewAttribute(const AttributeName, AttributeValue: string; Element: Pointer); virtual;
procedure OnCloseElement(Element: Pointer); virtual;
procedure OnNewComment(const Value: string; Element: Pointer); virtual;
procedure OnNewText(const Value: string; Element: Pointer); virtual;
procedure OnCData(const Value: string; Element: Pointer); virtual;
procedure OnNewProcessingInstructions(const Name, Params: string); virtual;
procedure OnDeleteIds(Sender: TObject; const Item: TList<TXMLElement>; Action: TCollectionNotification);
public
property Root: TXMLElement read FRoot;
property FullDocument: TList<TXMLNode> read FFullDocument;
property ElementsById: TDictionary<string, TList<TXMLElement>> read FElementsById;
constructor Create;
destructor Destroy; override;
procedure Clear;
function XPathFirst(const Path: string): TXMLElement;
procedure InitReader(Reader: TXMLMonoReader); virtual;
end;
TXMLPartialElement= class
private
FOwner: TXMLReader;
FStartPosition: Int64;
FAttributes: TDictionary<string, string>;
FFullAttributes: Boolean;
FName: string;
protected
procedure LoadAttributes;
function GetAttribute(const Index: string): string;
function GetAttributesCount: Integer;
public
constructor Create(AOwner: TXMLReader; AStartPosition: Int64; AName: string);
function NextSibling: TXMLPartialElement;
function NextElementSibling: TXMLPartialElement;
function FirstChild: TXMLPartialElement;
function FirstElementChild: TXMLPartialElement;
property Attribute[const Index: string]: string read GetAttribute;
function HasAttribute(const Name: string): Boolean;
property AttributesCount: Integer read GetAttributesCount;
property Name: string read FName;
end;
type
TStaticCharArray = array [0..0] of Char;
PStaticCharArray = ^TStaticCharArray;
implementation
const
XMLDecl = '<?xml';
{ TXMLReader }
destructor TXMLReader.Destroy;
begin
if FEncodingOwner then
FEncoding.Free;
if FOwned then
FStream.Free;
inherited;
end;
function TXMLReader.ExtendString(var Str: string; var Offset: Integer;
out LastIndex: Integer): Boolean;
var tmp: string;
begin
tmp:= ReadNextBufString;
if tmp = '' then
Exit(False);
Str:= Concat(PChar(@Str[Offset]), tmp);
Offset:= Low(string);
LastIndex:= High(Str);
Result:= True;
end;
procedure TXMLReader.ExtendStringFromBuffer(var str: string);
var Buffer: array [0..4095] of Byte;
Len, BufLen, OldLen: Integer;
OldPosition: Int64;
begin
OldPosition:= FStream.Position;
BufLen:= FStream.Read(Buffer[0], SizeOf(Buffer));
Len:= FEncoding.GetCharCount(Buffer, 0, BufLen);
OldLen:= Length(str) + Low(string);
SetLength(str, Length(str) + Len);
FEncoding.GetChars(Buffer, 0, BufLen, Slice(PStaticCharArray(@str[OldLen])^, Len), 0);
SetBufferOffset(OldPosition + FEncoding.GetByteCount(str, OldLen, Len));
end;
function TXMLReader.ExtendStringWithSafe(var Str: string; var Offset,
LastIndex, BeginPos: Integer): Boolean;
var tmp: string;
begin
tmp:= ReadNextBufString;
if tmp = '' then
Exit(False);
Str:= Concat(PChar(@Str[BeginPos]), tmp);
Dec(Offset, BeginPos - Low(string));
BeginPos:= Low(string);
LastIndex:= High(Str);
Result:= True;
end;
function TXMLReader.GetRoot: TXMLPartialElement;
begin
end;
function TXMLReader.Init: string;
var i, NextPos: Integer;
s: string;
version, Len, h: Integer;
open: Char;
tmp: string;
Buffer: array [0..4095] of Byte;
CodePage: Cardinal;
begin
if FEncodingOwner then
FEncoding.Free;
FEncoding:= nil;
FEncodingOwner:= False;
SetBufferOffset(0);
Len:= FStream.Read(Buffer[0], SizeOf(Buffer));
FBufferOffset:= FStream.Position;
i:= 0;
if (Len >= 3) and (Buffer[0] = $EF) and (Buffer[1] = $BB) and (Buffer[2] = $BF) then begin
Inc(i, 3);
FEncoding:= TEncoding.UTF8;
end else if (Len >= 2) and (Buffer[0] = $FF) and (Buffer[1] = $FE) then begin
Inc(i, 2);
FEncoding:= TEncoding.Unicode;
end else if (Len >= 2) and (Buffer[0] = $FE) and (Buffer[1] = $FF) then begin
Inc(i, 2);
FEncoding:= TEncoding.BigEndianUnicode;
end else if (Len >= 2) and (Buffer[1] = 0) then
FEncoding:= TEncoding.Unicode
else if (Len >= 2) and (Buffer[0] = 0) then
FEncoding:= TEncoding.BigEndianUnicode
else
FEncoding:= TEncoding.UTF8;
NextPos:= FEncoding.GetCharCount(Buffer, i, Len - i);
SetLength(s, NextPos);
FEncoding.GetChars(Buffer, i, Len - i, Slice(PStaticCharArray(@s[Low(string)])^, NextPos), 0);
SetBufferOffset(i + FEncoding.GetByteCount(s));
FIsStandalone:= False;
h:= High(s);
if StrLIComp(PChar(s), XMLDecl, Length(XMLDecl)) = 0 then begin
FWrongXML:= True;
i:= Length(XMLDecl) + Low(string);
if not Skip(s, i, h, 7) then
Exit;
if StrLComp(PChar(@s[i]), 'version', 7) = 0 then begin
Inc(i, 7);
if not Skip(s, i, h, 1) then
Exit;
if s[i] <> '=' then
Exit;
Inc(i);
if not Skip(s, i, h, 5) then
Exit;
open:= s[i];
if (open <> '"') and (open <> '''') then
Exit;
Inc(i);
version:= i;
if (s[i] <> '1') and (s[i + 1] <> '.') then
Exit;
Inc(i, 2);
while (s[i] >= '0') and (s[i] <= '9') do begin
while (s[i] >= '0') and (s[i] <= '9') do Inc(i);
if i > h then begin
if not ExtendStringWithSafe(s, i, h, version) then
Exit;
end;
end;
if open <> s[i] then
Exit;
SetString(FXMLVersion, PChar(@s[version]), i - version);
Inc(i);
end;
if not ReadEncoding(s, i, tmp) then
Exit;
if tmp <> '' then try
FEncoding:= TEncoding.GetEncoding(tmp);
FEncodingOwner:= True;
except
Exit;
end;
if not Skip(s, i, h, 10) then
Exit;
if StrLComp(PChar(@s[i]), 'standalone', 10) = 0 then begin
Inc(i, 10);
if not Skip(s, i, h, 1) then
Exit;
if s[i] <> '=' then
Exit;
Inc(i);
if not Skip(s, i, h, 4) then
Exit;
open:= s[i];
if StrLComp(PChar(@s[i + 1]), 'yes', 3) = 0 then begin
Inc(i, 4);
FIsStandalone:= True;
end else if StrLComp(PChar(@s[i + 1]), 'no', 2) = 0 then begin
Inc(i, 3);
FIsStandalone:= False;
end else
Exit;
if open <> s[i] then
Exit;
Inc(i);
end;
if not Skip(s, i, h, 2) then
Exit;
if StrLComp(PChar(@s[i]), '?>', 2) <> 0 then
Exit;
Inc(i, 2);
Result:= s;
FSkipedPart:= i;
FAfterReadedPosition:= FBufferOffset;
FWrongXML:= False;
end else begin
FXMLVersion:= '1.0';
FIsStandalone:= False;
Result:= ''
end;
end;
procedure TXMLReader.LoadFromFile(const FileName: string; const DefaultParserOptions: TXMLElementParserOptions);
var Stream: TFileStream;
begin
Stream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
LoadFromStream(Stream, DefaultParserOptions, True);
end;
procedure TXMLReader.LoadFromStream(AStream: TStream; const DefaultParserOptions: TXMLElementParserOptions; AOwned: Boolean);
begin
FOwned:= AOwned;
FStream:= AStream;
FBufferOffset:= -1;
Parse(Init, DefaultParserOptions);
end;
procedure TXMLReader.Parse(const BeginValue: string; const DefaultParserOptions: TXMLElementParserOptions);
var Count: Integer;
CurOpt, Restrict: TXMLElementParserOptions;
s, Name: string;
i, h: Integer;
CurElement: Pointer;
Names: TList<string>;
Elements: TList;
Options: TList<TXMLElementParserOptions>;
procedure CloseCurrentTag;
begin
if Assigned(CurOpt.OnCloseElement) then
CurOpt.OnCloseElement(CurElement);
CurOpt:= Options.Last;
if Assigned(CurOpt.OnCloseChildElement) then
CurOpt.OnCloseChildElement(CurElement);
if Assigned(CurOpt.OnNewInnerElement) then begin
Elements.Delete(Elements.Count - 1);
CurElement:= Elements.Last;
end;
Options.Delete(Options.Count - 1);
Names.Delete(Names.Count - 1);
end;
function _ReadComment: Boolean;
var vStart: Integer;
tmp, Value: string;
begin
if Assigned(CurOpt.OnComment) then begin
Result:= ReadComment(s, i, h, Value);
CurOpt.OnComment(Value, CurElement);
end else begin
Result:= SkipComment(s, i, h);
end;
end;
var Value: string;
NewElement: TXMLNewElement;
label _dropElement;
begin
s:= BeginValue;
if DefaultParserOptions.SkipSpaceText then begin
if not Skip(s, FSkipedPart, h, 2) then
Exit;
end else if FSkipedPart + 2 >= High(s) then
ExtendString(s, FSkipedPart, h);
if FSkipedPart > High(s) then
Exit;
{Restrict.ParseAttrib:= Assigned(FOnNewAttribute);
Restrict.ParseText:= Assigned(FOnNewText);
Restrict.ParseCData:= Assigned(FOnCData);
Restrict.ParseComments:= Assigned(FOnNewComment);
Restrict.ParseInnerElements:= Assigned(FOnNewElement);
Restrict.ParseProcessingInstructions:= Assigned(FOnNewProcessingInstructions);}
if not Assigned(DefaultParserOptions.OnNewInnerElement) then
Exit;
CurElement:= nil;
Elements:= nil;
Options:= nil;
Names:= TList<string>.Create;
try
Options:= TList<TXMLElementParserOptions>.Create;
Elements:= TList.Create;
Elements.Add(nil);
CurOpt:= DefaultParserOptions;
{CurOpt.ParseAttrib:= DefaultParserOptions.ParseAttrib and Restrict.ParseAttrib;
CurOpt.ParseText:= DefaultParserOptions.ParseText and Restrict.ParseText;
CurOpt.ParseCData:= DefaultParserOptions.ParseCData and Restrict.ParseCData;
CurOpt.ParseComments:= DefaultParserOptions.ParseComments and Restrict.ParseComments;
CurOpt.ParseInnerElements:= DefaultParserOptions.ParseInnerElements and Restrict.ParseInnerElements;
CurOpt.ParseProcessingInstructions:= DefaultParserOptions.ParseAttrib and Restrict.ParseProcessingInstructions;
CurOpt.SkipSpaceText:= DefaultParserOptions.SkipSpaceText;}
i:= FSkipedPart;
while i <= h do begin
while s[i] = '<' do begin
Inc(i);
case s[i] of
'!': begin
if h - i < 2 then
if not ExtendString(s, i, h) then
goto _dropElement;
if (s[i + 1] = '-') and (s[i + 2] = '-') then begin
Inc(i, 3);
if not _ReadComment then
goto _dropElement;
end else begin
if h - i < 7 then
if not ExtendString(s, i, h) then
goto _dropElement;
if StrLComp(@PChar(Pointer(s))[i], '[CDATA[', 7) = 0 then begin
if Elements.Count = 0 then
raise Exception.Create('Error Message');
Inc(i, 8);
if Assigned(CurOpt.OnCData) then
{$MESSAGE WARN 'Not realised'}
raise ENotImplemented.Create('CDATA')
else
SkipByMark(s, i, ']]>');
end else if StrLComp(@PChar(Pointer(s))[i], 'DOCTYPE', 7) = 0 then begin
if Elements.Count <> 0 then
raise Exception.Create('Error Message');
end else
goto _dropElement;
end;
end;
'?': begin
Inc(i);
if Assigned(CurOpt.OnNewProcessingInstruction) then begin
if ReadName(s, i, h, Name) then begin
if not Skip(s, i, h, 1) then
goto _dropElement;
if not SkipByMarkWithSave(s, Value, i, '?>') then
goto _dropElement;
Inc(i, 2);
CurOpt.OnNewProcessingInstruction(Name, Value);
end;
end else begin
if not SkipByMark(s, i, '?>') then
goto _dropElement;
end;
end;
//Close tag
'/': begin
Inc(i);
if ReadName(s, i, h, Name) then begin
if Name <> Names.Last then
raise Exception.Create('Error Message');
CloseCurrentTag;
if not Skip(s, i, h, 1) then
goto _dropElement;
if s[i] <> '>' then
goto _dropElement;
Inc(i);
end else
goto _dropElement;
end;
else
if ReadName(s, i, h, Name) then begin
Names.Add(Name);
Options.Add(CurOpt);
NewElement:= CurOpt.OnNewInnerElement;
FillChar(CurOpt, SizeOf(CurOpt), 0);
if Assigned(NewElement) then begin
CurOpt.SkipSpaceText:= DefaultParserOptions.SkipSpaceText;
CurElement:= NewElement(Name, @CurOpt, CurElement);
Elements.Add(CurElement);
{CurOpt.ParseAttrib:= CurOpt.ParseAttrib and Restrict.ParseAttrib;
CurOpt.ParseText:= CurOpt.ParseText and Restrict.ParseText;
CurOpt.ParseCData:= CurOpt.ParseCData and Restrict.ParseCData;
CurOpt.ParseComments:= CurOpt.ParseComments and Restrict.ParseComments;
CurOpt.ParseInnerElements:= CurOpt.ParseInnerElements and Restrict.ParseInnerElements;
CurOpt.ParseProcessingInstructions:= CurOpt.ParseAttrib and Restrict.ParseProcessingInstructions; }
end else begin
CurElement:= nil;
CurOpt.SkipSpaceText:= True;
end;
if not Skip(s, i, h, 1) then
Break;
if Assigned(CurOpt.OnNewAttribute) then begin
while (s[i] <> '/') and (s[i] <> '>') do begin
if not ReadName(s, i, h, Name) then
goto _dropElement;
if not Skip(s, i, h, 1) then
goto _dropElement;
if s[i] <> '=' then
goto _dropElement;
Inc(i);
if not Skip(s, i, h, 1) then
goto _dropElement;
if not ReadAttributeValue(s, i, h, Value) then
goto _dropElement;
CurOpt.OnNewAttribute(Name, Value, CurElement);
if not Skip(s, i, h, 1) then
goto _dropElement;
end;
end else begin
while (s[i] <> '/') and (s[i] <> '>') do begin
if not SkipName(s, i, h) then
goto _dropElement;
if not Skip(s, i, h, 1) then
goto _dropElement;
if s[i] <> '=' then
goto _dropElement;
Inc(i);
if not Skip(s, i, h, 1) then
goto _dropElement;
if not SkipAttributeValue(s, i, h) then
goto _dropElement;
if not Skip(s, i, h, 1) then
goto _dropElement;
end;
end;
if s[i] = '/' then begin
//Empty Tag
Inc(i);
if i > h then
if not ExtendString(s, i, h) then
goto _dropElement;
if s[i] = '>' then begin
Inc(i);
CloseCurrentTag;
end else
goto _dropElement;
end else if s[i] = '>' then begin
{$MESSAGE WARN 'Don''t needed???'}
{if not CurOpt.ParseInnerElements then
CurOpt.ParseAttrib:= False; }
Inc(i);
end else
goto _dropElement;
end else
goto _dropElement;
end;
if CurOpt.SkipSpaceText then
Skip(s, i, h, 2)
else begin
h:= High(s);
if i + 2 >= h then
ExtendString(s, i, h);
end;
end;
if Assigned(CurOpt.OnText) then begin
if not Assigned(CurOpt.OnNewProcessingInstruction) then begin
SkipByMarkWithSave(s, Name, i, '<');
end else
{$MESSAGE WARN 'Not realised'}
raise ENotImplemented.Create('ParseText');
CurOpt.OnText(Name, CurElement);
if CurOpt.SkipSpaceText then
Skip(s, i, h, 2)
else begin
h:= High(s);
if (i + 2 >= h) and not ExtendString(s, i, h) then
goto _dropElement;
end;
end else begin
Inc(i);
Skip(s, i, h, 2);
end;
end;
_dropElement:
finally
Names.Free;
Options.Free;
Elements.Free;
end;
end;
procedure TXMLReader.ParseString(const Value: string; const DefaultParserOptions: TXMLElementParserOptions);
begin
FOwned:= True;
FStream:= TMemoryStream.Create;
FBufferOffset:= -1;
FSkipedPart:= Low(string);
Parse(Value, DefaultParserOptions);
end;
function TXMLReader.ReadAttributeValue(var Str: string; var Position: Integer; out LastIndex: Integer;
var Value: string): Boolean;
var open: Char;
s, ref: string;
i, h, vStart: Integer;
begin
s:= Str;
i:= Position;
open:= s[i];
Inc(i);
vStart:= i;
h:= High(s);
if (open <> '''') and (open <> '"') then
Exit(False);
Result:= False;
while ((i <= h) or ExtendStringWithSafe(s, i, h, vStart)) and
(s[i] <> '<') and (s[i] <> open) do begin
Inc(i);
{if s[i] = '&' then begin
Inc(i);
if h - i < 2 then
if not ExtendStringWithSafe(s, i, h, vStart) then
Break;
if s[i] = '#' then begin
end else begin
if not ReadName(s, i, ref) then
Break;
end;
end;}
end;
if s[i] = open then begin
Value:= Copy(s, vStart, i - vStart);
Inc(i);
Result:= True;
end;
Str:= s;
Position:= i;
LastIndex:= h;
end;
function TXMLReader.ReadComment(var Str: string; var Position: Integer;
out LastIndex: Integer; var Value: string): Boolean;
var s: string;
i, vStart: Integer;
h: Integer;
begin
s:= Str;
i:= Position;
h:= High(s);
vStart:= i;
while (i <= h - 3 + Low(string)) or ExtendStringWithSafe(s, i, h, vStart) do begin
while (s[i] <> '-') and (i <= h) do
Inc(i);
if i <= h - 3 + Low(string) then begin
if StrLComp(PChar(@s[i]), '-->', 3) = 0 then begin
Value:= Copy(s, vStart, i - vStart);
Inc(i, 3);
Position:= i;
Str:= s;
Exit(True);
end else begin
while ((i <= h - 3 + Low(string)) or ExtendStringWithSafe(s, i, h, vStart)) and (s[i] = '-') do
Inc(i);
end;
end;
end;
if vStart = Low(string) then
Value:= s
else
Value:= Copy(s, vStart);
Position:= i;
Str:= s;
Result:= False;
end;
function TXMLReader.ReadEncoding(var Str: string; var Position: Integer;
var enc: string): Boolean;
var s, tmp: string;
i, h: Integer;
encStart: Integer;
open: Char;
begin
s:= Str;
i:= Position;
h:= High(s);
if not Skip(s, i, h, 8) then
Exit(False);
if StrLComp(PChar(@s[i]), 'encoding', 8) = 0 then begin
Inc(i, 8);
if not Skip(s, i, h, 1) then
Exit(False);
if s[i] <> '=' then
Exit(True);
Inc(i);
if not Skip(s, i, h, 1) then
Exit(False);
open:= s[i];
Inc(i);
if ((open <> '"') and (open <> '''')) or not (((s[i] >= 'A') and (s[i] <= 'Z')) or ((s[i] >= 'a') and (s[i] <= 'z'))) then
Exit(True);
encStart:= i;
while ((s[i] >= '0') and (s[i] <= '9')) or ((s[i] >= 'A') and (s[i] <= 'Z')) or
((s[i] >= 'a') and (s[i] <= 'z')) or (s[i] = '.') or (s[i] = '_') or (s[i] = '-') do begin
while ((s[i] >= '0') and (s[i] <= '9')) or ((s[i] >= 'A') and (s[i] <= 'Z')) or
((s[i] >= 'a') and (s[i] <= 'z')) or (s[i] = '.') or (s[i] = '_') or (s[i] = '-') do
Inc(i);
if i > h then begin
if not ExtendStringWithSafe(s, i, h, encStart) then
Exit;
end;
end;
if s[i] <> open then
Exit(True);
enc:= Copy(s, encStart, i - encStart);
Inc(i);
end;
Str:= S;
Position:= i;
Result:= True;
end;
function GetCharIndex(): Integer; inline;
begin
end;
function TXMLReader.ReadName(var Str: string; var Position: Integer; out LastIndex: Integer;
var Name: string): Boolean;
var s, tmp: string;
p, index, h, nameStart: Integer;
begin
s:= Str;
p:= Position;
h:= High(s);
nameStart:= p;
if (p > h) and not ExtendStringWithSafe(s, p, h, nameStart) then begin
Name:= '';
Exit(False);
end;
case Word(s[p]) of
Ord(':'), Ord('A')..Ord('Z'), Ord('_'), Ord('a')..Ord('z'), $C0..$D6, $D8..$F6, $F8..$2FF, $370..$37D,
$37F..$1FFF, $200C..$200D, $2070..$218F, $2C00..$2FEF, $3001..$D7FF, $F900..$FDCF, $FDF0..$FFFD:
Inc(p);
//$10000..$EFFFF
$D800..$DB7F: begin
if p >= h then
raise Exception.Create('Error Message');
Inc(p, 2);
end
else
Name:= '';
Result:= False;
Exit;
end;
while (p <= h) or ExtendStringWithSafe(s, p, h, nameStart) do
case Word(s[p]) of
Ord(':'), Ord('A')..Ord('Z'), Ord('_'), Ord('a')..Ord('z'), $C0..$D6, $D8..$F6, $F8..$2FF, $370..$37D,
$37F..$1FFF, $200C..$200D, $2070..$218F, $2C00..$2FEF, $3001..$D7FF, $F900..$FDCF, $FDF0..$FFFD,
Ord('-'), Ord('.'), Ord('0')..Ord('9'), $B7, $0300..$036F, $203F..$2040:
Inc(p);
//#$10000..#$EFFFF
$D800..$DB7F: begin
if p >= h then
raise Exception.Create('Error Message');
Inc(p, 2);
end
else
Break;
end;
if (nameStart = Low(string)) and (p - 1 = h) then
Name:= s
else
Name:= Copy(s, nameStart, p - nameStart);
Str:= s;
Position:= p;
LastIndex:= h;
Result:= p <= h;
end;
function TXMLReader.ReadNextBufString: string;
var Buffer: array [0..4095] of Byte;
Len, BufLen, NewBufLen: Integer;
OldPosition: Int64;
begin
OldPosition:= FStream.Position;
BufLen:= FStream.Read(Buffer[0], SizeOf(Buffer));
if BufLen = 0 then
Exit('');
Len:= FEncoding.GetCharCount(Buffer, 0, BufLen);
while Len = 0 do begin
Dec(BufLen);
if BufLen <= 0 then
raise Exception.Create('Wrong string encoding');
Len:= FEncoding.GetCharCount(Buffer, 0, BufLen);
end;
Result:= '';
SetLength(Result, Len);
FEncoding.GetChars(Buffer, 0, BufLen, Slice(PStaticCharArray(@Result[Low(string)])^, Len), 0);
NewBufLen:= FEncoding.GetByteCount(Result);
if BufLen <> NewBufLen then
Len:= BufLen;
SetBufferOffset(OldPosition + NewBufLen);
end;
procedure TXMLReader.SetBufferOffset(const Value: Int64);
begin
if FBufferOffset <> Value then begin
FBufferOffset:= Value;
FStream.Position:= Value;
end;
end;
function TXMLReader.Skip(var Str: string; var Position: Integer; out LastIndex: Integer;
MinLen: Integer): Boolean;
var j, h: Integer;
tmp, s: string;
begin
j:= Position;
s:= Str;
h:= High(s);
if (h - j + 1 >= MinLen) or ExtendString(s, j, h) then
repeat
while (s[j] = ' ') or (s[j] = #9) or (s[j] = #10) or (s[j] = #13) do
Inc(j);
if (h - j + 1 < MinLen) and not ExtendString(s, j, h) then
Break;
until (s[j] <> ' ') and (s[j] <> #9) and (s[j] <> #10) and (s[j] <> #13);
Position:= j;
Str:= s;
Result:= (h - j + 1 >= MinLen);
LastIndex:= High(Str);
end;
function TXMLReader.Skip(var Str: string; var Position: Integer;
MinLen: Integer): Boolean;
var h: Integer;
begin
Result:= Skip(Str, Position, h, MinLen);
end;
function TXMLReader.SkipAttributeValue(var Str: string;
var Position: Integer; out LastIndex: Integer): Boolean;
var open: Char;
s, ref: string;
i, h: Integer;
begin
s:= Str;
i:= Position;
open:= s[i];
Inc(i);
h:= High(s);
if (open <> '''') and (open <> '"') then
Exit(False);
Result:= False;
while ((i <= h) or ExtendString(s, i, h)) and
(s[i] <> '<') and (s[i] <> open) do begin
Inc(i);
end;
if s[i] = open then begin
Inc(i);
Result:= True;
end;
Str:= s;
Position:= i;
LastIndex:= h;
end;
function TXMLReader.SkipByMark(var Str: string; var Position: Integer;
const Mark: string): Boolean;
var j, h, ml: Integer;
tmp, s: string;
begin
j:= Position;
s:= Str;
h:= High(s);
ml:= High(Mark);
while (j <= h - ml + Low(string)) or ExtendString(s, j, h) do begin
while (s[j] <> Mark[Low(string)]) and (j <= h) do
Inc(j);
if (j <= h - ml + Low(string)) then begin
if (StrLComp(PChar(@PChar(Pointer(s))[j - 1]), @PChar(Pointer(Mark))[0], ml + 1 - Low(string)) = 0) then begin
Inc(j, ml + 1 - Low(string));
Position:= j;
Str:= s;
Exit(True);
end else
Inc(j);
end;
end;
Position:= j;
Str:= s;
Result:= False;
end;
function TXMLReader.SkipByMarkWithSave(var Str, Value: string; var Position: Integer;
const Mark: string): Boolean;
var j, h, ml, vStart: Integer;
tmp, s: string;
begin
j:= Position;
vStart:= j;
s:= Str;
h:= High(s);
ml:= High(Mark);
while (j <= h - ml + Low(string)) or ExtendStringWithSafe(s, j, h, vStart) do begin
while (s[j] <> Mark[Low(string)]) and (j <= h) do
Inc(j);
if (j <= h - ml + Low(string)) and (StrLComp(PWideChar(@s[j]), PWideChar(@Mark[Low(string)]), ml + 1 - Low(string)) = 0) then begin
Value:= Copy(s, vStart, j - vStart);
//Inc(j, ml + 1 - Low(string));
Position:= j;
Str:= s;
Exit(True);
end;
end;
Value:= Copy(s, vStart);
Position:= j;
Str:= s;
Result:= False;
end;
function TXMLReader.SkipComment(var Str: string; var Position: Integer;
out LastIndex: Integer): Boolean;
var s: string;
i: Integer;
h: Integer;
begin
s:= Str;
i:= Position;
h:= High(s);
Result:= False;
while (i <= h - 3 + Low(string)) or ExtendString(s, i, h) do begin
while (s[i] <> '-') and (i <= h) do
Inc(i);
if (i <= h - 3 + Low(string)) then begin
if StrLComp(PChar(@s[i]), '-->', 3) = 0 then begin
Inc(i, 3);
Result:= True;
Break;