Changeset 22 for trunk/UAcronym.pas
- Timestamp:
- May 4, 2016, 11:43:42 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UAcronym.pas
r20 r22 7 7 uses 8 8 Classes, SysUtils, Contnrs, XMLConf, XMLRead, XMLWrite, DOM, UXMLUtils, 9 SpecializedList ;9 SpecializedList, fphttpclient; 10 10 11 11 type … … 14 14 TAcronymDb = class; 15 15 TImportSources = class; 16 TImportFormats = class; 16 17 17 18 TAcronymSource = class … … 122 123 Meaning: TImportPattern; 123 124 Description: TImportPattern; 125 Formats: TImportFormats; 126 procedure Assign(Source: TImportFormat); 124 127 procedure SaveToNode(Node: TDOMNode); 125 128 procedure LoadFromNode(Node: TDOMNode); … … 132 135 procedure SaveToNode(Node: TDOMNode); 133 136 procedure LoadFromNode(Node: TDOMNode); 137 function SearchByName(Name: string): TImportFormat; 134 138 function SearchById(Id: Integer): TImportFormat; 135 139 end; … … 143 147 LastTime: TDateTime; 144 148 Sources: TImportSources; 149 procedure Process; 145 150 procedure Assign(Source: TImportSource); 146 151 procedure SaveToNode(Node: TDOMNode); … … 194 199 end; 195 200 201 function DownloadHTTP(URL: string; Stream: TStream): Boolean; 202 var 203 HTTPClient: TFPHTTPClient; 204 begin 205 HTTPClient := TFPHttpClient.Create(nil); 206 HTTPClient.Get(URL, Stream); 207 HTTPClient.Free; 208 Result := True; 209 end; 210 211 function StripHTML(S: string): string; 212 var 213 TagBegin, TagEnd, TagLength: Integer; 214 begin 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 225 end; 226 196 227 { TImportFormat } 228 229 procedure TImportFormat.Assign(Source: TImportFormat); 230 begin 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; 238 end; 197 239 198 240 procedure TImportFormat.SaveToNode(Node: TDOMNode); … … 263 305 { TImportFormats } 264 306 307 function TImportFormats.SearchByName(Name: string): TImportFormat; 308 var 309 I: Integer; 310 begin 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; 315 end; 316 265 317 procedure TImportFormats.UpdateIds; 266 318 var … … 323 375 324 376 { TImportSource } 377 378 procedure TImportSource.Process; 379 var 380 Stream: TMemoryStream; 381 S: string; 382 NewAcronym: TAcronymEntry; 383 P: Integer; 384 begin 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; 415 end; 325 416 326 417 procedure TImportSource.Assign(Source: TImportSource);
Note:
See TracChangeset
for help on using the changeset viewer.