From c122209d47ae055050dee3af2b83220914030b08 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 19 Mar 2022 11:47:16 -0700 Subject: Convert .wxl files namespaces Fixes 6639 --- src/wix/WixToolset.Converters/WixConverter.cs | 31 ++++++- .../LocalizationFixture.cs | 96 ++++++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/wix/test/WixToolsetTest.Converters/LocalizationFixture.cs (limited to 'src') diff --git a/src/wix/WixToolset.Converters/WixConverter.cs b/src/wix/WixToolset.Converters/WixConverter.cs index c009042d..b1215ffc 100644 --- a/src/wix/WixToolset.Converters/WixConverter.cs +++ b/src/wix/WixToolset.Converters/WixConverter.cs @@ -60,6 +60,8 @@ namespace WixToolset.Converters private static readonly XNamespace WixUiNamespace = "http://wixtoolset.org/schemas/v4/wxs/ui"; private static readonly XNamespace WixUtilNamespace = "http://wixtoolset.org/schemas/v4/wxs/util"; private static readonly XNamespace WixVSNamespace = "http://wixtoolset.org/schemas/v4/wxs/vs"; + private static readonly XNamespace WxlNamespace = "http://wixtoolset.org/schemas/v4/wxl"; + private static readonly XNamespace Wxl3Namespace = "http://schemas.microsoft.com/wix/2006/localization"; private static readonly XName AdminExecuteSequenceElementName = WixNamespace + "AdminExecuteSequence"; private static readonly XName AdminUISequenceSequenceElementName = WixNamespace + "AdminUISequence"; @@ -158,6 +160,10 @@ namespace WixToolset.Converters private static readonly XName DependencyCheckAttributeName = WixDependencyNamespace + "Check"; private static readonly XName DependencyEnforceAttributeName = WixDependencyNamespace + "Enforce"; + private static readonly XName WixLocalization4ElementName = WxlNamespace + "WixLocalization"; + private static readonly XName WixLocalization3ElementName = Wxl3Namespace + "WixLocalization"; + private static readonly XName WixLocalizationElementWithoutNamespaceName = XNamespace.None + "WixLocalization"; + private static readonly Dictionary OldToNewNamespaceMapping = new Dictionary() { { "http://schemas.microsoft.com/wix/BalExtension", "http://wixtoolset.org/schemas/v4/wxs/bal" }, @@ -263,6 +269,7 @@ namespace WixToolset.Converters { WixConverter.IncludeElementWithoutNamespaceName, this.ConvertElementWithoutNamespace }, { WixConverter.VerbElementName, this.ConvertVerbElement }, { WixConverter.UIRefElementName, this.ConvertUIRefElement }, + { WixConverter.WixLocalizationElementWithoutNamespaceName, this.ConvertWixLocalizationElementWithoutNamespace }, }; this.Messaging = messaging; @@ -551,11 +558,11 @@ namespace WixToolset.Converters // Gather any deprecated namespaces, then update this element tree based on those deprecations. var declaration = attribute; - if (element.Name == Wix3ElementName || element.Name == Include3ElementName) + if (element.Name == Wix3ElementName || element.Name == Include3ElementName || element.Name == WixLocalization3ElementName) { this.SourceVersion = 3; } - else if (element.Name == Wix4ElementName || element.Name == Include4ElementName) + else if (element.Name == Wix4ElementName || element.Name == Include4ElementName || element.Name == WixLocalization4ElementName) { this.SourceVersion = 4; } @@ -1866,6 +1873,26 @@ namespace WixToolset.Converters } } + /// + /// Converts a WixLocalization element. + /// + /// The WixLocalization element to convert. + /// The converted element. + private void ConvertWixLocalizationElementWithoutNamespace(XElement element) + { + if (this.OnInformation(ConverterTestType.XmlnsMissing, element, "The xmlns attribute is missing. It must be present with a value of '{0}'.", WxlNamespace.NamespaceName)) + { + element.Name = WxlNamespace.GetName(element.Name.LocalName); + + element.Add(new XAttribute("xmlns", WxlNamespace.NamespaceName)); // set the default namespace. + + foreach (var elementWithoutNamespace in element.DescendantsAndSelf().Where(e => XNamespace.None == e.Name.Namespace)) + { + elementWithoutNamespace.Name = WxlNamespace.GetName(elementWithoutNamespace.Name.LocalName); + } + } + } + private void ConvertInnerTextToAttribute(XElement element, string attributeName) { if (TryGetInnerText(element, out var text) && diff --git a/src/wix/test/WixToolsetTest.Converters/LocalizationFixture.cs b/src/wix/test/WixToolsetTest.Converters/LocalizationFixture.cs new file mode 100644 index 00000000..37fe58c7 --- /dev/null +++ b/src/wix/test/WixToolsetTest.Converters/LocalizationFixture.cs @@ -0,0 +1,96 @@ +// 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 WixToolsetTest.Converters +{ + using System; + using System.Xml.Linq; + using WixBuildTools.TestSupport; + using WixToolset.Converters; + using WixToolsetTest.Converters.Mocks; + using Xunit; + + public class LocalizationFixture : BaseConverterFixture + { + [Fact] + public void EnsureNoXmlDeclaration() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " Value", + ""); + + var expected = new[] + { + "", + " Value", + "" + }; + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new MockMessaging(); + var converter = new WixConverter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + Assert.Equal(2, errors); + + var actualLines = UnformattedDocumentLines(document); + WixAssert.CompareLineByLine(expected, actualLines); + } + + [Fact] + public void EnsureNamespace() + { + var parse = String.Join(Environment.NewLine, + "", + " Value", + ""); + + var expected = new[] + { + "", + " Value", + "" + }; + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new MockMessaging(); + var converter = new WixConverter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + Assert.Equal(1, errors); + + var actualLines = UnformattedDocumentLines(document); + WixAssert.CompareLineByLine(expected, actualLines); + } + + [Fact] + public void FixNamespace() + { + var parse = String.Join(Environment.NewLine, + "", + " Value", + ""); + + var expected = new[] + { + "", + " Value", + "" + }; + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new MockMessaging(); + var converter = new WixConverter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + Assert.Equal(1, errors); + + var actualLines = UnformattedDocumentLines(document); + WixAssert.CompareLineByLine(expected, actualLines); + } + } +} -- cgit v1.2.3-55-g6feb