Create ConversionState class for WixConverter to ensure state is reset.
Sean Hall committed
Dec 11, 2022 at 20:46 UTC
132cc6ae8de1bae87000a2108e832db520fed038
2 files changed
+93
-43
src/wix/WixToolset.Converters/ConversionState.cs
new
+49
@@ -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
+}
src/wix/WixToolset.Converters/WixConverter.cs
+44
-43
@@ -42,12 +42,6 @@ namespace WixToolset.Converters
42
/// </summary>
43
public sealed class WixConverter
44
{
45
- private enum ConvertOperation
46
- {
47
- Convert,
48
- Format,
49
- }
50
-
45
private static readonly Regex AddPrefix = new Regex(@"^[^a-zA-Z_]", RegexOptions.Compiled);
46
private static readonly Regex IllegalIdentifierCharacters = new Regex(@"[^A-Za-z0-9_\.]|\.{2,}", RegexOptions.Compiled); // non 'words' and assorted valid characters
47
@@ -339,8 +333,6 @@ namespace WixToolset.Converters
333
{ WixConverter.WixLocalizationUIElementName, this.ConvertWixLocalizationUIElement},
334
};
335
342
- this.ConversionMessages = new List<Message>();
343
-
336
this.Messaging = messaging;
337
338
this.IndentationAmount = indentationAmount;
@@ -354,7 +346,12 @@ namespace WixToolset.Converters
346
347
private CustomTableTarget CustomTableSetting { get; }
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; }
355
356
private HashSet<ConverterTestType> ErrorsAsWarnings { get; set; }
357
@@ -364,13 +361,26 @@ namespace WixToolset.Converters
361
362
private int IndentationAmount { get; set; }
363
367
- private ConvertOperation Operation { get; set; }
364
+ private ConvertOperation Operation
365
+ {
366
+ get { return this.State.Operation; }
367
+ }
368
369
- private string SourceFile { get; set; }
369
+ private string SourceFile
370
+ {
371
+ get { return this.State.SourceFile; }
372
+ }
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
+ }
379
373
- private XElement XRoot { get; set; }
380
+ private XElement XRoot
381
+ {
382
+ get { return this.State.XDocument.Root; }
383
+ }
384
385
/// <summary>
386
/// Convert a file.
@@ -382,27 +392,30 @@ namespace WixToolset.Converters
392
{
393
var savedDocument = false;
394
385
- if (this.TryOpenSourceFile(sourceFile, out var document))
395
+ if (this.TryOpenSourceFile(ConvertOperation.Convert, sourceFile))
396
{
387
- this.Convert(document);
397
+ this.Convert(this.State.XDocument);
398
399
// Fix Messages if requested and necessary.
400
if (saveConvertedFile && 0 < this.ConversionMessages.Count)
401
{
392
- savedDocument = this.SaveDocument(document);
402
+ savedDocument = this.SaveDocument(this.State.XDocument);
403
}
404
}
405
396
- return this.ReportMessages(document, savedDocument);
406
+ return this.ReportMessages(this.State.XDocument, savedDocument);
407
}
408
409
/// <summary>
410
/// Convert a document.
411
/// </summary>
412
/// <param name="document">The document to convert.</param>
413
+ /// <param name="sourceFile">The file that the document was loaded from.</param>
414
/// <returns>The number of conversions found.</returns>
404
- public int ConvertDocument(XDocument document)
415
+ public int ConvertDocument(XDocument document, string sourceFile = "InMemoryXml")
416
{
417
+ this.State = new ConversionState(ConvertOperation.Convert, sourceFile);
418
+ this.State.Initialize(document);
419
this.Convert(document);
420
421
return this.ReportMessages(document, false);
@@ -418,27 +431,30 @@ namespace WixToolset.Converters
431
{
432
var savedDocument = false;
433
421
- if (this.TryOpenSourceFile(sourceFile, out var document))
434
+ if (this.TryOpenSourceFile(ConvertOperation.Format, sourceFile))
435
{
423
- this.FormatDocument(document);
436
+ this.FormatDocument(this.State.XDocument);
437
438
// Fix Messages if requested and necessary.
439
if (saveConvertedFile && 0 < this.ConversionMessages.Count)
440
{
428
- savedDocument = this.SaveDocument(document);
441
+ savedDocument = this.SaveDocument(this.State.XDocument);
442
}
443
}
444
432
- return this.ReportMessages(document, savedDocument);
445
+ return this.ReportMessages(this.State.XDocument, savedDocument);
446
}
447
448
/// <summary>
449
/// Format a document.
450
/// </summary>
451
/// <param name="document">The document to format.</param>
452
+ /// <param name="sourceFile">The file that the document was loaded from.</param>
453
/// <returns>The number of Messages found.</returns>
440
- public int FormatDocument(XDocument document)
454
+ public int FormatDocument(XDocument document, string sourceFile = "InMemoryXml")
455
{
456
+ this.State = new ConversionState(ConvertOperation.Format, sourceFile);
457
+ this.State.Initialize(document);
458
this.Format(document);
459
460
return this.ReportMessages(document, false);
@@ -446,11 +462,6 @@ namespace WixToolset.Converters
462
463
private void Convert(XDocument document)
464
{
449
- // Reset the instance info.
450
- this.ConversionMessages.Clear();
451
- this.SourceVersion = 0;
452
- this.Operation = ConvertOperation.Convert;
453
-
465
// Remove the declaration.
466
if (null != document.Declaration
467
&& this.OnInformation(ConverterTestType.DeclarationPresent, document, "This file contains an XML declaration on the first line."))
@@ -459,8 +470,6 @@ namespace WixToolset.Converters
470
TrimLeadingText(document);
471
}
472
462
- this.XRoot = document.Root;
463
-
473
// Start converting the nodes at the top.
474
this.ConvertNodes(document.Nodes(), 0);
475
this.RemoveUnusedNamespaces(document.Root);
@@ -469,11 +478,6 @@ namespace WixToolset.Converters
478
479
private void Format(XDocument document)
480
{
472
- // Reset the instance info.
473
- this.ConversionMessages.Clear();
474
- this.SourceVersion = 0;
475
- this.Operation = ConvertOperation.Format;
476
-
481
// Remove the declaration.
482
if (null != document.Declaration
483
&& this.OnInformation(ConverterTestType.DeclarationPresent, document, "This file contains an XML declaration on the first line."))
@@ -482,29 +486,26 @@ namespace WixToolset.Converters
486
TrimLeadingText(document);
487
}
488
485
- this.XRoot = document.Root;
486
-
489
// Start converting the nodes at the top.
490
this.ConvertNodes(document.Nodes(), 0);
491
this.RemoveUnusedNamespaces(document.Root);
492
this.MoveNamespacesToRoot(document.Root);
493
}
494
493
- private bool TryOpenSourceFile(string sourceFile, out XDocument document)
495
+ private bool TryOpenSourceFile(ConvertOperation operation, string sourceFile)
496
{
495
- this.SourceFile = sourceFile;
497
+ this.State = new ConversionState(operation, sourceFile);
498
499
try
500
{
499
- document = XDocument.Load(this.SourceFile, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
501
+ this.State.Initialize();
502
+ return true;
503
}
504
catch (XmlException e)
505
{
506
this.OnError(ConverterTestType.XmlException, null, "The xml is invalid. Detail: '{0}'", e.Message);
504
- document = null;
507
+ return false;
508
}
506
-
507
- return document != null;
509
}
510
511
private bool SaveDocument(XDocument document)