File: fpdocfiles.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (1290 lines) | stat: -rw-r--r-- 32,882 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code 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.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Author: Tom Gregorovic
}
unit FPDocFiles;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Contnrs, FileUtil, DOM, XMLWrite, XMLRead;
  
type
  TFPDocInfo = record
    Packages: Integer;
    Modules: Integer;
    Topics: Integer;
    Elements: Integer;
    ElementsNonEmpty: Integer;
    Shorts: Integer;
    Descriptions: Integer;
    Errors: Integer;
    SeeAlsos: Integer;
    Examples: Integer;
  end;

const
  EmptyFPDocInfo: TFPDocInfo = (
    Packages: 0;
    Modules: 0;
    Topics: 0;
    Elements: 0;
    ElementsNonEmpty: 0;
    Shorts: 0;
    Descriptions: 0;
    Errors: 0;
    SeeAlsos: 0;
    Examples: 0;
    );
  
type
  TUniqueName = record
    Name: String;
    Indexes: Array of Integer;
    NonEmptyCount: Integer;
  end;

  TUniqueNameArray = Array of TUniqueName;
  
  TFPDocNode = class;

  { TFPDocItem }

  TFPDocItem = class
  private
    FParent: TFPDocItem;
    FModified: Boolean;
  public
    constructor Create(const AParent: TFPDocItem);
    function GetInfo: TFPDocInfo; virtual; abstract;
    
    procedure Modify; virtual;
    procedure Reset; virtual;
    function AddNode(const AName: String): TFPDocNode; virtual; abstract;
    function InsertNode(Index: Integer; const AName: String): TFPDocNode; virtual; abstract;
    procedure DeleteNode(ANode: TFPDocNode); virtual; abstract;
    procedure RenameNode(ANode: TFPDocNode; const AName: String); virtual; abstract;
  public
    property Modified: Boolean read FModified write FModified;
    property Parent: TFPDocItem read FParent;
  end;

  { TFPDocNode }

  TFPDocNodeClass = class of TFPDocNode;
  TFPDocNode = class(TFPDocItem)
  private
    FName: String;
    FDOMNode: TDOMNode;
    function GetDOMNodeValue(const AName: String): String;
    function GetDescription: String;
    function GetShort: String;
    procedure SetDOMNodeValue(const AName, AValue: String);
    procedure SetDescription(const AValue: String);
    procedure SetShort(const AValue: String);
  protected
    function InsertDOMNode(const AName: String; const ADOMNode: TDOMNode): TDOMNode; virtual;
    function GetEmpty: Boolean; virtual;
  public
    constructor Create(const AParent: TFPDocItem; const ADOMNode: TDOMNode);
    procedure Assign(ASource: TFPDocNode);
    function GetInfo: TFPDocInfo; override;
    
    procedure Rename(const S: String);
    procedure Delete;
  public
    property Name: String read FName;
    property Description: String read GetDescription write SetDescription;
    property Short: String read GetShort write SetShort;
    property Empty: Boolean read GetEmpty;
  end;

  { TFPDocElement }

  TFPDocElement = class(TFPDocNode)
  private
    FExamples: TList;
    function GetErrors: String;
    function GetExample(Index: Integer): String;
    function GetExamplesCount: Integer;
    function GetSeeAlso: String;
    procedure SetErrors(const AValue: String);
    procedure SetExample(Index: Integer; const AValue: String);
    procedure SetSeeAlso(const AValue: String);
  protected
    function GetEmpty: Boolean; override;
  public
    constructor Create(const AParent: TFPDocItem; const ADOMNode: TDOMNode);
    destructor Destroy; override;

    procedure Assign(ASource: TFPDocElement);
    function GetInfo: TFPDocInfo; override;
    
    procedure ParseExamples; virtual;
    
    function AddExample(const AFileName: String): Integer; virtual;
    function InsertExample(Index: Integer; const AFileName: String): Integer; virtual;
    procedure DeleteExample(Index: Integer); virtual;

    function AddNode(const AName: String): TFPDocNode; override;
    procedure DeleteNode(ANode: TFPDocNode); override;
    function InsertNode(Index: Integer; const AName: String): TFPDocNode;
      override;
    procedure RenameNode(ANode: TFPDocNode; const AName: String); override;
  public
    property Errors: String read GetErrors write SetErrors;
    property SeeAlso: String read GetSeeAlso write SetSeeAlso;
    property Examples[Index: Integer]: String read GetExample write SetExample;
    property ExamplesCount: Integer read GetExamplesCount;
  end;
  
  { TFPDocTopic }

  TFPDocTopic = class(TFPDocNode)
  private
    FTopics: TObjectList;
    function GetTopicsCount: Integer;
    function GetTopic(Index: Integer): TFPDocTopic;
  public
    constructor Create(const AParent: TFPDocItem; const ADOMNode: TDOMNode);
    destructor Destroy; override;

    procedure ParseTopics; virtual;
    procedure AddTopic(const ATopic: TFPDocTopic);
    procedure Assign(ASource: TFPDocTopic);
    function GetInfo: TFPDocInfo; override;
    
    procedure Reset; override;
    
    function AddTopic(const AName: String): TFPDocTopic; virtual;
    function InsertTopic(Index: Integer; const AName: String): TFPDocTopic; virtual;
    procedure DeleteNode(ANode: TFPDocNode); override;
    procedure RenameNode(ANode: TFPDocNode; const AName: String); override;
  public
    function AddNode(const AName: String): TFPDocNode; override;
    function InsertNode(Index: Integer; const AName: String): TFPDocNode;
      override;
    property Topics[Index: Integer]: TFPDocTopic read GetTopic;
    property TopicsCount: Integer read GetTopicsCount;
  end;
  
  { TFPDocNodeWithList }

  TFPDocNodeWithList = class(TFPDocTopic)
  private
    FNodes: TObjectList;
    FNames: TStringList;
    function GetCount: Integer;
    function GetNode(Index: Integer): TFPDocNode;
    function GetNodeByName(const Index: String): TFPDocNode;
  public
    constructor Create(const AParent: TFPDocItem; const ADOMNode: TDOMNode);
    destructor Destroy; override;
    procedure ParseNodes; virtual; abstract;
    
    procedure Reset; override;
    function GetUniqueNames: TUniqueNameArray;
        
    function AddNode(const AName: String): TFPDocNode; override;
    procedure DeleteNode(ANode: TFPDocNode); override;
    procedure RenameNode(ANode: TFPDocNode; const AName: String); override;
  public
    property Nodes[Index: Integer]: TFPDocNode read GetNode;
    property NodesByName[const Index: String]: TFPDocNode read GetNodeByName;
    property Count: Integer read GetCount;
    property Names: TStringList read FNames;
  end;
  
  { TFPDocModule }

  TFPDocModule = class(TFPDocNodeWithList)
  private
    function GetElement(Index: Integer): TFPDocElement;
    function GetElementByName(const Index: String): TFPDocElement;
  public
    function GetInfo: TFPDocInfo; override;
    procedure ParseNodes; override;
    
    function InsertNode(Index: Integer; const AName: String): TFPDocNode; override;
  public
    property Elements[Index: Integer]: TFPDocElement read GetElement;
    property ElementsByName[const Index: String]: TFPDocElement read GetElementByName;
  end;
  
  { TFPDocPackage }

  TFPDocPackage = class(TFPDocNodeWithList)
  private
    function GetModule(Index: Integer): TFPDocModule;
    function GetModuleByName(const Index: String): TFPDocModule;
  public
    function GetInfo: TFPDocInfo; override;
    procedure ParseNodes; override;
    
    function InsertNode(Index: Integer; const AName: String): TFPDocNode; override;
  public
    property Modules[Index: Integer]: TFPDocModule read GetModule;
    property ModulesByName[const Index: String]: TFPDocModule read GetModuleByName;
  end;
  
  TMoveElementEvent = procedure (const SrcPackage: TFPDocPackage;
    const SrcModule: TFPDocModule; const Src: TFPDocElement;
    const DestList: TStrings; var Dest: Integer) of object;
  
  { TFPDocFile }

  TFPDocFile = class(TFPDocNodeWithList)
  private
    FDocument: TXMLDocument;
    function GetPackage(Index: Integer): TFPDocPackage;
    function GetPackageByName(const Index: String): TFPDocPackage;
  public
    constructor Create(const FileName: String);
    constructor Create(Stream: TStream);
    destructor Destroy; override;
    
    procedure ParseNodes; override;
    
    procedure SaveToFile(const FileName: String);
    procedure AssignToSkeleton(const SkeletonFile: TFPDocFile;
      OnMoveElement: TMoveElementEvent);
    function GetInfo: TFPDocInfo; override;
    
    function InsertNode(Index: Integer; const AName: String): TFPDocNode; override;
  public
    property Packages[Index: Integer]: TFPDocPackage read GetPackage;
    property PackagesByName[const Index: String]: TFPDocPackage read GetPackageByName;
  end;
  
