-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLibXmlParser.pas
2742 lines (2431 loc) · 109 KB
/
LibXmlParser.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
(**
===============================================================================================
Name : LibXmlParser
Project : All Projects
Subject : Progressive XML 1.0 Parser for all types of XML 1.0 Files
===============================================================================================
Source, Legals ("Licence")
--------------------------
IN SHORT: Usage and distribution of this source code is free.
You use it completely on your own risk.
Terminology:
------------
- Start: Start of a buffer part
- Final: End (last character) of a buffer part
- DTD: Document Type Definition
- DTDc: Document Type Declaration
- XMLSpec: The current W3C XML 1.0 Recommendation (version 1.0 TE as of 2004-02-04), Chapter No.
- Cur*: Fields concerning the "Current" part passed back by the "Scan" method
===============================================================================================
Scanning the XML document
-------------------------
- Create TXmlParser Instance MyXml := TXmlParser.Create;
- Load XML Document MyXml.LoadFromFile (Filename);
- Start Scanning MyXml.StartScan;
- Scan Loop WHILE MyXml.Scan DO
- Test for Part Type CASE MyXml.CurPartType OF
- Handle Parts ... : ;;;
- Handle Parts ... : ;;;
- Handle Parts ... : ;;;
END;
- Destroy MyXml.Free;
===============================================================================================
Loading the XML document
------------------------
You can load the XML document from a file with the "LoadFromFile" method.
It is beyond the scope of this parser to perform HTTP or FTP accesses. If you want your
application to handle such requests (URLs), you can load the XML via HTTP or FTP or whatever
protocol and hand over the data buffer using the "LoadFromBuffer" or "SetBuffer" method.
"LoadFromBuffer" loads the internal buffer of TXmlParser with the given null-terminated
string, thereby creating a copy of that buffer.
"SetBuffer" just takes the pointer to another buffer, which means that the given
buffer pointer must be valid while the document is accessed via TXmlParser.
===============================================================================================
Encodings:
----------
This XML parser kind of "understands" the following encodings:
- UTF-8
- ISO-8859-1
- Windows-1252 ("ANSI")
Any flavor of multi-byte characters (and this includes UTF-16) is not supported. Sorry.
Every string which has to be passed to the application passes the virtual method
"TranslateEncoding" which translates the string from the current encoding (stored in
"CurEncoding") into the encoding the application wishes to receive.
The "TranslateEncoding" method that is built into TXmlParser assumes that the application
wants to receive Windows ANSI (Windows-1252, about the same as ISO-8859-1) and is able
to convert UTF-8 and ISO-8859-1 encodings.
For other source and target encodings, you will have to override "TranslateEncoding".
===============================================================================================
Buffer Handling
---------------
- The document must be loaded completely into a piece of RAM
- All character positions are referenced by PChar pointers
- The TXmlParser instance can either "own" the buffer itself (then, FBufferSize is > 0)
or reference the buffer of another instance or object (then, FBuffersize is 0 and
FBuffer is not NIL)
- The Property DocBuffer passes back a pointer to the first byte of the document. If there
is no document stored (FBuffer is NIL), the DocBuffer returns a pointer to a NULL character.
===============================================================================================
Whitespace Handling
-------------------
The TXmlParser property "Normalize" determines how Whitespace is returned in Text Content:
While Normalize is true, all leading and trailing whitespace characters are trimmed of, all
Whitespace is converted to Space #x20 characters and contiguous Whitespace characters are
compressed to one.
If the "Scan" method reports a ptContent part, the application can get the original text
with all whitespace characters by extracting the characters from "CurStart" to "CurFinal".
If the application detects an xml:space attribute, it can set "Normalize" accordingly or
use CurStart/CurFinal.
Please note that TXmlParser does _not_ normalize Line Breaks to single LineFeed characters
as the XMLSpec requires (XMLSpec 2.11).
The xml:space attribute is not handled by TXmlParser. This is on behalf of the application.
===============================================================================================
Non-XML-Conforming
------------------
TXmlParser does not conform 100 % exactly to the XMLSpec:
- UTF-16 is not supported (XMLSpec 2.2)
(Workaround: Convert UTF-16 to UTF-8 and hand the buffer over to TXmlParser)
- As the parser only works with single byte strings, all Unicode characters > 255
can currently not be handled correctly.
(Workaround: Use UTF-8 and handle UTF-8 characters by your own code)
- Line breaks are not normalized to single Linefeed #x0A characters (XMLSpec 2.11)
(Workaround: The Application can access the text contents on its own [CurStart, CurFinal],
thereby applying every normalization it wishes to)
- The attribute value normalization does not work exactly as defined in the
Second Edition of the XML 1.0 specification.
- See also the code parts marked with three consecutive exclamation marks. These are
parts which are not finished in the current code release.
This list may be incomplete, so it may grow if I get to know any other points.
As work on the parser proceeds, this list may also shrink.
// --- Delphi/Kylix/C++Builder Version Numbers
// As this is no code, this does not blow up your object or executable code at all
// Versions that will not work with this code (Delphi 1-3, .NET) will see invalid code and stop the compiler here
// The defines D4_OR_NEWER and D5_OR_NEWER are only kept for backward compatibility.
// They are not used in this code anymore.
(*$DEFINE HAS_CONTNRS_UNIT *) // The Contnrs Unit was introduced in Delphi 5
// Delphi 1, C++Builder 1
(*$IFDEF VER80 *)This Code will not compile in Delphi 1 (*$ENDIF*)
(*$IFDEF VER83 *)This Code will not compile in C++Builder 1 (*$ENDIF*)
// Delphi 2, C++Builder 2
(*$IFDEF VER90 *)This Code will not compile in Delphi 2 (*$ENDIF*)
(*$IFDEF VER93 *)This Code will not compile in C++Builder 2 (*$ENDIF*)
// Delphi 3, C++Builder 3
(*$IFDEF VER100 *)This Code will not compile in Delphi 3 (*$ENDIF*)
(*$IFDEF VER110 *)This Code will not compile in C++Builder 3 (*$ENDIF*)
// Delphi 4, C++Builder 4
(*$IFDEF VER120 *)(*$DEFINE D4_OR_NEWER *) (*$UNDEFINE HAS_CONTNRS_UNIT *) (*$ENDIF *)
(*$IFDEF VER125 *)(*$DEFINE D4_OR_NEWER *) (*$UNDEFINE HAS_CONTNRS_UNIT *) (*$ENDIF *)
// Delphi 5, C++Builder 5
(*$IFDEF VER130 *)(*$DEFINE D4_OR_NEWER *) (*$DEFINE D5_OR_NEWER *) (*$ENDIF *)
// Delphi 6, C++Builder 6, Kylix 1, Kylix 2
(*$IFDEF VER140 *)(*$DEFINE D4_OR_NEWER *) (*$DEFINE D5_OR_NEWER *) (*$ENDIF *)
// Delphi 7, Kylix 3
(*$IFDEF VER150 *)(*$DEFINE D4_OR_NEWER *) (*$DEFINE D5_OR_NEWER *) (*$ENDIF *)
// Delphi 8
(*$IFDEF VER160 *)This code will not compile for .NET (*$ENDIF *)
// Delphi 2005
(*$IFDEF VER170 *)(*$DEFINE D4_OR_NEWER *) (*$DEFINE D5_OR_NEWER *) (*$ENDIF *)
// Delphi 2006 #DremLIN 2009-09-17
(*$IFDEF VER180 *)(*$DEFINE D4_OR_NEWER *) (*$DEFINE D5_OR_NEWER *) (*$ENDIF *)
// Delphi 2009 #DremLIN 2009-09-17
(*$IFDEF VER200 *)(*$DEFINE D4_OR_NEWER *) (*$DEFINE D5_OR_NEWER *) (*$ENDIF *)
// Delphi 2010 #DremLIN 2009-09-17
(*$IFDEF VER210 *)(*$DEFINE D4_OR_NEWER *) (*$DEFINE D5_OR_NEWER *) (*$ENDIF *)
// Managed Code
(*$IFDEF MANAGEDCODE *)This code will note compile as Managed Code (*$ENDIF *)
(*$IFDEF CLR *)This code will note compile as Managed Code (*$ENDIF *)
UNIT LibXmlParser;
INTERFACE
USES
SysUtils, Classes,
(*$IFDEF HAS_CONTNRS_UNIT *)
Contnrs,
(*$ENDIF*)
Math;
CONST
CVersion = '1.1.1';
TYPE
TPartType = // --- Document Part Types
(ptNone, // Nothing
ptXmlProlog, // XML Prolog XmlSpec 2.8 / 4.3.1
ptComment, // Comment XmlSpec 2.5
ptPI, // Processing Instruction XmlSpec 2.6
ptDtdc, // Document Type Declaration XmlSpec 2.8
ptStartTag, // Start Tag XmlSpec 3.1
ptEmptyTag, // Empty-Element Tag XmlSpec 3.1
ptEndTag, // End Tag XmlSpec 3.1
ptContent, // Text Content between Tags
ptCData); // CDATA Section XmlSpec 2.7
TDtdElemType = // --- DTD Elements
(deElement, // !ELEMENT declaration
deAttList, // !ATTLIST declaration
deEntity, // !ENTITY declaration
deNotation, // !NOTATION declaration
dePI, // PI in DTD
deComment, // Comment in DTD
deError); // Error found in the DTD
TYPE
TAttrList = CLASS;
TEntityStack = CLASS;
TNvpList = CLASS;
TElemDef = CLASS;
TElemList = CLASS;
TEntityDef = CLASS;
TNotationDef = CLASS;
TDtdElementRec = RECORD // --- This Record is returned by the DTD parser callback function
Start, Final : PChar; // Start/End of the Element's Declaration
CASE ElementType : TDtdElemType OF // Type of the Element
deElement, // <!ELEMENT>
deAttList : (ElemDef : TElemDef); // <!ATTLIST>
deEntity : (EntityDef : TEntityDef); // <!ENTITY>
deNotation : (NotationDef : TNotationDef); // <!NOTATION>
dePI : (Target : PChar; // <?PI ?>
Content : PChar;
AttrList : TAttrList);
deError : (Pos : PChar); // Error
// deComment : ((No additional fields here)); // <!-- Comment -->
END;
TXmlParser = CLASS // --- Internal Properties and Methods
PROTECTED
FBuffer : PChar; // NIL if there is no buffer available
FBufferSize : INTEGER; // 0 if the buffer is not owned by the Document instance
FSource : STRING; // Name of Source of document. Filename for Documents loaded with LoadFromFile
FXmlVersion : STRING; // XML version from Document header. Default is '1.0'
FEncoding : STRING; // Encoding from Document header. Default is 'UTF-8'
FStandalone : BOOLEAN; // Standalone declaration from Document header. Default is 'yes'
FRootName : STRING; // Name of the Root Element (= DTD name)
FDtdcFinal : PChar; // Pointer to the '>' character terminating the DTD declaration
FNormalize : BOOLEAN; // If true: Pack Whitespace and don't return empty contents
EntityStack : TEntityStack; // Entity Stack for Parameter and General Entities
FCurEncoding : STRING; // Current Encoding during parsing (always uppercase)
PROCEDURE AnalyzeProlog; // Analyze XML Prolog or Text Declaration
PROCEDURE AnalyzeComment (Start : PChar; VAR Final : PChar); // Analyze Comments
PROCEDURE AnalyzePI (Start : PChar; VAR Final : PChar); // Analyze Processing Instructions (PI)
PROCEDURE AnalyzeDtdc; // Analyze Document Type Declaration
PROCEDURE AnalyzeDtdElements (Start : PChar; VAR Final : PChar); // Analyze DTD declarations
PROCEDURE AnalyzeTag; // Analyze Start/End/Empty-Element Tags
PROCEDURE AnalyzeCData; // Analyze CDATA Sections
PROCEDURE AnalyzeText (VAR IsDone : BOOLEAN); // Analyze Text Content between Tags
PROCEDURE AnalyzeElementDecl (Start : PChar; VAR Final : PChar);
PROCEDURE AnalyzeAttListDecl (Start : PChar; VAR Final : PChar);
PROCEDURE AnalyzeEntityDecl (Start : PChar; VAR Final : PChar);
PROCEDURE AnalyzeNotationDecl (Start : PChar; VAR Final : PChar);
PROCEDURE PushPE (VAR Start : PChar);
PROCEDURE ReplaceCharacterEntities (VAR Str : STRING);
PROCEDURE ReplaceParameterEntities (VAR Str : STRING);
FUNCTION GetDocBuffer : PChar; // Returns FBuffer or a pointer to a NUL char if Buffer is empty
PUBLIC // --- Document Properties
PROPERTY XmlVersion : STRING READ FXmlVersion; // XML version from the Document Prolog
PROPERTY Encoding : STRING READ FEncoding; // Document Encoding from Prolog
PROPERTY Standalone : BOOLEAN READ FStandalone; // Standalone Declaration from Prolog
PROPERTY RootName : STRING READ FRootName; // Name of the Root Element
PROPERTY Normalize : BOOLEAN READ FNormalize WRITE FNormalize; // True if Content is to be normalized
PROPERTY Source : STRING READ FSource; // Name of Document Source (Filename)
PROPERTY DocBuffer : PChar READ GetDocBuffer; // Returns document buffer
PUBLIC // --- DTD Objects
Elements : TElemList; // Elements: List of TElemDef (contains Attribute Definitions)
Entities : TNvpList; // General Entities: List of TEntityDef
ParEntities : TNvpList; // Parameter Entities: List of TEntityDef
Notations : TNvpList; // Notations: List of TNotationDef
PUBLIC
CONSTRUCTOR Create;
DESTRUCTOR Destroy; OVERRIDE;
// --- Document Handling
FUNCTION LoadFromFile (Filename : STRING;
FileMode : INTEGER = fmOpenRead OR fmShareDenyNone) : BOOLEAN;
// Loads Document from given file
FUNCTION LoadFromBuffer (Buffer : PChar) : BOOLEAN; // Loads Document from another buffer
PROCEDURE SetBuffer (Buffer : PChar); // References another buffer
PROCEDURE Clear; // Clear Document
PUBLIC
// --- Scanning through the document
CurPartType : TPartType; // Current Type
CurName : STRING; // Current Name
CurContent : STRING; // Current Normalized Content
CurStart : PChar; // Current First character
CurFinal : PChar; // Current Last character
CurAttr : TAttrList; // Current Attribute List
PROPERTY CurEncoding : STRING READ FCurEncoding; // Current Encoding (always uppercase)
PROCEDURE StartScan;
FUNCTION Scan : BOOLEAN;
// --- Events / Callbacks
FUNCTION LoadExternalEntity (SystemId, PublicId,
Notation : STRING) : TXmlParser; VIRTUAL;
FUNCTION TranslateEncoding (CONST Source : STRING) : STRING; VIRTUAL;
FUNCTION TranslateCharacter (CONST UnicodeValue : INTEGER) : STRING; VIRTUAL;
PROCEDURE DtdElementFound (DtdElementRec : TDtdElementRec); VIRTUAL;
END;
TValueType = // --- Attribute Value Type
(vtNormal, // Normal specified Attribute
vtImplied, // #IMPLIED attribute value
vtFixed, // #FIXED attribute value
vtDefault); // Attribute value from default value in !ATTLIST declaration
TAttrDefault = // --- Attribute Default Type
(adDefault, // Normal default value
adRequired, // #REQUIRED attribute
adImplied, // #IMPLIED attribute
adFixed); // #FIXED attribute
TAttrType = // --- Type of attribute
(atUnknown, // Unknown type
atCData, // Character data only
atID, // ID
atIdRef, // ID Reference
atIdRefs, // Several ID References, separated by Whitespace
atEntity, // Name of an unparsed Entity
atEntities, // Several unparsed Entity names, separated by Whitespace
atNmToken, // Name Token
atNmTokens, // Several Name Tokens, separated by Whitespace
atNotation, // A selection of Notation names (Unparsed Entity)
atEnumeration); // Enumeration
TElemType = // --- Element content type
(etEmpty, // Element is always empty
etAny, // Element can have any mixture of PCDATA and any elements
etChildren, // Element must contain only elements
etMixed); // Mixed PCDATA and elements
(*$IFDEF HAS_CONTNRS_UNIT *)
TObjectList = Contnrs.TObjectList; // Re-Export this identifier
(*$ELSE *)
TObjectList = CLASS (TList)
DESTRUCTOR Destroy; OVERRIDE;
PROCEDURE Delete (Index : INTEGER);
PROCEDURE Clear; OVERRIDE;
END;
(*$ENDIF *)
TNvpNode = CLASS // Name-Value Pair Node
Name : STRING;
Value : STRING;
CONSTRUCTOR Create (TheName : STRING = ''; TheValue : STRING = '');
END;
TNvpList = CLASS (TObjectList) // Name-Value Pair List
PROCEDURE Add (Node : TNvpNode);
FUNCTION Node (Name : STRING) : TNvpNode; OVERLOAD;
FUNCTION Node (Index : INTEGER) : TNvpNode; OVERLOAD;
FUNCTION Value (Name : STRING) : STRING; OVERLOAD;
FUNCTION Value (Index : INTEGER) : STRING; OVERLOAD;
FUNCTION Name (Index : INTEGER) : STRING;
END;
TAttr = CLASS (TNvpNode) // Attribute of a Start-Tag or Empty-Element-Tag
ValueType : TValueType;
AttrType : TAttrType;
END;
TAttrList = CLASS (TNvpList) // List of Attributes
PROCEDURE Analyze (Start : PChar; VAR Final : PChar);
END;
TEntityStack = CLASS (TObjectList) // Stack where current position is stored before parsing entities
PROTECTED
Owner : TXmlParser;
PUBLIC
CONSTRUCTOR Create (TheOwner : TXmlParser);
PROCEDURE Push (LastPos : PChar); OVERLOAD;
PROCEDURE Push (Instance : TObject; LastPos : PChar); OVERLOAD;
FUNCTION Pop : PChar; // Returns next char or NIL if EOF is reached. Frees Instance.
END;
TAttrDef = CLASS (TNvpNode) // Represents a <!ATTLIST Definition. "Value" is the default value
TypeDef : STRING; // Type definition from the DTD
Notations : STRING; // Notation List, separated by pipe symbols '|'
AttrType : TAttrType; // Attribute Type
DefaultType : TAttrDefault; // Default Type
END;
TElemDef = CLASS (TNvpList) // Represents a <!ELEMENT Definition. Is a list of TAttrDef-Nodes
Name : STRING; // Element name
ElemType : TElemType; // Element type
Definition : STRING; // Element definition from DTD
END;
TElemList = CLASS (TObjectList) // List of TElemDef nodes
FUNCTION Node (Name : STRING) : TElemDef;
PROCEDURE Add (Node : TElemDef);
END;
TEntityDef = CLASS (TNvpNode) // Represents a <!ENTITY Definition.
SystemId : STRING;
PublicId : STRING;
NotationName : STRING;
END;
TNotationDef = CLASS (TNvpNode) // Represents a <!NOTATION Definition. Value is the System ID
PublicId : STRING;
END;
TCharset = SET OF CHAR;
CONST
CWhitespace = [#32, #9, #13, #10]; // Whitespace characters (XmlSpec 2.3)
CLetter = [#$41..#$5A, #$61..#$7A, #$C0..#$D6, #$D8..#$F6, #$F8..#$FF];
CDigit = [#$30..#$39];
CNameChar = CLetter + CDigit + ['.', '-', '_', ':', #$B7];
CNameStart = CLetter + ['_', ':'];
CQuoteChar = ['"', ''''];
CPubidChar = [#32, ^M, ^J, #9, 'a'..'z', 'A'..'Z', '0'..'9',
'-', '''', '(', ')', '+', ',', '.', '/', ':',
'=', '?', ';', '!', '*', '#', '@', '$', '_', '%'];
CDStart = '<![CDATA[';
CDEnd = ']]>';
// --- Name Constants for the above enumeration types
CPartType_Name : ARRAY [TPartType] OF STRING =
('', 'XML Prolog', 'Comment', 'PI',
'DTD Declaration', 'Start Tag', 'Empty Tag', 'End Tag',
'Text', 'CDATA');
CValueType_Name : ARRAY [TValueType] OF STRING = ('Normal', 'Implied', 'Fixed', 'Default');
CAttrDefault_Name : ARRAY [TAttrDefault] OF STRING = ('Default', 'Required', 'Implied', 'Fixed');
CElemType_Name : ARRAY [TElemType] OF STRING = ('Empty', 'Any', 'Childs only', 'Mixed');
CAttrType_Name : ARRAY [TAttrType] OF STRING = ('Unknown', 'CDATA',
'ID', 'IDREF', 'IDREFS',
'ENTITY', 'ENTITIES',
'NMTOKEN', 'NMTOKENS',
'Notation', 'Enumeration');
FUNCTION ConvertWs (Source: STRING; PackWs: BOOLEAN) : STRING; // Convert WS to spaces #x20
PROCEDURE SetStringSF (VAR S : STRING; BufferStart, BufferFinal : PChar); // SetString by Start/Final of buffer
FUNCTION StrSFPas (Start, Finish : PChar) : STRING; // Convert buffer part to Pascal string
FUNCTION TrimWs (Source : STRING) : STRING; // Trim Whitespace
FUNCTION AnsiToUtf8 (Source : ANSISTRING) : STRING; // Convert Windows-1252 to UTF-8
FUNCTION Utf8ToAnsi (Source : STRING; UnknownChar : CHAR = '¿') : ANSISTRING; // Convert UTF-8 to Windows-1252
(*
===============================================================================================
TCustomXmlScanner event based component wrapper for TXmlParser
===============================================================================================
*)
TYPE
TXmlPrologEvent = PROCEDURE (Sender : TObject; XmlVersion, Encoding: STRING; Standalone : BOOLEAN) OF OBJECT;
TCommentEvent = PROCEDURE (Sender : TObject; Comment : STRING) OF OBJECT;
TPIEvent = PROCEDURE (Sender : TObject; Target, Content: STRING; Attributes : TAttrList) OF OBJECT;
TDtdEvent = PROCEDURE (Sender : TObject; RootElementName : STRING) OF OBJECT;
TStartTagEvent = PROCEDURE (Sender : TObject; TagName : STRING; Attributes : TAttrList) OF OBJECT;
TEndTagEvent = PROCEDURE (Sender : TObject; TagName : STRING) OF OBJECT;
TContentEvent = PROCEDURE (Sender : TObject; Content : STRING) OF OBJECT;
TElementEvent = PROCEDURE (Sender : TObject; ElemDef : TElemDef) OF OBJECT;
TEntityEvent = PROCEDURE (Sender : TObject; EntityDef : TEntityDef) OF OBJECT;
TNotationEvent = PROCEDURE (Sender : TObject; NotationDef : TNotationDef) OF OBJECT;
TErrorEvent = PROCEDURE (Sender : TObject; ErrorPos : PChar) OF OBJECT;
TExternalEvent = PROCEDURE (Sender : TObject; SystemId, PublicId, NotationId : STRING;
VAR Result : TXmlParser) OF OBJECT;
TEncodingEvent = FUNCTION (Sender : TObject; CurrentEncoding, Source : STRING) : STRING OF OBJECT;
TEncodeCharEvent = FUNCTION (Sender : TObject; UnicodeValue : INTEGER) : STRING OF OBJECT;
TCustomXmlScanner = CLASS (TComponent)
PROTECTED
FXmlParser : TXmlParser;
FOnXmlProlog : TXmlPrologEvent;
FOnComment : TCommentEvent;
FOnPI : TPIEvent;
FOnDtdRead : TDtdEvent;
FOnStartTag : TStartTagEvent;
FOnEmptyTag : TStartTagEvent;
FOnEndTag : TEndTagEvent;
FOnContent : TContentEvent;
FOnCData : TContentEvent;
FOnElement : TElementEvent;
FOnAttList : TElementEvent;
FOnEntity : TEntityEvent;
FOnNotation : TNotationEvent;
FOnDtdError : TErrorEvent;
FOnLoadExternal : TExternalEvent;
FOnTranslateEncoding : TEncodingEvent;
FOnTranslateCharacter : TEncodeCharEvent;
FStopParser : BOOLEAN;
FUNCTION GetNormalize : BOOLEAN;
PROCEDURE SetNormalize (Value : BOOLEAN);
PROCEDURE WhenXmlProlog(XmlVersion, Encoding: STRING; Standalone : BOOLEAN); VIRTUAL;
PROCEDURE WhenComment (Comment : STRING); VIRTUAL;
PROCEDURE WhenPI (Target, Content: STRING; Attributes : TAttrList); VIRTUAL;
PROCEDURE WhenDtdRead (RootElementName : STRING); VIRTUAL;
PROCEDURE WhenStartTag (TagName : STRING; Attributes : TAttrList); VIRTUAL;
PROCEDURE WhenEmptyTag (TagName : STRING; Attributes : TAttrList); VIRTUAL;
PROCEDURE WhenEndTag (TagName : STRING); VIRTUAL;
PROCEDURE WhenContent (Content : STRING); VIRTUAL;
PROCEDURE WhenCData (Content : STRING); VIRTUAL;
PROCEDURE WhenElement (ElemDef : TElemDef); VIRTUAL;
PROCEDURE WhenAttList (ElemDef : TElemDef); VIRTUAL;
PROCEDURE WhenEntity (EntityDef : TEntityDef); VIRTUAL;
PROCEDURE WhenNotation (NotationDef : TNotationDef); VIRTUAL;
PROCEDURE WhenDtdError (ErrorPos : PChar); VIRTUAL;
PUBLIC
CONSTRUCTOR Create (AOwner: TComponent); OVERRIDE;
DESTRUCTOR Destroy; OVERRIDE;
PROCEDURE LoadFromFile (Filename : TFilename); // Load XML Document from file
PROCEDURE LoadFromBuffer (Buffer : PChar); // Load XML Document from buffer
PROCEDURE SetBuffer (Buffer : PChar); // Refer to Buffer
FUNCTION GetFilename : TFilename;
PROCEDURE Execute; // Perform scanning
PROTECTED
PROPERTY XmlParser : TXmlParser READ FXmlParser;
PROPERTY StopParser : BOOLEAN READ FStopParser WRITE FStopParser;
PROPERTY Filename : TFilename READ GetFilename WRITE LoadFromFile;
PROPERTY Normalize : BOOLEAN READ GetNormalize WRITE SetNormalize;
PROPERTY OnXmlProlog : TXmlPrologEvent READ FOnXmlProlog WRITE FOnXmlProlog;
PROPERTY OnComment : TCommentEvent READ FOnComment WRITE FOnComment;
PROPERTY OnPI : TPIEvent READ FOnPI WRITE FOnPI;
PROPERTY OnDtdRead : TDtdEvent READ FOnDtdRead WRITE FOnDtdRead;
PROPERTY OnStartTag : TStartTagEvent READ FOnStartTag WRITE FOnStartTag;
PROPERTY OnEmptyTag : TStartTagEvent READ FOnEmptyTag WRITE FOnEmptyTag;
PROPERTY OnEndTag : TEndTagEvent READ FOnEndTag WRITE FOnEndTag;
PROPERTY OnContent : TContentEvent READ FOnContent WRITE FOnContent;
PROPERTY OnCData : TContentEvent READ FOnCData WRITE FOnCData;
PROPERTY OnElement : TElementEvent READ FOnElement WRITE FOnElement;
PROPERTY OnAttList : TElementEvent READ FOnAttList WRITE FOnAttList;
PROPERTY OnEntity : TEntityEvent READ FOnEntity WRITE FOnEntity;
PROPERTY OnNotation : TNotationEvent READ FOnNotation WRITE FOnNotation;
PROPERTY OnDtdError : TErrorEvent READ FOnDtdError WRITE FOnDtdError;
PROPERTY OnLoadExternal : TExternalEvent READ FOnLoadExternal WRITE FOnLoadExternal;
PROPERTY OnTranslateEncoding : TEncodingEvent READ FOnTranslateEncoding WRITE FOnTranslateEncoding;
PROPERTY OnTranslateCharacter : TEncodeCharEvent READ FOnTranslateCharacter WRITE FOnTranslateCharacter;
END;
(*
===============================================================================================
IMPLEMENTATION
===============================================================================================
*)
IMPLEMENTATION
(*
===============================================================================================
Unicode and UTF-8 stuff
===============================================================================================
*)
CONST
// --- Character Translation Table for Unicode <-> Win-1252
WIN1252_UNICODE : ARRAY [$00..$FF] OF WORD = (
$0000, $0001, $0002, $0003, $0004, $0005, $0006, $0007, $0008, $0009,
$000A, $000B, $000C, $000D, $000E, $000F, $0010, $0011, $0012, $0013,
$0014, $0015, $0016, $0017, $0018, $0019, $001A, $001B, $001C, $001D,
$001E, $001F, $0020, $0021, $0022, $0023, $0024, $0025, $0026, $0027,
$0028, $0029, $002A, $002B, $002C, $002D, $002E, $002F, $0030, $0031,
$0032, $0033, $0034, $0035, $0036, $0037, $0038, $0039, $003A, $003B,
$003C, $003D, $003E, $003F, $0040, $0041, $0042, $0043, $0044, $0045,
$0046, $0047, $0048, $0049, $004A, $004B, $004C, $004D, $004E, $004F,
$0050, $0051, $0052, $0053, $0054, $0055, $0056, $0057, $0058, $0059,
$005A, $005B, $005C, $005D, $005E, $005F, $0060, $0061, $0062, $0063,
$0064, $0065, $0066, $0067, $0068, $0069, $006A, $006B, $006C, $006D,
$006E, $006F, $0070, $0071, $0072, $0073, $0074, $0075, $0076, $0077,
$0078, $0079, $007A, $007B, $007C, $007D, $007E, $007F,
$20AC, $0081, $201A, $0192, $201E, $2026, $2020, $2021, $02C6, $2030,
$0160, $2039, $0152, $008D, $017D, $008F, $0090, $2018, $2019, $201C,
$201D, $2022, $2013, $2014, $02DC, $2122, $0161, $203A, $0153, $009D,
$017E, $0178, $00A0, $00A1, $00A2, $00A3, $00A4, $00A5, $00A6, $00A7,
$00A8, $00A9, $00AA, $00AB, $00AC, $00AD, $00AE, $00AF, $00B0, $00B1,
$00B2, $00B3, $00B4, $00B5, $00B6, $00B7, $00B8, $00B9, $00BA, $00BB,
$00BC, $00BD, $00BE, $00BF, $00C0, $00C1, $00C2, $00C3, $00C4, $00C5,
$00C6, $00C7, $00C8, $00C9, $00CA, $00CB, $00CC, $00CD, $00CE, $00CF,
$00D0, $00D1, $00D2, $00D3, $00D4, $00D5, $00D6, $00D7, $00D8, $00D9,
$00DA, $00DB, $00DC, $00DD, $00DE, $00DF, $00E0, $00E1, $00E2, $00E3,
$00E4, $00E5, $00E6, $00E7, $00E8, $00E9, $00EA, $00EB, $00EC, $00ED,
$00EE, $00EF, $00F0, $00F1, $00F2, $00F3, $00F4, $00F5, $00F6, $00F7,
$00F8, $00F9, $00FA, $00FB, $00FC, $00FD, $00FE, $00FF);
(* UTF-8 (somewhat simplified)
-----
Character Range Byte sequence
--------------- -------------------------- (x=Bits from original character)
$0000..$007F 0xxxxxxx
$0080..$07FF 110xxxxx 10xxxxxx
$8000..$FFFF 1110xxxx 10xxxxxx 10xxxxxx
Example
--------
Transforming the Unicode character U+00E4 LATIN SMALL LETTER A WITH DIAERESIS ("ä"):
ISO-8859-1, Decimal 228
Win1252, Hex $E4
ANSI Bin 1110 0100
abcd efgh
UTF-8 Binary 1100xxab 10cdefgh
Binary 11000011 10100100
Hex $C3 $A4
Decimal 195 164
ANSI Ã ¤ *)
FUNCTION AnsiToUtf8 (Source : ANSISTRING) : STRING;
(* Converts the given Windows ANSI (Windows-1252) String to UTF-8. *)
VAR
I : INTEGER; // Loop counter
U : WORD; // Current Unicode value
Len : INTEGER; // Current real length of "Result" string
BEGIN
SetLength (Result, Length (Source) * 3); // Worst case
Len := 0;
FOR I := 1 TO Length (Source) DO BEGIN
U := WIN1252_UNICODE [ORD (Source [I])];
CASE U OF
$0000..$007F : BEGIN
INC (Len);
Result [Len] := CHR (U);
END;
$0080..$07FF : BEGIN
INC (Len);
Result [Len] := CHR ($C0 OR (U SHR 6));
INC (Len);
Result [Len] := CHR ($80 OR (U AND $3F));
END;
$0800..$FFFF : BEGIN
INC (Len);
Result [Len] := CHR ($E0 OR (U SHR 12));
INC (Len);
Result [Len] := CHR ($80 OR ((U SHR 6) AND $3F));
INC (Len);
Result [Len] := CHR ($80 OR (U AND $3F));
END;
END;
END;
SetLength (Result, Len);
END;
FUNCTION Utf8ToAnsi (Source : STRING; UnknownChar : CHAR = '¿') : ANSISTRING;
(* Converts the given UTF-8 String to Windows ANSI (Win-1252).
If a character can not be converted, the "UnknownChar" is inserted. *)
VAR
SourceLen : INTEGER; // Length of Source string
I, K : INTEGER;
A : BYTE; // Current ANSI character value
U : WORD;
{$IFDEF UNICODE} // #2009-09-17 DremLIN
Ch : WIDECHAR; // Dest char
{$ELSE}
Ch : CHAR; // Dest char
{$ENDIF}
Len : INTEGER; // Current real length of "Result" string
BEGIN
SourceLen := Length (Source);
SetLength (Result, SourceLen); // Enough room to live
Len := 0;
I := 1;
WHILE I <= SourceLen DO BEGIN
A := ORD (Source [I]);
IF A < $80 THEN BEGIN // Range $0000..$007F
INC (Len);
{$IFDEF UNICODE} // #2009-09-17 DremLIN
Result [Len] := AnsiChar(Source[I]);
{$ELSE}
Result [Len] := Source [I];
{$ENDIF}
INC (I);
END
ELSE BEGIN // Determine U, Inc I
IF (A AND $E0 = $C0) AND (I < SourceLen) THEN BEGIN // Range $0080..$07FF
U := (WORD (A AND $1F) SHL 6) OR (ORD (Source [I+1]) AND $3F);
INC (I, 2);
END
ELSE IF (A AND $F0 = $E0) AND (I < SourceLen-1) THEN BEGIN // Range $0800..$FFFF
U := (WORD (A AND $0F) SHL 12) OR
(WORD (ORD (Source [I+1]) AND $3F) SHL 6) OR
( ORD (Source [I+2]) AND $3F);
INC (I, 3);
END
ELSE BEGIN // Unknown/unsupported
INC (I);
FOR K := 7 DOWNTO 0 DO
IF A AND (1 SHL K) = 0 THEN BEGIN
INC (I, (A SHR (K+1))-1);
BREAK;
END;
U := WIN1252_UNICODE [ORD (UnknownChar)];
END;
Ch := UnknownChar; // Retrieve ANSI char
FOR A := $00 TO $FF DO
IF WIN1252_UNICODE [A] = U THEN BEGIN
Ch := CHR (A);
BREAK;
END;
INC (Len);
{$IFDEF UNICODE} // #2009-09-17 DremLIN
Result [Len] := AnsiChar(Ch);
{$ELSE}
Result [Len] := Ch;
{$ENDIF}
END;
END;
SetLength (Result, Len);
END;
(*
===============================================================================================
"Special" Helper Functions
Don't ask me why. But including these functions makes the parser *DRAMATICALLY* faster
on my K6-233 machine. You can test it yourself just by commenting them out.
They do exactly the same as the Assembler routines defined in SysUtils.
(This is where you can see how great the Delphi compiler really is. The compiled code is
faster than hand-coded assembler! :-)
===============================================================================================
--> Just move this line below the StrScan function --> *)
FUNCTION StrPos (CONST Str, SearchStr : PChar) : PChar;
// Same functionality as SysUtils.StrPos
VAR
First : CHAR;
Len : INTEGER;
BEGIN
First := SearchStr^;
Len := StrLen (SearchStr);
Result := Str;
REPEAT
IF Result^ = First THEN
IF StrLComp (Result, SearchStr, Len) = 0 THEN BREAK;
IF Result^ = #0 THEN BEGIN
Result := NIL;
BREAK;
END;
INC (Result);
UNTIL FALSE;
END;
FUNCTION StrScan (CONST Start : PChar; CONST Ch : CHAR) : PChar;
// Same functionality as SysUtils.StrScan
BEGIN
Result := Start;
WHILE Result^ <> Ch DO BEGIN
IF Result^ = #0 THEN BEGIN
Result := NIL;
EXIT;
END;
INC (Result);
END;
END;
(*
===============================================================================================
Helper Functions
===============================================================================================
*)
FUNCTION DelChars (Source : STRING; CharsToDelete : TCharset) : STRING;
// Delete all "CharsToDelete" from the string
VAR
I : INTEGER;
BEGIN
Result := Source;
FOR I := Length (Result) DOWNTO 1 DO
IF Result [I] IN CharsToDelete THEN
Delete (Result, I, 1);
END;
FUNCTION TrimWs (Source : STRING) : STRING;
// Trimms off Whitespace characters from both ends of the string
VAR
I : INTEGER;
BEGIN
// --- Trim Left
I := 1;
WHILE (I <= Length (Source)) AND (Source [I] IN CWhitespace) DO
INC (I);
Result := Copy (Source, I, MaxInt);
// --- Trim Right
I := Length (Result);
WHILE (I > 1) AND (Result [I] IN CWhitespace) DO
DEC (I);
Delete (Result, I+1, Length (Result)-I);
END;
FUNCTION TrimAndPackSpace (Source : STRING) : STRING;
// Trim and pack contiguous space (#x20) characters
// Needed for attribute value normalization of non-CDATA attributes (XMLSpec 3.3.3)
VAR
I, T : INTEGER;
BEGIN
// --- Trim Left
T := 1;
FOR I := 1 to Length (Source) DO
IF Source [I] = #32
THEN INC (T)
ELSE BREAK;
IF T > 1
THEN Result := Copy (Source, T, MaxInt)
ELSE Result := Source;
// --- Trim Right
I := Length (Result);
WHILE (I > 1) AND (Result [I] = #32) DO
DEC (I);
Delete (Result, I+1, Length (Result)-I);
// --- Pack
FOR I := Length (Result) DOWNTO 2 DO
IF (Result [I] = #32) AND (Result [I-1] = #32) THEN
Delete (Result, I, 1);
END;
FUNCTION ConvertWs (Source: STRING; PackWs: BOOLEAN) : STRING;
// Converts all Whitespace characters to the Space #x20 character
// If "PackWs" is true, contiguous Whitespace characters are packed to one
VAR
I : INTEGER;
BEGIN
Result := Source;
FOR I := Length (Result) DOWNTO 1 DO
IF (Result [I] IN CWhitespace) THEN
IF PackWs AND (I > 1) AND (Result [I-1] IN CWhitespace)
THEN Delete (Result, I, 1)
ELSE Result [I] := #32;
END;
PROCEDURE SetStringSF (VAR S : STRING; BufferStart, BufferFinal : PChar);
BEGIN
SetString (S, BufferStart, BufferFinal-BufferStart+1);
END;
FUNCTION StrLPas (Start : PChar; Len : INTEGER) : STRING;
BEGIN
SetString (Result, Start, Len);
END;
FUNCTION StrSFPas (Start, Finish : PChar) : STRING;
BEGIN
SetString (Result, Start, Finish-Start+1);
END;
FUNCTION StrScanE (CONST Source : PChar; CONST CharToScanFor : CHAR) : PChar;
// If "CharToScanFor" is not found, StrScanE returns the last char of the
// buffer instead of NIL
BEGIN
Result := StrScan (Source, CharToScanFor);
IF Result = NIL THEN
Result := StrEnd (Source)-1;
END;
PROCEDURE ExtractName (Start : PChar; Terminators : TCharset; VAR Final : PChar);
(* Extracts the complete Name beginning at "Start".
It is assumed that the name is contained in Markup, so the '>' character is
always a Termination.
Start: IN Pointer to first char of name. Is always considered to be valid
Terminators: IN Characters which terminate the name
Final: OUT Pointer to last char of name *)
BEGIN
Final := Start;
Include (Terminators, #0);
Include (Terminators, '>');
WHILE NOT ((Final + 1)^ IN Terminators) DO
INC (Final);
END;
PROCEDURE ExtractQuote (Start : PChar; VAR Content : STRING; VAR Final : PChar);
(* Extract a string which is contained in single or double Quotes.
Start: IN Pointer to opening quote
Content: OUT The quoted string
Final: OUT Pointer to closing quote *)
BEGIN
Final := StrScan (Start+1, Start^);
IF Final = NIL THEN BEGIN
Final := StrEnd (Start+1)-1;
SetString (Content, Start+1, Final-Start);
END
ELSE
SetString (Content, Start+1, Final-1-Start);
END;
(*
===============================================================================================
TEntityStackNode
This Node is pushed to the "Entity Stack" whenever the parser parses entity replacement text.
The "Instance" field holds the Instance pointer of an External Entity buffer. When it is
popped, the Instance is freed.
The "Encoding" field holds the name of the Encoding. External Parsed Entities may have
another encoding as the document entity (XmlSpec 4.3.3). So when there is an "<?xml" PI
found in the stream (= Text Declaration at the beginning of external parsed entities), the
Encoding found there is used for the External Entity (is assigned to TXmlParser.CurEncoding)
Default Encoding is for the Document Entity is UTF-8. It is assumed that External Entities
have the same Encoding as the Document Entity, unless they carry a Text Declaration.
===============================================================================================
*)
TYPE
TEntityStackNode = CLASS
Instance : TObject;
Encoding : STRING;
LastPos : PChar;
END;
(*
===============================================================================================
TEntityStack
For nesting of Entities.
When there is an entity reference found in the data stream, the corresponding entity
definition is searched and the current position is pushed to this stack.
From then on, the program scans the entitiy replacement text as if it were normal content.
When the parser reaches the end of an entity, the current position is popped off the
stack again.
===============================================================================================
*)
CONSTRUCTOR TEntityStack.Create (TheOwner : TXmlParser);
BEGIN
INHERITED Create;
Owner := TheOwner;
END;
PROCEDURE TEntityStack.Push (LastPos : PChar);
BEGIN
Push (NIL, LastPos);
END;
PROCEDURE TEntityStack.Push (Instance : TObject; LastPos : PChar);
VAR
ESN : TEntityStackNode;
BEGIN
ESN := TEntityStackNode.Create;
ESN.Instance := Instance;
ESN.Encoding := Owner.FCurEncoding; // Save current Encoding
ESN.LastPos := LastPos;
Add (ESN);
END;
FUNCTION TEntityStack.Pop : PChar;
VAR
ESN : TEntityStackNode;
BEGIN
IF Count > 0 THEN BEGIN
ESN := TEntityStackNode (Items [Count-1]);
Result := ESN.LastPos;
IF ESN.Instance <> NIL THEN
ESN.Instance.Free;
IF ESN.Encoding <> '' THEN
Owner.FCurEncoding := ESN.Encoding; // Restore current Encoding
Delete (Count-1);
END
ELSE
Result := NIL;
END;
(*
===============================================================================================
TExternalID
-----------
XmlSpec 4.2.2: ExternalID ::= 'SYSTEM' S SystemLiteral |
'PUBLIC' S PubidLiteral S SystemLiteral
XmlSpec 4.7: PublicID ::= 'PUBLIC' S PubidLiteral
SystemLiteral and PubidLiteral are quoted
===============================================================================================
*)
TYPE
TExternalID = CLASS
PublicId : STRING;
SystemId : STRING;
Final : PChar;
CONSTRUCTOR Create (Start : PChar);
END;
CONSTRUCTOR TExternalID.Create (Start : PChar);
BEGIN
INHERITED Create;
Final := Start;
IF StrLComp (Start, 'SYSTEM', 6) = 0 THEN BEGIN
WHILE NOT (Final^ IN (CQuoteChar + [#0, '>', '['])) DO INC (Final);
IF NOT (Final^ IN CQuoteChar) THEN EXIT;
ExtractQuote (Final, SystemID, Final);
END
ELSE IF StrLComp (Start, 'PUBLIC', 6) = 0 THEN BEGIN
WHILE NOT (Final^ IN (CQuoteChar + [#0, '>', '['])) DO INC (Final);
IF NOT (Final^ IN CQuoteChar) THEN EXIT;
ExtractQuote (Final, PublicID, Final);