diff options
| author | Sean Hall <r.sean.hall@gmail.com> | 2022-12-11 20:46:41 -0600 |
|---|---|---|
| committer | Sean Hall <r.sean.hall@gmail.com> | 2022-12-12 21:51:07 -0600 |
| commit | 132cc6ae8de1bae87000a2108e832db520fed038 (patch) | |
| tree | a65dfaf1e3dfc199a6df9e36f68d0d36679dfd66 /src | |
| parent | 50e24e9cf2084b6cb67b5d8fc509163061408bb6 (diff) | |
| download | wix-132cc6ae8de1bae87000a2108e832db520fed038.tar.gz wix-132cc6ae8de1bae87000a2108e832db520fed038.tar.bz2 wix-132cc6ae8de1bae87000a2108e832db520fed038.zip | |
Create ConversionState class for WixConverter to ensure state is reset.
Diffstat (limited to 'src')
| -rw-r--r-- | src/wix/WixToolset.Converters/ConversionState.cs | 49 | ||||
| -rw-r--r-- | src/wix/WixToolset.Converters/WixConverter.cs | 87 |
2 files changed, 93 insertions, 43 deletions
diff --git a/src/wix/WixToolset.Converters/ConversionState.cs b/src/wix/WixToolset.Converters/ConversionState.cs new file mode 100644 index 00000000..cfb065f2 --- /dev/null +++ b/src/wix/WixToolset.Converters/ConversionState.cs | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixToolset.Converters | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.Collections.Generic; | ||
| 7 | using System.Linq; | ||
| 8 | using System.Xml; | ||
| 9 | using System.Xml.Linq; | ||
| 10 | using WixToolset.Data; | ||
| 11 | |||
| 12 | internal enum ConvertOperation | ||
| 13 | { | ||
| 14 | Convert, | ||
| 15 | Format, | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | internal class ConversionState | ||
| 20 | { | ||
| 21 | public ConversionState(ConvertOperation operation, string sourceFile) | ||
| 22 | { | ||
| 23 | this.ConversionMessages = new List<Message>(); | ||
| 24 | this.Operation = operation; | ||
| 25 | this.SourceFile = sourceFile; | ||
| 26 | this.SourceVersion = 0; | ||
| 27 | } | ||
| 28 | |||
| 29 | public List<Message> ConversionMessages { get; } | ||
| 30 | |||
| 31 | public ConvertOperation Operation { get; } | ||
| 32 | |||
| 33 | public string SourceFile { get; } | ||
| 34 | |||
| 35 | public int SourceVersion { get; set; } | ||
| 36 | |||
| 37 | public XDocument XDocument { get; set; } | ||
| 38 | |||
| 39 | public void Initialize() | ||
| 40 | { | ||
| 41 | this.XDocument = XDocument.Load(this.SourceFile, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
| 42 | } | ||
| 43 | |||
| 44 | public void Initialize(XDocument document) | ||
| 45 | { | ||
| 46 | this.XDocument = document; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
diff --git a/src/wix/WixToolset.Converters/WixConverter.cs b/src/wix/WixToolset.Converters/WixConverter.cs index 9359d0d2..133d8876 100644 --- a/src/wix/WixToolset.Converters/WixConverter.cs +++ b/src/wix/WixToolset.Converters/WixConverter.cs | |||
| @@ -42,12 +42,6 @@ namespace WixToolset.Converters | |||
| 42 | /// </summary> | 42 | /// </summary> |
| 43 | public sealed class WixConverter | 43 | public sealed class WixConverter |
| 44 | { | 44 | { |
| 45 | private enum ConvertOperation | ||
| 46 | { | ||
| 47 | Convert, | ||
| 48 | Format, | ||
| 49 | } | ||
| 50 | |||
| 51 | private static readonly Regex AddPrefix = new Regex(@"^[^a-zA-Z_]", RegexOptions.Compiled); | 45 | private static readonly Regex AddPrefix = new Regex(@"^[^a-zA-Z_]", RegexOptions.Compiled); |
| 52 | private static readonly Regex IllegalIdentifierCharacters = new Regex(@"[^A-Za-z0-9_\.]|\.{2,}", RegexOptions.Compiled); // non 'words' and assorted valid characters | 46 | private static readonly Regex IllegalIdentifierCharacters = new Regex(@"[^A-Za-z0-9_\.]|\.{2,}", RegexOptions.Compiled); // non 'words' and assorted valid characters |
| 53 | 47 | ||
| @@ -339,8 +333,6 @@ namespace WixToolset.Converters | |||
| 339 | { WixConverter.WixLocalizationUIElementName, this.ConvertWixLocalizationUIElement}, | 333 | { WixConverter.WixLocalizationUIElementName, this.ConvertWixLocalizationUIElement}, |
| 340 | }; | 334 | }; |
| 341 | 335 | ||
| 342 | this.ConversionMessages = new List<Message>(); | ||
| 343 | |||
| 344 | this.Messaging = messaging; | 336 | this.Messaging = messaging; |
| 345 | 337 | ||
| 346 | this.IndentationAmount = indentationAmount; | 338 | this.IndentationAmount = indentationAmount; |
| @@ -354,7 +346,12 @@ namespace WixToolset.Converters | |||
| 354 | 346 | ||
| 355 | private CustomTableTarget CustomTableSetting { get; } | 347 | private CustomTableTarget CustomTableSetting { get; } |
| 356 | 348 | ||
| 357 | private List<Message> ConversionMessages { get; } | 349 | private List<Message> ConversionMessages |
| 350 | { | ||
| 351 | get { return this.State.ConversionMessages; } | ||
| 352 | } | ||
| 353 | |||
| 354 | private ConversionState State { get; set; } | ||
| 358 | 355 | ||
| 359 | private HashSet<ConverterTestType> ErrorsAsWarnings { get; set; } | 356 | private HashSet<ConverterTestType> ErrorsAsWarnings { get; set; } |
| 360 | 357 | ||
| @@ -364,13 +361,26 @@ namespace WixToolset.Converters | |||
| 364 | 361 | ||
| 365 | private int IndentationAmount { get; set; } | 362 | private int IndentationAmount { get; set; } |
| 366 | 363 | ||
| 367 | private ConvertOperation Operation { get; set; } | 364 | private ConvertOperation Operation |
| 365 | { | ||
| 366 | get { return this.State.Operation; } | ||
| 367 | } | ||
| 368 | 368 | ||
| 369 | private string SourceFile { get; set; } | 369 | private string SourceFile |
| 370 | { | ||
| 371 | get { return this.State.SourceFile; } | ||
| 372 | } | ||
| 370 | 373 | ||
| 371 | private int SourceVersion { get; set; } | 374 | private int SourceVersion |
| 375 | { | ||
| 376 | get { return this.State.SourceVersion; } | ||
| 377 | set { this.State.SourceVersion = value; } | ||
| 378 | } | ||
| 372 | 379 | ||
| 373 | private XElement XRoot { get; set; } | 380 | private XElement XRoot |
| 381 | { | ||
| 382 | get { return this.State.XDocument.Root; } | ||
| 383 | } | ||
| 374 | 384 | ||
| 375 | /// <summary> | 385 | /// <summary> |
| 376 | /// Convert a file. | 386 | /// Convert a file. |
| @@ -382,27 +392,30 @@ namespace WixToolset.Converters | |||
| 382 | { | 392 | { |
| 383 | var savedDocument = false; | 393 | var savedDocument = false; |
| 384 | 394 | ||
| 385 | if (this.TryOpenSourceFile(sourceFile, out var document)) | 395 | if (this.TryOpenSourceFile(ConvertOperation.Convert, sourceFile)) |
| 386 | { | 396 | { |
| 387 | this.Convert(document); | 397 | this.Convert(this.State.XDocument); |
| 388 | 398 | ||
| 389 | // Fix Messages if requested and necessary. | 399 | // Fix Messages if requested and necessary. |
| 390 | if (saveConvertedFile && 0 < this.ConversionMessages.Count) | 400 | if (saveConvertedFile && 0 < this.ConversionMessages.Count) |
| 391 | { | 401 | { |
| 392 | savedDocument = this.SaveDocument(document); | 402 | savedDocument = this.SaveDocument(this.State.XDocument); |
| 393 | } | 403 | } |
| 394 | } | 404 | } |
| 395 | 405 | ||
| 396 | return this.ReportMessages(document, savedDocument); | 406 | return this.ReportMessages(this.State.XDocument, savedDocument); |
| 397 | } | 407 | } |
| 398 | 408 | ||
| 399 | /// <summary> | 409 | /// <summary> |
| 400 | /// Convert a document. | 410 | /// Convert a document. |
| 401 | /// </summary> | 411 | /// </summary> |
| 402 | /// <param name="document">The document to convert.</param> | 412 | /// <param name="document">The document to convert.</param> |
| 413 | /// <param name="sourceFile">The file that the document was loaded from.</param> | ||
| 403 | /// <returns>The number of conversions found.</returns> | 414 | /// <returns>The number of conversions found.</returns> |
| 404 | public int ConvertDocument(XDocument document) | 415 | public int ConvertDocument(XDocument document, string sourceFile = "InMemoryXml") |
| 405 | { | 416 | { |
| 417 | this.State = new ConversionState(ConvertOperation.Convert, sourceFile); | ||
| 418 | this.State.Initialize(document); | ||
| 406 | this.Convert(document); | 419 | this.Convert(document); |
| 407 | 420 | ||
| 408 | return this.ReportMessages(document, false); | 421 | return this.ReportMessages(document, false); |
| @@ -418,27 +431,30 @@ namespace WixToolset.Converters | |||
| 418 | { | 431 | { |
| 419 | var savedDocument = false; | 432 | var savedDocument = false; |
| 420 | 433 | ||
| 421 | if (this.TryOpenSourceFile(sourceFile, out var document)) | 434 | if (this.TryOpenSourceFile(ConvertOperation.Format, sourceFile)) |
| 422 | { | 435 | { |
| 423 | this.FormatDocument(document); | 436 | this.FormatDocument(this.State.XDocument); |
| 424 | 437 | ||
| 425 | // Fix Messages if requested and necessary. | 438 | // Fix Messages if requested and necessary. |
| 426 | if (saveConvertedFile && 0 < this.ConversionMessages.Count) | 439 | if (saveConvertedFile && 0 < this.ConversionMessages.Count) |
| 427 | { | 440 | { |
| 428 | savedDocument = this.SaveDocument(document); | 441 | savedDocument = this.SaveDocument(this.State.XDocument); |
| 429 | } | 442 | } |
| 430 | } | 443 | } |
| 431 | 444 | ||
| 432 | return this.ReportMessages(document, savedDocument); | 445 | return this.ReportMessages(this.State.XDocument, savedDocument); |
| 433 | } | 446 | } |
| 434 | 447 | ||
| 435 | /// <summary> | 448 | /// <summary> |
| 436 | /// Format a document. | 449 | /// Format a document. |
| 437 | /// </summary> | 450 | /// </summary> |
| 438 | /// <param name="document">The document to format.</param> | 451 | /// <param name="document">The document to format.</param> |
| 452 | /// <param name="sourceFile">The file that the document was loaded from.</param> | ||
| 439 | /// <returns>The number of Messages found.</returns> | 453 | /// <returns>The number of Messages found.</returns> |
| 440 | public int FormatDocument(XDocument document) | 454 | public int FormatDocument(XDocument document, string sourceFile = "InMemoryXml") |
| 441 | { | 455 | { |
| 456 | this.State = new ConversionState(ConvertOperation.Format, sourceFile); | ||
| 457 | this.State.Initialize(document); | ||
| 442 | this.Format(document); | 458 | this.Format(document); |
| 443 | 459 | ||
| 444 | return this.ReportMessages(document, false); | 460 | return this.ReportMessages(document, false); |
| @@ -446,11 +462,6 @@ namespace WixToolset.Converters | |||
| 446 | 462 | ||
| 447 | private void Convert(XDocument document) | 463 | private void Convert(XDocument document) |
| 448 | { | 464 | { |
| 449 | // Reset the instance info. | ||
| 450 | this.ConversionMessages.Clear(); | ||
| 451 | this.SourceVersion = 0; | ||
| 452 | this.Operation = ConvertOperation.Convert; | ||
| 453 | |||
| 454 | // Remove the declaration. | 465 | // Remove the declaration. |
| 455 | if (null != document.Declaration | 466 | if (null != document.Declaration |
| 456 | && this.OnInformation(ConverterTestType.DeclarationPresent, document, "This file contains an XML declaration on the first line.")) | 467 | && this.OnInformation(ConverterTestType.DeclarationPresent, document, "This file contains an XML declaration on the first line.")) |
| @@ -459,8 +470,6 @@ namespace WixToolset.Converters | |||
| 459 | TrimLeadingText(document); | 470 | TrimLeadingText(document); |
| 460 | } | 471 | } |
| 461 | 472 | ||
| 462 | this.XRoot = document.Root; | ||
| 463 | |||
| 464 | // Start converting the nodes at the top. | 473 | // Start converting the nodes at the top. |
| 465 | this.ConvertNodes(document.Nodes(), 0); | 474 | this.ConvertNodes(document.Nodes(), 0); |
| 466 | this.RemoveUnusedNamespaces(document.Root); | 475 | this.RemoveUnusedNamespaces(document.Root); |
| @@ -469,11 +478,6 @@ namespace WixToolset.Converters | |||
| 469 | 478 | ||
| 470 | private void Format(XDocument document) | 479 | private void Format(XDocument document) |
| 471 | { | 480 | { |
| 472 | // Reset the instance info. | ||
| 473 | this.ConversionMessages.Clear(); | ||
| 474 | this.SourceVersion = 0; | ||
| 475 | this.Operation = ConvertOperation.Format; | ||
| 476 | |||
| 477 | // Remove the declaration. | 481 | // Remove the declaration. |
| 478 | if (null != document.Declaration | 482 | if (null != document.Declaration |
| 479 | && this.OnInformation(ConverterTestType.DeclarationPresent, document, "This file contains an XML declaration on the first line.")) | 483 | && this.OnInformation(ConverterTestType.DeclarationPresent, document, "This file contains an XML declaration on the first line.")) |
| @@ -482,29 +486,26 @@ namespace WixToolset.Converters | |||
| 482 | TrimLeadingText(document); | 486 | TrimLeadingText(document); |
| 483 | } | 487 | } |
| 484 | 488 | ||
| 485 | this.XRoot = document.Root; | ||
| 486 | |||
| 487 | // Start converting the nodes at the top. | 489 | // Start converting the nodes at the top. |
| 488 | this.ConvertNodes(document.Nodes(), 0); | 490 | this.ConvertNodes(document.Nodes(), 0); |
| 489 | this.RemoveUnusedNamespaces(document.Root); | 491 | this.RemoveUnusedNamespaces(document.Root); |
| 490 | this.MoveNamespacesToRoot(document.Root); | 492 | this.MoveNamespacesToRoot(document.Root); |
| 491 | } | 493 | } |
| 492 | 494 | ||
| 493 | private bool TryOpenSourceFile(string sourceFile, out XDocument document) | 495 | private bool TryOpenSourceFile(ConvertOperation operation, string sourceFile) |
| 494 | { | 496 | { |
| 495 | this.SourceFile = sourceFile; | 497 | this.State = new ConversionState(operation, sourceFile); |
| 496 | 498 | ||
| 497 | try | 499 | try |
| 498 | { | 500 | { |
| 499 | document = XDocument.Load(this.SourceFile, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | 501 | this.State.Initialize(); |
| 502 | return true; | ||
| 500 | } | 503 | } |
| 501 | catch (XmlException e) | 504 | catch (XmlException e) |
| 502 | { | 505 | { |
| 503 | this.OnError(ConverterTestType.XmlException, null, "The xml is invalid. Detail: '{0}'", e.Message); | 506 | this.OnError(ConverterTestType.XmlException, null, "The xml is invalid. Detail: '{0}'", e.Message); |
| 504 | document = null; | 507 | return false; |
| 505 | } | 508 | } |
| 506 | |||
| 507 | return document != null; | ||
| 508 | } | 509 | } |
| 509 | 510 | ||
| 510 | private bool SaveDocument(XDocument document) | 511 | private bool SaveDocument(XDocument document) |