const
  SFPDocHeader  = 'fpdoc-descriptions';
  
  SFPDocPackage = 'package';
  SFPDocModule  = 'module';
  SFPDocTopic   = 'topic';
  SFPDocElement = 'element';

  SFPDocName    = 'name';
  SFPDocFile    = 'file';
  
  SFPDocDescr   = 'descr';
  SFPDocShort   = 'short';
  SFPDocErrors  = 'errors';
  SFPDocSeeAlso = 'seealso';
  SFPDocExample = 'example';
  
  function DbgS(const AInfo: TFPDocInfo): String; overload;

implementation

uses LCLProc;

function DbgS(const AInfo: TFPDocInfo): String;
begin
  Result :=
    SFPDocPackage + 's: ' + IntToStr(AInfo.Packages) + LineEnding +
    SFPDocModule + 's: ' + IntToStr(AInfo.Modules) + LineEnding +
    SFPDocTopic + 's: ' + IntToStr(AInfo.Topics) + LineEnding +
    SFPDocElement + 's: ' + IntToStr(AInfo.Elements) + LineEnding +
    SFPDocElement + 's NonEmpty: ' + IntToStr(AInfo.ElementsNonEmpty) + LineEnding +
    SFPDocShort + 's: ' + IntToStr(AInfo.Shorts) + LineEnding +
    SFPDocDescr + 's: ' + IntToStr(AInfo.Descriptions) + LineEnding +
    SFPDocErrors + 's: ' + IntToStr(AInfo.Errors) + LineEnding +
    SFPDocSeeAlso + 's: ' + IntToStr(AInfo.SeeAlsos) + LineEnding +
    SFPDocExample + 's: ' + IntToStr(AInfo.Examples);
