(**
===============================================================================================
Name    : LibXmlParser
===============================================================================================
Project : All Projects
===============================================================================================
Subject : Progressive XML Parser for all types of XML Files
===============================================================================================
Author  : Stefan Heymann
          Eschenweg 3
          72076 Tübingen
          GERMANY

E-Mail:   stefan@destructor.de
URL:      www.destructor.de
===============================================================================================
Source, Legals ("Licence")
--------------------------
The official site to get this parser is http://www.destructor.de/

Usage and Distribution of this Source Code is ruled by the
"Destructor.de Source code Licence" (DSL) which comes with this file or
can be downloaded at http://www.destructor.de/

IN SHORT: Usage and distribution of this source code is free.
          You use it completely on your own risk.

Postcardware
------------
If you like this code, please send a postcard of your city to my above address.
===============================================================================================
!!!  All parts of this code which are not finished or not conforming exactly to
     the XmlSpec are marked with three exclamation marks

-!-  Parts where the parser may be able to detect errors in the document's syntax are
     marked with the dash-exlamation mark-dash sequence.
===============================================================================================
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 Recommendation (version 1.0 as of 1998-02-10), 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

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 "PackSpaces" determines how Whitespace is returned in Text Content:
While PackSpaces 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 "PackSpaces" 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.
- 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.
===============================================================================================
Things Todo
-----------
- Introduce a new event/callback which is called when there is an unresolvable
  entity or character reference
- Support Unicode
- Use Streams instead of reading the whole XML into memory
===============================================================================================
Change History, Version numbers
-------------------------------
The Date is given in ISO Year-Month-Day (YYYY-MM-DD) order.
Versions are counted from 1.0.0 beginning with the version from 2000-03-16.
Unreleased versions don't get a version number.

Date        Author Version Changes
-----------------------------------------------------------------------------------------------
2000-03-16  HeySt  1.0.0   Start
2000-03-28  HeySt  1.0.1   Initial Publishing of TXmlParser on the destructor.de Web Site
2000-03-30  HeySt  1.0.2   TXmlParser.AnalyzeCData: Call "TranslateEncoding" for CurContent
2000-03-31  HeySt  1.0.3   Deleted the StrPosE function (was not needed anyway)
2000-04-04  HeySt  1.0.4   TDtdElementRec modified: Start/Final for all Elements;
                           Should be backwards compatible.
                           AnalyzeDtdc: Set CurPartType to ptDtdc
2000-04-23  HeySt  1.0.5   New class TObjectList. Eliminated reference to the Delphi 5
                           "Contnrs" unit so LibXmlParser is Delphi 4 compatible.
2000-07-03  HeySt  1.0.6   TNvpNode: Added Constructor
2000-07-11  HeySt  1.0.7   Removed "Windows" from USES clause
                           Added three-exclamation-mark comments for Utf8ToAnsi/AnsiToUtf8
                           Added three-exclamation-mark comments for CHR function calls
2000-07-23  HeySt  1.0.8   TXmlParser.Clear: CurAttr.Clear; EntityStack.Clear;
                           (This was not a bug; just defensive programming)
2000-07-29  HeySt  1.0.9   TNvpList: Added methods: Node(Index), Value(Index), Name(Index);
2000-10-07  HeySt          Introduced Conditional Defines
                           Uses Contnrs unit and its TObjectList class again for
                           Delphi 5 and newer versions
2001-01-30  HeySt          Introduced Version Numbering
                           Made LoadFromFile and LoadFromBuffer BOOLEAN functions
                           Introduced FileMode parameter for LoadFromFile
                           BugFix: TAttrList.Analyze: Must add CWhitespace to ExtractName call
                           Comments worked over
2001-02-28  HeySt  1.0.10  Completely worked over and tested the UTF-8 functions
                           Fixed a bug in TXmlParser.Scan which caused it to start over when it
                           was called after the end of scanning, resulting in an endless loop
                           TEntityStack is now a TObjectList instead of TList
2001-07-03  HeySt  1.0.11  Updated Compiler Version IFDEFs for Kylix
2001-07-11  HeySt  1.0.12  New TCustomXmlScanner component (taken over from LibXmlComps.pas)
2001-07-14  HeySt  1.0.13  Bugfix TCustomXmlScanner.FOnTranslateEncoding
2001-10-22  HeySt          Don't clear CurName anymore when the parser finds a CDATA section.
2001-12-03  HeySt  1.0.14  TObjectList.Clear: Make call to INHERITED method (fixes a memory leak)
2001-12-05  HeySt  1.0.15  TObjectList.Clear: removed call to INHERITED method
                           TObjectList.Destroy: Inserted SetCapacity call.
                           Reduces need for frequent re-allocation of pointer buffer
                           Dedicated to my father, Theodor Heymann

2002-05-01  pgm    1.1.00  Massive changes for Unicode Supoort
2002-05-23  pgm    1.1.01  Changes to improve performance via set ops for WideChars.
*)


// --- Delphi/Kylix Version Numbers
//     As this is no code, this does not blow up your object or executable code at all
       (*$DEFINE D1_OR_NEWER *)
       (*$IFNDEF VER80 *)
         (*$DEFINE D2_OR_NEWER *)
         (*$IFNDEF VER90 *)
           (*$DEFINE D3_OR_NEWER *)
           (*$IFNDEF VER100 *)
             (*$DEFINE D4_OR_NEWER *)
             (*$IFNDEF VER120 *)
               (*$DEFINE D5_OR_NEWER *)
               (*$IFNDEF VER130 *)
                 (*$IFDEF LINUX *)
                   (*$DEFINE K1_OR_NEWER *)
                 (*$ENDIF *)
                 (*$IFNDEF VER140 *)
                    (*$IFNDEF VER150 *)
if the compiler gets stuck here,
  you are using a compiler version unknown to this code.
  You will probably have to change this code accordingly.
  At first, try to comment out these lines and see what will happen.
                    (*$ENDIF *)
                 (*$ENDIF *)
               (*$ENDIF *)
             (*$ENDIF *)
           (*$ENDIF *)
         (*$ENDIF *)
       (*$ELSE *)
         (*$DEFINE WIN16 *)
       (*$ENDIF *)


unit LibXmlParser;

interface

uses
  SysUtils, Classes,
  (*$IFDEF D5_OR_NEWER *)// The Contnrs Unit was introduced in Delphi 5
  Contnrs,
  (*$ENDIF*)
  Unicode,
  Math;

const
  CVersion = '1.0.14'; // This variable will be updated for every release
                        // (I hope, I won't forget to do it everytime ...)

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: PWideChar; // 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: PWideChar; // <?PI ?>
        Content: PWideChar;
        AttrList: TAttrList);
      deError: (Pos: PWideChar); // Error
                       // deComment : ((No additional fields here));   // <!-- Comment -->
  end;

  TXmlParser = class // --- Internal Properties and Methods
  protected
    FBuffer: PWideChar; // NIL if there is no buffer available
    FBufferSize: INTEGER; // 0 if the buffer is not owned by the Document instance
    FSource: WideString; // Name of Source of document. Filename for Documents loaded with LoadFromFile

    FXmlVersion: WideString; // XML version from Document header. Default is '1.0'
    FEncoding: WideString; // Encoding from Document header. Default is 'UTF-8'
    FStandalone: BOOLEAN; // Standalone declaration from Document header. Default is 'yes'
    FRootName: WideString; // Name of the Root Element (= DTD name)
    FDtdcFinal: PWideChar; // 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: WideString; // Current Encoding during parsing

    procedure AnalyzeProlog; // Analyze XML Prolog or Text Declaration
    procedure AnalyzeComment(Start: PWideChar; var Final: PWideChar); // Analyze Comments
    procedure AnalyzePI(Start: PWideChar; var Final: PWideChar); // Analyze Processing Instructions (PI)
    procedure AnalyzeDtdc; // Analyze Document Type Declaration
    procedure AnalyzeDtdElements(Start: PWideChar; var Final: PWideChar); // 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: PWideChar; var Final: PWideChar);
    procedure AnalyzeAttListDecl(Start: PWideChar; var Final: PWideChar);
    procedure AnalyzeEntityDecl(Start: PWideChar; var Final: PWideChar);
    procedure AnalyzeNotationDecl(Start: PWideChar; var Final: PWideChar);

    procedure PushPE(var Start: PWideChar);
    procedure ReplaceCharacterEntities(var Str: WideString);
    procedure ReplaceParameterEntities(var Str: WideString);
    procedure ReplaceGeneralEntities(var Str: WideString);

    function GetDocBuffer: PWideChar; // Returns FBuffer or a pointer to a NUL char if Buffer is empty

  public // --- Document Properties
    property XmlVersion: WideString read FXmlVersion; // XML version from the Document Prolog
    property Encoding: WideString read FEncoding; // Document Encoding from Prolog
    property Standalone: BOOLEAN read FStandalone; // Standalone Declaration from Prolog
    property RootName: WideString read FRootName; // Name of the Root Element
    property Normalize: BOOLEAN read FNormalize write FNormalize; // True if Content is to be normalized
    property Source: WideString read FSource; // Name of Document Source (Filename)
    property DocBuffer: PWideChar 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: WideString;
      FileMode: INTEGER = fmOpenRead or fmShareDenyNone): BOOLEAN;
                                                                          // Loads Document from given file
    function LoadFromBuffer(Buffer: PWideChar): BOOLEAN; // Loads Document from another buffer
    procedure SetBuffer(Buffer: PWideChar); // References another buffer
    procedure Clear; // Clear Document

  public
                 // --- Scanning through the document
    CurPartType: TPartType; // Current Type
    CurName: WideString; // Current Name
    CurContent: WideString; // Current Normalized Content
    CurStart: PWideChar; // Current First character
    CurFinal: PWideChar; // Current Last character
    CurAttr: TAttrList; // Current Attribute List
    property CurEncoding: WideString read FCurEncoding; // Current Encoding
    procedure StartScan;
    function Scan: BOOLEAN;

                 // --- Events / Callbacks
    function LoadExternalEntity(SystemId, PublicId,
      Notation: WideString): TXmlParser; virtual;
    function TranslateEncoding(const Source: WideString): WideString; 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 D5_OR_NEWER *)
  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: WideString;
    Value: WideString;
    constructor Create(TheName: WideString = ''; TheValue: WideString = '');
  end;

  TNvpList = class(TObjectList) // Name-Value Pair List
    procedure Add(Node: TNvpNode);
    function Node(Name: WideString): TNvpNode; overload;
    function Node(Index: INTEGER): TNvpNode; overload;
    function Value(Name: WideString): WideString; overload;
    function Value(Index: INTEGER): WideString; overload;
    function Name(Index: INTEGER): WideString;
  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: PWideChar; var Final: PWideChar);
  end;

  TEntityStack = class(TObjectList) // Stack where current position is stored before parsing entities
  protected
    Owner: TXmlParser;
  public
    constructor Create(TheOwner: TXmlParser);
    procedure Push(LastPos: PWideChar); overload;
    procedure Push(Instance: TObject; LastPos: PWideChar); overload;
    function Pop: PWideChar; // 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: WideString; // Type definition from the DTD
    Notations: WideString; // 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: WideString; // Element name
    ElemType: TElemType; // Element type
    Definition: WideString; // Element definition from DTD
  end;

  TElemList = class(TObjectList) // List of TElemDef nodes
    function Node(Name: WideString): TElemDef;
    procedure Add(Node: TElemDef);
  end;

  TEntityDef = class(TNvpNode) // Represents a <!ENTITY Definition.
    SystemId: WideString;
    PublicId: WideString;
    NotationName: WideString;
  end;

  TNotationDef = class(TNvpNode) // Represents a <!NOTATION Definition. Value is the System ID
    PublicId: WideString;
  end;

  TUnicodeset = record
    Hibyte: byte;
    LoByteSet: set of byte;
  end;

  TInvalidCharArray = array[0..7] of TUnicodeSet;
  TNameStartArray = array[0..3] of TUnicodeSet;
  TNameCharArray = array[0..0] of TUnicodeSet;

  TCharset = set of Char;

