summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-xml/src/sax.pp
blob: 4ef8530c372ac1932dd0c1886907a7a72ce5a69a (plain)
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
{
    This file is part of the Free Component Library

    SAX 2 (Simple API for XML) implementation
    Copyright (c) 2000 - 2002 by
      Areca Systems GmbH / Sebastian Guenther, sg@freepascal.org

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}


unit SAX;

{$MODE objfpc}
{$H+}


interface

uses SysUtils, Classes;

resourcestring
  SSAXAttributeIndexError = 'Invalid attribute index %d';
  SSAXUnrecognizedFeature = 'Unknown SAX feature: "%s"';
  SSAXUnrecognizedProperty = 'Unknown SAX property: "%s"';

const
  XMLNS = 'http://www.w3.org/XML/1998/namespace';

type

  SAXString = WideString;
  SAXChar = WideChar;
  PSAXChar = ^SAXChar;

{ Exceptions }

  ESAXError = class(Exception);

  ESAXAttributeIndexError = class(ESAXError)
  public
    constructor Create(Index: Integer);
  end;

  ESAXParseException = class(ESAXError);
  ESAXNotRecognizedException = class(ESAXError);


{ TSAXInputSource: A single input source for an XML entity }

  TSAXInputSource = class
  private
    FStream: TStream;
    FEncoding: String;
    FPublicID, FSystemID: SAXString;
  public
    constructor Create; overload;
    constructor Create(AStream: TStream); overload;
    constructor Create(const ASystemID: SAXString); overload;
    property Stream: TStream read FStream write FStream;
    property Encoding: String read FEncoding write FEncoding;
    property PublicID: SAXString read FPublicID write FPublicID;
    property SystemID: SAXString read FSystemID write FSystemID;
  end;


{ TSAXAttributes: List of XML attributes }

  TSAXAttributeData = record
    URI, LocalName, QName, Value: SAXString;
    AttrType: String;
  end;

  PSAXAttributeData = ^TSAXAttributeData;

  TSAXAttributes = class
  protected
    FData: TFPList;
    function GetData(Index: Integer): PSAXAttributeData;
    property Data[Index:Integer]: PSAXAttributeData read GetData;
    procedure BadIndex(Index: Integer);
  public
    constructor Create; overload;
    constructor Create(Atts: TSAXAttributes); overload;
    destructor Destroy; override;

    function GetIndex(const QName: SAXString): Integer; overload;
    function GetIndex(const URI, LocalPart: SAXString): Integer; overload;
    function GetLength: Integer;
    function GetLocalName(Index: Integer): SAXString;
    function GetQName(Index: Integer): SAXString;
    function GetType(Index: Integer): String; overload;
    function GetType(const QName: SAXString): String; overload;
    function GetType(const URI, LocalName: SAXString): String; overload;
    function GetURI(Index: Integer): SAXString;
    function GetValue(Index: Integer): SAXString; overload;
    function GetValue(const QName: SAXString): SAXString; overload;
    function GetValue(const URI, LocalName: SAXString): SAXString; overload;

    // Manipulation methods:
    procedure Clear;
    procedure SetAttributes(Atts: TSAXAttributes);
    procedure AddAttribute(const AURI, ALocalName, AQName: SAXString;
      const AType: String; const AValue: SAXString);
    procedure SetAttribute(Index: Integer;
      const AURI, ALocalName, AQName: SAXString; const AType: String;
      const AValue: SAXString);
    procedure RemoveAttribute(Index: Integer);
    procedure SetURI(Index: Integer; const AURI: SAXString);
    procedure SetLocalName(Index: Integer; const ALocalName: SAXString);
    procedure SetQName(Index: Integer; const AQName: SAXString);
    procedure SetType(Index: Integer; const AType: String);
    procedure SetValue(Index: Integer; const AValue: SAXString);

    property Length: Integer read GetLength;
    property LocalNames[Index: Integer]: SAXString read GetLocalName;
    property QNames[Index: Integer]: SAXString read GetQName;
    property Types[Index: Integer]: String read GetType;
    property URIs[Index: Integer]: SAXString read GetURI;
    property Values[Index: Integer]: SAXString read GetValue;
  end;