end;

{ TFPDocItem }

constructor TFPDocItem.Create(const AParent: TFPDocItem);
begin
  FParent := AParent;
  FModified := False;
end;

procedure TFPDocItem.Modify;
begin
  FModified := True;
  if FParent <> nil then FParent.Modify;
end;

procedure TFPDocItem.Reset;
begin
  FModified := False;
end;

{ TFPDocNode }

function TFPDocNode.GetDOMNodeValue(const AName: String): String;
var
  N: TDOMNode;
  S: TStringStream;
  D: TDOMNode;
begin
  Result := '';
  N := FDOMNode.FindNode(AName);
  if N = nil then Exit;
  if N.FirstChild = nil then Exit;
  
  S := TStringStream.Create('');
  try
    D := N.FirstChild;
    while D <> nil do
    begin
      WriteXML(D, S);
      D := D.NextSibling;
    end;
    
    Result := S.DataString;
  finally
    S.Free;
  end;
end;

function TFPDocNode.GetDescription: String;
begin
  Result := GetDOMNodeValue(SFPDocDescr);
end;

function TFPDocNode.GetShort: String;
begin
  Result := GetDOMNodeValue(SFPDocShort);
end;

procedure TFPDocNode.SetDOMNodeValue(const AName, AValue: String);
var
  N: TDOMNode;
  S: TStream;
begin
  //DebugLn(FName, ' ', AName);
  N := FDOMNode.FindNode(AName);

  if N = nil then
  begin
    if AValue = '' then Exit;
    N := FDOMNode.OwnerDocument.CreateElement(AName);
    N := InsertDOMNode(AName, N);
    //DebugLn('New');
  end
  else
    while N.LastChild <> nil do N.RemoveChild(N.LastChild);

  if AValue = '' then Exit;

  S := TStringStream.Create(AValue);
  try
    ReadXMLFragment(N, S);
  finally
    S.Free;
  end;

  Modify;

end;

procedure TFPDocNode.SetDescription(const AValue: String);
begin
  SetDOMNodeValue(SFPDocDescr, AValue);
end;

procedure TFPDocNode.SetShort(const AValue: String);
begin
  SetDOMNodeValue(SFPDocShort, AValue);
end;

function TFPDocNode.InsertDOMNode(const AName: String; const ADOMNode: TDOMNode): TDOMNode;
begin
  if not FDOMNode.HasChildNodes then Result := FDOMNode.AppendChild(ADOMNode)
  else
  begin
    if AName = SFPDocShort then Result := FDOMNode.InsertBefore(ADOMNode, FDOMNode.FirstChild);
    if AName = SFPDocDescr then
    begin
      Result := FDOMNode.FindNode(SFPDocShort);
      if Result = nil then Result := FDOMNode.InsertBefore(ADOMNode, FDOMNode.FirstChild)
      else Result := FDOMNode.InsertBefore(ADOMNode, Result.NextSibling);
    end
    else
      if AName = SFPDocErrors then
      begin
        Result := FDOMNode.FindNode(SFPDocDescr);
        if Result = nil then Result := FDOMNode.FindNode(SFPDocShort);
        if Result = nil then Result := FDOMNode.InsertBefore(ADOMNode, FDOMNode.FirstChild)
        else Result := FDOMNode.InsertBefore(ADOMNode, Result.NextSibling);
      end
      else
        if AName = SFPDocSeeAlso then
        begin
          Result := FDOMNode.FindNode(SFPDocErrors);
          if Result = nil then Result := FDOMNode.FindNode(SFPDocDescr);
          if Result = nil then Result := FDOMNode.FindNode(SFPDocShort);
          if Result = nil then Result := FDOMNode.InsertBefore(ADOMNode, FDOMNode.FirstChild)
          else Result := FDOMNode.InsertBefore(ADOMNode, Result.NextSibling);
        end;
  end;
end;

function TFPDocNode.GetEmpty: Boolean;
begin
  Result := (Description = '') and (Short = '');
end;

constructor TFPDocNode.Create(const AParent: TFPDocItem; const ADOMNode: TDOMNode);
begin
  inherited Create(AParent);

  FDOMNode := ADOMNode;
  
  FName := (FDOMNode as TDOMElement).GetAttribute(SFPDocName);
end;

procedure TFPDocNode.Assign(ASource: TFPDocNode);
begin
  Short := ASource.Short;
  Description := ASource.Description;
end;

function TFPDocNode.GetInfo: TFPDocInfo;
begin
  Result.Packages := 0;
  Result.Modules := 0;
  Result.Topics := 0;
  Result.Elements := 0;
  Result.ElementsNonEmpty := 0;
  if Short <> '' then Result.Shorts := 1
  else Result.Shorts := 0;
  if Description <> '' then Result.Descriptions := 1
  else Result.Descriptions := 0;
  Result.Errors := 0;
  Result.SeeAlsos := 0;
  Result.Examples := 0;
end;

procedure TFPDocNode.Rename(const S: String);
begin
  if S <> Name then
    Parent.RenameNode(Self, S);
end;

procedure TFPDocNode.Delete;
begin
  Parent.DeleteNode(Self);
end;

{ TFPDocElement }

function TFPDocElement.GetEmpty: Boolean;
begin
  Result := (Description = '') and (Short = '') and (Errors = '') and
    (SeeAlso = '') and (ExamplesCount = 0);
end;

function TFPDocElement.GetErrors: String;
begin
  Result := GetDOMNodeValue(SFPDocErrors);
end;

function TFPDocElement.GetExample(Index: Integer): String;
begin
  Result := TDOMElement(FExamples[Index]).GetAttribute(SFPDocFile);
end;

function TFPDocElement.GetExamplesCount: Integer;
begin
  Result := FExamples.Count;
end;

function TFPDocElement.GetSeeAlso: String;
begin
  Result := GetDOMNodeValue(SFPDocSeeAlso);
end;

procedure TFPDocElement.SetErrors(const AValue: String);
begin
  SetDOMNodeValue(SFPDocErrors, AValue);
end;

procedure TFPDocElement.SetExample(Index: Integer; const AValue: String);
begin
  TDOMElement(FExamples[Index]).SetAttribute(SFPDocFile, AValue);
end;

procedure TFPDocElement.SetSeeAlso(const AValue: String);
begin
  SetDOMNodeValue(SFPDocSeeAlso, AValue);
end;

constructor TFPDocElement.Create(const AParent: TFPDocItem;
  const ADOMNode: TDOMNode);
begin
  inherited;
  
  FExamples := TList.Create;
  
  ParseExamples;
end;

destructor TFPDocElement.Destroy;
begin
  FExamples.Free;

  inherited Destroy;
end;


procedure TFPDocElement.Assign(ASource: TFPDocElement);
var
  I: Integer;
begin
  inherited Assign(ASource);

  Errors := ASource.Errors;
  SeeAlso := ASource.SeeAlso;
  
  FExamples.Clear;
  DebugLn('Assign Examples: ' + ASource.Name + ' ' + DbgS(ASource.ExamplesCount));
  for I := 0 to ASource.ExamplesCount - 1 do AddExample(ASource.Examples[I]);
end;

function TFPDocElement.GetInfo: TFPDocInfo;
begin
  Result := inherited GetInfo;
  
  Result.Elements := 1;
  if Empty then Result.ElementsNonEmpty := 0
  else Result.ElementsNonEmpty := 1;
  if Errors <> '' then Result.Errors := 1
  else Result.Errors := 0;
  if SeeAlso <> '' then Result.SeeAlsos := 1
  else Result.SeeAlsos := 0;
  Result.Examples := ExamplesCount;
end;

procedure TFPDocElement.ParseExamples;
var
  I: TDOMNode;
begin
  FExamples.Clear;

  I := FDOMNode.FirstChild;
  while I <> nil do
  begin
    if I.NodeName = SFPDocExample then
    begin
      FExamples.Add(Pointer(I));
    end;

    I := I.NextSibling;
  end;
end;

function TFPDocElement.AddExample(const AFileName: String): Integer;
begin
  Result := InsertExample(ExamplesCount, AFileName);
end;

function TFPDocElement.InsertExample(Index: Integer; const AFileName: String): Integer;
var
  E: TDOMElement;
  N: TDOMNode;
