⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adxparsr.pas

📁 测试用例
💻 PAS
📖 第 1 页 / 共 5 页
字号:
(***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is TurboPower Async Professional
 *
 * The Initial Developer of the Original Code is
 * TurboPower Software
 *
 * Portions created by the Initial Developer are Copyright (C) 1991-2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** *)

{*********************************************************}
{*                   ADXPARSR.PAS 4.06                   *}
{*********************************************************}
{*                    XML parser code                    *}
{*********************************************************}

{Global defines potentially affecting this unit}
{$I AWDEFINE.INC}

unit AdXParsr;

interface

uses
{$IFDEF WIN32}
  Windows,
  Graphics,
  Controls,
{$ENDIF}
{$IFDEF LINUX}
  Libc,
  Types,
{$ENDIF}
  SysUtils,
  Classes,
  OOMisc,
  AdXBase,
  AdXChrFlt;

type
  StringIds = array[0..1] of DOMString;

{== Event types ======================================================}
  TApdDocTypeDeclEvent = procedure(oOwner : TObject;
                                  sDecl,
                                  sId0,
                                  sId1   : DOMString) of object;
  TApdValueEvent = procedure(oOwner : TObject;
                            sValue : DOMString) of object;
  TApdAttributeEvent = procedure(oOwner     : TObject;
                                sName,
                                sValue     : DOMString;
                                bSpecified : Boolean) of object;
  TApdProcessInstrEvent = procedure(oOwner : TObject;
                                   sName,
                                   sValue : DOMString) of object;
  TApdResolveEvent = procedure(oOwner     : TObject;
                        const sName,
                              sPublicId,
                              sSystemId  : DOMString;
                          var sValue     : DOMString) of object;
  TApdNonXMLEntityEvent = procedure(oOwner        : TObject;
                                   sEntityName,
                                   sPublicId,
                                   sSystemId,
                                   sNotationName : DOMString) of object;
  TApdPreserveSpaceEvent = procedure(oOwner       : TObject;
                                    sElementName : DOMString;
                                var bPreserve    : Boolean) of object;
{== Class types ======================================================}
  TApdParser = class(TApdBaseComponent)
  protected
    { Private declarations }
    FAttrEnum : TStringList;
    FAttributeType : TStringList;
    FBufferSize : Integer;
    FCDATA : Boolean;
    FContext : Integer;
    FCurrentElement : DOMString;
    FCurrentElementContent : Integer;
    FCurrentPath : string;
    FDataBuffer : DOMString;
    FDocStack : TList;
    FElementInfo : TStringList;
    FEntityInfo : TStringList;
    FErrors : TStringList;
    FFilter : TApdInCharFilter;
    FInCharSet : TApdCharEncoding;
    FNormalizeData : Boolean;
    FNotationInfo : TStringList;
    FOnAttribute : TApdAttributeEvent;
    FOnCDATASection : TApdValueEvent;
    FOnCharData : TApdValueEvent;
    FOnComment : TApdValueEvent;
    FOnDocTypeDecl : TApdDocTypeDeclEvent;
    FOnEndDocument : TNotifyEvent;
    FOnEndElement : TApdValueEvent;
    FOnIgnorableWhitespace : TApdValueEvent;
    FOnNonXMLEntity : TApdNonXMLEntityEvent;
    FOnPreserveSpace : TApdPreserveSpaceEvent;
    FOnProcessingInstruction : TApdProcessInstrEvent;
    FOnResolveEntity : TApdResolveEvent;
    FOnStartDocument : TNotifyEvent;
    FOnStartElement : TApdValueEvent;
    FOnBeginElement : TApdValueEvent;
    FPreserve : Boolean;
    FRaiseErrors : Boolean;
    FTagAttributes : TStringList;
    FTempFiles : TStringList;
    FUrl : DOMString;
    FIsStandAlone : Boolean;
    FHasExternals : Boolean;
    FXMLDecParsed : Boolean;

    procedure Cleanup;
    procedure CheckParamEntityNesting(const aString : DOMString);
    procedure DataBufferAppend(const sVal : DOMString);
    procedure DataBufferFlush;
    procedure DataBufferNormalize;
    function DataBufferToString : DOMString;
    function DeclaredAttributes(const sName : DOMString;
                                      aIdx  : Integer) : TStringList;
    function GetAttributeDefaultValueType(const sElemName,
                                                sAttrName : DOMString)
                                                          : Integer;
    function GetAttributeExpandedValue(const sElemName,
                                             sAttrName : DOMString;
                                             aIdx      : Integer)
                                                       : DOMString;
    function GetElementContentType(const sName : DOMString;
                                         aIdx  : Integer) : Integer;
    function GetElementIndexOf(const sElemName : DOMString) : Integer;
    function GetEntityIndexOf(const sEntityName : DOMString;
                                    aPEAllowed  : Boolean) : Integer;
    function GetEntityNotationName(const sEntityName : DOMString)
                                                     : DOMString;
    function GetEntityPublicId(const sEntityName : DOMString)
                                                 : DOMString;
    function GetEntitySystemId(const sEntityName : DOMString)
                                                 : DOMString;
    function GetEntityType(const sEntityName : DOMString;
                                 aPEAllowed   : Boolean) : Integer;
    function GetEntityValue(const sEntityName : DOMString;
                                  aPEAllowed  : Boolean) : DOMString;
    function GetErrorCount : Integer;
    function GetExternalTextEntityValue(const sName,
                                              sPublicId : DOMString;
                                              sSystemId : DOMString)
                                                        : DOMString;
    function GetInCharSet : TApdCharEncoding;
    procedure Initialize;
    function IsEndDocument : Boolean;
    function IsWhitespace(const cVal : DOMChar) : Boolean;
    function LoadDataSource(sSrcName   : string;
                            oErrors    : TStringList) : Boolean;
    function ParseAttribute(const sName : DOMString) : DOMString;
    function ParseEntityRef(bPEAllowed : Boolean) : DOMString;
    procedure ParseCDSect;
    function  ParseCharRef : DOMChar;
    procedure ParseComment;
    procedure ParseContent;
    procedure ParseDocTypeDecl;
    procedure ParseDocument;
    procedure ParseEndTag;
    procedure ParseEq;
    procedure ParseElement;
    procedure ParseMisc;
    function ParseParameterEntityRef(aPEAllowed : Boolean;
                                     bSkip      : Boolean) : DOMString;
    procedure ParsePCData(aInEntityRef : Boolean);
    procedure ParsePI;
    function ParsePIEx : Boolean;
      { Returns true if an XML declaration was found }
    procedure ParsePrim;
    procedure ParseProlog;
    procedure ParseUntil(const S : array of Longint);
    procedure ParseWhitespace;
    procedure ParseXMLDeclaration;
    procedure PopDocument;
    procedure PushDocument;
    procedure PushString(const sVal : DOMString);
    function ReadChar(const UpdatePos : Boolean) : DOMChar;
    procedure ReadExternalIds(bInNotation : Boolean;
                          var sIds        : StringIds);
    function ReadLiteral(wFlags    : Integer;
                     var HasEntRef : Boolean) : DOMString;
    function ReadNameToken(aValFirst : Boolean) : DOMString;
    procedure Require(const S : array of Longint);
    procedure RequireWhitespace;
    procedure SetAttribute(const sElemName,
                                 sName      : DOMString;
                                 wType      : Integer;
                           const sEnum,
                                 sValue     : DOMString;
                                 wValueType : Integer);
    procedure SetElement(const sName         : DOMString;
                               wType         : Integer;
                         const sContentModel : DOMString);
    procedure SetEntity(const sEntityName   : DOMString;
                              wClass        : Integer;
                        const sPublicId,
                              sSystemId,
                              sValue,
                              sNotationName : DOMString;
                              aIsPE         : Boolean);
    procedure SetInternalEntity(const sName, sValue : DOMString;
                                      aIsPE         : Boolean);
    procedure SetNotation(const sNotationName, sPublicId, sSystemId
                                                          : DOMString);
    procedure SkipChar;
    procedure SkipWhitespace(aNextDoc : Boolean);
    function TryRead(const S : array of Longint) : Boolean;
    procedure ValidateAttribute(const aValue    : DOMString;
                                      HasEntRef : Boolean);
    procedure ValidateCData(const CDATA : DOMString);
    procedure ValidateElementName(const aName : DOMString);
    procedure ValidateEncName(const aValue : string);
    procedure ValidateEntityValue(const aValue   : DOMString;
                                        aQuoteCh : DOMChar);
    function ValidateNameChar(const First : Boolean;
                              const Char  : DOMChar) : Boolean;
    procedure ValidatePCData(const aString      : DOMString;
                                   aInEntityRef : Boolean);
    procedure ValidatePublicID(const aString : DOMString);
    procedure ValidateVersNum(const aString : string);

  protected
    { Protected declarations }
    property OnIgnorableWhitespace : TApdValueEvent
      read FOnIgnorableWhitespace
      write FOnIgnorableWhitespace;

  public
    { Public declarations }
    constructor Create(oOwner : TComponent); override;
    destructor Destroy; override;

    function GetErrorMsg(wIdx : Integer) : DOMString;
    function ParseDataSource(const sSource : string) : Boolean;
    function ParseMemory(var aBuffer; aSize : Longint) : Boolean;
    function ParseStream(oStream : TStream) : Boolean;

    property ErrorCount : Integer
      read GetErrorCount;

    property Errors : TStringList
      read FErrors;

    property InCharSet : TApdCharEncoding
      read GetInCharSet;

    property IsStandAlone : Boolean
      read FIsStandAlone;

    property HasExternals : Boolean
      read FHasExternals;

    { Published declarations }
    property BufferSize : Integer
      read FBufferSize
      write FBufferSize
      default 8192;

    property NormalizeData : Boolean
      read FNormalizeData
      write FNormalizeData
      default True;

    property RaiseErrors : Boolean
      read FRaiseErrors
      write FRaiseErrors
      default False;

    property OnAttribute : TApdAttributeEvent
      read FOnAttribute
      write FOnAttribute;

    property OnCDATASection : TApdValueEvent
      read FOnCDATASection
      write FOnCDATASection;

    property OnCharData : TApdValueEvent
      read FOnCharData
      write FOnCharData;

    property OnComment : TApdValueEvent
      read FOnComment
      write FOnComment;

    property OnDocTypeDecl : TApdDocTypeDeclEvent
      read FOnDocTypeDecl
      write FOnDocTypeDecl;


    property OnEndDocument : TNotifyEvent
      read FOnEndDocument
      write FOnEndDocument;

    property OnEndElement : TApdValueEvent
      read FOnEndElement
      write FOnEndElement;

    property OnNonXMLEntity : TApdNonXMLEntityEvent
      read FOnNonXMLEntity
      write FOnNonXMLEntity;

    property OnPreserveSpace : TApdPreserveSpaceEvent
      read FOnPreserveSpace
      write FOnPreserveSpace;

    property OnProcessingInstruction : TApdProcessInstrEvent
      read FOnProcessingInstruction
      write FOnProcessingInstruction;

    property OnResolveEntity : TApdResolveEvent
      read FOnResolveEntity
      write FOnResolveEntity;

    property OnStartDocument : TNotifyEvent
      read FOnStartDocument
      write FOnStartDocument;

    property OnStartElement : TApdValueEvent
      read FOnStartElement
      write FOnStartElement;

    property OnBeginElement : TApdValueEvent
      read FOnBeginElement
      write FOnBeginElement;
  end;