{ TSAXReader: Reading an XML document using callbacks }

  TCharactersEvent = procedure(Sender: TObject; const ch: PSAXChar; AStart, ALength: Integer) of object;
  TCommentEvent = procedure(Sender: TObject; const ch: PSAXChar; AStart, ALength: Integer) of object;
  TEndElementEvent = procedure(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString) of object;
  TEndPrefixMappingEvent = procedure(Sender: TObject; const Prefix: SAXString) of object;
  TIgnorableWhitespaceEvent = procedure(Sender: TObject; const ch: PSAXChar; AStart, ALength: Integer) of object;
  TProcessingInstructionEvent = procedure(Sender: TObject; const Target, Data: SAXString) of object;
  TSkippedEntityEvent = procedure(Sender: TObject; const Name: SAXString) of object;
  TStartElementEvent = procedure(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString; Atts: TSAXAttributes) of object;
  TStartPrefixMappingEvent = procedure(Sender: TObject; const Prefix, URI: SAXString) of object;
  TNotationDeclEvent = procedure(Sender: TObject; const Name, PublicID, SystemID: SAXString) of object;
  TUnparsedEntityDeclEvent = procedure(Sender: TObject; const Name, PublicID, SystemID, NotationName: SAXString) of object;
  TResolveEntityEvent = function(Sender: TObject; const PublicID, SystemID: SAXString): TSAXInputSource of object;
  TErrorEvent = procedure(Sender: TObject; AException: ESAXParseException) of object;
  TFatalErrorEvent = procedure(Sender: TObject; AException: ESAXParseException) of object;
  TWarningEvent = procedure(Sender: TObject; AException: ESAXParseException) of object;


  TSAXReader = class
  private
    FOnCharacters: TCharactersEvent;
    FOnComment: TCommentEvent;
    FOnEndDocument: TNotifyEvent;
    FOnEndElement: TEndElementEvent;
    FOnEndPrefixMapping: TEndPrefixMappingEvent;
    FOnIgnorableWhitespace: TIgnorableWhitespaceEvent;
    FOnProcessingInstruction: TProcessingInstructionEvent;
    FOnSkippedEntity: TSkippedEntityEvent;
    FOnStartDocument: TNotifyEvent;
    FOnStartElement: TStartElementEvent;
    FOnStartPrefixMapping: TStartPrefixMappingEvent;
    FOnNotationDecl: TNotationDeclEvent;
    FOnUnparsedEntityDecl: TUnparsedEntityDeclEvent;
    FOnResolveEntity: TResolveEntityEvent;
    FOnError: TErrorEvent;
    FOnFatalError: TFatalErrorEvent;
    FOnWarning: TWarningEvent;
  protected
    FCurColumnNumber, FCurLineNumber: Integer;
    FCurPublicID, FCurSystemID: SAXString;
    FStopFlag: Boolean;

    function GetFeature(const Name: String): Boolean; virtual;
    function GetProperty(const Name: String): TObject; virtual;
    procedure SetFeature(const Name: String; Value: Boolean); virtual;
    procedure SetProperty(const Name: String; Value: TObject); virtual;

    // Notification of the content of a document
    procedure DoCharacters(const ch: PSAXChar; AStart, ALength: Integer); virtual;
    procedure DoComment(const ch: PSAXChar; AStart, ALength: Integer); virtual;
    procedure DoEndDocument; virtual;
    procedure DoEndElement(const NamespaceURI, LocalName, QName: SAXString); virtual;
    procedure DoEndPrefixMapping(const Prefix: SAXString); virtual;
    procedure DoIgnorableWhitespace(const ch: PSAXChar; AStart, ALength: Integer); virtual;
    procedure DoProcessingInstruction(const Target, Data: SAXString); virtual;
    procedure DoSkippedEntity(const Name: SAXString); virtual;
    procedure DoStartDocument; virtual;
    procedure DoStartElement(const NamespaceURI, LocalName, QName: SAXString; Atts: TSAXAttributes); virtual;
    procedure DoStartPrefixMapping(const Prefix, URI: SAXString); virtual;

    // Notification of basic DTD-related events
    procedure DoNotationDecl(const Name, PublicID, SystemID: SAXString); virtual;
    procedure DoUnparsedEntityDecl(const Name, PublicID,
      SystemID, NotationName: SAXString); virtual;

    // Resolving entities
    function DoResolveEntity(const PublicID,
      SystemID: SAXString): TSAXInputSource; virtual;

    // SAX error handlers
    procedure DoError(AException: ESAXParseException); virtual;
    procedure DoFatalError(AException: ESAXParseException); virtual;
    procedure DoWarning(AException: ESAXParseException); virtual;
  public
    procedure Parse(AInput: TSAXInputSource); virtual; abstract; overload;
    procedure Parse(const SystemID: SAXString); virtual; overload;
    procedure ParseStream(AStream: TStream);
    procedure Abort;

    // Current location
    property CurColumnNumber: Integer read FCurColumnNumber;
    property CurLineNumber: Integer read FCurLineNumber;
    property CurPublicID: SAXString read FCurPublicID;
    property CurSystemID: SAXString read FCurSystemID;

    property Features[const Name: String]: Boolean read GetFeature write SetFeature;
    property Properties[const Name: String]: TObject read GetProperty write SetProperty;

    // Content handler callbacks
    property OnCharacters: TCharactersEvent read FOnCharacters write FOnCharacters;
    property OnComment: TCommentEvent read FOnComment write FOnComment;
    property OnEndDocument: TNotifyEvent read FOnEndDocument write FOnEndDocument;
    property OnEndElement: TEndElementEvent read FOnEndElement write FOnEndElement;
    property OnEndPrefixMapping: TEndPrefixMappingEvent read FOnEndPrefixMapping write FOnEndPrefixMapping;
    property OnIgnorableWhitespace: TIgnorableWhitespaceEvent read FOnIgnorableWhitespace write FOnIgnorableWhitespace;
    property OnProcessingInstruction: TProcessingInstructionEvent read FOnProcessingInstruction write FOnProcessingInstruction;
    property OnSkippedEntity: TSkippedEntityEvent read FOnSkippedEntity write FOnSkippedEntity;
    property OnStartDocument: TNotifyEvent read FOnStartDocument write FOnStartDocument;
    property OnStartElement: TStartElementEvent read FOnStartElement write FOnStartElement;
    property OnStartPrefixMapping: TStartPrefixMappingEvent read FOnStartPrefixMapping write FOnStartPrefixMapping;
    // DTD handler callbacks
    property OnNotationDecl: TNotationDeclEvent read FOnNotationDecl write FOnNotationDecl;
    property OnUnparsedEntityDecl: TUnparsedEntityDeclEvent read FOnUnparsedEntityDecl write FOnUnparsedEntityDecl;
    // Entity resolver callbacks
    property OnResolveEntity: TResolveEntityEvent read FOnResolveEntity write FOnResolveEntity;
    // Error handler callbacks
    property OnError: TErrorEvent read FOnError write FOnError;
    property OnFatalError: TFatalErrorEvent read FOnFatalError write FOnFatalError;
    property OnWarning: TWarningEvent read FOnWarning write FOnWarning;
  end;