begin
  if Index < 0 then Index := 0;
  if Index > ExamplesCount then Index := ExamplesCount;
  E := FDOMNode.OwnerDocument.CreateElement(SFPDocExample);
  E.SetAttribute(SFPDocFile, AFileName);
  
  if Index >= ExamplesCount then
    N := FDOMNode.AppendChild(E)
  else
    N := FDOMNode.InsertBefore(E, TDOMNode(FExamples[Index]));
    
  FExamples.Insert(Index, Pointer(N));
  Result := Index;
end;

procedure TFPDocElement.DeleteExample(Index: Integer);
begin
  FDOMNode.RemoveChild(TDOMNode(FExamples[Index]));
  FExamples.Delete(Index);
end;

function TFPDocElement.AddNode(const AName: String): TFPDocNode;
begin
  raise Exception.Create('');
  Result:=nil;
end;

procedure TFPDocElement.DeleteNode(ANode: TFPDocNode);
begin
  raise Exception.Create('');
end;

function TFPDocElement.InsertNode(Index: Integer; const AName: String
  ): TFPDocNode;
begin
  raise Exception.Create('');
  Result:=nil;
end;

procedure TFPDocElement.RenameNode(ANode: TFPDocNode; const AName: String);
begin
  raise Exception.Create('');
end;

{ TFPDocTopic }

function TFPDocTopic.GetTopicsCount: Integer;
begin
  Result := FTopics.Count;
end;

function TFPDocTopic.GetTopic(Index: Integer): TFPDocTopic;
begin
  Result := FTopics[Index] as TFPDocTopic;
end;

constructor TFPDocTopic.Create(const AParent: TFPDocItem; const ADOMNode: TDOMNode);
begin
  inherited;
  
  FTopics := TObjectList.Create(True);
  //DebugLn(Name);
  
  ParseTopics;
end;

destructor TFPDocTopic.Destroy;
begin
  FTopics.Free;
  
  inherited Destroy;
end;

procedure TFPDocTopic.ParseTopics;
var
  I: TDOMNode;
  T: TFPDocTopic;
begin
  FTopics.Clear;

  I := FDOMNode.FirstChild;
  while I <> nil do
  begin
    if I.NodeName = SFPDocTopic then
    begin
      T := TFPDocTopic.Create(Self, I);
      FTopics.Add(T);
    end;

    I := I.NextSibling;
  end;
end;

procedure TFPDocTopic.AddTopic(const ATopic: TFPDocTopic);
begin
  AddTopic(ATopic.Name).Assign(ATopic);
end;

procedure TFPDocTopic.Assign(ASource: TFPDocTopic);
var
  I: Integer;
begin
  inherited Assign(ASource);
  
  for I := 0 to ASource.TopicsCount - 1 do
    AddTopic(ASource.Topics[I]);
end;

function TFPDocTopic.GetInfo: TFPDocInfo;
var
  I: Integer;
  Info: TFPDocInfo;
begin
  Result := inherited GetInfo;
  
  if ClassType <> TFPDocTopic then Result.Topics := 0
  else Result.Topics := 1;
  
  
  for I := 0 to TopicsCount - 1 do
  begin
    Info := Topics[I].GetInfo;
    
    Result.Topics := Result.Topics + Info.Topics;
    Result.Shorts := Result.Shorts + Info.Shorts;
    Result.Descriptions := Result.Descriptions + Info.Descriptions;
  end;
end;

procedure TFPDocTopic.Reset;
var
  I: Integer;
begin
  inherited;
  
  for I := 0 to TopicsCount - 1 do Topics[I].Reset;
end;

function TFPDocTopic.AddTopic(const AName: String): TFPDocTopic;
begin
  Result := InsertTopic(TopicsCount, AName);
end;

function TFPDocTopic.InsertTopic(Index: Integer; const AName: String): TFPDocTopic;
var
  N: TDOMElement;
begin
  N := FDOMNode.OwnerDocument.CreateElement(SFPDocTopic);
  N.AttribStrings[SFPDocName] := AName;
  
  if Index < 0 then Index := 0;
  if Index > TopicsCount then Index := TopicsCount;
  if Index < TopicsCount then
    Result := TFPDocTopic.Create(Self, FDOMNode.InsertBefore(N, Topics[Index].FDOMNode))
  else
    Result := TFPDocTopic.Create(Self, FDOMNode.AppendChild(N));
    
  FTopics.Insert(Index, Result);
  Result.Modify;
end;

procedure TFPDocTopic.DeleteNode(ANode: TFPDocNode);
begin
  FDOMNode.RemoveChild(ANode.FDOMNode);
  FTopics.Remove(ANode);
  Modify;
end;

procedure TFPDocTopic.RenameNode(ANode: TFPDocNode; const AName: String);
begin
  (ANode.FDOMNode as TDOMElement).SetAttribute(SFPDocName, AName);
  ANode.FName := AName;
  ANode.Modify;
