1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
// 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.
namespace WixToolset.Tools.WixCop
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using WixToolset.Data;
using WixToolset.Extensibility.Services;
using WixToolset.Tools.Core;
/// <summary>
/// WiX source code converter.
/// </summary>
public class Converter
{
private const char XDocumentNewLine = '\n'; // XDocument normalizes "\r\n" to just "\n".
private static readonly XNamespace WixNamespace = "http://wixtoolset.org/schemas/v4/wxs";
private static readonly XName FileElementName = WixNamespace + "File";
private static readonly XName ExePackageElementName = WixNamespace + "ExePackage";
private static readonly XName MsiPackageElementName = WixNamespace + "MsiPackage";
private static readonly XName MspPackageElementName = WixNamespace + "MspPackage";
private static readonly XName MsuPackageElementName = WixNamespace + "MsuPackage";
private static readonly XName PayloadElementName = WixNamespace + "Payload";
private static readonly XName WixElementWithoutNamespaceName = XNamespace.None + "Wix";
private static readonly Dictionary<string, XNamespace> OldToNewNamespaceMapping = new Dictionary<string, XNamespace>()
{
{ "http://schemas.microsoft.com/wix/BalExtension", "http://wixtoolset.org/schemas/v4/wxs/bal" },
{ "http://schemas.microsoft.com/wix/ComPlusExtension", "http://wixtoolset.org/schemas/v4/wxs/complus" },
{ "http://schemas.microsoft.com/wix/DependencyExtension", "http://wixtoolset.org/schemas/v4/wxs/dependency" },
{ "http://schemas.microsoft.com/wix/DifxAppExtension", "http://wixtoolset.org/schemas/v4/wxs/difxapp" },
{ "http://schemas.microsoft.com/wix/FirewallExtension", "http://wixtoolset.org/schemas/v4/wxs/firewall" },
{ "http://schemas.microsoft.com/wix/GamingExtension", "http://wixtoolset.org/schemas/v4/wxs/gaming" },
{ "http://schemas.microsoft.com/wix/IIsExtension", "http://wixtoolset.org/schemas/v4/wxs/iis" },
{ "http://schemas.microsoft.com/wix/MsmqExtension", "http://wixtoolset.org/schemas/v4/wxs/msmq" },
{ "http://schemas.microsoft.com/wix/NetFxExtension", "http://wixtoolset.org/schemas/v4/wxs/netfx" },
{ "http://schemas.microsoft.com/wix/PSExtension", "http://wixtoolset.org/schemas/v4/wxs/powershell" },
{ "http://schemas.microsoft.com/wix/SqlExtension", "http://wixtoolset.org/schemas/v4/wxs/sql" },
{ "http://schemas.microsoft.com/wix/TagExtension", "http://wixtoolset.org/schemas/v4/wxs/tag" },
{ "http://schemas.microsoft.com/wix/UtilExtension", "http://wixtoolset.org/schemas/v4/wxs/util" },
{ "http://schemas.microsoft.com/wix/VSExtension", "http://wixtoolset.org/schemas/v4/wxs/vs" },
{ "http://wixtoolset.org/schemas/thmutil/2010", "http://wixtoolset.org/schemas/v4/thmutil" },
{ "http://schemas.microsoft.com/wix/2009/Lux", "http://wixtoolset.org/schemas/v4/lux" },
{ "http://schemas.microsoft.com/wix/2006/wi", "http://wixtoolset.org/schemas/v4/wxs" },
{ "http://schemas.microsoft.com/wix/2006/localization", "http://wixtoolset.org/schemas/v4/wxl" },
{ "http://schemas.microsoft.com/wix/2006/libraries", "http://wixtoolset.org/schemas/v4/wixlib" },
{ "http://schemas.microsoft.com/wix/2006/objects", "http://wixtoolset.org/schemas/v4/wixobj" },
{ "http://schemas.microsoft.com/wix/2006/outputs", "http://wixtoolset.org/schemas/v4/wixout" },
{ "http://schemas.microsoft.com/wix/2007/pdbs", "http://wixtoolset.org/schemas/v4/wixpdb" },
{ "http://schemas.microsoft.com/wix/2003/04/actions", "http://wixtoolset.org/schemas/v4/wi/actions" },
{ "http://schemas.microsoft.com/wix/2006/tables", "http://wixtoolset.org/schemas/v4/wi/tables" },
{ "http://schemas.microsoft.com/wix/2006/WixUnit", "http://wixtoolset.org/schemas/v4/wixunit" },
};
private readonly Dictionary<XName, Action<XElement>> ConvertElementMapping;
/// <summary>
/// Instantiate a new Converter class.
/// </summary>
/// <param name="indentationAmount">Indentation value to use when validating leading whitespace.</param>
/// <param name="errorsAsWarnings">Test errors to display as warnings.</param>
/// <param name="ignoreErrors">Test errors to ignore.</param>
public Converter(IMessaging messaging, int indentationAmount, IEnumerable<string> errorsAsWarnings = null, IEnumerable<string> ignoreErrors = null)
{
// workaround IDE0009 bug
/*this.ConvertElementMapping = new Dictionary<XName, Action<XElement>>()
{
{ Converter.FileElementName, this.ConvertFileElement },
{ Converter.ExePackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.MsiPackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.MspPackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.MsuPackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.PayloadElementName, this.ConvertSuppressSignatureValidation },
{ Converter.WixElementWithoutNamespaceName, this.ConvertWixElementWithoutNamespace },
};*/
this.ConvertElementMapping = new Dictionary<XName, Action<XElement>>
{
{ Converter.FileElementName, this.ConvertFileElement },
{ Converter.ExePackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.MsiPackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.MspPackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.MsuPackageElementName, this.ConvertSuppressSignatureValidation },
{ Converter.PayloadElementName, this.ConvertSuppressSignatureValidation },
{ Converter.WixElementWithoutNamespaceName, this.ConvertWixElementWithoutNamespace }
};
this.Messaging = messaging;
this.IndentationAmount = indentationAmount;
this.ErrorsAsWarnings = new HashSet<ConverterTestType>(this.YieldConverterTypes(errorsAsWarnings));
this.IgnoreErrors = new HashSet<ConverterTestType>(this.YieldConverterTypes(ignoreErrors));
}
private int Errors { get; set; }
private HashSet<ConverterTestType> ErrorsAsWarnings { get; set; }
private HashSet<ConverterTestType> IgnoreErrors { get; set; }
private IMessaging Messaging { get; }
private int IndentationAmount { get; set; }
private string SourceFile { get; set; }
/// <summary>
/// Convert a file.
/// </summary>
/// <param name="sourceFile">The file to convert.</param>
/// <param name="saveConvertedFile">Option to save the converted errors that are found.</param>
/// <returns>The number of errors found.</returns>
public int ConvertFile(string sourceFile, bool saveConvertedFile)
{
XDocument document;
// Set the instance info.
this.Errors = 0;
this.SourceFile = sourceFile;
try
{
document = XDocument.Load(this.SourceFile, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
}
catch (XmlException e)
{
this.OnError(ConverterTestType.XmlException, null, "The xml is invalid. Detail: '{0}'", e.Message);
return this.Errors;
}
this.ConvertDocument(document);
// Fix errors if requested and necessary.
if (saveConvertedFile && 0 < this.Errors)
{
try
{
using (var writer = File.CreateText(this.SourceFile))
{
document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces);
}
}
catch (UnauthorizedAccessException)
{
this.OnError(ConverterTestType.UnauthorizedAccessException, null, "Could not write to file.");
}
}
return this.Errors;
}
/// <summary>
/// Convert a document.
/// </summary>
/// <param name="document">The document to convert.</param>
/// <returns>The number of errors found.</returns>
public int ConvertDocument(XDocument document)
{
var declaration = document.Declaration;
// Convert the declaration.
if (null != declaration)
{
if (!String.Equals("utf-8", declaration.Encoding, StringComparison.OrdinalIgnoreCase))
{
if (this.OnError(ConverterTestType.DeclarationEncodingWrong, document.Root, "The XML declaration encoding is not properly set to 'utf-8'."))
{
declaration.Encoding = "utf-8";
}
}
}
else // missing declaration
{
if (this.OnError(ConverterTestType.DeclarationMissing, null, "This file is missing an XML declaration on the first line."))
{
document.Declaration = new XDeclaration("1.0", "utf-8", null);
document.Root.AddBeforeSelf(new XText(XDocumentNewLine.ToString()));
}
}
// Start converting the nodes at the top.
this.ConvertNodes(document.Nodes(), 0);
return this.Errors;
}
private void ConvertNodes(IEnumerable<XNode> nodes, int level)
{
// Note we operate on a copy of the node list since we may
// remove some whitespace nodes during this processing.
foreach (var node in nodes.ToList())
{
if (node is XText text)
{
if (!String.IsNullOrWhiteSpace(text.Value))
{
text.Value = text.Value.Trim();
}
else if (node.NextNode is XCData cdata)
{
this.EnsurePrecedingWhitespaceRemoved(text, node, ConverterTestType.WhitespacePrecedingNodeWrong);
}
else if (node.NextNode is XElement element)
{
this.EnsurePrecedingWhitespaceCorrect(text, node, level, ConverterTestType.WhitespacePrecedingNodeWrong);
}
else if (node.NextNode is null) // this is the space before the close element
{
if (node.PreviousNode is null || node.PreviousNode is XCData)
{
this.EnsurePrecedingWhitespaceRemoved(text, node.Parent, ConverterTestType.WhitespacePrecedingEndElementWrong);
}
else if (level == 0) // root element's close tag
{
this.EnsurePrecedingWhitespaceCorrect(text, node, 0, ConverterTestType.WhitespacePrecedingEndElementWrong);
}
else
{
this.EnsurePrecedingWhitespaceCorrect(text, node, level - 1, ConverterTestType.WhitespacePrecedingEndElementWrong);
}
}
}
else if (node is XElement element)
{
this.ConvertElement(element);
this.ConvertNodes(element.Nodes(), level + 1);
}
}
}
private void EnsurePrecedingWhitespaceCorrect(XText whitespace, XNode node, int level, ConverterTestType testType)
{
if (!Converter.LeadingWhitespaceValid(this.IndentationAmount, level, whitespace.Value))
{
var message = testType == ConverterTestType.WhitespacePrecedingEndElementWrong ? "The whitespace preceding this end element is incorrect." : "The whitespace preceding this node is incorrect.";
if (this.OnError(testType, node, message))
{
Converter.FixupWhitespace(this.IndentationAmount, level, whitespace);
}
}
}
private void EnsurePrecedingWhitespaceRemoved(XText whitespace, XNode node, ConverterTestType testType)
{
if (!String.IsNullOrEmpty(whitespace.Value))
{
var message = testType == ConverterTestType.WhitespacePrecedingEndElementWrong ? "The whitespace preceding this end element is incorrect." : "The whitespace preceding this node is incorrect.";
if (this.OnError(testType, node, message))
{
whitespace.Remove();
}
}
}
private void ConvertElement(XElement element)
{
// Gather any deprecated namespaces, then update this element tree based on those deprecations.
var deprecatedToUpdatedNamespaces = new Dictionary<XNamespace, XNamespace>();
foreach (var declaration in element.Attributes().Where(a => a.IsNamespaceDeclaration))
{
if (Converter.OldToNewNamespaceMapping.TryGetValue(declaration.Value, out var ns))
{
if (this.OnError(ConverterTestType.XmlnsValueWrong, declaration, "The namespace '{0}' is out of date. It must be '{1}'.", declaration.Value, ns.NamespaceName))
{
deprecatedToUpdatedNamespaces.Add(declaration.Value, ns);
}
}
}
if (deprecatedToUpdatedNamespaces.Any())
{
Converter.UpdateElementsWithDeprecatedNamespaces(element.DescendantsAndSelf(), deprecatedToUpdatedNamespaces);
}
// Apply any specialized conversion actions.
if (this.ConvertElementMapping.TryGetValue(element.Name, out var convert))
{
convert(element);
}
}
private void ConvertFileElement(XElement element)
{
if (null == element.Attribute("Id"))
{
var attribute = element.Attribute("Name");
if (null == attribute)
{
attribute = element.Attribute("Source");
}
if (null != attribute)
{
var name = Path.GetFileName(attribute.Value);
if (this.OnError(ConverterTestType.AssignAnonymousFileId, element, "The file id is being updated to '{0}' to ensure it remains the same as the default", name))
{
IEnumerable<XAttribute> attributes = element.Attributes().ToList();
element.RemoveAttributes();
element.Add(new XAttribute("Id", ToolsCommon.GetIdentifierFromName(name)));
element.Add(attributes);
}
}
}
}
private void ConvertSuppressSignatureValidation(XElement element)
{
var suppressSignatureValidation = element.Attribute("SuppressSignatureValidation");
if (null != suppressSignatureValidation)
{
if (this.OnError(ConverterTestType.SuppressSignatureValidationDeprecated, element, "The chain package element contains deprecated '{0}' attribute. Use the 'EnableSignatureValidation' instead.", suppressSignatureValidation))
{
if ("no" == suppressSignatureValidation.Value)
{
element.Add(new XAttribute("EnableSignatureValidation", "yes"));
}
}
suppressSignatureValidation.Remove();
}
}
/// <summary>
/// Converts a Wix element.
/// </summary>
/// <param name="element">The Wix element to convert.</param>
/// <returns>The converted element.</returns>
private void ConvertWixElementWithoutNamespace(XElement element)
{
if (this.OnError(ConverterTestType.XmlnsMissing, element, "The xmlns attribute is missing. It must be present with a value of '{0}'.", WixNamespace.NamespaceName))
{
element.Name = WixNamespace.GetName(element.Name.LocalName);
element.Add(new XAttribute("xmlns", WixNamespace.NamespaceName)); // set the default namespace.
foreach (var elementWithoutNamespace in element.Elements().Where(e => XNamespace.None == e.Name.Namespace))
{
elementWithoutNamespace.Name = WixNamespace.GetName(elementWithoutNamespace.Name.LocalName);
}
}
}
private IEnumerable<ConverterTestType> YieldConverterTypes(IEnumerable<string> types)
{
if (null != types)
{
foreach (var type in types)
{
if (Enum.TryParse<ConverterTestType>(type, true, out var itt))
{
yield return itt;
}
else // not a known ConverterTestType
{
this.OnError(ConverterTestType.ConverterTestTypeUnknown, null, "Unknown error type: '{0}'.", type);
}
}
}
}
private static void UpdateElementsWithDeprecatedNamespaces(IEnumerable<XElement> elements, Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces)
{
foreach (var element in elements)
{
if (deprecatedToUpdatedNamespaces.TryGetValue(element.Name.Namespace, out var ns))
{
element.Name = ns.GetName(element.Name.LocalName);
}
// Remove all the attributes and add them back to with their namespace updated (as necessary).
IEnumerable<XAttribute> attributes = element.Attributes().ToList();
element.RemoveAttributes();
foreach (var attribute in attributes)
{
var convertedAttribute = attribute;
if (attribute.IsNamespaceDeclaration)
{
if (deprecatedToUpdatedNamespaces.TryGetValue(attribute.Value, out ns))
{
convertedAttribute = ("xmlns" == attribute.Name.LocalName) ? new XAttribute(attribute.Name.LocalName, ns.NamespaceName) : new XAttribute(XNamespace.Xmlns + attribute.Name.LocalName, ns.NamespaceName);
}
}
else if (deprecatedToUpdatedNamespaces.TryGetValue(attribute.Name.Namespace, out ns))
{
convertedAttribute = new XAttribute(ns.GetName(attribute.Name.LocalName), attribute.Value);
}
element.Add(convertedAttribute);
}
}
}
/// <summary>
/// Determine if the whitespace preceding a node is appropriate for its depth level.
/// </summary>
/// <param name="indentationAmount">Indentation value to use when validating leading whitespace.</param>
/// <param name="level">The depth level that should match this whitespace.</param>
/// <param name="whitespace">The whitespace to validate.</param>
/// <returns>true if the whitespace is legal; false otherwise.</returns>
private static bool LeadingWhitespaceValid(int indentationAmount, int level, string whitespace)
{
// Strip off leading newlines; there can be an arbitrary number of these.
whitespace = whitespace.TrimStart(XDocumentNewLine);
var indentation = new string(' ', level * indentationAmount);
return whitespace == indentation;
}
/// <summary>
/// Fix the whitespace in a whitespace node.
/// </summary>
/// <param name="indentationAmount">Indentation value to use when validating leading whitespace.</param>
/// <param name="level">The depth level of the desired whitespace.</param>
/// <param name="whitespace">The whitespace node to fix.</param>
private static void FixupWhitespace(int indentationAmount, int level, XText whitespace)
{
var value = new StringBuilder(whitespace.Value.Length);
// Keep any previous preceeding new lines.
var newlines = whitespace.Value.TakeWhile(c => c == XDocumentNewLine).Count();
// Ensure there is always at least one new line before the indentation.
value.Append(XDocumentNewLine, newlines == 0 ? 1 : newlines);
whitespace.Value = value.Append(' ', level * indentationAmount).ToString();
}
/// <summary>
/// Output an error message to the console.
/// </summary>
/// <param name="converterTestType">The type of converter test.</param>
/// <param name="node">The node that caused the error.</param>
/// <param name="message">Detailed error message.</param>
/// <param name="args">Additional formatted string arguments.</param>
/// <returns>Returns true indicating that action should be taken on this error, and false if it should be ignored.</returns>
private bool OnError(ConverterTestType converterTestType, XObject node, string message, params object[] args)
{
if (this.IgnoreErrors.Contains(converterTestType)) // ignore the error
{
return false;
}
// Increase the error count.
this.Errors++;
var sourceLine = (null == node) ? new SourceLineNumber(this.SourceFile ?? "wixcop.exe") : new SourceLineNumber(this.SourceFile, ((IXmlLineInfo)node).LineNumber);
var warning = this.ErrorsAsWarnings.Contains(converterTestType);
var display = String.Format(CultureInfo.CurrentCulture, message, args);
var msg = new Message(sourceLine, warning ? MessageLevel.Warning : MessageLevel.Error, (int)converterTestType, "{0} ({1})", display, converterTestType.ToString());
this.Messaging.Write(msg);
return true;
}
/// <summary>
/// Converter test types. These are used to condition error messages down to warnings.
/// </summary>
private enum ConverterTestType
{
/// <summary>
/// Internal-only: displayed when a string cannot be converted to an ConverterTestType.
/// </summary>
ConverterTestTypeUnknown,
/// <summary>
/// Displayed when an XML loading exception has occurred.
/// </summary>
XmlException,
/// <summary>
/// Displayed when a file cannot be accessed; typically when trying to save back a fixed file.
/// </summary>
UnauthorizedAccessException,
/// <summary>
/// Displayed when the encoding attribute in the XML declaration is not 'UTF-8'.
/// </summary>
DeclarationEncodingWrong,
/// <summary>
/// Displayed when the XML declaration is missing from the source file.
/// </summary>
DeclarationMissing,
/// <summary>
/// Displayed when the whitespace preceding a CDATA node is wrong.
/// </summary>
WhitespacePrecedingCDATAWrong,
/// <summary>
/// Displayed when the whitespace preceding a node is wrong.
/// </summary>
WhitespacePrecedingNodeWrong,
/// <summary>
/// Displayed when an element is not empty as it should be.
/// </summary>
NotEmptyElement,
/// <summary>
/// Displayed when the whitespace following a CDATA node is wrong.
/// </summary>
WhitespaceFollowingCDATAWrong,
/// <summary>
/// Displayed when the whitespace preceding an end element is wrong.
/// </summary>
WhitespacePrecedingEndElementWrong,
/// <summary>
/// Displayed when the xmlns attribute is missing from the document element.
/// </summary>
XmlnsMissing,
/// <summary>
/// Displayed when the xmlns attribute on the document element is wrong.
/// </summary>
XmlnsValueWrong,
/// <summary>
/// Assign an identifier to a File element when on Id attribute is specified.
/// </summary>
AssignAnonymousFileId,
/// <summary>
/// SuppressSignatureValidation attribute is deprecated and replaced with EnableSignatureValidation.
/// </summary>
SuppressSignatureValidationDeprecated,
}
}
}
|