implementation

{.$R *.RES}

uses
  AdExcept;


{== TApdEntityInfo ====================================================}
type
  TApdEntityInfo = class(TObject)
  private
    FEntityClass  : Integer;
    FIsPE         : Boolean;
    FPublicId     : DOMString;
    FSystemId     : DOMString;
    FValue        : DOMString;
    FNotationName : DOMString;
  public
    property EntityClass : Integer
      read FEntityClass
      write FEntityClass;

    property IsParameterEntity : Boolean
      read FIsPE
      write FIsPE;

    property NotationName : DOMString
      read FNotationName
      write FNotationName;

    property PublicId : DOMString
      read FPublicId
      write FPublicId;

    property SystemId : DOMString
      read FSystemId
      write FSystemId;

    property Value : DOMString
      read FValue
      write FValue;
  end;
{== TApdNotationInfo ==================================================}
  TApdNotationInfo = class(TObject)
  private
    FPublicId : DOMString;
    FSystemId : DOMString;
  public
    property PublicId : DOMString
      read FPublicId
      write FPublicId;

    property SystemId : DOMString
      read FSystemId
      write FSystemId;
  end;
{== TApdAttributeInfo =================================================}
  TApdAttributeInfo = class(TObject)
  private
    FType      : Integer;
    FValue     : DOMString;
    FValueType : Integer;
    FEnum      : DOMString;
    FLookup    : DOMString;
  public
    property AttrType : Integer
      read FType
      write FType;

    property Enum : DOMString
      read FEnum
      write FEnum;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -