diff options
Diffstat (limited to 'src/wixcop/Converter.cs')
-rw-r--r-- | src/wixcop/Converter.cs | 56 |
1 files changed, 25 insertions, 31 deletions
diff --git a/src/wixcop/Converter.cs b/src/wixcop/Converter.cs index a204ebe0..7e8486ab 100644 --- a/src/wixcop/Converter.cs +++ b/src/wixcop/Converter.cs | |||
@@ -1,6 +1,6 @@ | |||
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. | 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 | 2 | ||
3 | namespace WixCop | 3 | namespace WixToolset.Tools.WixCop |
4 | { | 4 | { |
5 | using System; | 5 | using System; |
6 | using System.Collections.Generic; | 6 | using System.Collections.Generic; |
@@ -141,7 +141,7 @@ namespace WixCop | |||
141 | { | 141 | { |
142 | try | 142 | try |
143 | { | 143 | { |
144 | using (StreamWriter writer = File.CreateText(this.SourceFile)) | 144 | using (var writer = File.CreateText(this.SourceFile)) |
145 | { | 145 | { |
146 | document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces); | 146 | document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces); |
147 | } | 147 | } |
@@ -162,7 +162,7 @@ namespace WixCop | |||
162 | /// <returns>The number of errors found.</returns> | 162 | /// <returns>The number of errors found.</returns> |
163 | public int ConvertDocument(XDocument document) | 163 | public int ConvertDocument(XDocument document) |
164 | { | 164 | { |
165 | XDeclaration declaration = document.Declaration; | 165 | var declaration = document.Declaration; |
166 | 166 | ||
167 | // Convert the declaration. | 167 | // Convert the declaration. |
168 | if (null != declaration) | 168 | if (null != declaration) |
@@ -206,7 +206,7 @@ namespace WixCop | |||
206 | } | 206 | } |
207 | 207 | ||
208 | // Convert this node if it is an element. | 208 | // Convert this node if it is an element. |
209 | XElement element = node as XElement; | 209 | var element = node as XElement; |
210 | 210 | ||
211 | if (null != element) | 211 | if (null != element) |
212 | { | 212 | { |
@@ -215,7 +215,7 @@ namespace WixCop | |||
215 | // Convert all children of this element. | 215 | // Convert all children of this element. |
216 | IEnumerable<XNode> children = element.Nodes().ToList(); | 216 | IEnumerable<XNode> children = element.Nodes().ToList(); |
217 | 217 | ||
218 | foreach (XNode child in children) | 218 | foreach (var child in children) |
219 | { | 219 | { |
220 | this.ConvertNode(child, level + 1); | 220 | this.ConvertNode(child, level + 1); |
221 | } | 221 | } |
@@ -225,9 +225,9 @@ namespace WixCop | |||
225 | private void ConvertElement(XElement element) | 225 | private void ConvertElement(XElement element) |
226 | { | 226 | { |
227 | // Gather any deprecated namespaces, then update this element tree based on those deprecations. | 227 | // Gather any deprecated namespaces, then update this element tree based on those deprecations. |
228 | Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces = new Dictionary<XNamespace, XNamespace>(); | 228 | var deprecatedToUpdatedNamespaces = new Dictionary<XNamespace, XNamespace>(); |
229 | 229 | ||
230 | foreach (XAttribute declaration in element.Attributes().Where(a => a.IsNamespaceDeclaration)) | 230 | foreach (var declaration in element.Attributes().Where(a => a.IsNamespaceDeclaration)) |
231 | { | 231 | { |
232 | XNamespace ns; | 232 | XNamespace ns; |
233 | 233 | ||
@@ -258,7 +258,7 @@ namespace WixCop | |||
258 | { | 258 | { |
259 | if (null == element.Attribute("Id")) | 259 | if (null == element.Attribute("Id")) |
260 | { | 260 | { |
261 | XAttribute attribute = element.Attribute("Name"); | 261 | var attribute = element.Attribute("Name"); |
262 | 262 | ||
263 | if (null == attribute) | 263 | if (null == attribute) |
264 | { | 264 | { |
@@ -267,7 +267,7 @@ namespace WixCop | |||
267 | 267 | ||
268 | if (null != attribute) | 268 | if (null != attribute) |
269 | { | 269 | { |
270 | string name = Path.GetFileName(attribute.Value); | 270 | var name = Path.GetFileName(attribute.Value); |
271 | 271 | ||
272 | if (this.OnError(ConverterTestType.AssignAnonymousFileId, element, "The file id is being updated to '{0}' to ensure it remains the same as the default", name)) | 272 | if (this.OnError(ConverterTestType.AssignAnonymousFileId, element, "The file id is being updated to '{0}' to ensure it remains the same as the default", name)) |
273 | { | 273 | { |
@@ -282,7 +282,7 @@ namespace WixCop | |||
282 | 282 | ||
283 | private void ConvertSuppressSignatureValidation(XElement element) | 283 | private void ConvertSuppressSignatureValidation(XElement element) |
284 | { | 284 | { |
285 | XAttribute suppressSignatureValidation = element.Attribute("SuppressSignatureValidation"); | 285 | var suppressSignatureValidation = element.Attribute("SuppressSignatureValidation"); |
286 | 286 | ||
287 | if (null != suppressSignatureValidation) | 287 | if (null != suppressSignatureValidation) |
288 | { | 288 | { |
@@ -311,7 +311,7 @@ namespace WixCop | |||
311 | 311 | ||
312 | element.Add(new XAttribute("xmlns", WixNamespace.NamespaceName)); // set the default namespace. | 312 | element.Add(new XAttribute("xmlns", WixNamespace.NamespaceName)); // set the default namespace. |
313 | 313 | ||
314 | foreach (XElement elementWithoutNamespace in element.Elements().Where(e => XNamespace.None == e.Name.Namespace)) | 314 | foreach (var elementWithoutNamespace in element.Elements().Where(e => XNamespace.None == e.Name.Namespace)) |
315 | { | 315 | { |
316 | elementWithoutNamespace.Name = WixNamespace.GetName(elementWithoutNamespace.Name.LocalName); | 316 | elementWithoutNamespace.Name = WixNamespace.GetName(elementWithoutNamespace.Name.LocalName); |
317 | } | 317 | } |
@@ -326,9 +326,7 @@ namespace WixCop | |||
326 | private void ConvertWhitespace(XNode node, int level) | 326 | private void ConvertWhitespace(XNode node, int level) |
327 | { | 327 | { |
328 | // Fix the whitespace before this node. | 328 | // Fix the whitespace before this node. |
329 | XText whitespace = node.PreviousNode as XText; | 329 | if (node.PreviousNode is XText whitespace) |
330 | |||
331 | if (null != whitespace) | ||
332 | { | 330 | { |
333 | if (XmlNodeType.CDATA == node.NodeType) | 331 | if (XmlNodeType.CDATA == node.NodeType) |
334 | { | 332 | { |
@@ -351,9 +349,7 @@ namespace WixCop | |||
351 | } | 349 | } |
352 | 350 | ||
353 | // Fix the whitespace after CDATA nodes. | 351 | // Fix the whitespace after CDATA nodes. |
354 | XCData cdata = node as XCData; | 352 | if (node is XCData cdata) |
355 | |||
356 | if (null != cdata) | ||
357 | { | 353 | { |
358 | whitespace = cdata.NextNode as XText; | 354 | whitespace = cdata.NextNode as XText; |
359 | 355 | ||
@@ -368,9 +364,7 @@ namespace WixCop | |||
368 | else | 364 | else |
369 | { | 365 | { |
370 | // Fix the whitespace inside and after this node (except for Error which may contain just whitespace). | 366 | // Fix the whitespace inside and after this node (except for Error which may contain just whitespace). |
371 | XElement element = node as XElement; | 367 | if (node is XElement element && "Error" != element.Name.LocalName) |
372 | |||
373 | if (null != element && "Error" != element.Name.LocalName) | ||
374 | { | 368 | { |
375 | if (!element.HasElements && !element.IsEmpty && String.IsNullOrEmpty(element.Value.Trim())) | 369 | if (!element.HasElements && !element.IsEmpty && String.IsNullOrEmpty(element.Value.Trim())) |
376 | { | 370 | { |
@@ -403,7 +397,7 @@ namespace WixCop | |||
403 | { | 397 | { |
404 | if (null != types) | 398 | if (null != types) |
405 | { | 399 | { |
406 | foreach (string type in types) | 400 | foreach (var type in types) |
407 | { | 401 | { |
408 | ConverterTestType itt; | 402 | ConverterTestType itt; |
409 | 403 | ||
@@ -421,7 +415,7 @@ namespace WixCop | |||
421 | 415 | ||
422 | private static void UpdateElementsWithDeprecatedNamespaces(IEnumerable<XElement> elements, Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces) | 416 | private static void UpdateElementsWithDeprecatedNamespaces(IEnumerable<XElement> elements, Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces) |
423 | { | 417 | { |
424 | foreach (XElement element in elements) | 418 | foreach (var element in elements) |
425 | { | 419 | { |
426 | XNamespace ns; | 420 | XNamespace ns; |
427 | 421 | ||
@@ -434,9 +428,9 @@ namespace WixCop | |||
434 | IEnumerable<XAttribute> attributes = element.Attributes().ToList(); | 428 | IEnumerable<XAttribute> attributes = element.Attributes().ToList(); |
435 | element.RemoveAttributes(); | 429 | element.RemoveAttributes(); |
436 | 430 | ||
437 | foreach (XAttribute attribute in attributes) | 431 | foreach (var attribute in attributes) |
438 | { | 432 | { |
439 | XAttribute convertedAttribute = attribute; | 433 | var convertedAttribute = attribute; |
440 | 434 | ||
441 | if (attribute.IsNamespaceDeclaration) | 435 | if (attribute.IsNamespaceDeclaration) |
442 | { | 436 | { |
@@ -477,7 +471,7 @@ namespace WixCop | |||
477 | } | 471 | } |
478 | 472 | ||
479 | // check the spaces | 473 | // check the spaces |
480 | foreach (char character in whitespace) | 474 | foreach (var character in whitespace) |
481 | { | 475 | { |
482 | if (' ' != character) | 476 | if (' ' != character) |
483 | { | 477 | { |
@@ -496,9 +490,9 @@ namespace WixCop | |||
496 | /// <param name="whitespace">The whitespace node to fix.</param> | 490 | /// <param name="whitespace">The whitespace node to fix.</param> |
497 | private static void FixWhitespace(int indentationAmount, int level, XText whitespace) | 491 | private static void FixWhitespace(int indentationAmount, int level, XText whitespace) |
498 | { | 492 | { |
499 | int newLineCount = 0; | 493 | var newLineCount = 0; |
500 | 494 | ||
501 | for (int i = 0; i + 1 < whitespace.Value.Length; ++i) | 495 | for (var i = 0; i + 1 < whitespace.Value.Length; ++i) |
502 | { | 496 | { |
503 | if (XDocumentNewLine == whitespace.Value.Substring(i, 2)) | 497 | if (XDocumentNewLine == whitespace.Value.Substring(i, 2)) |
504 | { | 498 | { |
@@ -516,7 +510,7 @@ namespace WixCop | |||
516 | whitespace.Value = String.Empty; | 510 | whitespace.Value = String.Empty; |
517 | 511 | ||
518 | // add the correct number of newlines | 512 | // add the correct number of newlines |
519 | for (int i = 0; i < newLineCount; ++i) | 513 | for (var i = 0; i < newLineCount; ++i) |
520 | { | 514 | { |
521 | whitespace.Value = String.Concat(whitespace.Value, XDocumentNewLine); | 515 | whitespace.Value = String.Concat(whitespace.Value, XDocumentNewLine); |
522 | } | 516 | } |
@@ -543,9 +537,9 @@ namespace WixCop | |||
543 | // Increase the error count. | 537 | // Increase the error count. |
544 | this.Errors++; | 538 | this.Errors++; |
545 | 539 | ||
546 | SourceLineNumber sourceLine = (null == node) ? new SourceLineNumber(this.SourceFile ?? "wixcop.exe") : new SourceLineNumber(this.SourceFile, ((IXmlLineInfo)node).LineNumber); | 540 | var sourceLine = (null == node) ? new SourceLineNumber(this.SourceFile ?? "wixcop.exe") : new SourceLineNumber(this.SourceFile, ((IXmlLineInfo)node).LineNumber); |
547 | bool warning = this.ErrorsAsWarnings.Contains(converterTestType); | 541 | var warning = this.ErrorsAsWarnings.Contains(converterTestType); |
548 | string display = String.Format(CultureInfo.CurrentCulture, message, args); | 542 | var display = String.Format(CultureInfo.CurrentCulture, message, args); |
549 | 543 | ||
550 | var msg = new Message(sourceLine, warning ? MessageLevel.Warning : MessageLevel.Error, (int)converterTestType, "{0} ({1})", display, converterTestType.ToString()); | 544 | var msg = new Message(sourceLine, warning ? MessageLevel.Warning : MessageLevel.Error, (int)converterTestType, "{0} ({1})", display, converterTestType.ToString()); |
551 | 545 | ||