end;

function TFPDocTopic.AddNode(const AName: String): TFPDocNode;
begin
  raise Exception.Create('');
  Result:=nil;
end;

function TFPDocTopic.InsertNode(Index: Integer; const AName: String
  ): TFPDocNode;
begin
  raise Exception.Create('');
  Result:=nil;
end;

{ TFPDocNodeWithList }

function TFPDocNodeWithList.GetCount: Integer;
begin
  Result := FNodes.Count;
end;

function TFPDocNodeWithList.GetNode(Index: Integer): TFPDocNode;
begin
  Result := FNodes[Index] as TFPDocNode;
end;

function TFPDocNodeWithList.GetNodeByName(const Index: String): TFPDocNode;
var
  I: Integer;
begin
  I := FNames.IndexOf(Index);
  if I = -1 then
    Result := nil
  else
    Result := FNames.Objects[I] as TFPDocNode;
end;

constructor TFPDocNodeWithList.Create(const AParent: TFPDocItem; const ADOMNode: TDOMNode);
begin
  inherited;
  
  FNodes := TObjectList.Create(True);
  FNames := TStringList.Create;
  FNames.Sorted := True;

  ParseNodes;
end;

destructor TFPDocNodeWithList.Destroy;
begin
  FNames.Free;
  FNodes.Free;

  inherited Destroy;
end;

procedure TFPDocNodeWithList.Reset;
var
  I: Integer;
begin
  inherited;
  
  for I := 0 to Count - 1 do Nodes[I].Reset;
end;

function TFPDocNodeWithList.GetUniqueNames: TUniqueNameArray;
var
  I, J: Integer;
  Found: Boolean;
begin
  SetLength(Result, 0);

  for I := 0 to Count - 1 do
  begin
    Found := False;
    for J := 0 to High(Result) do
    begin
      if Result[J].Name = Nodes[I].Name then
      begin
        Found := True;
        
        SetLength(Result[J].Indexes, Length(Result[J].Indexes) + 1);
        Result[J].Indexes[High(Result[J].Indexes)] := I;
        if not Nodes[I].Empty then Inc(Result[High(Result)].NonEmptyCount);
        
        Break;
      end;
    end;

    if not Found then
    begin
      SetLength(Result, Length(Result) + 1);
      Result[High(Result)].Name := Nodes[I].Name;
      SetLength(Result[High(Result)].Indexes, 1);
      Result[High(Result)].Indexes[0] := I;
      if Nodes[I].Empty then Result[High(Result)].NonEmptyCount := 0
      else Result[High(Result)].NonEmptyCount := 1;
    end;
  end;
end;

function TFPDocNodeWithList.AddNode(const AName: String): TFPDocNode;
begin
  Result := InsertNode(Count, AName);
end;

procedure TFPDocNodeWithList.DeleteNode(ANode: TFPDocNode);
begin
  if ANode is TFPDocTopic then inherited
  else
  begin
    FDOMNode.RemoveChild(ANode.FDOMNode);
    FNames.Delete(FNames.IndexOfObject(ANode));
    FNodes.Remove(ANode);
    Modify;
  end;
end;

procedure TFPDocNodeWithList.RenameNode(ANode: TFPDocNode; const AName: String);
begin
  if not (ANode is TFPDocTopic) then
    FNames.Strings[FNames.IndexOfObject(ANode)] := AName;
    
  inherited;
end;

{ TFPDocModule }

function TFPDocModule.GetElement(Index: Integer): TFPDocElement;
begin
  Result := FNodes[Index] as TFPDocElement;
end;

function TFPDocModule.GetElementByName(const Index: String): TFPDocElement;
var
  I: Integer;
begin
  I := FNames.IndexOf(Index);
  if I = -1 then
    Result := nil
  else
    Result := FNames.Objects[I] as TFPDocElement;
end;

function TFPDocModule.GetInfo: TFPDocInfo;
var
  I: Integer;
  Info: TFPDocInfo;
begin
  Result := inherited GetInfo;

  Result.Modules := 1;

  for I := 0 to Count - 1 do
  begin
    Info := Elements[I].GetInfo;

    Result.Elements := Result.Elements + Info.Elements;
    Result.ElementsNonEmpty := Result.ElementsNonEmpty + Info.ElementsNonEmpty;
    Result.Shorts := Result.Shorts + Info.Shorts;
    Result.Descriptions := Result.Descriptions + Info.Descriptions;
    Result.Errors := Result.Errors + Info.Errors;
    Result.SeeAlsos := Result.SeeAlsos + Info.SeeAlsos;
    Result.Examples := Result.Examples + Info.Examples;
  end;
end;

procedure TFPDocModule.ParseNodes;
var
  I: TDOMNode;
  N: TFPDocElement;