{ TSAXFilter: XML filter }

  TSAXFilter = class(TSAXReader)
  private
    FParent: TSAXReader;
  protected
    procedure DoCharacters(const ch: PSAXChar; AStart, ALength: Integer); override;
    procedure DoEndDocument; override;
    procedure DoEndElement(const NamespaceURI, LocalName, QName: SAXString); override;
    procedure DoEndPrefixMapping(const Prefix: SAXString); override;
    procedure DoIgnorableWhitespace(const ch: PSAXChar; AStart, ALength: Integer); override;
    procedure DoProcessingInstruction(const Target, Data: SAXString); override;
    procedure DoSkippedEntity(const Name: SAXString); override;
    procedure DoStartDocument; override;
    procedure DoStartElement(const NamespaceURI, LocalName, QName: SAXString; Atts: TSAXAttributes); override;
    procedure DoStartPrefixMapping(const Prefix, URI: SAXString); override;
    procedure DoNotationDecl(const Name, PublicID, SystemID: SAXString); override;
    procedure DoUnparsedEntityDecl(const Name, PublicID, SystemID, NotationName: SAXString); override;
    function DoResolveEntity(const PublicID, SystemID: SAXString): TSAXInputSource; override;
    procedure DoError(AException: ESAXParseException); override;
    procedure DoFatalError(AException: ESAXParseException); override;
    procedure DoWarning(AException: ESAXParseException); override;
  public
    property Parent: TSAXReader read FParent write FParent;
  end;


// ===================================================================
// ===================================================================

implementation


constructor ESAXAttributeIndexError.Create(Index: Integer);
begin
  inherited CreateFmt(SSAXAttributeIndexError, [Index]);
