1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
{
This file is part of the Free Component Library
Object model for DTD.
Copyright (c) 2010 by Sergei Gorelkin, sergei_gorelkin@mail.ru
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit dtdmodel;
{$ifdef fpc}
{$MODE objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils, xmlutils;
type
TCPType = (ctName, ctChoice, ctSeq);
TCPQuant = (cqOnce, cqZeroOrOnce, cqZeroOrMore, cqOnceOrMore);
TContentParticle = class(TObject)
private
FParent: TContentParticle;
FChildren: TFPList;
FIndex: Integer;
FDef: TObject;
FCPType: TCPType;
FCPQuant: TCPQuant;
function GetChildCount: Integer;
function GetChild(Index: Integer): TContentParticle;
public
destructor Destroy; override;
function Add: TContentParticle;
function IsRequired: Boolean;
function FindFirst(aDef: TObject): TContentParticle;
function FindNext(aDef: TObject; ChildIdx: Integer): TContentParticle;
function MoreRequired(ChildIdx: Integer): Boolean;
property ChildCount: Integer read GetChildCount;
property Children[Index: Integer]: TContentParticle read GetChild;
property Def: TObject read FDef write FDef;
property CPType: TCPType read FCPType write FCPType;
property CPQuant: TCPQuant read FCPQuant write FCPQuant;
end;
TDTDObject = class(TObject)
private
FExternallyDeclared: Boolean;
public
property ExternallyDeclared: Boolean read FExternallyDeclared write FExternallyDeclared;
end;
TAttrDefault = (
adImplied,
adDefault,
adRequired,
adFixed
);
TAttributeDef = class(TDTDObject)
private
FData: PNodeData;
FDataType: TAttrDataType;
FDefault: TAttrDefault;
FTag: Cardinal;
FIsNamespaceDecl: Boolean;
FEnumeration: array of WideString;
public
constructor Create(aName: PHashItem; aColonPos: Integer);
destructor Destroy; override;
function AddEnumToken(Buf: PWideChar; Len: Integer): Boolean;
function HasEnumToken(const aValue: WideString): Boolean;
property Data: PNodeData read FData;
property Default: TAttrDefault read FDefault write FDefault;
property DataType: TAttrDataType read FDataType write FDataType;
property Tag: Cardinal read FTag write FTag;
property IsNamespaceDecl: Boolean read FIsNamespaceDecl;
end;
TElementContentType = (
ctUndeclared,
ctAny,
ctEmpty,
ctMixed,
ctChildren
);
TElementDecl = class(TDTDObject)
private
FAttrDefs: TFPList;
FNeedsDefaultPass: Boolean;
function GetAttrDefCount: Integer;
function AttrDefByIndex(index: Integer): TAttributeDef;
public
ContentType: TElementContentType;
IDAttr: TAttributeDef;
NotationAttr: TAttributeDef;
RootCP: TContentParticle;
destructor Destroy; override;
function GetAttrDef(aName: PHashItem): TAttributeDef;
procedure AddAttrDef(aDef: TAttributeDef);
property AttrDefCount: Integer read GetAttrDefCount;
property AttrDefs[index: Integer]: TAttributeDef read AttrDefByIndex;
property NeedsDefaultPass: Boolean read FNeedsDefaultPass;
end;
TEntityDecl = class(TDTDObject)
public
FName: WideString; // TODO: change to PHashItem
FInputEncoding: WideString;
FXMLEncoding: WideString;
FPublicID: WideString;
FSystemID: WideString;
FNotationName: WideString;
FURI: WideString;
FReplacementText: WideString;
FXMLVersion: TXMLVersion;
FPrefetched: Boolean;
FResolved: Boolean;
FOnStack: Boolean;
FBetweenDecls: Boolean;
FIsPE: Boolean;
FStartLocation: TLocation;
FCharCount: Cardinal;
end;
TNotationDecl = class(TDTDObject)
public
FName: WideString;
FPublicID: WideString;
FSystemID: WideString;
end;
TDTDModel = class
private
FRefCount: Integer;
FNameTable: THashTable;
FEntities: THashTable;
FNotations: THashTable;
function GetEntities: THashTable;
function GetNotations: THashTable;
public
FName: WideString;
FSystemID: WideString;
FPublicID: WideString;
FInternalSubset: WideString;
constructor Create(aNameTable: THashTable);
destructor Destroy; override;
function Reference: TDTDModel;
procedure Release;
property Entities: THashTable read GetEntities;
property Notations: THashTable read GetNotations;
end;
implementation
{ TDTDModel }
function TDTDModel.GetEntities: THashTable;
begin
if FEntities = nil then
FEntities := THashTable.Create(256, True);
Result := FEntities;
end;
function TDTDModel.GetNotations: THashTable;
begin
if FNotations = nil then
FNotations := THashTable.Create(256, True);
Result := FNotations;
end;
constructor TDTDModel.Create(aNameTable: THashTable);
begin
FNameTable := aNameTable;
FRefCount := 1;
end;
destructor TDTDModel.Destroy;
begin
FEntities.Free;
FNotations.Free;
inherited Destroy;
end;
function TDTDModel.Reference: TDTDModel;
begin
Inc(FRefCount);
Result := Self;
end;
procedure TDTDModel.Release;
begin
if Assigned(Self) then
begin
Dec(FRefCount);
if FRefCount = 0 then
self.Destroy;
end;
end;
{ TContentParticle }
function TContentParticle.Add: TContentParticle;
begin
if FChildren = nil then
FChildren := TFPList.Create;
Result := TContentParticle.Create;
Result.FParent := Self;
Result.FIndex := FChildren.Add(Result);
end;
destructor TContentParticle.Destroy;
var
I: Integer;
begin
if Assigned(FChildren) then
for I := FChildren.Count-1 downto 0 do
TObject(FChildren[I]).Free;
FChildren.Free;
inherited Destroy;
end;
function TContentParticle.GetChild(Index: Integer): TContentParticle;
begin
Result := TContentParticle(FChildren[Index]);
end;
function TContentParticle.GetChildCount: Integer;
begin
if Assigned(FChildren) then
Result := FChildren.Count
else
Result := 0;
end;
function TContentParticle.IsRequired: Boolean;
var
I: Integer;
begin
Result := (CPQuant = cqOnce) or (CPQuant = cqOnceOrMore);
// do not return True if all children are optional
if (CPType <> ctName) and Result then
begin
for I := 0 to ChildCount-1 do
begin
Result := Children[I].IsRequired;
if Result then Exit;
end;
end;
end;
function TContentParticle.MoreRequired(ChildIdx: Integer): Boolean;
var
I: Integer;
begin
Result := False;
if CPType = ctSeq then
begin
for I := ChildIdx + 1 to ChildCount-1 do
begin
Result := Children[I].IsRequired;
if Result then Exit;
end;
end;
if Assigned(FParent) then
Result := FParent.MoreRequired(FIndex);
end;
function TContentParticle.FindFirst(aDef: TObject): TContentParticle;
var
I: Integer;
begin
Result := nil;
case CPType of
ctSeq:
for I := 0 to ChildCount-1 do with Children[I] do
begin
Result := FindFirst(aDef);
if Assigned(Result) or IsRequired then
Exit;
end;
ctChoice:
for I := 0 to ChildCount-1 do with Children[I] do
begin
Result := FindFirst(aDef);
if Assigned(Result) then
Exit;
end;
else // ctName
if aDef = Self.Def then
Result := Self
end;
end;
function TContentParticle.FindNext(aDef: TObject;
ChildIdx: Integer): TContentParticle;
var
I: Integer;
begin
Result := nil;
if CPType = ctSeq then // search sequence to its end
begin
for I := ChildIdx + 1 to ChildCount-1 do with Children[I] do
begin
Result := FindFirst(aDef);
if (Result <> nil) or IsRequired then
Exit;
end;
end;
if (CPQuant = cqZeroOrMore) or (CPQuant = cqOnceOrMore) then
Result := FindFirst(aDef);
if (Result = nil) and Assigned(FParent) then
Result := FParent.FindNext(aDef, FIndex);
end;
{ TElementDecl }
function TElementDecl.GetAttrDefCount: Integer;
begin
if Assigned(FAttrDefs) then
Result := FAttrDefs.Count
else
Result := 0;
end;
function TElementDecl.AttrDefByIndex(index: Integer): TAttributeDef;
begin
if Assigned(FAttrDefs) then
Result := TAttributeDef(FAttrDefs[index])
else
Result := nil;
end;
destructor TElementDecl.Destroy;
var
i: Integer;
begin
RootCP.Free;
if Assigned(FAttrDefs) then
begin
for i := FAttrDefs.Count-1 downto 0 do
TObject(FAttrDefs.List^[i]).Free;
FAttrDefs.Free;
end;
inherited Destroy;
end;
function TElementDecl.GetAttrDef(aName: PHashItem): TAttributeDef;
var
i: Integer;
begin
if Assigned(FAttrDefs) then
begin
for i := 0 to FAttrDefs.Count-1 do
begin
Result := TAttributeDef(FAttrDefs.List^[i]);
if Result.FData^.FQName = aName then
Exit;
end;
end;
Result := nil;
end;
procedure TElementDecl.AddAttrDef(aDef: TAttributeDef);
begin
if FAttrDefs = nil then
FAttrDefs := TFPList.Create;
FAttrDefs.Add(aDef);
if aDef.Default in [adRequired, adDefault, adFixed] then
FNeedsDefaultPass := True;
end;
{ TAttributeDef }
constructor TAttributeDef.Create(aName: PHashItem; aColonPos: Integer);
begin
New(FData);
FillChar(FData^, sizeof(TNodeData), 0);
FData^.FIsDefault := True;
FData^.FQName := aName;
FData^.FColonPos := aColonPos;
FData^.FTypeInfo := Self;
FIsNamespaceDecl := ((Length(aName^.Key) = 5) or (aColonPos = 6)) and
(Pos(WideString('xmlns'), aName^.Key) = 1);
end;
destructor TAttributeDef.Destroy;
var
curr, tmp: PNodeData;
begin
curr := FData;
while Assigned(curr) do
begin
tmp := curr^.FNext;
Dispose(curr);
curr := tmp;
end;
inherited Destroy;
end;
function TAttributeDef.AddEnumToken(Buf: PWideChar; Len: Integer): Boolean;
var
I, L: Integer;
begin
// TODO: this implementaion is the slowest possible...
Result := False;
L := Length(FEnumeration);
for I := 0 to L-1 do
begin
if (Len = Length(FEnumeration[i])) and
CompareMem(Pointer(FEnumeration[i]), Buf, Len*sizeof(WideChar)) then
Exit;
end;
SetLength(FEnumeration, L+1);
SetString(FEnumeration[L], Buf, Len);
Result := True;
end;
function TAttributeDef.HasEnumToken(const aValue: WideString): Boolean;
var
I: Integer;
begin
Result := True;
if Length(FEnumeration) = 0 then
Exit;
for I := 0 to Length(FEnumeration)-1 do
begin
if FEnumeration[I] = aValue then
Exit;
end;
Result := False;
end;
end.
|