begin
  FNodes.Clear;
  FNames.Clear;

  I := FDOMNode.FirstChild;
  while I <> nil do
  begin
    if I.NodeName = SFPDocElement then
    begin
      N := TFPDocElement.Create(Self, I);
      FNodes.Add(N);
      FNames.AddObject(N.Name, N);
    end;

    I := I.NextSibling;
  end;
end;

function TFPDocModule.InsertNode(Index: Integer; const AName: String): TFPDocNode;
var
  N: TDOMElement;
begin
  N := FDOMNode.OwnerDocument.CreateElement(SFPDocElement);
  N.AttribStrings[SFPDocName] := AName;

  if Index < 0 then Index := 0;
  if Index > Count then Index := Count;
  if Index < Count then
    Result := TFPDocElement.Create(Self, FDOMNode.InsertBefore(N, Elements[Index].FDOMNode))
  else
    if TopicsCount = 0 then
      Result := TFPDocElement.Create(Self, FDOMNode.AppendChild(N))
    else
      Result := TFPDocElement.Create(Self, FDOMNode.InsertBefore(N, Topics[0].FDOMNode));

  FNodes.Insert(Index, Result);
  FNames.AddObject(AName, Result);
  Result.Modify;
end;


{ TFPDocPackage }

function TFPDocPackage.GetModule(Index: Integer): TFPDocModule;
begin
  Result := FNodes[Index] as TFPDocModule;
end;

function TFPDocPackage.GetModuleByName(const Index: String): TFPDocModule;
var
  I: Integer;
begin
  I := FNames.IndexOf(Index);
  if I = -1 then
    Result := nil
  else
    Result := FNames.Objects[I] as TFPDocModule;
end;

function TFPDocPackage.GetInfo: TFPDocInfo;
var
  I: Integer;
  Info: TFPDocInfo;
begin
  Result := inherited GetInfo;

  Result.Packages := 1;

  for I := 0 to Count - 1 do
  begin
    Info := Modules[I].GetInfo;

    Result.Modules := Result.Modules + Info.Modules;
    Result.Elements := Result.Elements + Info.Elements;
    Result.ElementsNonEmpty := Result.ElementsNonEmpty + Info.ElementsNonEmpty;
    Result.Shorts := Result.Shorts + Info.Shorts;
    Result.Descriptions := Result.Descriptions + Info.Descriptions;
    Result.Errors := Result.Errors + Info.Errors;
    Result.SeeAlsos := Result.SeeAlsos + Info.SeeAlsos;
    Result.Examples := Result.Examples + Info.Examples;
  end;
end;

procedure TFPDocPackage.ParseNodes;
var
  I: TDOMNode;
  N: TFPDocModule;
begin
  FNodes.Clear;
  FNames.Clear;

  I := FDOMNode.FirstChild;
  while I <> nil do
  begin
    if I.NodeName = SFPDocModule then
    begin
      N := TFPDocModule.Create(Self, I);
      FNodes.Add(N);
      FNames.AddObject(N.Name, N);
    end;

    I := I.NextSibling;
  end;
end;

function TFPDocPackage.InsertNode(Index: Integer; const AName: String
  ): TFPDocNode;
var
  N: TDOMElement;
begin
  N := FDOMNode.OwnerDocument.CreateElement(SFPDocModule);
  N.AttribStrings[SFPDocName] := AName;

  if Index < 0 then Index := 0;
  if Index > Count then Index := Count;
  if Index < Count then
    Result := TFPDocModule.Create(Self, FDOMNode.InsertBefore(N, Modules[Index].FDOMNode))
  else
    if TopicsCount = 0 then
      Result := TFPDocModule.Create(Self, FDOMNode.AppendChild(N))
    else
      Result := TFPDocModule.Create(Self, FDOMNode.InsertBefore(N, Topics[0].FDOMNode));

  FNodes.Insert(Index, Result);
  FNames.AddObject(AName, Result);
  Result.Modify;
end;

{ TFPDocFile }

function TFPDocFile.GetPackage(Index: Integer): TFPDocPackage;
begin
  Result := Nodes[Index] as TFPDocPackage;
end;

function TFPDocFile.GetPackageByName(const Index: String): TFPDocPackage;
begin
  Result := NodesByName[Index] as TFPDocPackage;
end;

constructor TFPDocFile.Create(const FileName: String);
var
  F: TFileStream;
begin
  F := TFileStream.Create(UTF8ToSys(FileName), fmOpenRead);
  try
    Create(F);
  finally
    F.Free;
  end;
end;

constructor TFPDocFile.Create(Stream: TStream);
var
  R: TDOMNode;
begin
  ReadXMLFile(FDocument, Stream);

  R := FDocument.FindNode(SFPDocHeader);
  if R = nil then
    raise Exception.Create('Invalid FPDoc file!');
    
  inherited Create(nil, R);