end;


{ TSAXInputSource }

constructor TSAXInputSource.Create;
begin
  inherited Create;
end;

constructor TSAXInputSource.Create(AStream: TStream);
begin
  inherited Create;
  FStream := AStream;
end;

constructor TSAXInputSource.Create(const ASystemID: SAXString);
begin
  inherited Create;
  FSystemID := ASystemID;
end;


{ TSAXAttributes }

constructor TSAXAttributes.Create;
begin
  inherited Create;
  FData := TFPList.Create;
end;

constructor TSAXAttributes.Create(Atts: TSAXAttributes);
begin
  inherited Create;
  FData := TFPList.Create;
  SetAttributes(Atts);
end;

destructor TSAXAttributes.Destroy;
begin
  Clear;
  FData.Free;
  inherited Destroy;
end;

function TSAXAttributes.GetIndex(const QName: SAXString): Integer;
begin
  Result := 0;
  while Result < FData.Count do
  begin
    if Data[Result]^.QName = QName then
      exit;
    Inc(Result);
  end;
  Result := -1;
end;

function TSAXAttributes.GetIndex(const URI, LocalPart: SAXString): Integer;
begin
  Result := 0;
  while Result < FData.Count do
  begin
    if (Data[Result]^.URI = URI) and (Data[Result]^.LocalName = LocalPart) then
      exit;
    Inc(Result);
  end;
  Result := -1;
end;

function TSAXAttributes.GetLength: Integer;
begin
  Result := FData.Count;
end;

function TSAXAttributes.GetLocalName(Index: Integer): SAXString;
begin
  if (Index >= 0) and (Index < FData.Count) then
    Result := Data[Index]^.LocalName
  else
    SetLength(Result, 0);
end;

function TSAXAttributes.GetQName(Index: Integer): SAXString;
begin
  if (Index >= 0) and (Index < FData.Count) then
    Result := Data[Index]^.QName
  else
    SetLength(Result, 0);
end;

function TSAXAttributes.GetType(Index: Integer): String;
begin
  if (Index >= 0) and (Index < FData.Count) then
    Result := Data[Index]^.AttrType
  else
    SetLength(Result, 0);
end;

function TSAXAttributes.GetType(const QName: SAXString): String;
var
  i: Integer;
begin
  for i := 0 to FData.Count - 1 do
    if Data[i]^.QName = QName then
    begin
      Result := Data[i]^.AttrType;
      exit;
    end;
  SetLength(Result, 0);
end;

function TSAXAttributes.GetType(const URI, LocalName: SAXString): String;
var
  i: Integer;
begin
  for i := 0 to FData.Count - 1 do
    if (Data[i]^.URI = URI) and (Data[i]^.LocalName = LocalName) then
    begin
      Result := Data[i]^.AttrType;
      exit;
    end;
  SetLength(Result, 0);
end;

function TSAXAttributes.GetURI(Index: Integer): SAXString;
begin
  if (Index >= 0) and (Index < FData.Count) then
    Result := Data[Index]^.URI
  else
    SetLength(Result, 0);
end;

function TSAXAttributes.GetValue(Index: Integer): SAXString;
begin
  if (Index >= 0) and (Index < FData.Count) then
    Result := Data[Index]^.Value
  else
    SetLength(Result, 0);
end;

function TSAXAttributes.GetValue(const QName: SAXString): SAXString;
var
  i: Integer;
begin
  for i := 0 to FData.Count - 1 do
    if Data[i]^.QName = QName then
    begin
      Result := Data[i]^.Value;
      exit;
    end;
  SetLength(Result, 0);
end;

function TSAXAttributes.GetValue(const URI, LocalName: SAXString): SAXString;
var
  i: Integer;
begin
  for i := 0 to FData.Count - 1 do
    if (Data[i]^.URI = URI) and (Data[i]^.LocalName = LocalName) then
    begin
      Result := Data[i]^.Value;
      exit;
    end;
  SetLength(Result, 0);
end;

procedure TSAXAttributes.Clear;
var
  i: Integer;
begin
  for i := 0 to FData.Count - 1 do
    Dispose(PSAXAttributeData(FData[i]));
end;

procedure TSAXAttributes.SetAttributes(Atts: TSAXAttributes);
var
  i: Integer;
