Changeset 22 for trunk/UAcronym.pas


Ignore:
Timestamp:
May 4, 2016, 11:43:42 PM (8 years ago)
Author:
chronos
Message:
  • Added: Now import formats can be specified using new edit windows.
  • Added: Import source can now be downloaded and processed including download from https URLs.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UAcronym.pas

    r20 r22  
    77uses
    88  Classes, SysUtils, Contnrs, XMLConf, XMLRead, XMLWrite, DOM, UXMLUtils,
    9   SpecializedList;
     9  SpecializedList, fphttpclient;
    1010
    1111type
     
    1414  TAcronymDb = class;
    1515  TImportSources = class;
     16  TImportFormats = class;
    1617
    1718  TAcronymSource = class
     
    122123    Meaning: TImportPattern;
    123124    Description: TImportPattern;
     125    Formats: TImportFormats;
     126    procedure Assign(Source: TImportFormat);
    124127    procedure SaveToNode(Node: TDOMNode);
    125128    procedure LoadFromNode(Node: TDOMNode);
     
    132135    procedure SaveToNode(Node: TDOMNode);
    133136    procedure LoadFromNode(Node: TDOMNode);
     137    function SearchByName(Name: string): TImportFormat;
    134138    function SearchById(Id: Integer): TImportFormat;
    135139  end;
     
    143147    LastTime: TDateTime;
    144148    Sources: TImportSources;
     149    procedure Process;
    145150    procedure Assign(Source: TImportSource);
    146151    procedure SaveToNode(Node: TDOMNode);
     
    194199end;
    195200
     201function DownloadHTTP(URL: string; Stream: TStream): Boolean;
     202var
     203  HTTPClient: TFPHTTPClient;
     204begin
     205  HTTPClient := TFPHttpClient.Create(nil);
     206  HTTPClient.Get(URL, Stream);
     207  HTTPClient.Free;
     208  Result := True;
     209end;
     210
     211function StripHTML(S: string): string;
     212var
     213  TagBegin, TagEnd, TagLength: Integer;
     214begin
     215  TagBegin := Pos( '<', S);      // search position of first <
     216
     217  while (TagBegin > 0) do begin  // while there is a < in S
     218    TagEnd := Pos('>', S);              // find the matching >
     219    TagLength := TagEnd - TagBegin + 1;
     220    Delete(S, TagBegin, TagLength);     // delete the tag
     221    TagBegin:= Pos( '<', S);            // search for next <
     222  end;
     223
     224  Result := S;                   // give the result
     225end;
     226
    196227{ TImportFormat }
     228
     229procedure TImportFormat.Assign(Source: TImportFormat);
     230begin
     231  Name := Source.Name;
     232  Acronym.StartString := Source.Acronym.StartString;
     233  Acronym.EndString := Source.Acronym.EndString;
     234  Meaning.StartString := Source.Meaning.StartString;
     235  Meaning.EndString := Source.Meaning.EndString;
     236  Description.StartString := Source.Description.StartString;
     237  Description.EndString := Source.Description.EndString;
     238end;
    197239
    198240procedure TImportFormat.SaveToNode(Node: TDOMNode);
     
    263305{ TImportFormats }
    264306
     307function TImportFormats.SearchByName(Name: string): TImportFormat;
     308var
     309  I: Integer;
     310begin
     311  I := 0;
     312  while (I < Count) and (TImportFormat(Items[I]).Name <> Name) do Inc(I);
     313  if I < Count then Result := TImportFormat(Items[I])
     314    else Result := nil;
     315end;
     316
    265317procedure TImportFormats.UpdateIds;
    266318var
     
    323375
    324376{ TImportSource }
     377
     378procedure TImportSource.Process;
     379var
     380  Stream: TMemoryStream;
     381  S: string;
     382  NewAcronym: TAcronymEntry;
     383  P: Integer;
     384begin
     385  Stream := TMemoryStream.Create;
     386  NewAcronym := TAcronymEntry.Create;
     387  try
     388    if DownloadHTTP(URL, Stream) then begin
     389      Stream.Position := 0;
     390      SetLength(S, Stream.Size);
     391      Stream.Read(S[1], Length(S));
     392      repeat
     393        P := Pos(Format.Acronym.StartString, S);
     394        if P > 0 then begin
     395          // Acronym
     396          Delete(S, 1, P + Length(Format.Acronym.StartString) - 1);
     397          P := Pos(Format.Acronym.EndString, S);
     398          NewAcronym.Name := Trim(StripHTML(Copy(S, 1, P - 1)));
     399          Delete(S, 1, P + Length(Format.Acronym.EndString) - 1);
     400
     401          // Meaning
     402          P := Pos(Format.Meaning.StartString, S);
     403          Delete(S, 1, P + Length(Format.Meaning.StartString) - 1);
     404          P := Pos(Format.Meaning.EndString, S);
     405          NewAcronym.Meaning := Trim(StripHTML(Copy(S, 1, P - 1)));
     406          Delete(S, 1, P + Length(Format.Meaning.EndString) - 1);
     407          Sources.AcronymDb.AddAcronym(NewAcronym.Name, NewAcronym.Meaning);
     408        end else Break;
     409      until False;
     410    end;
     411  finally
     412    NewAcronym.Free;
     413    Stream.Free;
     414  end;
     415end;
    325416
    326417procedure TImportSource.Assign(Source: TImportSource);
Note: See TracChangeset for help on using the changeset viewer.