Changeset 15 for trunk/UAcronym.pas
- Timestamp:
- Apr 29, 2016, 12:09:44 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UAcronym.pas
r14 r15 6 6 7 7 uses 8 Classes, SysUtils, Contnrs ;8 Classes, SysUtils, Contnrs, XMLConf, XMLRead, XMLWrite, DOM, UXMLUtils; 9 9 10 10 type 11 11 TAcronymCategories = class; 12 12 TAcronymMeanings = class; 13 TAcronymDb = class; 13 14 14 15 TAcronymSource = class … … 24 25 25 26 TAcronym = class 27 Db: TAcronymDb; 26 28 Name: string; 27 29 Meanings: TAcronymMeanings; 30 procedure SaveToNode(Node: TDOMNode); 31 procedure LoadFromNode(Node: TDOMNode); 28 32 constructor Create; 29 33 destructor Destroy; override; 34 end; 35 36 { TAcronyms } 37 38 TAcronyms = class(TObjectList) 39 Db: TAcronymDb; 40 procedure SaveToNode(Node: TDOMNode); 41 procedure LoadFromNode(Node: TDOMNode); 42 function SearchByName(Name: string): TAcronym; 43 function AddAcronym(Name: string): TAcronym; 30 44 end; 31 45 … … 39 53 Categories: TAcronymCategories; 40 54 Source: TAcronymSource; 55 procedure SaveToNode(Node: TDOMNode); 56 procedure LoadFromNode(Node: TDOMNode); 41 57 constructor Create; 42 58 destructor Destroy; override; … … 46 62 47 63 TAcronymMeanings = class(TObjectList) 64 Acronym: TAcronym; 65 procedure SaveToNode(Node: TDOMNode); 66 procedure LoadFromNode(Node: TDOMNode); 48 67 function SearchByName(Name: string): TAcronymMeaning; 49 68 function AddMeaning(Name: string): TAcronymMeaning; 50 69 end; 51 70 52 { TAcronyms }53 54 TAcronyms = class(TObjectList)55 function SearchByName(Name: string): TAcronym;56 function AddAcronym(Name: string): TAcronym;57 end;58 59 71 { TAcronymCategory } 60 72 61 73 TAcronymCategory = class 74 Id: Integer; 62 75 Name: string; 63 76 AcronymMeanings: TAcronymMeanings; 77 procedure SaveToNode(Node: TDOMNode); 78 procedure LoadFromNode(Node: TDOMNode); 64 79 constructor Create; 65 80 destructor Destroy; override; … … 69 84 70 85 TAcronymCategories = class(TObjectList) 86 procedure UpdateIds; 87 procedure SaveToNode(Node: TDOMNode); 88 procedure LoadFromNode(Node: TDOMNode); 71 89 function SearchByName(Name: string): TAcronymCategory; 90 function SearchById(Id: Integer): TAcronymCategory; 72 91 function AddContext(Name: string): TAcronymCategory; 73 92 procedure AssignToStrings(Strings: TStrings); … … 98 117 procedure LoadFromFile(FileName: string); 99 118 procedure SaveToFile(FileName: string); 119 procedure LoadFromFileCSV(FileName: string); 120 procedure SaveToFileCSV(FileName: string); 100 121 procedure FilterList(AName: string; Items: TAcronymMeanings); 101 122 function AddAcronym(AcronymName, MeaningName: string): TAcronymMeaning; … … 105 126 function AcronymComparer(Item1, Item2: Pointer): Integer; 106 127 128 107 129 implementation 130 131 resourcestring 132 SWrongFileFormat = 'Wrong file format'; 108 133 109 134 function AcronymComparer(Item1, Item2: Pointer): Integer; … … 126 151 127 152 { TAcronymMeanings } 153 154 procedure TAcronymMeanings.SaveToNode(Node: TDOMNode); 155 var 156 I: Integer; 157 NewNode2: TDOMNode; 158 begin 159 for I := 0 to Count - 1 do 160 with TAcronymMeaning(Items[I]) do begin 161 NewNode2 := Node.OwnerDocument.CreateElement('Meaning'); 162 Node.AppendChild(NewNode2); 163 SaveToNode(NewNode2); 164 end; 165 end; 166 167 procedure TAcronymMeanings.LoadFromNode(Node: TDOMNode); 168 var 169 Node2: TDOMNode; 170 NewItem: TAcronymMeaning; 171 begin 172 Count := 0; 173 Node2 := Node.FirstChild; 174 while Assigned(Node2) and (Node2.NodeName = 'Meaning') do begin 175 NewItem := TAcronymMeaning.Create; 176 NewItem.Acronym := Acronym; 177 NewItem.LoadFromNode(Node2); 178 Add(NewItem); 179 Node2 := Node2.NextSibling; 180 end; 181 end; 128 182 129 183 function TAcronymMeanings.SearchByName(Name: string): TAcronymMeaning; … … 146 200 { TAcronymMeaning } 147 201 202 procedure TAcronymMeaning.SaveToNode(Node: TDOMNode); 203 var 204 NewNode: TDOMNode; 205 NewNode2: TDOMNode; 206 I: Integer; 207 begin 208 WriteString(Node, 'Name', Name); 209 WriteString(Node, 'Description', Description); 210 WriteString(Node, 'Language', Language); 211 212 NewNode := Node.OwnerDocument.CreateElement('Categories'); 213 Node.AppendChild(NewNode); 214 for I := 0 to Categories.Count - 1 do begin 215 NewNode2 := NewNode.OwnerDocument.CreateElement('Category'); 216 NewNode.AppendChild(NewNode2); 217 WriteInteger(NewNode2, 'Id', TAcronymCategory(Categories[I]).Id); 218 end; 219 end; 220 221 procedure TAcronymMeaning.LoadFromNode(Node: TDOMNode); 222 var 223 Node2: TDOMNode; 224 Node3: TDOMNode; 225 Category: TAcronymCategory; 226 Id: Integer; 227 begin 228 Name := ReadString(Node, 'Name', ''); 229 Description := ReadString(Node, 'Description', ''); 230 Language := ReadString(Node, 'Language', ''); 231 232 Node3 := Node.FindNode('Categories'); 233 if Assigned(Node3) then begin 234 Node2 := Node3.FirstChild; 235 while Assigned(Node2) and (Node2.NodeName = 'Category') do begin 236 Id := ReadInteger(Node2, 'Id', 0); 237 Category := Acronym.Db.Categories.SearchById(Id); 238 if Assigned(Category) then begin 239 Categories.Add(Category); 240 Category.AcronymMeanings.Add(Self); 241 end; 242 Node2 := Node2.NextSibling; 243 end; 244 end; 245 end; 246 148 247 constructor TAcronymMeaning.Create; 149 248 begin … … 163 262 { TAcronyms } 164 263 264 procedure TAcronyms.SaveToNode(Node: TDOMNode); 265 var 266 I: Integer; 267 NewNode2: TDOMNode; 268 begin 269 for I := 0 to Count - 1 do 270 with TAcronym(Items[I]) do begin 271 NewNode2 := Node.OwnerDocument.CreateElement('Acronym'); 272 Node.AppendChild(NewNode2); 273 SaveToNode(NewNode2); 274 end; 275 end; 276 277 procedure TAcronyms.LoadFromNode(Node: TDOMNode); 278 var 279 Node2: TDOMNode; 280 NewItem: TAcronym; 281 begin 282 Count := 0; 283 Node2 := Node.FirstChild; 284 while Assigned(Node2) and (Node2.NodeName = 'Acronym') do begin 285 NewItem := TAcronym.Create; 286 NewItem.Db := Db; 287 NewItem.LoadFromNode(Node2); 288 Add(NewItem); 289 Node2 := Node2.NextSibling; 290 end; 291 end; 292 165 293 function TAcronyms.SearchByName(Name: string): TAcronym; 166 294 var … … 182 310 { TAcronymCategories } 183 311 312 procedure TAcronymCategories.UpdateIds; 313 var 314 LastId: Integer; 315 I: Integer; 316 begin 317 // Get highest used ID 318 LastId := 0; 319 for I := 0 to Count - 1 do begin 320 if TAcronymCategory(Items[I]).Id > LastId then LastId := TAcronymCategory(Items[I]).Id; 321 end; 322 // Add ID to new items without ID 323 for I := 0 to Count - 1 do begin 324 if TAcronymCategory(Items[I]).Id = 0 then begin 325 Inc(LastId); 326 TAcronymCategory(Items[I]).Id := LastId; 327 end; 328 end; 329 end; 330 331 procedure TAcronymCategories.SaveToNode(Node: TDOMNode); 332 var 333 I: Integer; 334 NewNode2: TDOMNode; 335 begin 336 UpdateIds; 337 for I := 0 to Count - 1 do 338 with TAcronymCategory(Items[I]) do begin 339 NewNode2 := Node.OwnerDocument.CreateElement('Category'); 340 Node.AppendChild(NewNode2); 341 SaveToNode(NewNode2); 342 end; 343 end; 344 345 procedure TAcronymCategories.LoadFromNode(Node: TDOMNode); 346 var 347 Node2: TDOMNode; 348 NewItem: TAcronymCategory; 349 begin 350 Count := 0; 351 Node2 := Node.FirstChild; 352 while Assigned(Node2) and (Node2.NodeName = 'Category') do begin 353 NewItem := TAcronymCategory.Create; 354 NewItem.LoadFromNode(Node2); 355 Add(NewItem); 356 Node2 := Node2.NextSibling; 357 end; 358 UpdateIds; 359 end; 360 184 361 function TAcronymCategories.SearchByName(Name: string): TAcronymCategory; 185 362 var … … 192 369 end; 193 370 371 function TAcronymCategories.SearchById(Id: Integer): TAcronymCategory; 372 var 373 I: Integer; 374 begin 375 I := 0; 376 while (I < Count) and (TAcronymCategory(Items[I]).Id <> Id) do Inc(I); 377 if I < Count then Result := TAcronymCategory(Items[I]) 378 else Result := nil; 379 end; 380 194 381 function TAcronymCategories.AddContext(Name: string): TAcronymCategory; 195 382 begin … … 221 408 { TAcronym } 222 409 410 procedure TAcronym.SaveToNode(Node: TDOMNode); 411 var 412 NewNode: TDOMNode; 413 begin 414 WriteString(Node, 'Name', Name); 415 416 NewNode := Node.OwnerDocument.CreateElement('Meanings'); 417 Node.AppendChild(NewNode); 418 Meanings.SaveToNode(NewNode); 419 end; 420 421 procedure TAcronym.LoadFromNode(Node: TDOMNode); 422 var 423 NewNode: TDOMNode; 424 begin 425 Name := ReadString(Node, 'Name', ''); 426 427 NewNode := Node.FindNode('Meanings'); 428 if Assigned(NewNode) then 429 Meanings.LoadFromNode(NewNode); 430 end; 431 223 432 constructor TAcronym.Create; 224 433 begin 225 434 Meanings := TAcronymMeanings.Create; 435 Meanings.Acronym := Self; 226 436 end; 227 437 … … 233 443 234 444 { TAcronymCategory } 445 446 procedure TAcronymCategory.SaveToNode(Node: TDOMNode); 447 begin 448 WriteString(Node, 'Name', Name); 449 WriteInteger(Node, 'Id', Id); 450 end; 451 452 procedure TAcronymCategory.LoadFromNode(Node: TDOMNode); 453 begin 454 Name := ReadString(Node, 'Name', ''); 455 Id := ReadInteger(Node, 'Id', 0); 456 end; 235 457 236 458 constructor TAcronymCategory.Create; … … 255 477 Sources := TAcronymSources.Create; 256 478 Acronyms := TAcronyms.Create; 479 Acronyms.Db := Self; 257 480 Categories := TAcronymCategories.Create; 258 481 end; … … 267 490 268 491 procedure TAcronymDb.LoadFromFile(FileName: string); 492 var 493 NewNode: TDOMNode; 494 Doc: TXMLDocument; 495 RootNode: TDOMNode; 496 begin 497 if ExtractFileExt(FileName) = '.csv' then begin 498 LoadFromFileCSV(FileName); 499 Exit; 500 end; 501 Self.FileName := FileName; 502 ReadXMLFile(Doc, FileName); 503 with Doc do try 504 if Doc.DocumentElement.NodeName <> 'AcronymDecoderProject' then 505 raise Exception.Create(SWrongFileFormat); 506 RootNode := Doc.DocumentElement; 507 with RootNode do begin 508 NewNode := FindNode('Categories'); 509 if Assigned(NewNode) then 510 Categories.LoadFromNode(NewNode); 511 512 // Load acronyms after categories because of references 513 NewNode := FindNode('Acronyms'); 514 if Assigned(NewNode) then 515 Acronyms.LoadFromNode(NewNode); 516 end; 517 finally 518 Doc.Free; 519 end; 520 end; 521 522 procedure TAcronymDb.SaveToFile(FileName: string); 523 var 524 NewNode: TDOMNode; 525 Doc: TXMLDocument; 526 RootNode: TDOMNode; 527 begin 528 if ExtractFileExt(FileName) = '.csv' then begin 529 SaveToFileCSV(FileName); 530 Exit; 531 end; 532 Self.FileName := FileName; 533 Doc := TXMLDocument.Create; 534 with Doc do try 535 RootNode := CreateElement('AcronymDecoderProject'); 536 AppendChild(RootNode); 537 with RootNode do begin 538 NewNode := OwnerDocument.CreateElement('Categories'); 539 AppendChild(NewNode); 540 Categories.SaveToNode(NewNode); 541 542 // Save acronyms after categories because of references 543 NewNode := OwnerDocument.CreateElement('Acronyms'); 544 AppendChild(NewNode); 545 Acronyms.SaveToNode(NewNode); 546 end; 547 ForceDirectories(ExtractFileDir(FileName)); 548 WriteXMLFile(Doc, FileName); 549 finally 550 Doc.Free; 551 end; 552 Modified := False; 553 end; 554 555 procedure TAcronymDb.LoadFromFileCSV(FileName: string); 269 556 var 270 557 F: TStringList; … … 321 608 end; 322 609 323 procedure TAcronymDb.SaveToFile (FileName: string);610 procedure TAcronymDb.SaveToFileCSV(FileName: string); 324 611 var 325 612 I: Integer;
Note:
See TracChangeset
for help on using the changeset viewer.