begin
  FData.Count := Atts.Length;
  for i := 0 to FData.Count - 1 do
    with Data[i]^ do
    begin
      URI := Atts.URIs[i];
      LocalName := Atts.LocalNames[i];
      QName := Atts.QNames[i];
      AttrType := Atts.Types[i];
      Value := Atts.Values[i];
    end;
end;

procedure TSAXAttributes.AddAttribute(const AURI, ALocalName, AQName: SAXString;
  const AType: String; const AValue: SAXString);
var
  p: PSAXAttributeData;
begin
  New(p);
  FData.Add(p);
  p^.URI := AURI;
  p^.LocalName := ALocalName;
  p^.QName := AQName;
  p^.AttrType := AType;
  p^.Value := AValue;
end;

procedure TSAXAttributes.SetAttribute(Index: Integer;
  const AURI, ALocalName, AQName: SAXString; const AType: String;
  const AValue: SAXString);
begin
  if (Index >= 0) and (Index < FData.Count) then
    with Data[Index]^ do
    begin
      URI := AURI;
      LocalName := ALocalName;
      QName := AQName;
      AttrType := AType;
      Value := AValue;
    end
  else
    BadIndex(Index);
end;

procedure TSAXAttributes.RemoveAttribute(Index: Integer);
begin
  if (Index >= 0) and (Index < FData.Count) then
  begin
    FData.Delete(Index);
  end else
    BadIndex(Index);
end;

procedure TSAXAttributes.SetURI(Index: Integer; const AURI: SAXString);
begin
  if (Index >= 0) and (Index < FData.Count) then
    Data[Index]^.URI := AURI
  else
    BadIndex(Index);
end;

procedure TSAXAttributes.SetLocalName(Index: Integer;
  const ALocalName: SAXString);
begin
  if (Index >= 0) and (Index < FData.Count) then
    Data[Index]^.LocalName := ALocalName
  else
    BadIndex(Index);
end;

procedure TSAXAttributes.SetQName(Index: Integer; const AQName: SAXString);
begin
  if (Index >= 0) and (Index < FData.Count) then
    Data[Index]^.QName := AQName
  else
    BadIndex(Index);
end;

procedure TSAXAttributes.SetType(Index: Integer; const AType: String);
begin
  if (Index >= 0) and (Index < FData.Count) then
    Data[Index]^.AttrType := AType
  else
    BadIndex(Index);
end;

procedure TSAXAttributes.SetValue(Index: Integer; const AValue: SAXString);
begin
  if (Index >= 0) and (Index < FData.Count) then
    Data[Index]^.Value := AValue
  else
    BadIndex(Index);
end;

function TSAXAttributes.GetData(Index: Integer): PSAXAttributeData;
begin
  Result := PSAXAttributeData(FData[Index]);
end;

procedure TSAXAttributes.BadIndex(Index: Integer);
begin
  raise ESAXAttributeIndexError.Create(Index) at pointer(get_caller_addr(get_frame));
end;


{ TSAXReader }

procedure TSAXReader.Parse(const SystemID: SAXString);
var
  Input: TSAXInputSource;
begin
  Input := TSAXInputSource.Create(SystemID);
  try
    Input.Stream := TFileStream.Create(SystemID, fmOpenRead);
    try
      Parse(Input);
    finally
      Input.Stream.Free;
    end;
  finally
    Input.Free;
  end;
end;

procedure TSAXReader.ParseStream(AStream: TStream);
var
  Input: TSAXInputSource;
begin
  Input := TSAXInputSource.Create(AStream);
  try
    Parse(Input);
  finally
    Input.Free;
  end;
end;

procedure TSAXReader.Abort;
begin
  FStopFlag := True;
end;

function TSAXReader.DoResolveEntity(const PublicID,
  SystemID: SAXString): TSAXInputSource;
begin
  if Assigned(OnResolveEntity) then
    Result := OnResolveEntity(Self, PublicID, SystemID)
  else
    Result := nil;
end;

procedure TSAXReader.DoNotationDecl(const Name, PublicID, SystemID: SAXString);
begin
  if Assigned(OnNotationDecl) then
    OnNotationDecl(Self, Name, PublicID, SystemID);
end;

procedure TSAXReader.DoUnparsedEntityDecl(const Name, PublicID,
  SystemID, NotationName: SAXString);
begin
  if Assigned(OnUnparsedEntityDecl) then
    OnUnparsedEntityDecl(Self, Name, PublicID, SystemID, NotationName);
