Changeset 20 for trunk/UAcronym.pas
- Timestamp:
- May 4, 2016, 6:50:29 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UAcronym.pas
r17 r20 13 13 TAcronymMeanings = class; 14 14 TAcronymDb = class; 15 TImportSources = class; 15 16 16 17 TAcronymSource = class … … 107 108 end; 108 109 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 109 161 { TAcronymDb } 110 162 … … 114 166 Acronyms: TAcronyms; 115 167 Categories: TAcronymCategories; 168 ImportSources: TImportSources; 169 ImportFormats: TImportFormats; 116 170 Modified: Boolean; 117 171 constructor Create; … … 140 194 end; 141 195 196 { TImportFormat } 197 198 procedure TImportFormat.SaveToNode(Node: TDOMNode); 199 begin 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); 208 end; 209 210 procedure TImportFormat.LoadFromNode(Node: TDOMNode); 211 begin 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', ''); 220 end; 221 222 { TImportSources } 223 224 function TImportSources.SearchByName(Name: string): TImportSource; 225 var 226 I: Integer; 227 begin 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; 232 end; 233 234 procedure TImportSources.SaveToNode(Node: TDOMNode); 235 var 236 I: Integer; 237 NewNode2: TDOMNode; 238 begin 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; 245 end; 246 247 procedure TImportSources.LoadFromNode(Node: TDOMNode); 248 var 249 Node2: TDOMNode; 250 NewItem: TImportSource; 251 begin 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; 261 end; 262 263 { TImportFormats } 264 265 procedure TImportFormats.UpdateIds; 266 var 267 LastId: Integer; 268 I: Integer; 269 begin 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; 282 end; 283 284 procedure TImportFormats.SaveToNode(Node: TDOMNode); 285 var 286 I: Integer; 287 NewNode2: TDOMNode; 288 begin 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; 296 end; 297 298 procedure TImportFormats.LoadFromNode(Node: TDOMNode); 299 var 300 Node2: TDOMNode; 301 NewItem: TImportFormat; 302 begin 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; 312 end; 313 314 function TImportFormats.SearchById(Id: Integer): TImportFormat; 315 var 316 I: Integer; 317 begin 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; 322 end; 323 324 { TImportSource } 325 326 procedure TImportSource.Assign(Source: TImportSource); 327 begin 328 Name := Source.Name; 329 URL := Source.URL; 330 Format := Source.Format; 331 end; 332 333 procedure TImportSource.SaveToNode(Node: TDOMNode); 334 begin 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); 339 end; 340 341 procedure TImportSource.LoadFromNode(Node: TDOMNode); 342 begin 343 Name := ReadString(Node, 'Name', ''); 344 URL := ReadString(Node, 'URL', ''); 345 Format := Sources.AcronymDb.ImportFormats.SearchById(ReadInteger(Node, 'ImportFormat', -1)); 346 end; 347 348 constructor TImportSource.Create; 349 begin 350 Format := nil; 351 end; 352 353 destructor TImportSource.Destroy; 354 begin 355 inherited Destroy; 356 end; 357 142 358 { TAcronymEntry } 143 359 … … 149 365 destructor TAcronymEntry.Destroy; 150 366 begin 151 Categories.Free;367 FreeAndNil(Categories); 152 368 inherited Destroy; 153 369 end; … … 492 708 Acronyms.Db := Self; 493 709 Categories := TAcronymCategories.Create; 710 ImportSources := TImportSources.Create; 711 ImportSources.AcronymDb := Self; 712 ImportFormats := TImportFormats.Create; 494 713 end; 495 714 496 715 destructor TAcronymDb.Destroy; 497 716 begin 717 FreeAndNil(ImportFormats); 718 FreeAndNil(ImportSources); 498 719 FreeAndNil(Sources); 499 720 FreeAndNil(Acronyms); … … 527 748 if Assigned(NewNode) then 528 749 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); 529 758 end; 530 759 finally … … 557 786 AppendChild(NewNode); 558 787 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); 559 796 end; 560 797 ForceDirectories(ExtractFileDir(FileName));
Note:
See TracChangeset
for help on using the changeset viewer.