Changeset 20 for trunk/UAcronym.pas


Ignore:
Timestamp:
May 4, 2016, 6:50:29 PM (9 years ago)
Author:
chronos
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UAcronym.pas

    r17 r20  
    1313  TAcronymMeanings = class;
    1414  TAcronymDb = class;
     15  TImportSources = class;
    1516
    1617  TAcronymSource = class
     
    107108  end;
    108109
     110  TImportPattern = record
     111    StartString: string;
     112    EndString: string;
     113    NewItem: Boolean;
     114  end;
     115
     116  { TImportFormat }
     117
     118  TImportFormat = class
     119    Id: Integer;
     120    Name: string;
     121    Acronym: TImportPattern;
     122    Meaning: TImportPattern;
     123    Description: TImportPattern;
     124    procedure SaveToNode(Node: TDOMNode);
     125    procedure LoadFromNode(Node: TDOMNode);
     126  end;
     127
     128  { TImportFormats }
     129
     130  TImportFormats = class(TObjectList)
     131    procedure UpdateIds;
     132    procedure SaveToNode(Node: TDOMNode);
     133    procedure LoadFromNode(Node: TDOMNode);
     134    function SearchById(Id: Integer): TImportFormat;
     135  end;
     136
     137  { TImportSource }
     138
     139  TImportSource = class
     140    Name: string;
     141    URL: string;
     142    Format: TImportFormat;
     143    LastTime: TDateTime;
     144    Sources: TImportSources;
     145    procedure Assign(Source: TImportSource);
     146    procedure SaveToNode(Node: TDOMNode);
     147    procedure LoadFromNode(Node: TDOMNode);
     148    constructor Create;
     149    destructor Destroy; override;
     150  end;
     151
     152  { TImportSources }
     153
     154  TImportSources = class(TObjectList)
     155    AcronymDb: TAcronymDb;
     156    function SearchByName(Name: string): TImportSource;
     157    procedure SaveToNode(Node: TDOMNode);
     158    procedure LoadFromNode(Node: TDOMNode);
     159  end;
     160
    109161  { TAcronymDb }
    110162
     
    114166    Acronyms: TAcronyms;
    115167    Categories: TAcronymCategories;
     168    ImportSources: TImportSources;
     169    ImportFormats: TImportFormats;
    116170    Modified: Boolean;
    117171    constructor Create;
     
    140194end;
    141195
     196{ TImportFormat }
     197
     198procedure TImportFormat.SaveToNode(Node: TDOMNode);
     199begin
     200  WriteInteger(Node, 'Id', Id);
     201  WriteString(Node, 'Name', Name);
     202  WriteString(Node, 'AcronymStartString', Acronym.StartString);
     203  WriteString(Node, 'AcronymEndString', Acronym.EndString);
     204  WriteString(Node, 'MeaningStartString', Meaning.StartString);
     205  WriteString(Node, 'MeaningEndString', Meaning.EndString);
     206  WriteString(Node, 'DescriptionStartString', Description.StartString);
     207  WriteString(Node, 'DescriptionEndString', Description.EndString);
     208end;
     209
     210procedure TImportFormat.LoadFromNode(Node: TDOMNode);
     211begin
     212  Id := ReadInteger(Node, 'Id', 0);
     213  Name := ReadString(Node, 'Name', '');
     214  Acronym.StartString := ReadString(Node, 'AcronymStartString', '');
     215  Acronym.EndString := ReadString(Node, 'AcronymEndString', '');
     216  Meaning.StartString := ReadString(Node, 'MeaningStartString', '');
     217  Meaning.EndString := ReadString(Node, 'MeaningEndString', '');
     218  Description.StartString := ReadString(Node, 'DescriptionStartString', '');
     219  Description.EndString := ReadString(Node, 'DescriptionEndString', '');
     220end;
     221
     222{ TImportSources }
     223
     224function TImportSources.SearchByName(Name: string): TImportSource;
     225var
     226  I: Integer;
     227begin
     228  I := 0;
     229  while (I < Count) and (TImportSource(Items[I]).Name <> Name) do Inc(I);
     230  if I < Count then Result := TImportSource(Items[I])
     231    else Result := nil;
     232end;
     233
     234procedure TImportSources.SaveToNode(Node: TDOMNode);
     235var
     236  I: Integer;
     237  NewNode2: TDOMNode;
     238begin
     239  for I := 0 to Count - 1 do
     240  with TImportSource(Items[I]) do begin
     241    NewNode2 := Node.OwnerDocument.CreateElement('ImportSource');
     242    Node.AppendChild(NewNode2);
     243    SaveToNode(NewNode2);
     244  end;
     245end;
     246
     247procedure TImportSources.LoadFromNode(Node: TDOMNode);
     248var
     249  Node2: TDOMNode;
     250  NewItem: TImportSource;
     251begin
     252  Count := 0;
     253  Node2 := Node.FirstChild;
     254  while Assigned(Node2) and (Node2.NodeName = 'ImportSource') do begin
     255    NewItem := TImportSource.Create;
     256    NewItem.Sources := Self;
     257    NewItem.LoadFromNode(Node2);
     258    Add(NewItem);
     259    Node2 := Node2.NextSibling;
     260  end;
     261end;
     262
     263{ TImportFormats }
     264
     265procedure TImportFormats.UpdateIds;
     266var
     267  LastId: Integer;
     268  I: Integer;
     269begin
     270  // Get highest used ID
     271  LastId := 0;
     272  for I := 0 to Count - 1 do begin
     273    if TImportFormat(Items[I]).Id > LastId then LastId := TImportFormat(Items[I]).Id;
     274  end;
     275  // Add ID to new items without ID
     276  for I := 0 to Count - 1 do begin
     277    if TImportFormat(Items[I]).Id = 0 then begin
     278      Inc(LastId);
     279      TImportFormat(Items[I]).Id := LastId;
     280    end;
     281  end;
     282end;
     283
     284procedure TImportFormats.SaveToNode(Node: TDOMNode);
     285var
     286  I: Integer;
     287  NewNode2: TDOMNode;
     288begin
     289  UpdateIds;
     290  for I := 0 to Count - 1 do
     291  with TImportFormat(Items[I]) do begin
     292    NewNode2 := Node.OwnerDocument.CreateElement('ImportFormat');
     293    Node.AppendChild(NewNode2);
     294    SaveToNode(NewNode2);
     295  end;
     296end;
     297
     298procedure TImportFormats.LoadFromNode(Node: TDOMNode);
     299var
     300  Node2: TDOMNode;
     301  NewItem: TImportFormat;
     302begin
     303  Count := 0;
     304  Node2 := Node.FirstChild;
     305  while Assigned(Node2) and (Node2.NodeName = 'ImportFormat') do begin
     306    NewItem := TImportFormat.Create;
     307    NewItem.LoadFromNode(Node2);
     308    Add(NewItem);
     309    Node2 := Node2.NextSibling;
     310  end;
     311  UpdateIds;
     312end;
     313
     314function TImportFormats.SearchById(Id: Integer): TImportFormat;
     315var
     316  I: Integer;
     317begin
     318  I := 0;
     319  while (I < Count) and (TImportFormat(Items[I]).Id <> Id) do Inc(I);
     320  if I < Count then Result := TImportFormat(Items[I])
     321    else Result := nil;
     322end;
     323
     324{ TImportSource }
     325
     326procedure TImportSource.Assign(Source: TImportSource);
     327begin
     328  Name := Source.Name;
     329  URL := Source.URL;
     330  Format := Source.Format;
     331end;
     332
     333procedure TImportSource.SaveToNode(Node: TDOMNode);
     334begin
     335  WriteString(Node, 'Name', Name);
     336  WriteString(Node, 'URL', URL);
     337  if Assigned(Format) then WriteInteger(Node, 'ImportFormat', Format.Id)
     338    else WriteInteger(Node, 'ImportFormat', -1);
     339end;
     340
     341procedure TImportSource.LoadFromNode(Node: TDOMNode);
     342begin
     343  Name := ReadString(Node, 'Name', '');
     344  URL := ReadString(Node, 'URL', '');
     345  Format := Sources.AcronymDb.ImportFormats.SearchById(ReadInteger(Node, 'ImportFormat', -1));
     346end;
     347
     348constructor TImportSource.Create;
     349begin
     350  Format := nil;
     351end;
     352
     353destructor TImportSource.Destroy;
     354begin
     355  inherited Destroy;
     356end;
     357
    142358{ TAcronymEntry }
    143359
     
    149365destructor TAcronymEntry.Destroy;
    150366begin
    151   Categories.Free;
     367  FreeAndNil(Categories);
    152368  inherited Destroy;
    153369end;
     
    492708  Acronyms.Db := Self;
    493709  Categories := TAcronymCategories.Create;
     710  ImportSources := TImportSources.Create;
     711  ImportSources.AcronymDb := Self;
     712  ImportFormats := TImportFormats.Create;
    494713end;
    495714
    496715destructor TAcronymDb.Destroy;
    497716begin
     717  FreeAndNil(ImportFormats);
     718  FreeAndNil(ImportSources);
    498719  FreeAndNil(Sources);
    499720  FreeAndNil(Acronyms);
     
    527748      if Assigned(NewNode) then
    528749        Acronyms.LoadFromNode(NewNode);
     750
     751      NewNode := FindNode('ImportFormats');
     752      if Assigned(NewNode) then
     753        ImportFormats.LoadFromNode(NewNode);
     754
     755      NewNode := FindNode('ImportSources');
     756      if Assigned(NewNode) then
     757        ImportSources.LoadFromNode(NewNode);
    529758    end;
    530759  finally
     
    557786      AppendChild(NewNode);
    558787      Acronyms.SaveToNode(NewNode);
     788
     789      NewNode := OwnerDocument.CreateElement('ImportFormats');
     790      AppendChild(NewNode);
     791      ImportFormats.SaveToNode(NewNode);
     792
     793      NewNode := OwnerDocument.CreateElement('ImportSources');
     794      AppendChild(NewNode);
     795      ImportSources.SaveToNode(NewNode);
    559796    end;
    560797    ForceDirectories(ExtractFileDir(FileName));
Note: See TracChangeset for help on using the changeset viewer.