end;

function TSAXReader.GetFeature(const Name: String): Boolean;
begin
  raise ESAXNotRecognizedException.CreateFmt(SSAXUnrecognizedFeature, [Name]);
  Result := False;
end;

function TSAXReader.GetProperty(const Name: String): TObject;
begin
  raise ESAXNotRecognizedException.CreateFmt(SSAXUnrecognizedProperty, [Name]);
  Result := nil;
end;

procedure TSAXReader.SetFeature(const Name: String; Value: Boolean);
begin
  raise ESAXNotRecognizedException.CreateFmt(SSAXUnrecognizedFeature, [Name]);
end;

procedure TSAXReader.SetProperty(const Name: String; Value: TObject);
begin
  raise ESAXNotRecognizedException.CreateFmt(SSAXUnrecognizedProperty, [Name]);
end;

procedure TSAXReader.DoCharacters(const ch: PSAXChar;
  AStart, ALength: Integer);
begin
  if Assigned(OnCharacters) then
    OnCharacters(Self, ch, AStart, ALength);
end;

procedure TSAXReader.DoComment(const ch: PSAXChar;
  AStart, ALength: Integer);
begin
  if Assigned(OnComment) then
    OnComment(Self, ch, AStart, ALength);
end;

procedure TSAXReader.DoEndDocument;
begin
  if Assigned(OnEndDocument) then
    OnEndDocument(Self);
end;

procedure TSAXReader.DoEndElement(const NamespaceURI,
  LocalName, QName: SAXString);
begin
  if Assigned(OnEndElement) then
    OnEndElement(Self, NamespaceURI, LocalName, QName);
end;

procedure TSAXReader.DoEndPrefixMapping(const Prefix: SAXString);
begin
  if Assigned(OnEndPrefixMapping) then
    OnEndPrefixMapping(Self, Prefix);
end;

procedure TSAXReader.DoIgnorableWhitespace(const ch: PSAXChar;
  AStart, ALength: Integer);
begin
  if Assigned(OnIgnorableWhitespace) then
    OnIgnorableWhitespace(Self, ch, AStart, ALength);
end;

procedure TSAXReader.DoProcessingInstruction(const Target,
  Data: SAXString);
begin
  if Assigned(OnProcessingInstruction) then
    OnProcessingInstruction(Self, Target, Data);
end;

procedure TSAXReader.DoSkippedEntity(const Name: SAXString);
begin
  if Assigned(OnSkippedEntity) then
    OnSkippedEntity(Self, Name);
end;

procedure TSAXReader.DoStartDocument;
begin
  if Assigned(OnStartDocument) then
    OnStartDocument(Self);
end;

procedure TSAXReader.DoStartElement(const NamespaceURI,
  LocalName, QName: SAXString; Atts: TSAXAttributes);
begin
  if Assigned(OnStartElement) then
    OnStartElement(Self, NamespaceURI, LocalName, QName, Atts);
end;

procedure TSAXReader.DoStartPrefixMapping(const Prefix, URI: SAXString);
begin
  if Assigned(OnStartPrefixMapping) then
    OnStartPrefixMapping(Self, Prefix, URI);
end;

procedure TSAXReader.DoError(AException: ESAXParseException);
begin
  if Assigned(OnError) then
    OnError(Self, AException);
  AException.Free;
end;

procedure TSAXReader.DoFatalError(AException: ESAXParseException);
begin
  if Assigned(OnFatalError) then
    OnFatalError(Self, AException)
  else
    raise AException;
  AException.Free;
end;

procedure TSAXReader.DoWarning(AException: ESAXParseException);
begin
  if Assigned(OnWarning) then
    OnWarning(Self, AException);
  AException.Free;
end;


{ TSAXFilter }

function TSAXFilter.DoResolveEntity(const PublicID,
  SystemID: SAXString): TSAXInputSource;
begin
  if Assigned(OnResolveEntity) then
    Result := OnResolveEntity(Self, PublicID, SystemID)
  else if Assigned(Parent) then
    Result := Parent.DoResolveEntity(PublicID, SystemID)
  else
    Result := nil;
end;

procedure TSAXFilter.DoNotationDecl(const Name, PublicID, SystemID: SAXString);
begin
  if Assigned(OnNotationDecl) then
    OnNotationDecl(Self, Name, PublicID, SystemID)
  else if Assigned(Parent) then
    Parent.DoNotationDecl(Name, PublicID, SystemID);
