unit dxml_sax_common;

interface

uses
  dansiview,
  dxml_sax;

type
// Ignores all subtags
TXmlReaderIgnore = object(TXmlReader)
private
  FParentReader: PXmlReader;
  FDepth2: LongInt;
public
  constructor Init(ParentReader: PXmlReader);
  procedure OnOpenTagBegin(Parser: PXmlParser; const Name: TAnsiView); virtual;
  procedure OnCloseTag(Parser: PXmlParser; const Name: TAnsiView); virtual;
  property ParentReader: PXmlReader read FParentReader write FParentReader;
  property Depth2: LongInt read FDepth2;
end;

TXmlReaderSwitchable = object(TXmlReader)
private
  FParentReader: PXmlReader;
  FIgnoreReader: TXmlReaderIgnore;
public
  constructor Init(aParentReader: PXmlReader);
  destructor Done; virtual;
  procedure IgnoreSubtags(Parser: PXmlParser);
  procedure OnOpenTagBegin(Parser: PXmlParser; const Name: TAnsiView); virtual;
  procedure OnCloseTag(Parser: PXmlParser; const Name: TAnsiView); virtual;
  property ParentReader: PXmlReader read FParentReader write FParentReader;
end;


implementation

constructor TXmlReaderIgnore.Init(ParentReader: PXmlReader);
begin
  FParentReader := ParentReader;
  FDepth2 := 0;
end;

procedure TXmlReaderIgnore.OnOpenTagBegin(Parser: PXmlParser; const Name: TAnsiView);
begin
  Inc(FDepth2);
end;

procedure TXmlReaderIgnore.OnCloseTag(Parser: PXmlParser; const Name: TAnsiView);
begin
  if FDepth2 <= 0 then begin
    FDepth2 := 0;
    Parser^.SetReader(FParentReader);
  end else
    Dec(FDepth2);
end;

constructor TXmlReaderSwitchable.Init(aParentReader: PXmlReader);
begin
  inherited Init;
  FParentReader := aParentReader;
  FIgnoreReader.Init(@Self);
end;

destructor TXmlReaderSwitchable.Done;
begin
  FIgnoreReader.Done;
  inherited Done;
end;

procedure TXmlReaderSwitchable.IgnoreSubtags(Parser: PXmlParser);
begin
  Parser^.SetReader(@FIgnoreReader);
end;

procedure TXmlReaderSwitchable.OnOpenTagBegin(Parser: PXmlParser; const Name: TAnsiView);
begin
  IgnoreSubtags(Parser);
end;

procedure TXmlReaderSwitchable.OnCloseTag(Parser: PXmlParser; const Name: TAnsiView);
begin
  if FParentReader <> nil then
    Parser^.SetReader(FParentReader);
end;

end.