end;

destructor TFPDocFile.Destroy;
begin
  FDocument.Free;

  inherited Destroy;
end;

procedure TFPDocFile.ParseNodes;
var
  I: TDOMNode;
  P: TFPDocPackage;
begin
  FNodes.Clear;
  FNames.Clear;
  
  I := FDOMNode.FirstChild;
  while I <> nil do
  begin
    if I.NodeName = SFPDocPackage then
    begin
      P := TFPDocPackage.Create(Self, I);
      FNodes.Add(P);
      FNames.AddObject(P.Name, P);
    end;
    
    I := I.NextSibling;
  end;
end;

procedure TFPDocFile.SaveToFile(const FileName: String);
begin
  WriteXMLFile(FDocument, FileName);
  Reset;
end;

procedure TFPDocFile.AssignToSkeleton(const SkeletonFile: TFPDocFile;
  OnMoveElement: TMoveElementEvent);
var
  I, J, K, L, M: Integer;
  P1, P2: TFPDocPackage;
  M1, M2: TFPDocModule;
  E1, E2: TFPDocElement;
  Elements1, Elements2: TUniqueNameArray;
  DestList: TStringList;
  
  procedure AskMove;
  var
    N: Integer;
  begin
    N := M;
    OnMoveElement(P1, M1, E1, DestList, N);
    if N <> -1 then
    begin
      M2.Elements[N].Assign(E1);
      if not M2.Elements[N].Empty then
        DestList.Objects[N] := TObject(1);
    end;
  end;
  
begin
  for I := 0 to Count - 1 do
  begin
    P1 := Packages[I];
    P2 := SkeletonFile.PackagesByName[P1.Name];
    P2.Assign(P1);
    
    for J := 0 to P1.Count - 1 do
    begin
      M1 := P1.Modules[J];
      M2 := P2.ModulesByName[M1.Name];
      M2.Assign(M1);
      
      DestList := TStringList.Create;
      try
        for K := 0 to M2.Count - 1 do
        begin
          E2 := M2.Elements[K];
          DestList.Add(E2.Name);
        end;
        
        Elements1 := M1.GetUniqueNames;
        Elements2 := M2.GetUniqueNames;
        
        for K := 0 to High(Elements1) do
        begin
          if Elements1[K].NonEmptyCount = 0 then Continue;

          M := -1;
          for L := 0 to High(Elements2) do
          begin
            if Elements1[K].Name = Elements2[L].Name then
            begin
              M := L;
            end;
          end;

          for L := 0 to High(Elements1[K].Indexes) do
          begin
            E1 := M1.Elements[Elements1[K].Indexes[L]];
            if E1.Empty then Continue;
            
            if M = -1 then AskMove
            else
              if L > High(Elements2[M].Indexes) then AskMove
              else
              begin
                E2 := M2.Elements[Elements2[M].Indexes[L]];
                if not E2.Empty then AskMove
                else
                begin
                  E2.Assign(E1);
                  
                  if not E2.Empty then
                    DestList.Objects[Elements2[M].Indexes[L]] := TObject(1);
                end;
              end;
          end;
        end;
      finally
        DestList.Free;
      end;
    end;
  end;
end;

function TFPDocFile.GetInfo: TFPDocInfo;
var
  I: Integer;
  Info: TFPDocInfo;
begin
  Result := EmptyFPDocInfo;

  for I := 0 to Count - 1 do
  begin
    Info := Packages[I].GetInfo;

    Result.Packages := Result.Packages + Info.Packages;
    Result.Modules := Result.Modules + Info.Modules;
    Result.Elements := Result.Elements + Info.Elements;
    Result.ElementsNonEmpty := Result.ElementsNonEmpty + Info.ElementsNonEmpty;
    Result.Shorts := Result.Shorts + Info.Shorts;
    Result.Descriptions := Result.Descriptions + Info.Descriptions;
    Result.Errors := Result.Errors + Info.Errors;
    Result.SeeAlsos := Result.SeeAlsos + Info.SeeAlsos;
    Result.Examples := Result.Examples + Info.Examples;
  end;
end;

function TFPDocFile.InsertNode(Index: Integer; const AName: String): TFPDocNode;
var
  N: TDOMElement;
begin
  N := FDocument.CreateElement(SFPDocPackage);
  N.AttribStrings[SFPDocName] := AName;

  if Index < 0 then Index := 0;
  if Index > Count then Index := Count;
  if Index < Count then
    Result := TFPDocPackage.Create(Self, FDocument.InsertBefore(N, Packages[Index].FDOMNode))
  else
    Result := TFPDocPackage.Create(Self, FDocument.AppendChild(N));

  FNodes.Insert(Index, Result);
  FNames.AddObject(AName, Result);
  Result.Modify;
end;



end.