forked from neslib/Neslib.Xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeslib.Xml.pas
2726 lines (2326 loc) · 73.9 KB
/
Neslib.Xml.pas
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 Neslib.Xml;
{$INCLUDE 'Neslib.inc'}
interface
uses
System.Classes,
System.SysUtils,
Neslib.Utf8,
Neslib.Xml.IO,
Neslib.Xml.Types,
Neslib.Collections;
type
{ The type of a TXmlNode }
TXmlNodeType = (
{ The node is an element (eg. <node>), and may have child nodes. }
Element,
{ The node represents a text string.
This is the text between a start and end element.
For example, given the XML string '<foo>bar</foo>', the <foo> element
contains a single child node of type Text (with value 'bar').
A single element may contain multiple text nodes in case these are
seperated by a child node. For example, '<foo>bar<child/>baz</foo>'.
Here, the <foo> node contains 3 child nodes:
* A Text node with value 'bar'
* An Element node with name 'child'
* Another Text node with value 'baz' }
Text,
{ The node represents a comment (as in <!--foo-->).
The value of the node does not contain markup characters (eg. it is just
'foo' in this example). }
Comment,
{ The node represents a comment (as in <![CDATA[foo]]>).
The value of the node does not contain markup characters (eg. it is just
'foo' in this example). }
CData);
type
TXmlDocument = class;
{ A single attribute in a TXmlNode.
Note that it is legal to access properties and methods of a nil-attribute.
This will just not do anything. This way, you don't need to check the
validity of an attribute before using it. }
TXmlAttribute = record
{$REGION 'Internal Declarations'}
private const
S_FALSE = 'false';
S_TRUE = 'true';
private type
TEnumerator = record
private
FCurrent: PUInt64;
private
function GetCurrent: TXmlAttribute; inline;
public
constructor Create(const ANode: PUInt64);
function MoveNext: Boolean; inline;
property Current: TXmlAttribute read GetCurrent;
end;
private
FAttribute: PUInt64;
function GetNameIndex: Integer;
function GetName: XmlString; inline;
procedure SetName(const AValue: XmlString);
function GetNext: TXmlAttribute;
procedure SetNext(const AValue: TXmlAttribute);
function GetValue: XmlString;
procedure SetValue(const AValue: XmlString); overload;
private
function GetBlock: PByte; inline;
function GetDocument: TXmlDocument; inline;
procedure Free; inline;
{$ENDREGION 'Internal Declarations'}
public
{ (In)equality operators that can be used to compare two attributes.
You can also compare against nil-pointer (eg. if (MyAttr <> nil) then...') }
class operator Equal(const ALeft: TXmlAttribute; const ARight: Pointer): Boolean; overload; inline; static;
class operator Equal(const ALeft, ARight: TXmlAttribute): Boolean; overload; inline; static;
class operator NotEqual(const ALeft: TXmlAttribute; const ARight: Pointer): Boolean; overload; inline; static;
class operator NotEqual(const ALeft, ARight: TXmlAttribute): Boolean; overload; inline; static;
{ Tries to convert the value of the attribute to a Boolean.
Parameters:
ADefault: (optional) default value to return in case the value cannot
be converted. Defaults to False.
Returns:
The attribute value as a Boolean, or ADefault in case the value cannot
be converted, or this attribute is nil. }
function ToBoolean(const ADefault: Boolean = False): Boolean;
{ Tries to convert the value of the attribute to a 32-bit Integer.
Parameters:
ADefault: (optional) default value to return in case the value cannot
be converted. Defaults to 0.
Returns:
The attribute value as an integer, or ADefault in case the value cannot
be converted, or this attribute is nil. }
function ToInteger(const ADefault: Integer = 0): Integer; inline;
{ Tries to convert the value of the attribute to a 32-bit Integer.
Parameters:
ADefault: (optional) default value to return in case the value cannot
be converted. Defaults to 0.
Returns:
The attribute value as an integer, or ADefault in case the value cannot
be converted, or this attribute is nil. }
function ToInt32(const ADefault: Int32 = 0): Int32; inline;
{ Tries to convert the value of the attribute to a 64-bit Integer.
Parameters:
ADefault: (optional) default value to return in case the value cannot
be converted. Defaults to 0.
Returns:
The attribute value as an integer, or ADefault in case the value cannot
be converted, or this attribute is nil. }
function ToInt64(const ADefault: Int64 = 0): Int64; inline;
{ Tries to convert the value of the attribute to a single-precision
floating-point value.
Parameters:
ADefault: (optional) default value to return in case the value cannot
be converted. Defaults to 0.
Returns:
The attribute value as a Single, or ADefault in case the value cannot
be converted, or this attribute is nil. }
function ToSingle(const ADefault: Single = 0): Single; inline;
{ Tries to convert the value of the attribute to a double-precision
floating-point value.
Parameters:
ADefault: (optional) default value to return in case the value cannot
be converted. Defaults to 0.
Returns:
The attribute value as a Double, or ADefault in case the value cannot
be converted, or this attribute is nil. }
function ToDouble(const ADefault: Double = 0): Double; inline;
{ Sets the value of this attribute to a Boolean value.
Parameters:
AValue: the value to set.
Does nothing in case this attribute is nil. }
procedure SetValue(const AValue: Boolean); overload; inline;
{ Sets the value of this attribute to a 32-bit Integer value.
Parameters:
AValue: the value to set.
Does nothing in case this attribute is nil. }
procedure SetValue(const AValue: Int32); overload; inline;
{ Sets the value of this attribute to a 64-bit Integer value.
Parameters:
AValue: the value to set.
Does nothing in case this attribute is nil. }
procedure SetValue(const AValue: Int64); overload; inline;
{ Sets the value of this attribute to a single-precision floating-point
value.
Parameters:
AValue: the value to set.
Does nothing in case this attribute is nil. }
procedure SetValue(const AValue: Single); overload; inline;
{ Sets the value of this attribute to a double-precision floating-point
value.
Parameters:
AValue: the value to set.
Does nothing in case this attribute is nil. }
procedure SetValue(const AValue: Double); overload; inline;
{ The name of this attribute.
Returns an empty string when this attribute is nil.
Setting the name has no effect if this attribute is nil. }
property Name: XmlString read GetName write SetName;
{ The value of this attribute.
Returns an empty string when this attribute is nil.
Setting the value has no effect if this attribute is nil. }
property Value: XmlString read GetValue write SetValue;
{ The next attribute in this list of attributes of a TXmlNode.
Returns nil if this was the last attribute in the list. }
property Next: TXmlAttribute read GetNext;
end;
{ A set of attribute. This is a helper type that enables you to enumerate
the attribute in a TXmlNode, as in:
for var Attr in Node.Attributes do ... }
TXmlAttributes = record
{$REGION 'Internal Declarations'}
private
FNode: PUInt64;
{$ENDREGION 'Internal Declarations'}
public
{ Returns an attribute enumerator to enable for..in enumeration, as in:
for var Attr in Node.Attributes do ... }
function GetEnumerator: TXmlAttribute.TEnumerator; inline;
end;
{ A single XML node.
A single type is used for all supported types of nodes (Elements, Text,
Comments and CData)
Note that it is legal to access properties and methods of a nil-node.
This will just not do anything. This way, you don't need to check the
validity of a node before using it. For example, you can write:
Node.ChildByName('foo').ChildByName('bar').Value := 'baz';
This doesn't do anything is Node is nil, or any of the ChildByName methods
return nil. }
TXmlNode = record
{$REGION 'Internal Declarations'}
private type
TEnumerator = record
private
FCurrent: PUInt64;
private
function GetCurrent: TXmlNode; inline;
public
constructor Create(const ANode: PUInt64);
function MoveNext: Boolean; inline;
property Current: TXmlNode read GetCurrent;
end;
private const
ID_PARENT = 0;
ID_FIRST_CHILD = 1;
ID_NEXT_SIBLING = 2;
ID_PREV_SIBLING = 3;
ID_FIRST_ATTRIBUTE = 4;
private
FNode: PUInt64;
function GetIsEmpty: Boolean; inline;
function GetNodeType: TXmlNodeType; inline;
function GetValue: XmlString;
procedure SetValue(const AValue: XmlString);
function GetText: XmlString;
function GetValueIndex: Integer; inline;
procedure SetValueIndex(const AValue: Integer); inline;
function GetParent: TXmlNode;
procedure SetParent(const AValue: TXmlNode);
function GetAttributes: TXmlAttributes; inline;
function GetFirstAttribute: TXmlAttribute;
procedure SetFirstAttribute(const AValue: TXmlAttribute);
function GetFirstChild: TXmlNode;
procedure SetFirstChild(const AValue: TXmlNode);
function GetNextSibling: TXmlNode;
procedure SetNextSibling(const AValue: TXmlNode);
function GetPrevSibling: TXmlNode;
procedure SetPrevSibling(const AValue: TXmlNode);
function GetPrevSiblingEx: TXmlNode;
function GetDocument: TXmlDocument; inline;
private
procedure Free;
procedure InternalAddChild(const AChild: TXmlNode);
function InternalAddAttribute(const ANameIndex: Integer; const AValue: XmlString): TXmlAttribute;
function GetBlock: PByte; inline;
{$ENDREGION 'Internal Declarations'}
public
{ (In)equality operators that can be used to compare two attributes.
You can also compare against nil-pointer (eg. if (MyAttr <> nil) then...') }
class operator Equal(const ALeft: TXmlNode; const ARight: Pointer): Boolean; overload; inline; static;
class operator Equal(const ALeft, ARight: TXmlNode): Boolean; overload; inline; static;
class operator NotEqual(const ALeft: TXmlNode; const ARight: Pointer): Boolean; overload; inline; static;
class operator NotEqual(const ALeft, ARight: TXmlNode): Boolean; overload; inline; static;
{ Returns a nil node }
class function Create: TXmlNode; inline; static;
{ Returns a node enumerator to enable for..in enumeration, as in:
for var Child in Node do ... }
function GetEnumerator: TEnumerator; inline;
{ Returns the first child element with a given name.
Parameters:
AElementName: the name to search for.
Returns:
The first child element with the given element name, or nil if there is
none. }
function ElementByName(const AElementName: XmlString): TXmlNode;
{ Returns the first child element with an attribute with a given name and
value.
Parameters:
AAttributeName: the name of the attribute to search for.
AAttributeValue: the value of the attribute to search for.
Returns:
The first child element with an attribute with the given name and value,
or nil if there is none. }
function ElementByAttribute(const AAttributeName,
AAttributeValue: XmlString): TXmlNode; overload;
{ Returns the first child element of a given name, and with an attribute
with a given name and value.
Parameters:
AElementName: the name of the element to search for.
AAttributeName: the name of the attribute to search for.
AAttributeValue: the value of the attribute to search for.
Returns:
The first child element with the given name that has an attribute with
the given atrribute name and value, or nil if there is none. }
function ElementByAttribute(const AElementName, AAttributeName,
AAttributeValue: XmlString): TXmlNode; overload;
{ Returns the next sibling element with a given name.
Parameters:
AElementName: the name to search for.
Returns:
The next sibling element with the given element name, or nil if there is
none. }
function NextSiblingByName(const AElementName: XmlString): TXmlNode;
{ Returns the previous sibling element with a given name.
Parameters:
AElementName: the name to search for.
Returns:
The previous sibling element with the given element name, or nil if there
is none. }
function PrevSiblingByName(const AElementName: XmlString): TXmlNode;
{ Returns the first attribute with a given name.
Parameters:
AAttributeName: the attribute name to search for.
Returns:
The first attribute with the given name, or nil if there is none. }
function AttributeByName(const AAttributeName: XmlString): TXmlAttribute;
{ Adds an attribute.
Parameters:
AName: the name of the attribute.
AValue: the value of the attribute.
Returns:
The newly added attribute, or nil if this node is nil.
This method does *not* check for duplicate attribute names. }
function AddAttribute(const AName, AValue: XmlString): TXmlAttribute; overload;
function AddAttribute(const AName: XmlString; const AValue: Boolean): TXmlAttribute; overload; inline;
function AddAttribute(const AName: XmlString; const AValue: Int32): TXmlAttribute; overload; inline;
function AddAttribute(const AName: XmlString; const AValue: Int64): TXmlAttribute; overload; inline;
function AddAttribute(const AName: XmlString; const AValue: Single): TXmlAttribute; overload; inline;
function AddAttribute(const AName: XmlString; const AValue: Double): TXmlAttribute; overload; inline;
{ Removes an attribute with a given name.
Parameters:
AName: the name of the attribute to remove.
This method does nothing if there is no attribute with the given name,
or if this node is nil. }
procedure RemoveAttribute(const AName: XmlString); overload; inline;
{ Removes an attribute.
Parameters:
AAttr: the attribute to remove.
This method does nothing if the attribute does not belong to this node,
or if this node is nil. }
procedure RemoveAttribute(const AAttr: TXmlAttribute); overload;
{ Removes all attributes from this node. }
procedure RemoveAllAttributes;
{ Adds a child to this node.
Parameters:
AType: the type of node to add.
AValue: the value of the node (depending on AType).
Returns:
The newly added child node, or nil in case this node is nil.
It may be easier to use the shortcut methods AddElement, AddText,
AddCData and AddComment instead. }
function AddChild(const AType: TXmlNodeType; const AValue: XmlString): TXmlNode;
{ Adds a child element to this node.
Parameters:
AName: the name of the element.
Returns:
The newly added child element, or nil in case this node is nil.
This is a shortcut for AddChild(TXmlNodeType.Element, AName). }
function AddElement(const AName: XmlString): TXmlNode; inline;
{ Adds a child text node to this node.
Parameters:
AText: the text of the node.
Returns:
The newly added child text node, or nil in case this node is nil.
This is a shortcut for AddChild(TXmlNodeType.Text, AText). }
function AddText(const AText: XmlString): TXmlNode; inline;
{ Adds a child CData node to this node.
Parameters:
ACData: the CData.
Returns:
The newly added child CData node, or nil in case this node is nil.
This is a shortcut for AddChild(TXmlNodeType.CData, ACData). }
function AddCData(const ACData: XmlString): TXmlNode; inline;
{ Adds a child comment node to this node.
Parameters:
AComment: the comment.
Returns:
The newly added child comment node, or nil in case this node is nil.
This is a shortcut for AddChild(TXmlNodeType.Comment, AComment). }
function AddComment(const AComment: XmlString): TXmlNode; inline;
{ Removes a child element with a given name.
Parameters:
AElementName: the name of the child element to remove.
This method does nothing if there is no child element with the given name,
or if this node is nil. }
procedure RemoveChild(const AElementName: XmlString); overload; inline;
{ Removes a child node.
Parameters:
AChild: the child node to remove.
This method does nothing if the child node does not belong to this node,
or if this node is nil. }
procedure RemoveChild(const AChild: TXmlNode); overload;
{ Removes all child nodes from this node. }
procedure RemoveAllChildren;
{ Whether the node is empty (nil) }
property IsEmpty: Boolean read GetIsEmpty;
{ The type of the this node:
* Element: the node is an element (eg. <node>), and may have child nodes.
* Text: the node represents a text string. This is the text between a
start and end element.
For example, given the XML string '<foo>bar</foo>', the <foo> element
contains a single child node of type Text (with value 'bar').
A single element may contain multiple text nodes in case these are
seperated by a child node. For example, '<foo>bar<child/>baz</foo>'.
Here, the <foo> node contains 3 child nodes:
- A Text node with value 'bar'
- An Element node with name 'child'
- Another Text node with value 'baz'
* Comment: the node represents a comment (as in <!--foo-->). The Value of
the node does not contain markup characters (eg. it is just 'foo' in
this example).
* CData: the node represents a comment (as in <![CDATA[foo]]>). The Value
of the node does not contain markup characters (eg. it is just 'foo' in
this example). }
property NodeType: TXmlNodeType read GetNodeType;
{ The value of this node. Its meaning depents on the NodeType:
* For Element nodes, this is the name of the element.
* For Text nodes, this is the text.
* For Comment nodes, this is the comment.
* For CData nodes, this is the CData. }
property Value: XmlString read GetValue write SetValue;
{ The text of this node. This differs from the Value property in case this
is an Element:
* For Element nodes, this is the concatenation of all direct children of
this node that are of type Text or CData. A space will be added between
concatenated strings if needed.
* For Text nodes, this is the text.
* For Comment nodes, this is the comment.
* For CData nodes, this is the CData.
For example, given this XML code:
<node>foo<child/>bar</node>
The the Text property of <node> will return 'foo bar' (with a space
between them) }
property Text: XmlString read GetText;
{ This parent of this node, or nil if this is the root of the document
(See IXmlDocument.Root) }
property Parent: TXmlNode read GetParent;
{ The attributes of this node. This returns a helper type that can be used
to enumerate all attributes (as in: for var Attr in Node do...).
Use FirstAttribute to enumerate the attributes yourself.
Returns an empty set of attributes in case this is not an Element node. }
property Attributes: TXmlAttributes read GetAttributes;
{ The first attribute of this node, or nil in case this is not an Element
node, or if the element has no attributes, or this node is nil. You can
use this to manually enumerate the attributes, as in:
var Attr := Node.FirstAttribute;
while (Attr <> nil) do
begin
DoSomethingWith(Attr);
Attr := Attr.Next;
end; }
property FirstAttribute: TXmlAttribute read GetFirstAttribute;
{ The first child node of this node, or nil in case this is not an Element
node, or if the element has no children, or this node is nil. You can use
this to manually enumerate the child nodes, as in:
var Child := Node.FirstChild;
while (Child <> nil) do
begin
DoSomethingWith(Child);
Child := Child.NextSibling;
end; }
property FirstChild: TXmlNode read GetFirstChild;
{ The next sibling of this node, or nil in case this is not an Element
node, or this node is nil, or this node doesn't have any siblings. You can
use this to manually enumerate the child nodes, as in:
var Child := Node.FirstChild;
while (Child <> nil) do
begin
DoSomethingWith(Child);
Child := Child.NextSibling;
end; }
property NextSibling: TXmlNode read GetNextSibling;
{ The previous sibling of this node, or nil in case this is not an Element
node, or this node is nil, or this node doesn't have any siblings. }
property PrevSibling: TXmlNode read GetPrevSiblingEx;
end;
{ A XML document. This is the entry point of this XML library.
To create an instance, use TXmlDocument.Create. }
IXmlDocument = interface
['{9C4D673A-2750-4DE3-B700-AA463996D2F3}']
{$REGION 'Internal Declarations'}
function GetRoot: TXmlNode;
function GetDocumentElement: TXmlNode;
function GetInternPool: TXmlStringInternPool;
{$ENDREGION 'Internal Declarations'}
{ Clears the document. }
procedure Clear;
{ Loads the document from a file.
Parameters:
AFilename: the name of the XML file to load.
Raises:
EXmlParserError if the XML file is invalid. }
procedure Load(const AFilename: String); overload;
{ Loads the document from a stream.
Parameters:
AStream: the XML stream to load.
Raises:
EXmlParserError if the XML stream is invalid.
Just clears the document in case AStream is nil. }
procedure Load(const AStream: TStream); overload;
{ Loads the document from a UTF-8 encoded byte array.
Parameters:
ABytes: the byte array with UTF-8 encoding XML data.
Raises:
EXmlParserError if the XML data is invalid.
Just clears the document in case ABytes is nil. }
procedure Load(const ABytes: TBytes); overload;
{ Loads the document using an XML reader.
Parameters:
AReader: the XML reader used to load the XML data.
Raises:
EXmlParserError if the XML data is invalid.
Just clears the document in case AReader is nil. }
procedure Load(const AReader: TXmlReader); overload;
{ Parses an XML string into this document.
Parameters:
AXml: the XML data.
Raises:
EXmlParserError if the XML data is invalid. }
procedure Parse(const AXml: XmlString);
{ Saves the XML document to a file.
Parameters:
AFilename: the name of the XML file to save to.
AOptions: (optional) XML formatting options. }
procedure Save(const AFilename: String;
const AOptions: TXmlOutputOptions = DEFAULT_XML_OUTPUT_OPTIONS); overload;
{ Saves the XML document to a stream.
Parameters:
AStream: the stream to save to.
AOptions: (optional) XML formatting options.
Does nothing if AStream is nil. }
procedure Save(const AStream: TStream;
const AOptions: TXmlOutputOptions = DEFAULT_XML_OUTPUT_OPTIONS); overload;
{ Saves the XML document to an XML writer.
Parameters:
AWriter: the XML writer to use.
Does nothing if AWriter is nil. }
procedure Save(const AWriter: TXmlWriter); overload;
{ Converts the XML document to an UTF-8 encoded byte array.
Parameters:
AOptions: (optional) XML formatting options.
The output XML as an UTF-8 encoded byte array. }
function ToBytes(const AOptions: TXmlOutputOptions = DEFAULT_XML_OUTPUT_OPTIONS): TBytes;
{ Converts the XML document to an XML string.
Parameters:
AOptions: (optional) XML formatting options.
The output XML. }
function ToXml(const AOptions: TXmlOutputOptions = DEFAULT_XML_OUTPUT_OPTIONS): XmlString;
{ The Root of the document. Each document has an implicit root element
without a name. This is *not* the first element in the XML file (use the
DocumentElement property for that).
For example, consider this XML:
<!--some comment-->
<foo>bar</foo>
In this case, the Root node contains two child nodes: the first one is of
type Comment (with Value 'some comment') and the second one is of type
Element (with Value 'foo'). The second node is also the one returned by
the DocumentElement property.
Note that this property never returns nil. }
property Root: TXmlNode read GetRoot;
{ The document element node of this document. This is the first child node
that is of type Element.
For example, consider this XML:
<!--some comment-->
<foo>bar</foo>
In this case, the DocumentElement property returns the 'foo' element.
Returns nil if the document is empty. }
property DocumentElement: TXmlNode read GetDocumentElement;
end;
{ Implements the IXmlDocument interface }
TXmlDocument = class(TInterfacedObject, IXmlDocument)
{$REGION 'Internal Declarations'}
private class var
FPageSize: Integer;
{$IFDEF MSWINDOWS}
FAllocationGranularity: Integer;
{$ENDIF}
{$IFDEF CPU64BITS}
FBaseAddress: IntPtr;
{$ENDIF}
private
FRoot: TXmlNode;
FInternPool: TXmlStringInternPool;
FPointerMap: TXmlPointerMap;
FBlockCur: PByte;
FBlockEnd: PByte;
FFreeList: TStack<PUInt64>;
FAllocations: TList<Pointer>;
FNextBlock: PByte;
FLastBlock: PByte;
FClearing: Boolean;
private
constructor InternalCreate;
procedure Grow;
function AllocBlock: Pointer;
function CreateNode(const AType: TXmlNodeType): TXmlNode;
procedure ReturnNode(const ANode: TXmlNode); inline;
function CreateAttribute(const ANameIndex: Integer;
const AValue: XmlString): TXmlAttribute;
procedure ReturnAttribute(const AAttr: TXmlAttribute); inline;
{$IFDEF CPU64BITS}
private
class procedure StoreString(const ANodeOrAttr: PUInt64;
const AValue: XmlString); static;
class function RetrieveString(const ANodeOrAttr: PUInt64): XmlString; static;
{$ENDIF}
protected
{ IXmlDocument }
function GetRoot: TXmlNode;
function GetDocumentElement: TXmlNode;
function GetInternPool: TXmlStringInternPool;
procedure Clear;
procedure Load(const AFilename: String); overload;
procedure Load(const AStream: TStream); overload;
procedure Load(const ABytes: TBytes); overload;
procedure Load(const AReader: TXmlReader); overload;
procedure Parse(const AXml: XmlString);
procedure Save(const AFilename: String;
const AOptions: TXmlOutputOptions); overload;
procedure Save(const AStream: TStream;
const AOptions: TXmlOutputOptions); overload;
procedure Save(const AWriter: TXmlWriter); overload;
function ToBytes(const AOptions: TXmlOutputOptions = DEFAULT_XML_OUTPUT_OPTIONS): TBytes;
function ToXml(const AOptions: TXmlOutputOptions): XmlString;
{$ENDREGION 'Internal Declarations'}
public
{ Creates a new empty document }
class function Create: IXmlDocument; overload;
{ Creates a new document with a document element.
Parameters:
AElementName: name of the element to add. }
class function Create(const AElementName: XmlString): IXmlDocument; overload;
destructor Destroy; override;
end;
implementation
{$POINTERMATH ON}
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
{$ELSE}
Posix.UniStd,
Posix.SysMman,
{$ENDIF}
Neslib.SysUtils;
const
BLOCK_SIZE = 4096;
MIN_DELTA = 8;
MAX_DELTA = BLOCK_SIZE - 8;
NIL_BITS = 0;
HASH_BITS = $1FF;
type
PUInt32 = PCardinal;
{ TXmlAttribute }
{ VVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVV +-----NN NNNNNNNN NNNNNNN> >>>>>>>>
V: Value string pointer (32 bits)
+: Whether V is actually an index into the "additional strings" of the string
pool. Only used on 64-bit platforms when the string pointer does not fit
into 32 bits.
-: Unused (5 bits)
N: Name Index (17 bits)
>: next sibling (9 bits) }
class operator TXmlAttribute.Equal(const ALeft: TXmlAttribute;
const ARight: Pointer): Boolean;
begin
Result := (ALeft.FAttribute = ARight);
end;
class operator TXmlAttribute.Equal(const ALeft, ARight: TXmlAttribute): Boolean;
begin
Result := (ALeft.FAttribute = ARight.FAttribute);
end;
procedure TXmlAttribute.Free;
begin
if (FAttribute <> nil) then
begin
SetValue(''); // Decrease ref count
var Doc := GetDocument;
Assert(Doc <> nil);
Doc.ReturnAttribute(Self);
end;
end;
function TXmlAttribute.GetBlock: PByte;
begin
Result := PByte(UIntPtr(FAttribute) and not (BLOCK_SIZE - 1));
end;
function TXmlAttribute.GetDocument: TXmlDocument;
begin
var Block := GetBlock;
if (Block = nil) then
Result := nil
else
Result := TXmlDocument(PPointer(Block)^);
end;
function TXmlAttribute.GetName: XmlString;
{ VVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVV +-----NN NNNNNNNN NNNNNNN> >>>>>>>> }
begin
var Index := GetNameIndex;
if (Index <= 0) then
Exit('');
var Doc := GetDocument;
Assert(Doc <> nil);
Result := Doc.FInternPool.Get(Index);
end;
function TXmlAttribute.GetNameIndex: Integer;
{ VVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVV +-----NN NNNNNNNN NNNNNNN> >>>>>>>> }
begin
if (FAttribute = nil) then
Result := 0
else
begin
{$IFDEF CPU32BITS}
Result := (PUInt32(FAttribute)^ shr 9) and $1FFFF;
{$ELSE}
Result := (FAttribute^ shr 9) and $1FFFF;
{$ENDIF}
end;
end;
function TXmlAttribute.GetNext: TXmlAttribute;
{ VVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVV +-----NN NNNNNNNN NNNNNNN> >>>>>>>> }
begin
if (FAttribute = nil) then
Result.FAttribute := nil
else
begin
var Bits: UIntPtr;
{$IFDEF CPU32BITS}
Bits := PUInt32(FAttribute)^ and $1FF;
{$ELSE}
Bits := FAttribute^ and $1FF;
{$ENDIF}
if (Bits = NIL_BITS) then
Result.FAttribute := nil
else if (Bits = HASH_BITS) then
begin
var Doc := GetDocument;
Assert(Doc <> nil);
Result.FAttribute := Doc.FPointerMap.Get(FAttribute, 0);
end
else
begin
var Block := GetBlock;
Result.FAttribute := Pointer(Block + (Bits shl 3));
end;
end;
end;
function TXmlAttribute.GetValue: XmlString;
{ VVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVV +-----NN NNNNNNNN NNNNNNN> >>>>>>>> }
begin
if (FAttribute = nil) then
Exit('');
{$IFDEF CPU32BITS}
Result := XmlString(PPointer(FAttribute)[1]);
{$ELSE}
Result := TXmlDocument.RetrieveString(FAttribute);
{$ENDIF}
end;
class operator TXmlAttribute.NotEqual(const ALeft,
ARight: TXmlAttribute): Boolean;
begin
Result := (ALeft.FAttribute <> ARight.FAttribute);
end;
class operator TXmlAttribute.NotEqual(const ALeft: TXmlAttribute;
const ARight: Pointer): Boolean;
begin
Result := (ALeft.FAttribute <> ARight);
end;
procedure TXmlAttribute.SetName(const AValue: XmlString);
{ VVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVV +-----NN NNNNNNNN NNNNNNN> >>>>>>>> }
begin
if (FAttribute <> nil) then
begin
var Doc := GetDocument;
Assert(Doc <> nil);
var Index: UIntPtr := Doc.FInternPool.Get(AValue);
{$IFDEF CPU32BITS}
PUInt32(FAttribute)^ := (PUInt32(FAttribute)^ and $FC0001FF) or (Index shl 9);
{$ELSE}
FAttribute^ := (FAttribute^ and $FFFFFFFFFC0001FF) or (Index shl 9);
{$ENDIF}
end;
end;
procedure TXmlAttribute.SetNext(const AValue: TXmlAttribute);
{ VVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVV +-----NN NNNNNNNN NNNNNNN> >>>>>>>> }
begin
if (FAttribute <> nil) then
begin
var Block := GetBlock;
var Delta: IntPtr := PByte(AValue) - Block;
if (Delta >= MIN_DELTA) and (Delta < MAX_DELTA) then
begin
var Bits: UIntPtr := Delta shr 3;
{$IFDEF CPU32BITS}
PUInt32(FAttribute)^ := (PUInt32(FAttribute)^ and $FFFFFE00) or Bits;
{$ELSE}
FAttribute^ := (FAttribute^ and $FFFFFFFFFFFFFE00) or Bits;
{$ENDIF}
end
else
begin
{$IFDEF CPU32BITS}
PUInt32(FAttribute)^ := PUInt32(FAttribute)^ or $000001FF;
{$ELSE}
FAttribute^ := FAttribute^ or $00000000000001FF;
{$ENDIF}
var Doc := TXmlDocument(PPointer(Block)^);
Assert(Doc <> nil);
Doc.FPointerMap.Map(FAttribute, 0, AValue.FAttribute);
end;
end;
end;
procedure TXmlAttribute.SetValue(const AValue: Int32);
begin
{$IFDEF XML_UTF8}
SetValue(IntToUtf8Str(AValue));
{$ELSE}
SetValue(IntToStr(AValue));
{$ENDIF}
end;
procedure TXmlAttribute.SetValue(const AValue: Boolean);
begin
if (AValue) then