end;

procedure TSAXFilter.DoUnparsedEntityDecl(const Name, PublicID,
  SystemID, NotationName: SAXString);
begin
  if Assigned(OnUnparsedEntityDecl) then
    OnUnparsedEntityDecl(Self, Name, PublicID, SystemID, NotationName)
  else if Assigned(Parent) then
    Parent.DoUnparsedEntityDecl(Name, PublicID, SystemID, NotationName);
end;

procedure TSAXFilter.DoCharacters(const ch: PSAXChar;
  AStart, ALength: Integer);
begin
  if Assigned(OnCharacters) then
    OnCharacters(Self, ch, AStart, ALength)
  else if Assigned(Parent) then
    Parent.DoCharacters(ch, AStart, ALength);
end;

procedure TSAXFilter.DoEndDocument;
begin
  if Assigned(OnEndDocument) then
    OnEndDocument(Self)
  else if Assigned(Parent) then
    Parent.DoEndDocument;
end;

procedure TSAXFilter.DoEndElement(const NamespaceURI,
  LocalName, QName: SAXString);
begin
  if Assigned(OnEndElement) then
    OnEndElement(Self, NamespaceURI, LocalName, QName)
  else if Assigned(Parent) then
    Parent.DoEndElement(NamespaceURI, LocalName, QName);
end;

procedure TSAXFilter.DoEndPrefixMapping(const Prefix: SAXString);
begin
  if Assigned(OnEndPrefixMapping) then
    OnEndPrefixMapping(Self, Prefix)
  else if Assigned(Parent) then
    Parent.DoEndPrefixMapping(Prefix);
end;

procedure TSAXFilter.DoIgnorableWhitespace(const ch: PSAXChar;
  AStart, ALength: Integer);
begin
  if Assigned(OnIgnorableWhitespace) then
    OnIgnorableWhitespace(Self, ch, AStart, ALength)
  else if Assigned(Parent) then
    Parent.DoIgnorableWhitespace(ch, AStart, ALength);
end;

procedure TSAXFilter.DoProcessingInstruction(const Target,
  Data: SAXString);
begin
  if Assigned(OnProcessingInstruction) then
    OnProcessingInstruction(Self, Target, Data)
  else if Assigned(Parent) then
    Parent.DoProcessingInstruction(Target, Data);
end;

procedure TSAXFilter.DoSkippedEntity(const Name: SAXString);
begin
  if Assigned(OnSkippedEntity) then
    OnSkippedEntity(Self, Name)
  else if Assigned(Parent) then
    Parent.DoSkippedEntity(Name);
end;

procedure TSAXFilter.DoStartDocument;
begin
  if Assigned(OnStartDocument) then
    OnStartDocument(Self)
  else if Assigned(Parent) then
    Parent.DoStartDocument;
end;

procedure TSAXFilter.DoStartElement(const NamespaceURI,
  LocalName, QName: SAXString; Atts: TSAXAttributes);
begin
  if Assigned(OnStartElement) then
    OnStartElement(Self, NamespaceURI, LocalName, QName, Atts)
  else if Assigned(Parent) then
    Parent.DoStartElement(NamespaceURI, LocalName, QName, Atts);
end;

procedure TSAXFilter.DoStartPrefixMapping(const Prefix, URI: SAXString);
begin
  if Assigned(OnStartPrefixMapping) then
    OnStartPrefixMapping(Self, Prefix, URI)
  else if Assigned(Parent) then
    Parent.DoStartPrefixMapping(Prefix, URI);
end;

procedure TSAXFilter.DoError(AException: ESAXParseException);
begin
  if Assigned(OnError) then
    OnError(Self, AException)
  else if Assigned(Parent) then
    Parent.DoError(AException);
  AException.Free;
end;

procedure TSAXFilter.DoFatalError(AException: ESAXParseException);
begin
  if Assigned(OnFatalError) then
    OnFatalError(Self, AException)
  else if Assigned(Parent) then
    Parent.DoFatalError(AException)
  else
    raise AException;
  AException.Free;
end;

procedure TSAXFilter.DoWarning(AException: ESAXParseException);
begin
  if Assigned(OnWarning) then
    OnWarning(Self, AException)
  else if Assigned(Parent) then
    Parent.DoWarning(AException);
  AException.Free;
end;


end.