const
{
From the W3C XML Spec...
-Name start characters must have one of the categories
    Ll, Lu, Lo, Lt, Nl.
-Name characters other than Name-start characters must have one of the categories
    Mc, Me, Mn, Lm, or Nd.
-Characters in the compatibility area (i.e. with character code greater
    than #xF900 and less than #xFFFE) are not allowed in XML names.
-Characters which have a font or compatibility decomposition
    (i.e. those with a "compatibility formatting tag" in field 5 of
    the database -- marked by field 5 beginning with a "<") are not allowed.
-The following characters are treated as name-start characters rather than
    name characters, because the property file classifies them as
    Alphabetic: [#x02BB-#x02C1], #x0559, #x06E5, #x06E6.
-Characters #x20DD-#x20E0 are excluded (in accordance with Unicode 2.0, section 5.14).
-Character #x00B7 is classified as an extender, because the
    property list so identifies it.
-Character #x0387 is added as a name character, because #x00B7
    is its canonical equivalent.
-Characters ':' and '_' are allowed as name-start characters.
-Characters '-' and '.' are allowed as name characters
}

  // TCharsets we use, these are sets of Ansi Chars
  CWhitespace = [#$20, #$09, #$0D, #$0A];
  CQuoteChar = ['"', ''''];

  // TUnicodeSets we use.. these are sets defined by the XML Spec
  CInvalidNameChars: TInvalidCharArray = (
    (HiByte: $20; LoByteSet: [$DD..$E0]), // #x20DD -> #x20E0
    (HiByte: $F9; LoByteSet: [$00..$FF]), // #xF900 -> #xF9FF
    (HiByte: $FA; LoByteSet: [$00..$FF]), // #xFA00 -> #xFAFF
    (HiByte: $FB; LoByteSet: [$00..$FF]), // #xFB00 -> #xFBFF
    (HiByte: $FC; LoByteSet: [$00..$FF]), // #xFC00 -> #xFCFF
    (HiByte: $FD; LoByteSet: [$00..$FF]), // #xFD00 -> #xFDFF
    (HiByte: $FE; LoByteSet: [$00..$FF]), // #xFE00 -> #xFEFF
    (HiByte: $FF; LoByteSet: [$00..$FE]) // #xFF00 -> #xFFFE
    );

  CNameStart: TNameStartArray = (
    (HiByte: $00; LoByteSet: [58, 95]), // '_' and ':'
    (HiByte: $02; LoByteSet: [$BB..$C1]), // #x02BB -> #x02C1
    (HiByte: $05; LoByteSet: [$59]), // #x0559
    (HiByte: $06; LoByteSet: [$E5, $E6]) // #x06E5, #x06E6
    );

  CNameChar: TNameCharArray = (
    (HiByte: $03; LoByteSet: [$87]) // #0387
    );

  // String contstants
  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: WideString; PackWs: BOOLEAN): WideString; // Convert WS to spaces #x20
procedure SetStringSF(var S: WideString; BufferStart, BufferFinal: PWideChar); // SetString by Start/Final of buffer
function StrSFPas(Start, Finish: PWideChar): WideString; // Convert buffer part to Pascal string
function TrimWs(Source: WideString): WideString; // Trim Whitespace

(*
FUNCTION  AnsiToUtf8  (Source : ANSISTRING) : WideString;                            // Convert Win-1252 to UTF-8
FUNCTION  Utf8ToAnsi  (Source : WideString; UnknownChar : CHAR = '¿') : ANSISTRING;  // Convert UTF-8 to Win-1252
*)


(*
===============================================================================================
TCustomXmlScanner event based component wrapper for TXmlParser
===============================================================================================
*)

type
  TCustomXmlScanner = class;
  TXmlPrologEvent = procedure(Sender: TObject; XmlVersion, Encoding: WideString; Standalone: BOOLEAN) of object;
  TCommentEvent = procedure(Sender: TObject; Comment: WideString) of object;
  TPIEvent = procedure(Sender: TObject; Target, Content: WideString; Attributes: TAttrList) of object;
  TDtdEvent = procedure(Sender: TObject; RootElementName: WideString) of object;
  TStartTagEvent = procedure(Sender: TObject; TagName: WideString; Attributes: TAttrList) of object;
  TEndTagEvent = procedure(Sender: TObject; TagName: WideString) of object;
  TContentEvent = procedure(Sender: TObject; Content: WideString) 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: PWideChar) of object;
  TExternalEvent = procedure(Sender: TObject; SystemId, PublicId, NotationId: WideString;
    var Result: TXmlParser) of object;
  TEncodingEvent = function(Sender: TObject; CurrentEncoding, Source: WideString): WideString 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;
    FStopParser: BOOLEAN;
    function GetNormalize: BOOLEAN;
    procedure SetNormalize(Value: BOOLEAN);

    procedure WhenXmlProlog(XmlVersion, Encoding: WideString; Standalone: BOOLEAN); virtual;
    procedure WhenComment(Comment: WideString); virtual;
    procedure WhenPI(Target, Content: WideString; Attributes: TAttrList); virtual;
    procedure WhenDtdRead(RootElementName: WideString); virtual;
    procedure WhenStartTag(TagName: WideString; Attributes: TAttrList); virtual;
    procedure WhenEmptyTag(TagName: WideString; Attributes: TAttrList); virtual;
    procedure WhenEndTag(TagName: WideString); virtual;
    procedure WhenContent(Content: WideString); virtual;
    procedure WhenCData(Content: WideString); virtual;
    procedure WhenElement(ElemDef: TElemDef); virtual;
    procedure WhenAttList(ElemDef: TElemDef); virtual;
    procedure WhenEntity(EntityDef: TEntityDef); virtual;
    procedure WhenNotation(NotationDef: TNotationDef); virtual;
    procedure WhenDtdError(ErrorPos: PWideChar); virtual;

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure LoadFromFile(Filename: TFilename); // Load XML Document from file
    procedure LoadFromBuffer(Buffer: PWideChar); // Load XML Document from buffer
    procedure SetBuffer(Buffer: PWideChar); // 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;
  end;

(*
===============================================================================================
IMPLEMENTATION
===============================================================================================
*)

implementation

(*
===============================================================================================
"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: PWideChar): PWideChar;
         // Same functionality as SysUtils.StrPos
var
  First: WideChar;
  Len: INTEGER;
begin
  First := SearchStr^;
  Len := StrLenW(SearchStr);
  Result := Str;
  repeat
    if Result^ = First then
      if StrLCompW(Result, SearchStr, Len) = 0 then BREAK;
    if Result^ = #0 then begin
      Result := nil;
      BREAK;
    end;
    INC(Result);
  until FALSE;
end;


function StrScan(const Start: PWideChar; const Ch: WideChar): PWideChar;
         // 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 WideCharInSet(c: WideChar; chars: TCharset): boolean; overload;
var
  hib: byte;
  loc: Char;
begin
    // Check to see if this widechar is in a set of ANSI chars
    // If the hibyte is != 0, then we know it's not ansi.
  Result := false;
  hib := Hi(Ord(c));
  if (hib = 0) then begin
    loc := Chr(Lo(Ord(c)));
    Result := (loc in chars);
  end;
end;

function WideCharInSet(p: PWideChar; chars: TCharset): boolean; overload;
begin
  Result := WideCharInSet(p^, chars);
end;

// pgm May 23, 2002 - Added XML Spec stuff
{
From the W3C XML Spec...
-Name start characters must have one of the categories
    Ll, Lu, Lo, Lt, Nl.
  UC_LL = $00008000; // Letter, Lowercase
  UC_LU = $00004000; // Letter, Uppercase
  UC_LO = $00040000; // Letter, Other
  UC_LT = $00010000; // Letter, Titlecase
  UC_NL = $00000010; // Number, Letter

-Name characters other than Name-start characters must have one of the categories
    Mc, Me, Mn, Lm, or Nd.
  UC_MC = $00000002; // Mark, Spacing Combining
  UC_ME = $00000004; // Mark, Enclosing
  UC_MN = $00000001; // Mark, Non-Spacing
  UC_LM = $00020000; // Letter, Modifier
  UC_ND = $00000008; // Number, Decimal Digit

-Characters in the compatibility area (i.e. with character code greater
    than #xF900 and less than #xFFFE) are not allowed in XML names.
-Characters which have a font or compatibility decomposition
    (i.e. those with a "compatibility formatting tag" in field 5 of
    the database -- marked by field 5 beginning with a "<") are not allowed.
-The following characters are treated as name-start characters rather than
    name characters, because the property file classifies them as
    Alphabetic: [#x02BB-#x02C1], #x0559, #x06E5, #x06E6.
-Characters #x20DD-#x20E0 are excluded (in accordance with Unicode 2.0, section 5.14).
-Character #x00B7 is classified as an extender, because the
    property list so identifies it.
-Character #x0387 is added as a name character, because #x00B7
    is its canonical equivalent.
-Characters ':' and '_' are allowed as name-start characters.
-Characters '-' and '.' are allowed as name characters
}

function UnicodeLookup(C: UCS4; s: array of TUnicodeset): boolean;
var
  i: integer;
  lb, hb: byte;
  u: TUnicodeSet;
begin
    // Lookup this Unicode char in an lookup table
    // The tables are arrays of structs, where each array element
    // is a TUnicodeSet
  Result := false;
  lb := Lo(C);
  hb := Hi(C);

    // loop thru all elements in the array
  for i := 0 to Length(s) - 1 do begin
    u := s[i];
    if (u.Hibyte = hb) and (lb in u.LoByteSet) then begin
      Result := true;
      exit;
    end;
  end;
end;

function UnicodeIsXMLNameStart(C: UCS4): boolean;
begin
    // Is this char a valid XML name "start" char??
  Result := false;

  if (UnicodeLookup(C, CInvalidNameChars)) then exit;

  if (UnicodeLookup(C, CNameStart)) then
    Result := true
  else
    Result := IsProperty(C, UC_LL or UC_LU or UC_LO or UC_LT or UC_NL, 0);
end;

function UnicodeIsXMLName(C: UCS4): boolean;
begin
    // Is this character a valid XML name char?
  if (UnicodeLookup(C, CInvalidNameChars)) then
    Result := false
  else if (UnicodeLookup(C, CNameChar)) then
    Result := true
  else
    Result := UnicodeIsXMLNameStart(C) or
      IsProperty(C, UC_MC or UC_ME or UC_MN or UC_LM or UC_ND, 0);
end;

function DelWhitespace(Source: WideString): WideString;
var
  i: integer;
  c: WideChar;
begin
  Result := '';
  for i := 0 to Length(Source) do begin
    c := Source[i];
    if (not WideCharInSet(C, CWhitespace)) then
      Result := Result + c;
  end;
end;


function TrimWS(Source: WideString): WideString;
begin
  Result := WideTrimRight(WideTrimLeft(Source));
end;

function ConvertWS(Source: WideString; PackWS: boolean): WideString;
var
  i: integer;
  c: WideChar;
  inws: boolean;
begin
    // Converts all Whitespace characters to the Space #x20 character
    // If "PackWs" is true, contiguous Whitespace characters are packed to one
  Result := '';
  inws := false;
  for i := 1 to Length(Source) do begin
    c := Source[i];
    if (WideCharInSet(c, CWhitespace)) then begin
      if ((inws) and (PackWS)) then
                // do nothing
      else begin
        inws := true;
        Result := Result + c;
      end;
    end
    else begin
      Result := Result + c;
      inws := false;
    end;
  end;
end;

procedure SetStringSF(var S: WideString; BufferStart, BufferFinal: PWideChar);
begin
  SetString(S, BufferStart, BufferFinal - BufferStart + 1);
end;

function StrLPas(Start: PWideChar; Len: INTEGER): WideString;
begin
  SetString(Result, Start, Len);
end;

function StrSFPas(Start, Finish: PWideChar): WideString;
begin
  SetString(Result, Start, Finish - Start + 1);
end;


function StrScanE(const Source: PWideChar; const CharToScanFor: WideChar): PWideChar;
          // 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 := StrEndW(Source) - 1;
end;

procedure ExtractName(Start: PWideChar; Terminators: TCharset; var Final: PWideChar);
          {
            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: TCharset of additional ansi characters to check as terminators
            Final:       OUT Pointer to last char of name
          }
var
  cont: boolean;
  hib: byte;
  c: Cardinal;
  loc: Char;
begin
  Final := Start + 1; // start at the next character past "Start"
  Terminators := Terminators + ['>']; // always look for ">"
  cont := true;
  while (cont) do begin
    c := Ord(Final^);

        // if the char doesn't meet the Unicode func, check the charset if it's
        // an ANSI char (HiByte = 0)
    hib := Hi(c);
    loc := Chr(Lo(C));
    if (hib = 0) then
      cont := not (loc in Terminators);

    if (Final^ = #0) then cont := false;
    if (cont) then inc(Final);
  end;
  dec(Final);
end;


procedure ExtractQuote(Start: PWideChar; var Content: WideString; var Final: PWideChar);
          (* 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 := StrEndW(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: WideString;
    LastPos: PWideChar;
  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: PWideChar);
begin
  Push(nil, LastPos);
end;


procedure TEntityStack.Push(Instance: TObject; LastPos: PWideChar);
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: PWideChar;
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: WideString;
    SystemId: WideString;
    Final: PWideChar;
    constructor Create(Start: PWideChar);
  end;

constructor TExternalID.Create(Start: PWideChar);
begin
  inherited Create;
  Final := Start;
  if StrLCompW(Start, 'SYSTEM', 6) = 0 then begin
    while not (WideCharInSet(Final, CQuoteChar + [#0, '>', '['])) do inc(Final);
    if not (WideCharInSet(Final, CQuoteChar)) then exit;
    ExtractQuote(Final, SystemID, Final);
  end
  else if StrLCompW(Start, 'PUBLIC', 6) = 0 then begin
    while not (WideCharInSet(Final, CQuoteChar + [#0, '>', '['])) do inc(Final);
    if not (WideCharInSet(Final, CQuoteChar)) then exit;
    ExtractQuote(Final, PublicID, Final);
    INC(Final);
    while not (WideCharInSet(Final, CQuoteChar + [#0, '>', '['])) do inc(Final);
    if not (WideCharInSet(Final, CQuoteChar)) then exit;
    ExtractQuote(Final, SystemID, Final);
  end;
end;


(*
===============================================================================================
TXmlParser
===============================================================================================
*)

constructor TXmlParser.Create;
begin
  inherited Create;
  FBuffer := nil;
  FBufferSize := 0;
  Elements := TElemList.Create;
  Entities := TNvpList.Create;
  ParEntities := TNvpList.Create;
  Notations := TNvpList.Create;
  CurAttr := TAttrList.Create;
  EntityStack := TEntityStack.Create(Self);
  Clear;
end;


destructor TXmlParser.Destroy;
begin
  Clear;
  Elements.Free;
  Entities.Free;
  ParEntities.Free;
  Notations.Free;
  CurAttr.Free;
  EntityStack.Free;
  inherited Destroy;
end;


procedure TXmlParser.Clear;
          // Free Buffer and clear all object attributes
begin
  if (FBufferSize > 0) and (FBuffer <> nil) then
    FreeMem(FBuffer);
  FBuffer := nil;
  FBufferSize := 0;
  FSource := '';
  FXmlVersion := '';
  FEncoding := '';
  FStandalone := FALSE;
  FRootName := '';
  FDtdcFinal := nil;
  FNormalize := TRUE;
  Elements.Clear;
  Entities.Clear;
  ParEntities.Clear;
  Notations.Clear;
  CurAttr.Clear;
  EntityStack.Clear;
end;


function TXmlParser.LoadFromFile(Filename: WideString; FileMode: INTEGER = fmOpenRead or fmShareDenyNone): BOOLEAN;
          // Loads Document from given file
          // Returns TRUE if successful
var

  wsl: TWideStringList;

begin
  Result := FALSE;
  Clear;

  // pgm May 29, 2002 - TODO: use a better way of doing this
  // We can't use a TextFile here since it could be unicode..
  // Just use an encapsulation around a WideStringList

  wsl := TWideStringList.Create();
  wsl.LoadFromFile(Filename);

  Self.LoadFromBuffer(PWideChar(wsl.Text));

  wsl.Free();

end;


function TXmlParser.LoadFromBuffer(Buffer: PWideChar): BOOLEAN;
          // Loads Document from another buffer
          // Returns TRUE if successful
          // The "Source" property becomes '<MEM>' if successful
begin
  Result := FALSE;
  Clear;
  FBufferSize := sizeof(WideChar) * (StrLenW(Buffer) + 1);
  try
    GetMem(FBuffer, FBufferSize);
  except
    Clear;
    EXIT;
  end;
  StrCopyW(FBuffer, Buffer);
  FSource := '<MEM>';
  Result := TRUE;
end;


procedure TXmlParser.SetBuffer(Buffer: PWideChar); // References another buffer
begin
  Clear;
  FBuffer := Buffer;
  FBufferSize := 0;
  FSource := '<REFERENCE>';
end;


//-----------------------------------------------------------------------------------------------
// Scanning through the document
//-----------------------------------------------------------------------------------------------

procedure TXmlParser.StartScan;
begin
  CurPartType := ptNone;
  CurName := '';
  CurContent := '';
  CurStart := nil;
  CurFinal := nil;
  CurAttr.Clear;
  EntityStack.Clear;
end;


function TXmlParser.Scan: BOOLEAN;
          // Scans the next Part
          // Returns TRUE if a part could be found, FALSE if there is no part any more
          //
          // "IsDone" can be set to FALSE by AnalyzeText in order to go to the next part
          // if there is no Content due to normalization
var
  IsDone: BOOLEAN;
begin
  repeat
    IsDone := TRUE;

    // --- Start of next Part
    if CurStart = nil
      then CurStart := DocBuffer
    else CurStart := CurFinal + 1;
    CurFinal := CurStart;

    // --- End of Document of Pop off a new part from the Entity stack?
    if CurStart^ = #0 then
      CurStart := EntityStack.Pop;

    // --- No Document or End Of Document: Terminate Scan
    if (CurStart = nil) or (CurStart^ = #0) then begin
      CurStart := StrEndW(DocBuffer);
      CurFinal := CurStart - 1;
      EntityStack.Clear;
      Result := FALSE;
      EXIT;
    end;

    if StrLCompW(CurStart, '<?xml', 5) = 0 then AnalyzeProlog // XML Prolog, Text Declaration
    else if StrLCompW(CurStart, '<?', 2) = 0 then AnalyzePI(CurStart, CurFinal) // PI
    else if StrLCompW(CurStart, '<!--', 4) = 0 then AnalyzeComment(CurStart, CurFinal) // Comment
    else if StrLCompW(CurStart, '<!DOCTYPE', 9) = 0 then AnalyzeDtdc // DTDc
    else if StrLCompW(CurStart, CDStart, Length(CDStart)) = 0 then AnalyzeCdata // CDATA Section
    else if StrLCompW(CurStart, '<', 1) = 0 then AnalyzeTag // Start-Tag, End-Tag, Empty-Element-Tag
    else AnalyzeText(IsDone); // Text Content
  until IsDone;
  Result := TRUE;
end;


procedure TXmlParser.AnalyzeProlog;
          // Analyze XML Prolog or Text Declaration
var
  F: PWideChar;
begin
  CurAttr.Analyze(CurStart + 5, F);
  if EntityStack.Count = 0 then begin
    FXmlVersion := CurAttr.Value('version');
    FEncoding := CurAttr.Value('encoding');
    FStandalone := CurAttr.Value('standalone') = 'yes';
  end;
  CurFinal := StrPos(F, '?>');
  if CurFinal <> nil
    then INC(CurFinal)
  else CurFinal := StrEndW(CurStart) - 1;
  FCurEncoding := CurAttr.Value('encoding');
  if FCurEncoding = '' then
    FCurEncoding := 'UTF-8'; // Default XML Encoding is UTF-8
  CurPartType := ptXmlProlog;
  CurName := '';
  CurContent := '';
end;


procedure TXmlParser.AnalyzeComment(Start: PWideChar; var Final: PWideChar);
          // Analyze Comments
begin
  Final := StrPos(Start + 4, '-->');
  if Final = nil
    then Final := StrEndW(Start) - 1
  else INC(Final, 2);
  CurPartType := ptComment;
end;


procedure TXmlParser.AnalyzePI(Start: PWideChar; var Final: PWideChar);
          // Analyze Processing Instructions (PI)
          // This is also called for Character
var
  F: PWideChar;
begin
  CurPartType := ptPI;
  Final := StrPos(Start + 2, '?>');
  if Final = nil
    then Final := StrEndW(Start) - 1
  else INC(Final);
  ExtractName(Start + 2, CWhitespace + ['?', '>'], F);
  SetStringSF(CurName, Start + 2, F);
  SetStringSF(CurContent, F + 1, Final - 2);
  CurAttr.Analyze(F + 1, F);
end;


procedure TXmlParser.AnalyzeDtdc;
          (* Analyze Document Type Declaration
                 doctypedecl  ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
                 markupdecl   ::= elementdecl | AttlistDecl | EntityDecl | NotationDecl | PI | Comment
                 PEReference  ::= '%' Name ';'

                 elementdecl  ::= '<!ELEMENT' S Name S contentspec S?                    '>'
                 AttlistDecl  ::= '<!ATTLIST' S Name AttDef* S?                          '>'
                 EntityDecl   ::= '<!ENTITY' S Name S EntityDef S?                       '>' |
                                  '<!ENTITY' S '%' S Name S PEDef S?                     '>'
                 NotationDecl ::= '<!NOTATION' S Name S (ExternalID |  PublicID) S?      '>'
                 PI           ::=  '<?' PITarget (S (Char* - (Char* '?>' Char* )))?     '?>'
                 Comment      ::= '<!--' ((Char - '-') | ('-' (Char - '-')))*          '-->'  *)
type
  TPhase = (phName, phDtd, phInternal, phFinishing);
var
  Phase: TPhase;
  F: PWideChar;
  ExternalID: TExternalID;
  ExternalDTD: TXmlParser;
  DER: TDtdElementRec;
begin
  DER.Start := CurStart;
  EntityStack.Clear; // Clear stack for Parameter Entities
  CurPartType := ptDtdc;

  // --- Don't read DTDc twice
  if FDtdcFinal <> nil then begin
    CurFinal := FDtdcFinal;
    EXIT;
  end;

  // --- Scan DTDc
  CurFinal := CurStart + 9; // First char after '<!DOCTYPE'
  Phase := phName;
  repeat
    case CurFinal^ of
      '%': begin
          PushPE(CurFinal);
          CONTINUE;
        end;
      #0: if EntityStack.Count = 0 then
          BREAK
        else begin
          CurFinal := EntityStack.Pop;
          CONTINUE;
        end;
      '[': begin
          Phase := phInternal;
          AnalyzeDtdElements(CurFinal + 1, CurFinal);
          CONTINUE;
        end;
      ']': Phase := phFinishing;
      '>': BREAK;
    else if not (WideCharInSet(CurFinal, CWhitespace)) then begin
      case Phase of
        phName: if (UnicodeIsXMLNameStart(Ord(CurFinal^))) then begin
            ExtractName(CurFinal, CWhitespace + ['[', '>'], F);
            SetStringSF(FRootName, CurFinal, F);
            CurFinal := F;
            Phase := phDtd;
          end;
        phDtd: if (StrLCompW(CurFinal, 'SYSTEM', 6) = 0) or
          (StrLCompW(CurFinal, 'PUBLIC', 6) = 0) then begin
            ExternalID := TExternalID.Create(CurFinal);
            ExternalDTD := LoadExternalEntity(ExternalId.SystemId, ExternalID.PublicId, '');
            F := StrPos(ExternalDtd.DocBuffer, '<!');
            if F <> nil then
              AnalyzeDtdElements(F, F);
            ExternalDTD.Free;
            CurFinal := ExternalID.Final;
            ExternalID.Free;
          end;
      else begin
          DER.ElementType := deError;
          DER.Pos := CurFinal;
          DER.Final := CurFinal;
          DtdElementFound(DER);
        end;
      end;

    end;
    end;
    INC(CurFinal);
  until FALSE;

  CurPartType := ptDtdc;
  CurName := '';
  CurContent := '';

  // It is an error in the document if "EntityStack" is not empty now
  if EntityStack.Count > 0 then begin
    DER.ElementType := deError;
    DER.Final := CurFinal;
    DER.Pos := CurFinal;
    DtdElementFound(DER);
  end;

  EntityStack.Clear; // Clear stack for General Entities
  FDtdcFinal := CurFinal;
end;


procedure TXmlParser.AnalyzeDtdElements(Start: PWideChar; var Final: PWideChar);
          // Analyze the "Elements" of a DTD contained in the external or
          // internal DTD subset.
var
  DER: TDtdElementRec;
begin
  Final := Start;
  repeat
    case Final^ of
      '%': begin
          PushPE(Final);
          CONTINUE;
        end;
      #0: if EntityStack.Count = 0 then
          BREAK
        else begin
          CurFinal := EntityStack.Pop;
          CONTINUE;
        end;
      ']',
        '>': BREAK;
      '<': if StrLCompW(Final, '<!ELEMENT', 9) = 0 then AnalyzeElementDecl(Final, Final)
        else if StrLCompW(Final, '<!ATTLIST', 9) = 0 then AnalyzeAttListDecl(Final, Final)
        else if StrLCompW(Final, '<!ENTITY', 8) = 0 then AnalyzeEntityDecl(Final, Final)
        else if StrLCompW(Final, '<!NOTATION', 10) = 0 then AnalyzeNotationDecl(Final, Final)
        else if StrLCompW(Final, '<?', 2) = 0 then begin // PI in DTD
          DER.ElementType := dePI;
          DER.Start := Final;
          AnalyzePI(Final, Final);
          DER.Target := PWideChar(CurName);
          DER.Content := PWideChar(CurContent);
          DER.AttrList := CurAttr;
          DER.Final := Final;
          DtdElementFound(DER);
        end
        else if StrLCompW(Final, '<!--', 4) = 0 then begin // Comment in DTD
          DER.ElementType := deComment;
          DER.Start := Final;
          AnalyzeComment(Final, Final);
          DER.Final := Final;
          DtdElementFound(DER);
        end
        else begin
          DER.ElementType := deError;
          DER.Start := Final;
          DER.Pos := Final;
          DER.Final := Final;
          DtdElementFound(DER);
        end;

    end;
    INC(Final);
  until FALSE;
end;


procedure TXmlParser.AnalyzeTag;
          // Analyze Tags
var
  S, F: PWideChar;
  Attr: TAttr;
  ElemDef: TElemDef;
  AttrDef: TAttrDef;
  I: INTEGER;
begin
  CurPartType := ptStartTag;
  S := CurStart + 1;
  if S^ = '/' then begin
    CurPartType := ptEndTag;
    INC(S);
  end;
  ExtractName(S, CWhitespace + ['/'], F);
  SetStringSF(CurName, S, F);
  CurAttr.Analyze(F + 1, CurFinal);
  if CurFinal^ = '/' then begin
    CurPartType := ptEmptyTag;
  end;
  CurFinal := StrScanE(CurFinal, '>');

  // --- Set Default Attribute values for nonexistent attributes
  if (CurPartType = ptStartTag) or (CurPartType = ptEmptyTag) then begin
    ElemDef := Elements.Node(CurName);
    if ElemDef <> nil then begin
      for I := 0 to ElemDef.Count - 1 do begin
        AttrDef := TAttrDef(ElemDef[I]);
        Attr := TAttr(CurAttr.Node(AttrDef.Name));
        if (Attr = nil) and (AttrDef.Value <> '') then begin
          Attr := TAttr.Create(AttrDef.Name, AttrDef.Value);
          Attr.ValueType := vtDefault;
          CurAttr.Add(Attr);
        end;
        if Attr <> nil then begin
          case AttrDef.DefaultType of
            adDefault: ;
            adRequired: ; // -!- It is an error in the document if "Attr.Value" is an empty string
            adImplied: Attr.ValueType := vtImplied;
            adFixed: begin
                Attr.ValueType := vtFixed;
                Attr.Value := AttrDef.Value;
              end;
          end;
          Attr.AttrType := AttrDef.AttrType;
        end;
      end;
    end;

    // --- Normalize Attribute Values. XmlSpec:
           // - a character reference is processed by appending the referenced character to the attribute value
           // - an entity reference is processed by recursively processing the replacement text of the entity
           // - a whitespace character (#x20, #xD, #xA, #x9) is processed by appending #x20 to the normalized value,
           //   except that only a single #x20 is appended for a "#xD#xA" sequence that is part of an external
           //   parsed entity or the literal entity value of an internal parsed entity
           // - other characters are processed by appending them to the normalized value
           // If the declared value is not CDATA, then the XML processor must further process the
           // normalized attribute value by discarding any leading and trailing space (#x20) characters,
           // and by replacing sequences of space (#x20) characters by a single space (#x20) character.
           // All attributes for which no declaration has been read should be treated by a
           // non-validating parser as if declared CDATA.
           // !!! The XML 1.0 SE specification is somewhat different here
           //     This code does not conform exactly to this specification
    for I := 0 to CurAttr.Count - 1 do
      with TAttr(CurAttr[I]) do begin
        ReplaceGeneralEntities(Value);
        ReplaceCharacterEntities(Value);
        if (AttrType <> atCData) and (AttrType <> atUnknown)
          then Value := TranslateEncoding(TrimWs(ConvertWs(Value, TRUE)))
        else Value := TranslateEncoding(ConvertWs(Value, FALSE));
      end;
  end;
end;


procedure TXmlParser.AnalyzeCData;
          // Analyze CDATA Sections
begin
  CurPartType := ptCData;
  CurFinal := StrPos(CurStart, CDEnd);
  if CurFinal = nil then begin
    CurFinal := StrEndW(CurStart) - 1;
    CurContent := TranslateEncoding(WideString(CurStart + Length(CDStart)));
  end
  else begin
    SetStringSF(CurContent, CurStart + Length(CDStart), CurFinal - 1);
    INC(CurFinal, Length(CDEnd) - 1);
    CurContent := TranslateEncoding(CurContent);
  end;
end;


procedure TXmlParser.AnalyzeText(var IsDone: BOOLEAN);
          (* Analyzes Text Content between Tags. CurFinal will point to the last content character.
             Content ends at a '<' character or at the end of the document.
             Entity References and Character Entity references are resolved.
             If PackSpaces is TRUE, contiguous Whitespace Characters will be compressed to
             one Space #x20 character, Whitespace at the beginning and end of content will
             be trimmed off and content which is or becomes empty is not returned to
             the application (in this case, "IsDone" is set to FALSE which causes the
             Scan method to proceed directly to the next part. *)

  procedure ProcessEntity;
            (* Is called if there is an ampsersand '&' character found in the document.
               IN  "CurFinal" points to the ampersand
               OUT "CurFinal" points to the first character after the semi-colon ';' *)
  var
    P: PWideChar;
    Name: WideString;
    EntityDef: TEntityDef;
    ExternalEntity: TXmlParser;
  begin
    P := StrScan(CurFinal, ';');
    if P <> nil then begin
      SetStringSF(Name, CurFinal + 1, P - 1);

      // Is it a Character Entity?
      if (CurFinal + 1)^ = '#' then begin
        // pgm 5/8/02 - NOT SURE IF THIS WORKS W/ UNICODE!!
        // todo: Verify Char Entities work correctly w/ unicode
        if (((CurFinal + 2)^ = 'X') or ((CurFinal + 2)^ = 'x'))
        // IF UpCase ((CurFinal+2)^) = 'X'       // !!! Can't use "CHR" for Unicode characters > 255:
        then CurContent := CurContent + CHR(StrToIntDef('$' + Copy(Name, 3, MaxInt), 32))
        else CurContent := CurContent + CHR(StrToIntDef(Copy(Name, 2, MaxInt), 32));
        CurFinal := P + 1;
        EXIT;
      end

      // Is it a Predefined Entity?
      else if Name = 'lt' then begin CurContent := CurContent + '<'; CurFinal := P + 1; EXIT; end
      else if Name = 'gt' then begin CurContent := CurContent + '>'; CurFinal := P + 1; EXIT; end
      else if Name = 'amp' then begin CurContent := CurContent + '&'; CurFinal := P + 1; EXIT; end
      else if Name = 'apos' then begin CurContent := CurContent + ''''; CurFinal := P + 1; EXIT; end
      else if Name = 'quot' then begin CurContent := CurContent + '"'; CurFinal := P + 1; EXIT; end;

      // Replace with Entity from DTD
      EntityDef := TEntityDef(Entities.Node(Name));
      if EntityDef <> nil then begin
        if EntityDef.Value <> '' then begin
          EntityStack.Push(P + 1);
          CurFinal := PWideChar(EntityDef.Value);
        end
        else begin
          ExternalEntity := LoadExternalEntity(EntityDef.SystemId, EntityDef.PublicId, EntityDef.NotationName);
          EntityStack.Push(ExternalEntity, P + 1);
          CurFinal := ExternalEntity.DocBuffer;
        end;
      end
      else begin
        CurContent := CurContent + Name;
        CurFinal := P + 1;
      end;
    end
    else begin
      INC(CurFinal);
    end;
  end;

var
  C: INTEGER;
begin
  CurFinal := CurStart;
  CurPartType := ptContent;
  CurContent := '';
  C := 0;
  repeat
    case CurFinal^ of
      '&': begin
          CurContent := CurContent + TranslateEncoding(StrLPas(CurFinal - C, C));
          C := 0;
          ProcessEntity;
          CONTINUE;
        end;
      #0: begin
          if EntityStack.Count = 0 then
            BREAK
          else begin
            CurContent := CurContent + TranslateEncoding(StrLPas(CurFinal - C, C));
            C := 0;
            CurFinal := EntityStack.Pop;
            CONTINUE;
          end;
        end;
      '<': BREAK;
    else INC(C);
    end;
    INC(CurFinal);
  until FALSE;
  CurContent := CurContent + TranslateEncoding(StrLPas(CurFinal - C, C));
  DEC(CurFinal);

  if FNormalize then begin
    CurContent := ConvertWs(TrimWs(CurContent), TRUE);
    IsDone := CurContent <> ''; // IsDone will only get FALSE if PackSpaces is TRUE
  end;
end;


procedure TXmlParser.AnalyzeElementDecl(Start: PWideChar; var Final: PWideChar);
          (* Parse <!ELEMENT declaration starting at "Start"
             Final must point to the terminating '>' character
             XmlSpec 3.2:
                 elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
                 contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
                 Mixed       ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*'   |
                                 '(' S? '#PCDATA' S? ')'
                 children    ::= (choice | seq) ('?' | '*' | '+')?
                 choice      ::= '(' S? cp ( S? '|' S? cp )* S? ')'
                 cp          ::= (Name | choice | seq) ('?' | '*' | '+')?
                 seq         ::= '(' S? cp ( S? ',' S? cp )* S? ')'

             More simply:
                 contentspec ::= EMPTY
                                 ANY
                                 '(#PCDATA)'
                                 '(#PCDATA | A | B)*'
                                 '(A, B, C)'
                                 '(A | B | C)'
                                 '(A?, B*, C+),
                                 '(A, (B | C | D)* )'                       *)
var
  Element: TElemDef;
  Elem2: TElemDef;
  F: PWideChar;
  DER: TDtdElementRec;
begin
  Element := TElemDef.Create;
  Final := Start + 9;
  DER.Start := Start;
  repeat
    if Final^ = '>' then BREAK;

    if (UnicodeIsXMLNameStart(Ord(Final^))) and (Element.Name = '') then begin
      ExtractName(Final, CWhitespace, F);
      SetStringSF(Element.Name, Final, F);
      Final := F;
      F := StrScan(Final + 1, '>');
      if F = nil then begin
        // Element.Definition := STRING (Final);
        Element.Definition := WideString(Final);
        Final := StrEndW(Final);
        BREAK;
      end
      else begin
        SetStringSF(Element.Definition, Final + 1, F - 1);
        Final := F;
        BREAK;
      end;
    end;
    INC(Final);
  until FALSE;
  Element.Definition := DelWhitespace(Element.Definition);
  ReplaceParameterEntities(Element.Definition);
  if Element.Definition = 'EMPTY' then Element.ElemType := etEmpty
  else if Element.Definition = 'ANY' then Element.ElemType := etAny
  else if Copy(Element.Definition, 1, 8) = '(#PCDATA' then Element.ElemType := etMixed
  else if Copy(Element.Definition, 1, 1) = '(' then Element.ElemType := etChildren
  else Element.ElemType := etAny;

  Elem2 := Elements.Node(Element.Name);
  if Elem2 <> nil then
    Elements.Delete(Elements.IndexOf(Elem2));
  Elements.Add(Element);
  Final := StrScanE(Final, '>');
  DER.ElementType := deElement;
  DER.ElemDef := Element;
  DER.Final := Final;
  DtdElementFound(DER);
end;


procedure TXmlParser.AnalyzeAttListDecl(Start: PWideChar; var Final: PWideChar);
          (* Parse <!ATTLIST declaration starting at "Start"
             Final must point to the terminating '>' character
             XmlSpec 3.3:
                 AttlistDecl    ::= '<!ATTLIST' S Name AttDef* S? '>'
                 AttDef         ::= S Name S AttType S DefaultDecl
                 AttType        ::= StringType | TokenizedType | EnumeratedType
                 StringType     ::= 'CDATA'
                 TokenizedType  ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
                 EnumeratedType ::= NotationType | Enumeration
                 NotationType   ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
                 Enumeration    ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
                 DefaultDecl    ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
                 AttValue       ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
            Examples:
                 <!ATTLIST address
                           A1 CDATA "Default"
                           A2 ID    #REQUIRED
                           A3 IDREF #IMPLIED
                           A4 IDREFS #IMPLIED
                           A5 ENTITY #FIXED "&at;&#252;"
                           A6 ENTITIES #REQUIRED
                           A7 NOTATION (WMF | DXF) "WMF"
                           A8 (A | B | C) #REQUIRED>                *)
type
  TPhase = (phElementName, phName, phType, phNotationContent, phDefault);
var
  Phase: TPhase;
  F: PWideChar;
  ElementName: WideString;
  ElemDef: TElemDef;
  AttrDef: TAttrDef;
  AttrDef2: TAttrDef;
  Strg: WideString;
  DER: TDtdElementRec;
begin
  Final := Start + 9; // The character after <!ATTLIST
  Phase := phElementName;
  DER.Start := Start;
  AttrDef := nil;
  ElemDef := nil;
  repeat
    if not (WideCharInSet(Final, CWhitespace)) then
      case Final^ of
        '%': begin
            PushPE(Final);
            CONTINUE;
          end;
        #0: if EntityStack.Count = 0 then
            BREAK
          else begin
            Final := EntityStack.Pop;
            CONTINUE;
          end;
        '>': BREAK;
      else case Phase of
          phElementName: begin
              ExtractName(Final, CWhitespace + CQuoteChar + ['#'], F);
              SetStringSF(ElementName, Final, F);
              Final := F;
              ElemDef := Elements.Node(ElementName);
              if ElemDef = nil then begin
                ElemDef := TElemDef.Create;
                ElemDef.Name := ElementName;
                ElemDef.Definition := 'ANY';
                ElemDef.ElemType := etAny;
                Elements.Add(ElemDef);
              end;
              Phase := phName;
            end;
          phName: begin
              AttrDef := TAttrDef.Create;
              ExtractName(Final, CWhitespace + CQuoteChar + ['#'], F);
              SetStringSF(AttrDef.Name, Final, F);
              Final := F;
              AttrDef2 := TAttrDef(ElemDef.Node(AttrDef.Name));
              if AttrDef2 <> nil then
                ElemDef.Delete(ElemDef.IndexOf(AttrDef2));
              ElemDef.Add(AttrDef);
              Phase := phType;
            end;
          phType: begin
              if Final^ = '(' then begin
                F := StrScan(Final + 1, ')');
                if F <> nil
                  then SetStringSF(AttrDef.TypeDef, Final + 1, F - 1)
                else AttrDef.TypeDef := WideString(Final + 1);
                AttrDef.TypeDef := DelWhitespace(AttrDef.TypeDef);
                AttrDef.AttrType := atEnumeration;
                ReplaceParameterEntities(AttrDef.TypeDef);
                ReplaceCharacterEntities(AttrDef.TypeDef);
                Phase := phDefault;
              end
              else if StrLCompW(Final, 'NOTATION', 8) = 0 then begin
                INC(Final, 8);
                AttrDef.AttrType := atNotation;
                Phase := phNotationContent;
              end
              else begin
                ExtractName(Final, CWhitespace + CQuoteChar + ['#'], F);
                SetStringSF(AttrDef.TypeDef, Final, F);
                if AttrDef.TypeDef = 'CDATA' then AttrDef.AttrType := atCData
                else if AttrDef.TypeDef = 'ID' then AttrDef.AttrType := atId
                else if AttrDef.TypeDef = 'IDREF' then AttrDef.AttrType := atIdRef
                else if AttrDef.TypeDef = 'IDREFS' then AttrDef.AttrType := atIdRefs
                else if AttrDef.TypeDef = 'ENTITY' then AttrDef.AttrType := atEntity
                else if AttrDef.TypeDef = 'ENTITIES' then AttrDef.AttrType := atEntities
                else if AttrDef.TypeDef = 'NMTOKEN' then AttrDef.AttrType := atNmToken
                else if AttrDef.TypeDef = 'NMTOKENS' then AttrDef.AttrType := atNmTokens;
                Phase := phDefault;
              end
            end;
          phNotationContent: begin
              F := StrScan(Final, ')');
              if F <> nil then
                SetStringSF(AttrDef.Notations, Final + 1, F - 1)
              else begin
                AttrDef.Notations := WideString(Final + 1);
                Final := StrEndW(Final);
              end;
              ReplaceParameterEntities(AttrDef.Notations);
              AttrDef.Notations := DelWhitespace(AttrDef.Notations);
              Phase := phDefault;
            end;
          phDefault: begin
              if Final^ = '#' then begin
                ExtractName(Final, CWhitespace + CQuoteChar, F);
                SetStringSF(Strg, Final, F);
                Final := F;
                ReplaceParameterEntities(Strg);
                if Strg = '#REQUIRED' then begin AttrDef.DefaultType := adRequired; Phase := phName; end
                else if Strg = '#IMPLIED' then begin AttrDef.DefaultType := adImplied; Phase := phName; end
                else if Strg = '#FIXED' then AttrDef.DefaultType := adFixed;
              end
              else if (WideCharInSet(Final, CQuoteChar)) then begin
                ExtractQuote(Final, AttrDef.Value, Final);
                ReplaceParameterEntities(AttrDef.Value);
                ReplaceCharacterEntities(AttrDef.Value);
                Phase := phName;
              end;
              if Phase = phName then begin
                AttrDef := nil;
              end;
            end;

        end;
      end;
    INC(Final);
  until FALSE;

  Final := StrScan(Final, '>');

  DER.ElementType := deAttList;
  DER.ElemDef := ElemDef;
  DER.Final := Final;
  DtdElementFound(DER);
end;


procedure TXmlParser.AnalyzeEntityDecl(Start: PWideChar; var Final: PWideChar);
          (* Parse <!ENTITY declaration starting at "Start"
             Final must point to the terminating '>' character
             XmlSpec 4.2:
                 EntityDecl  ::= '<!ENTITY' S Name S EntityDef S? '>'   |
                                 '<!ENTITY' S '%' S Name S PEDef S? '>'
                 EntityDef   ::= EntityValue | (ExternalID NDataDecl?)
                 PEDef       ::= EntityValue | ExternalID
                 NDataDecl   ::= S 'NDATA' S Name
                 EntityValue ::= '"' ([^%&"] | PEReference | EntityRef | CharRef)* '"'   |
                                 "'" ([^%&'] | PEReference | EntityRef | CharRef)* "'"
                 PEReference ::= '%' Name ';'

             Examples
                 <!ENTITY test1 "Stefan Heymann">                   <!-- Internal, general, parsed              -->
                 <!ENTITY test2 SYSTEM "ent2.xml">                  <!-- External, general, parsed              -->
                 <!ENTITY test2 SYSTEM "ent3.gif" NDATA gif>        <!-- External, general, unparsed            -->
                 <!ENTITY % test3 "<!ELEMENT q ANY>">               <!-- Internal, parameter                    -->
                 <!ENTITY % test6 SYSTEM "ent6.xml">                <!-- External, parameter                    -->
                 <!ENTITY test4 "&test1; ist lieb">                 <!-- IGP, Replacement text <> literal value -->
                 <!ENTITY test5 "<p>Dies ist ein Test-Absatz</p>">  <!-- IGP, See XmlSpec 2.4                   -->
          *)
type
  TPhase = (phName, phContent, phNData, phNotationName, phFinalGT);
var
  Phase: TPhase;
  IsParamEntity: BOOLEAN;
  F: PWideChar;
  ExternalID: TExternalID;
  EntityDef: TEntityDef;
  EntityDef2: TEntityDef;
  DER: TDtdElementRec;
begin
  Final := Start + 8; // First char after <!ENTITY
  DER.Start := Start;
  Phase := phName;
  IsParamEntity := FALSE;
  EntityDef := TEntityDef.Create;
  repeat
    if not (WideCharInSet(Final, CWhitespace)) then
      case Final^ of
        '%': IsParamEntity := TRUE;
        '>': BREAK;
      else case Phase of
          phName: if (UnicodeIsXMLNameStart(Ord(Final^))) then begin
              ExtractName(Final, CWhitespace + CQuoteChar, F);
              SetStringSF(EntityDef.Name, Final, F);
              Final := F;
              Phase := phContent;
            end;
          phContent: if (WideCharInSet(Final, CQuoteChar)) then begin
              ExtractQuote(Final, EntityDef.Value, Final);
              Phase := phFinalGT;
            end
            else if (StrLCompW(Final, 'SYSTEM', 6) = 0) or
              (StrLCompW(Final, 'PUBLIC', 6) = 0) then begin
              ExternalID := TExternalID.Create(Final);
              EntityDef.SystemId := ExternalID.SystemId;
              EntityDef.PublicId := ExternalID.PublicId;
              Final := ExternalID.Final;
              Phase := phNData;
              ExternalID.Free;
            end;
          phNData: if StrLCompW(Final, 'NDATA', 5) = 0 then begin
              INC(Final, 4);
              Phase := phNotationName;
            end;
          phNotationName: if (UnicodeIsXMLNameStart(Ord(Final^))) then begin
              ExtractName(Final, CWhitespace + ['>'], F);
              SetStringSF(EntityDef.NotationName, Final, F);
              Final := F;
              Phase := phFinalGT;
            end;
          phFinalGT: ; // -!- There is an error in the document if this branch is called
        end;
      end;
    INC(Final);
  until FALSE;
  if IsParamEntity then begin
    EntityDef2 := TEntityDef(ParEntities.Node(EntityDef.Name));
    if EntityDef2 <> nil then
      ParEntities.Delete(ParEntities.IndexOf(EntityDef2));
    ParEntities.Add(EntityDef);
    ReplaceCharacterEntities(EntityDef.Value);
  end
  else begin
    EntityDef2 := TEntityDef(Entities.Node(EntityDef.Name));
    if EntityDef2 <> nil then
      Entities.Delete(Entities.IndexOf(EntityDef2));
    Entities.Add(EntityDef);
    ReplaceParameterEntities(EntityDef.Value); //  Create replacement texts (see XmlSpec 4.5)
    ReplaceCharacterEntities(EntityDef.Value);
  end;
  Final := StrScanE(Final, '>');

  DER.ElementType := deEntity;
  DER.EntityDef := EntityDef;
  DER.Final := Final;
  DtdElementFound(DER);
end;


procedure TXmlParser.AnalyzeNotationDecl(Start: PWideChar; var Final: PWideChar);
          // Parse <!NOTATION declaration starting at "Start"
          // Final must point to the terminating '>' character
          // XmlSpec 4.7: NotationDecl ::=  '<!NOTATION' S Name S (ExternalID |  PublicID) S? '>'
type
  TPhase = (phName, phExtId, phEnd);
var
  ExternalID: TExternalID;
  Phase: TPhase;
  F: PWideChar;
  NotationDef: TNotationDef;
  DER: TDtdElementRec;
begin
  Final := Start + 10; // Character after <!NOTATION
  DER.Start := Start;
  Phase := phName;
  NotationDef := TNotationDef.Create;
  repeat
    if not (WideCharInSet(Final, CWhitespace)) then
      case Final^ of
        '>',
          #0: BREAK;
      else case Phase of
          phName: begin
              ExtractName(Final, CWhitespace + ['>'], F);
              SetStringSF(NotationDef.Name, Final, F);
              Final := F;
              Phase := phExtId;
            end;
          phExtId: begin
              ExternalID := TExternalID.Create(Final);
              NotationDef.Value := ExternalID.SystemId;
              NotationDef.PublicId := ExternalID.PublicId;
              Final := ExternalId.Final;
              ExternalId.Free;
              Phase := phEnd;
            end;
          phEnd: ; // -!- There is an error in the document if this branch is called
        end;
      end;
    INC(Final);
  until FALSE;
  Notations.Add(NotationDef);
  Final := StrScanE(Final, '>');

  DER.ElementType := deNotation;
  DER.NotationDef := NotationDef;
  DER.Final := Final;
  DtdElementFound(DER);
end;


procedure TXmlParser.PushPE(var Start: PWideChar);
          (* If there is a parameter entity reference found in the data stream,
             the current position will be pushed to the entity stack.
             Start:  IN  Pointer to the '%' character starting the PE reference
                     OUT Pointer to first character of PE replacement text *)
var
  P: PWideChar;
  EntityDef: TEntityDef;
begin
  P := StrScan(Start, ';');
  if P <> nil then begin
    EntityDef := TEntityDef(ParEntities.Node(StrSFPas(Start + 1, P - 1)));
    if EntityDef <> nil then begin
      EntityStack.Push(P + 1);
      Start := PWideChar(EntityDef.Value);
    end
    else
      Start := P + 1;
  end;
end;


procedure TXmlParser.ReplaceCharacterEntities(var Str: WideString);
          // Replaces all Character Entity References in the String
var
  Start: INTEGER;
  PAmp: PWideChar;
  PSemi: PWideChar;
  PosAmp: INTEGER;
  Len: INTEGER; // Length of Entity Reference
begin
  if Str = '' then EXIT;
  Start := 1;
  repeat
    PAmp := StrPos(PWideChar(Str) + Start - 1, '&#');
    if PAmp = nil then BREAK;
    PSemi := StrScan(PAmp + 2, ';');
    if PSemi = nil then BREAK;
    PosAmp := PAmp - PWideChar(Str) + 1;
    Len := PSemi - PAmp + 1;
    if (Str[PosAmp + 2] = 'x')
    //todo: Need a better way to generate Unicode char entities
    //IF CompareText (Str [PosAmp+2], 'x') = 0          // !!! Can't use "CHR" for Unicode characters > 255
    then Str[PosAmp] := WideChar(CHR(StrToIntDef('$' + Copy(Str, PosAmp + 3, Len - 4), 0)))
    else Str[PosAmp] := WideChar(CHR(StrToIntDef(Copy(Str, PosAmp + 2, Len - 3), 32)));

    //todo: Build a DeleteWideChars() procedure to use instead of Delete();
    Delete(Str, PosAmp + 1, Len - 1);
    Start := PosAmp + 1;
  until FALSE;
end;


procedure TXmlParser.ReplaceParameterEntities(var Str: WideString);
          // Recursively replaces all Parameter Entity References in the String
  procedure ReplaceEntities(var Str: WideString);
  var
    Start: INTEGER;
    PAmp: PWideChar;
    PSemi: PWideChar;
    PosAmp: INTEGER;
    Len: INTEGER;
    Entity: TEntityDef;
    Repl: WideString; // Replacement
  begin
    if Str = '' then EXIT;
    Start := 1;
    repeat
      PAmp := StrPos(PWideChar(Str) + Start - 1, '%');
      if PAmp = nil then BREAK;
      PSemi := StrScan(PAmp + 2, ';');
      if PSemi = nil then BREAK;
      PosAmp := PAmp - PWideChar(Str) + 1;
      Len := PSemi - PAmp + 1;
      Entity := TEntityDef(ParEntities.Node(Copy(Str, PosAmp + 1, Len - 2)));
      if Entity <> nil then begin
        Repl := Entity.Value;
        ReplaceEntities(Repl); // Recursion
      end
      else
        Repl := Copy(Str, PosAmp, Len);
      Delete(Str, PosAmp, Len);
      Insert(Repl, Str, PosAmp);
      Start := PosAmp + Length(Repl);
    until FALSE;
  end;
begin
  ReplaceEntities(Str);
end;


procedure TXmlParser.ReplaceGeneralEntities(var Str: WideString);
          // Recursively replaces General Entity References in the String
  procedure ReplaceEntities(var Str: WideString);
  var
    Start: INTEGER;
    PAmp: PWideChar;
    PSemi: PWideChar;
    PosAmp: INTEGER;
    Len: INTEGER;
    EntityDef: TEntityDef;
    EntName: WideString;
    Repl: WideString; // Replacement
    ExternalEntity: TXmlParser;
  begin
    if Str = '' then EXIT;
    Start := 1;
    repeat
      PAmp := StrPos(PWideChar(Str) + Start - 1, '&');
      if PAmp = nil then BREAK;
      PSemi := StrScan(PAmp + 2, ';');
      if PSemi = nil then BREAK;
      PosAmp := PAmp - PWideChar(Str) + 1;
      Len := PSemi - PAmp + 1;
      EntName := Copy(Str, PosAmp + 1, Len - 2);
      if EntName = 'lt' then Repl := '<'
      else if EntName = 'gt' then Repl := '>'
      else if EntName = 'amp' then Repl := '&'
      else if EntName = 'apos' then Repl := ''''
      else if EntName = 'quot' then Repl := '"'
      else begin
        EntityDef := TEntityDef(Entities.Node(EntName));
        if EntityDef <> nil then begin
          if EntityDef.Value <> '' then // Internal Entity
            Repl := EntityDef.Value
          else begin // External Entity
            ExternalEntity := LoadExternalEntity(EntityDef.SystemId, EntityDef.PublicId, EntityDef.NotationName);
            Repl := WideString(ExternalEntity.DocBuffer);
            // Repl := StrPas (ExternalEntity.DocBuffer);  // !!! What if it contains a Text Declaration?
            ExternalEntity.Free;
          end;
          ReplaceEntities(Repl); // Recursion
        end
        else
          Repl := Copy(Str, PosAmp, Len);
      end;
      Delete(Str, PosAmp, Len);
      Insert(Repl, Str, PosAmp);
      Start := PosAmp + Length(Repl);
    until FALSE;
  end;
begin
  ReplaceEntities(Str);
end;


function TXmlParser.LoadExternalEntity(SystemId, PublicId, Notation: WideString): TXmlParser;
          // This will be called whenever there is a Parsed External Entity or
          // the DTD External Subset to be parsed.
          // It has to create a TXmlParser instance and load the desired Entity.
          // This instance of LoadExternalEntity assumes that "SystemId" is a valid
          // file name (relative to the Document source) and loads this file using
          // the LoadFromFile method.
var
  Filename: string;
begin
  // --- Convert System ID to complete filename
  Filename := StringReplace(SystemId, '/', '\', [rfReplaceAll]);
  if Copy(FSource, 1, 1) <> '<' then
    if (Copy(Filename, 1, 2) = '\\') or (Copy(Filename, 2, 1) = ':') then
      // Already has an absolute Path
    else begin
      Filename := ExtractFilePath(FSource) + Filename;
    end;

  // --- Load the File
  Result := TXmlParser.Create;
  Result.LoadFromFile(Filename);
end;


function TXmlParser.TranslateEncoding(const Source: WideString): WideString;
          // The member variable "CurEncoding" always holds the name of the current
          // encoding, e.g. 'UTF-8' or 'ISO-8859-1'.
          // This virtual method "TranslateEncoding" is responsible for translating
          // the content passed in the "Source" parameter to the Encoding which
          // is expected by the application.
          // This instance of "TranlateEncoding" assumes that the Application expects
          // Windows ANSI (Win1252) strings. It is able to transform UTF-8 or ISO-8859-1
          // encodings.
          // If you want your application to understand or create other encodings, you
          // override this function.
begin

    // pgm - we are unicode now, so don't translate anything.. everything is UCS-2
  Result := Source;

    (*
    IF CurEncoding = 'UTF-8'
    THEN Result := Utf8ToAnsi (Source)
    ELSE Result := Source;
    *)
end;


procedure TXmlParser.DtdElementFound(DtdElementRec: TDtdElementRec);
          // This method is called for every element which is found in the DTD
          // declaration. The variant record TDtdElementRec is passed which
          // holds informations about the element.
          // You can override this function to handle DTD declarations.
          // Note that when you parse the same Document instance a second time,
          // the DTD will not get parsed again.
begin
end;


function TXmlParser.GetDocBuffer: PWideChar;
         // Returns FBuffer or a pointer to a NUL char if Buffer is empty
begin
  if FBuffer = nil
    then Result := #0
  else Result := FBuffer;
end;


(*$IFNDEF D5_OR_NEWER
===============================================================================================
TObjectList
===============================================================================================
*)

destructor TObjectList.Destroy;
begin
  Clear;
  SetCapacity(0);
  inherited Destroy;
end;


procedure TObjectList.Delete(Index: INTEGER);
begin
  if (Index < 0) or (Index >= Count) then EXIT;
  TObject(Items[Index]).Free;
  inherited Delete(Index);
end;


procedure TObjectList.Clear;
begin
  while Count > 0 do
    Delete(Count - 1);
end;

(*$ENDIF *)

(*
===============================================================================================
TNvpNode
--------
Node base class for the TNvpList
===============================================================================================
*)

constructor TNvpNode.Create(TheName, TheValue: WideString);
begin
  inherited Create;
  Name := TheName;
  Value := TheValue;
end;


(*
===============================================================================================
TNvpList
--------
A generic List of Name-Value Pairs, based on the TObjectList introduced in Delphi 5
===============================================================================================
*)

procedure TNvpList.Add(Node: TNvpNode);
var
  I: INTEGER;
begin
  for I := Count - 1 downto 0 do
    if Node.Name > TNvpNode(Items[I]).Name then begin
      Insert(I + 1, Node);
      EXIT;
    end;
  Insert(0, Node);
end;



function TNvpList.Node(Name: WideString): TNvpNode;
          // Binary search for Node
var
  L, H: INTEGER; // Low, High Limit
  T, C: INTEGER; // Test Index, Comparison result
  Last: INTEGER; // Last Test Index
begin
  if Count = 0 then begin
    Result := nil;
    EXIT;
  end;

  L := 0;
  H := Count;
  Last := -1;
  repeat
    T := (L + H) div 2;
    if T = Last then BREAK;
    Result := TNvpNode(Items[T]);
    // C := CompareStr (Result.Name, Name);
    C := StrCompW(PWideChar(Result.Name), PWideChar(Name));
    if C = 0 then EXIT
    else if C < 0 then L := T
    else H := T;
    Last := T;
  until FALSE;
  Result := nil;
end;


function TNvpList.Node(Index: INTEGER): TNvpNode;
begin
  if (Index < 0) or (Index >= Count)
    then Result := nil
  else Result := TNvpNode(Items[Index]);
end;


function TNvpList.Value(Name: WideString): WideString;
var
  Nvp: TNvpNode;
begin
  Nvp := TNvpNode(Node(Name));
  if Nvp <> nil
    then Result := Nvp.Value
  else Result := '';
end;


function TNvpList.Value(Index: INTEGER): WideString;
begin
  if (Index < 0) or (Index >= Count)
    then Result := ''
  else Result := TNvpNode(Items[Index]).Value;
end;


function TNvpList.Name(Index: INTEGER): WideString;
begin
  if (Index < 0) or (Index >= Count)
    then Result := ''
  else Result := TNvpNode(Items[Index]).Name;
end;


(*
===============================================================================================
TAttrList
List of Attributes. The "Analyze" method extracts the Attributes from the given Buffer.
Is used for extraction of Attributes in Start-Tags, Empty-Element Tags and the "pseudo"
attributes in XML Prologs, Text Declarations and PIs.
===============================================================================================
*)

procedure TAttrList.Analyze(Start: PWideChar; var Final: PWideChar);
          // Analyze the Buffer for Attribute=Name pairs.
          // Terminates when there is a character which is not IN CNameStart
          // (e.g. '?>' or '>' or '/>')
type
  TPhase = (phName, phEq, phValue);
var
  Phase: TPhase;
  F: PWideChar;
  Name: WideString;
  Value: WideString;
  Attr: TAttr;
begin
  Clear;
  Phase := phName;
  Final := Start;
  repeat
    if (Final^ = #0) or (Final^ = '>') then BREAK;
    if not (WideCharInSet(Final, CWhitespace)) then
      case Phase of
        phName: begin
            if not (UnicodeIsXMLNameStart(Ord(Final^))) then exit;
            ExtractName(Final, CWhitespace + ['=', '+', '/'], F);
            SetStringSF(Name, Final, F);
            Final := F;
            Phase := phEq;
          end;
        phEq: begin
            if Final^ = '=' then
              Phase := phValue
          end;
        phValue: begin
            if (WideCharInSet(Final, CQuoteChar)) then begin
              ExtractQuote(Final, Value, F);
              Attr := TAttr.Create;
              Attr.Name := Name;
              Attr.Value := Value;
              Attr.ValueType := vtNormal;
              Add(Attr);
              Final := F;
              Phase := phName;
            end;
          end;
      end;
    INC(Final);
  until FALSE;
end;


(*
===============================================================================================
TElemList
List of TElemDef nodes.
===============================================================================================
*)

function TElemList.Node(Name: WideString): TElemDef;
          // Binary search for the Node with the given Name
var
  L, H: INTEGER; // Low, High Limit
  T, C: INTEGER; // Test Index, Comparison result
  Last: INTEGER; // Last Test Index
begin
  if Count = 0 then begin
    Result := nil;
    EXIT;
  end;

  L := 0;
  H := Count;
  Last := -1;
  repeat
    T := (L + H) div 2;
    if T = Last then BREAK;
    Result := TElemDef(Items[T]);
    C := StrCompW(PWideChar(Result.Name), PWideChar(Name));
    if C = 0 then EXIT
    else if C < 0 then L := T
    else H := T;
    Last := T;
  until FALSE;
  Result := nil;
end;


procedure TElemList.Add(Node: TElemDef);
var
  I: INTEGER;
begin
  for I := Count - 1 downto 0 do
    if Node.Name > TElemDef(Items[I]).Name then begin
      Insert(I + 1, Node);
      EXIT;
    end;
  Insert(0, Node);
end;


(*
===============================================================================================
TScannerXmlParser
A TXmlParser descendant for the TCustomXmlScanner component
===============================================================================================
*)

type
  TScannerXmlParser = class(TXmlParser)
    Scanner: TCustomXmlScanner;
    constructor Create(TheScanner: TCustomXmlScanner);
    function LoadExternalEntity(SystemId, PublicId,
      Notation: WideString): TXmlParser; override;
    function TranslateEncoding(const Source: WideString): WideString; override;
    procedure DtdElementFound(DtdElementRec: TDtdElementRec); override;
  end;

constructor TScannerXmlParser.Create(TheScanner: TCustomXmlScanner);
begin
  inherited Create;
  Scanner := TheScanner;
end;


function TScannerXmlParser.LoadExternalEntity(SystemId, PublicId, Notation: WideString): TXmlParser;
begin
  if Assigned(Scanner.FOnLoadExternal)
    then Scanner.FOnLoadExternal(Scanner, SystemId, PublicId, Notation, Result)
  else Result := inherited LoadExternalEntity(SystemId, PublicId, Notation);
end;


function TScannerXmlParser.TranslateEncoding(const Source: WideString): WideString;
begin
  if Assigned(Scanner.FOnTranslateEncoding)
    then Result := Scanner.FOnTranslateEncoding(Scanner, CurEncoding, Source)
  else Result := inherited TranslateEncoding(Source);
end;


procedure TScannerXmlParser.DtdElementFound(DtdElementRec: TDtdElementRec);
begin
  with DtdElementRec do
    case ElementType of
      deElement: Scanner.WhenElement(ElemDef);
      deAttList: Scanner.WhenAttList(ElemDef);
      deEntity: Scanner.WhenEntity(EntityDef);
      deNotation: Scanner.WhenNotation(NotationDef);
      dePI: Scanner.WhenPI(WideString(Target), WideString(Content), AttrList);
      deComment: Scanner.WhenComment(StrSFPas(Start, Final));
      deError: Scanner.WhenDtdError(Pos);
    end;
end;


(*
===============================================================================================
TCustomXmlScanner
===============================================================================================
*)

constructor TCustomXmlScanner.Create(AOwner: TComponent);
begin
  inherited;
  FXmlParser := TScannerXmlParser.Create(Self);
end;


destructor TCustomXmlScanner.Destroy;
begin
  FXmlParser.Free;
  inherited;
end;


procedure TCustomXmlScanner.LoadFromFile(Filename: TFilename);
          // Load XML Document from file
begin
  FXmlParser.LoadFromFile(Filename);
end;


procedure TCustomXmlScanner.LoadFromBuffer(Buffer: PWideChar);
          // Load XML Document from buffer
begin
  FXmlParser.LoadFromBuffer(Buffer);
end;


procedure TCustomXmlScanner.SetBuffer(Buffer: PWideChar);
          // Refer to Buffer
begin
  FXmlParser.SetBuffer(Buffer);
end;


function TCustomXmlScanner.GetFilename: TFilename;
begin
  Result := FXmlParser.Source;
end;


function TCustomXmlScanner.GetNormalize: BOOLEAN;
begin
  Result := FXmlParser.Normalize;
end;


procedure TCustomXmlScanner.SetNormalize(Value: BOOLEAN);
begin
  FXmlParser.Normalize := Value;
end;


procedure TCustomXmlScanner.WhenXmlProlog(XmlVersion, Encoding: WideString; Standalone: BOOLEAN);
          // Is called when the parser has parsed the <? xml ?> declaration of the prolog
begin
  if Assigned(FOnXmlProlog) then FOnXmlProlog(Self, XmlVersion, Encoding, Standalone);
end;


procedure TCustomXmlScanner.WhenComment(Comment: WideString);
          // Is called when the parser has parsed a <!-- comment -->
begin
  if Assigned(FOnComment) then FOnComment(Self, Comment);
end;


procedure TCustomXmlScanner.WhenPI(Target, Content: WideString; Attributes: TAttrList);
          // Is called when the parser has parsed a <?processing instruction ?>
begin
  if Assigned(FOnPI) then FOnPI(Self, Target, Content, Attributes);
end;


procedure TCustomXmlScanner.WhenDtdRead(RootElementName: WideString);
          // Is called when the parser has completely parsed the DTD
begin
  if Assigned(FOnDtdRead) then FOnDtdRead(Self, RootElementName);
end;


procedure TCustomXmlScanner.WhenStartTag(TagName: WideString; Attributes: TAttrList);
          // Is called when the parser has parsed a start tag like <p>
begin
  if Assigned(FOnStartTag) then FOnStartTag(Self, TagName, Attributes);
end;


procedure TCustomXmlScanner.WhenEmptyTag(TagName: WideString; Attributes: TAttrList);
          // Is called when the parser has parsed an Empty Element Tag like <br/>
begin
  if Assigned(FOnEmptyTag) then FOnEmptyTag(Self, TagName, Attributes);
end;


procedure TCustomXmlScanner.WhenEndTag(TagName: WideString);
          // Is called when the parser has parsed an End Tag like </p>
begin
  if Assigned(FOnEndTag) then FOnEndTag(Self, TagName);
end;


procedure TCustomXmlScanner.WhenContent(Content: WideString);
          // Is called when the parser has parsed an element's text content
begin
  if Assigned(FOnContent) then FOnContent(Self, Content);
end;


procedure TCustomXmlScanner.WhenCData(Content: WideString);
          // Is called when the parser has parsed a CDATA section
begin
  if Assigned(FOnCData) then FOnCData(Self, Content);
end;


procedure TCustomXmlScanner.WhenElement(ElemDef: TElemDef);
          // Is called when the parser has parsed an <!ELEMENT> definition
          // inside the DTD
begin
  if Assigned(FOnElement) then FOnElement(Self, ElemDef);
end;


procedure TCustomXmlScanner.WhenAttList(ElemDef: TElemDef);
          // Is called when the parser has parsed an <!ATTLIST> definition
          // inside the DTD
begin
  if Assigned(FOnAttList) then FOnAttList(Self, ElemDef);
end;


procedure TCustomXmlScanner.WhenEntity(EntityDef: TEntityDef);
          // Is called when the parser has parsed an <!ENTITY> definition
          // inside the DTD
begin
  if Assigned(FOnEntity) then FOnEntity(Self, EntityDef);
end;


procedure TCustomXmlScanner.WhenNotation(NotationDef: TNotationDef);
          // Is called when the parser has parsed a <!NOTATION> definition
          // inside the DTD
begin
  if Assigned(FOnNotation) then FOnNotation(Self, NotationDef);
end;


procedure TCustomXmlScanner.WhenDtdError(ErrorPos: PWideChar);
          // Is called when the parser has found an Error in the DTD
begin
  if Assigned(FOnDtdError) then FOnDtdError(Self, ErrorPos);
end;


procedure TCustomXmlScanner.Execute;
          // Perform scanning
          // Scanning is done synchronously, i.e. you can expect events to be triggered
          // in the order of the XML data stream. Execute will finish when the whole XML
          // document has been scanned or when the StopParser property has been set to TRUE.
begin
  FStopParser := FALSE;
  FXmlParser.StartScan;
  while FXmlParser.Scan and (not FStopParser) do
    case FXmlParser.CurPartType of
      ptNone: ;
      ptXmlProlog: WhenXmlProlog(FXmlParser.XmlVersion, FXmlParser.Encoding, FXmlParser.Standalone);
      ptComment: WhenComment(StrSFPas(FXmlParser.CurStart, FXmlParser.CurFinal));
      ptPI: WhenPI(FXmlParser.CurName, FXmlParser.CurContent, FXmlParser.CurAttr);
      ptDtdc: WhenDtdRead(FXmlParser.RootName);
      ptStartTag: WhenStartTag(FXmlParser.CurName, FXmlParser.CurAttr);
      ptEmptyTag: WhenEmptyTag(FXmlParser.CurName, FXmlParser.CurAttr);
      ptEndTag: WhenEndTag(FXmlParser.CurName);
      ptContent: WhenContent(FXmlParser.CurContent);
      ptCData: WhenCData(FXmlParser.CurContent);
    end;
end;


end.
