diff options
| author | Rob Mensching <rob@firegiant.com> | 2020-06-23 16:43:11 -0700 |
|---|---|---|
| committer | Rob Mensching <rob@firegiant.com> | 2020-06-23 16:56:04 -0700 |
| commit | acba90f005779fa03ee05c3c87148bb6141ba60e (patch) | |
| tree | 97fb00a86df2a92e8346ebd2f4a239df7d2f166d /src/WixToolset.Converters | |
| parent | 9c235551b2fb961cf10ecb307c252fd5de377513 (diff) | |
| download | wix-acba90f005779fa03ee05c3c87148bb6141ba60e.tar.gz wix-acba90f005779fa03ee05c3c87148bb6141ba60e.tar.bz2 wix-acba90f005779fa03ee05c3c87148bb6141ba60e.zip | |
Do *not* require XML declaration
Diffstat (limited to 'src/WixToolset.Converters')
| -rw-r--r-- | src/WixToolset.Converters/Wix3Converter.cs | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/src/WixToolset.Converters/Wix3Converter.cs b/src/WixToolset.Converters/Wix3Converter.cs index 2d603c4f..8c7d2049 100644 --- a/src/WixToolset.Converters/Wix3Converter.cs +++ b/src/WixToolset.Converters/Wix3Converter.cs | |||
| @@ -203,9 +203,9 @@ namespace WixToolset.Converters | |||
| 203 | { | 203 | { |
| 204 | try | 204 | try |
| 205 | { | 205 | { |
| 206 | using (var writer = File.CreateText(this.SourceFile)) | 206 | using (var writer = XmlWriter.Create(this.SourceFile, new XmlWriterSettings { OmitXmlDeclaration = true })) |
| 207 | { | 207 | { |
| 208 | document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces); | 208 | document.Save(writer); |
| 209 | } | 209 | } |
| 210 | } | 210 | } |
| 211 | catch (UnauthorizedAccessException) | 211 | catch (UnauthorizedAccessException) |
| @@ -228,26 +228,17 @@ namespace WixToolset.Converters | |||
| 228 | 228 | ||
| 229 | var declaration = document.Declaration; | 229 | var declaration = document.Declaration; |
| 230 | 230 | ||
| 231 | // Convert the declaration. | 231 | // Remove the declaration. |
| 232 | if (null != declaration) | 232 | if (null != declaration) |
| 233 | { | 233 | { |
| 234 | if (!String.Equals("utf-8", declaration.Encoding, StringComparison.OrdinalIgnoreCase)) | 234 | if (this.OnError(ConverterTestType.DeclarationPresent, null, "This file contains an XML declaration on the first line.")) |
| 235 | { | 235 | { |
| 236 | if (this.OnError(ConverterTestType.DeclarationEncodingWrong, document.Root, "The XML declaration encoding is not properly set to 'utf-8'.")) | 236 | document.Declaration = null; |
| 237 | { | ||
| 238 | declaration.Encoding = "utf-8"; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | else // missing declaration | ||
| 243 | { | ||
| 244 | if (this.OnError(ConverterTestType.DeclarationMissing, null, "This file is missing an XML declaration on the first line.")) | ||
| 245 | { | ||
| 246 | document.Declaration = new XDeclaration("1.0", "utf-8", null); | ||
| 247 | document.Root.AddBeforeSelf(new XText(XDocumentNewLine.ToString())); | ||
| 248 | } | 237 | } |
| 249 | } | 238 | } |
| 250 | 239 | ||
| 240 | TrimLeadingText(document); | ||
| 241 | |||
| 251 | // Start converting the nodes at the top. | 242 | // Start converting the nodes at the top. |
| 252 | this.ConvertNodes(document.Nodes(), 0); | 243 | this.ConvertNodes(document.Nodes(), 0); |
| 253 | 244 | ||
| @@ -903,6 +894,26 @@ namespace WixToolset.Converters | |||
| 903 | return !String.IsNullOrEmpty(value); | 894 | return !String.IsNullOrEmpty(value); |
| 904 | } | 895 | } |
| 905 | 896 | ||
| 897 | private static bool IsTextNode(XNode node, out XText text) | ||
| 898 | { | ||
| 899 | text = null; | ||
| 900 | |||
| 901 | if (node.NodeType == XmlNodeType.Text || node.NodeType == XmlNodeType.CDATA) | ||
| 902 | { | ||
| 903 | text = (XText)node; | ||
| 904 | } | ||
| 905 | |||
| 906 | return text != null; | ||
| 907 | } | ||
| 908 | |||
| 909 | private static void TrimLeadingText(XDocument document) | ||
| 910 | { | ||
| 911 | while (IsTextNode(document.Nodes().FirstOrDefault(), out var text)) | ||
| 912 | { | ||
| 913 | text.Remove(); | ||
| 914 | } | ||
| 915 | } | ||
| 916 | |||
| 906 | private static string TrimTextValue(XText text) | 917 | private static string TrimTextValue(XText text) |
| 907 | { | 918 | { |
| 908 | var value = text.Value; | 919 | var value = text.Value; |
| @@ -1052,6 +1063,11 @@ namespace WixToolset.Converters | |||
| 1052 | /// Explicit auto-GUID unnecessary. | 1063 | /// Explicit auto-GUID unnecessary. |
| 1053 | /// </summary> | 1064 | /// </summary> |
| 1054 | AutoGuidUnnecessary, | 1065 | AutoGuidUnnecessary, |
| 1066 | |||
| 1067 | /// <summary> | ||
| 1068 | /// Displayed when the XML declaration is present in the source file. | ||
| 1069 | /// </summary> | ||
| 1070 | DeclarationPresent, | ||
| 1055 | } | 1071 | } |
| 1056 | } | 1072 | } |
| 1057 | } | 1073 | } |
