aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2019-10-26 13:35:15 -0700
committerRob Mensching <rob@firegiant.com>2019-10-26 13:37:47 -0700
commit94da334adaafa4444fb278991aa01cce58d9f758 (patch)
tree1f9938c6579004d48abcea02ebb8a210c44af118 /src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs
parent6c1ae2593faab59e1a01c96794e0835a6fcd0626 (diff)
downloadwix-94da334adaafa4444fb278991aa01cce58d9f758.tar.gz
wix-94da334adaafa4444fb278991aa01cce58d9f758.tar.bz2
wix-94da334adaafa4444fb278991aa01cce58d9f758.zip
Rename Output to WindowsInstallerData
Diffstat (limited to 'src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs')
-rw-r--r--src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs264
1 files changed, 264 insertions, 0 deletions
diff --git a/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs b/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs
new file mode 100644
index 00000000..0855997c
--- /dev/null
+++ b/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs
@@ -0,0 +1,264 @@
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
3namespace WixToolset.Data.WindowsInstaller
4{
5 using System;
6 using System.Collections.Generic;
7 using System.Globalization;
8 using System.Linq;
9 using System.Xml;
10
11 /// <summary>
12 /// Output is generated by the linker.
13 /// </summary>
14 public sealed class WindowsInstallerData
15 {
16 internal const string XmlNamespaceUri = "http://wixtoolset.org/schemas/v4/windowsinstallerdata";
17 internal const string XmlElementName = "windowsInstallerData";
18
19 private static readonly Version CurrentVersion = new Version("4.0.0.0");
20 private const string WixOutputStreamName = "wix-wid.xml";
21
22 /// <summary>
23 /// Creates a new empty output object.
24 /// </summary>
25 /// <param name="sourceLineNumbers">The source line information for the output.</param>
26 public WindowsInstallerData(SourceLineNumber sourceLineNumbers)
27 {
28 this.SourceLineNumbers = sourceLineNumbers;
29 this.SubStorages = new List<SubStorage>();
30 this.Tables = new TableIndexedCollection();
31 }
32
33 /// <summary>
34 /// Gets the type of the output.
35 /// </summary>
36 /// <value>Type of the output.</value>
37 public OutputType Type { get; set; }
38
39 /// <summary>
40 /// Gets or sets the codepage for this output.
41 /// </summary>
42 /// <value>Codepage of the output.</value>
43 public int Codepage { get; set; }
44
45 /// <summary>
46 /// Gets the source line information for this output.
47 /// </summary>
48 /// <value>The source line information for this output.</value>
49 public SourceLineNumber SourceLineNumbers { get; private set; }
50
51 /// <summary>
52 /// Gets the substorages in this output.
53 /// </summary>
54 /// <value>The substorages in this output.</value>
55 public ICollection<SubStorage> SubStorages { get; private set; }
56
57 /// <summary>
58 /// Gets the tables contained in this output.
59 /// </summary>
60 /// <value>Collection of tables.</value>
61 public TableIndexedCollection Tables { get; private set; }
62
63 /// <summary>
64 /// Loads an output from a path on disk.
65 /// </summary>
66 /// <param name="path">Path to output file saved on disk.</param>
67 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
68 /// <returns>Output object.</returns>
69 public static WindowsInstallerData Load(string path, bool suppressVersionCheck)
70 {
71 using (var wixout = WixOutput.Read(path))
72 using (var stream = wixout.GetDataStream(WixOutputStreamName))
73 using (var reader = XmlReader.Create(stream, null, wixout.Uri.AbsoluteUri))
74 {
75 try
76 {
77 reader.MoveToContent();
78 return WindowsInstallerData.Read(reader, suppressVersionCheck);
79 }
80 catch (XmlException xe)
81 {
82 throw new WixCorruptFileException(path, "wixout", xe);
83 }
84 }
85 }
86
87 /// <summary>
88 /// Saves an output to a <c>WixOutput</c> container.
89 /// </summary>
90 /// <param name="wixout">Container to save to.</param>
91 public void Save(WixOutput wixout)
92 {
93 using (var writer = XmlWriter.Create(wixout.CreateDataStream(WixOutputStreamName)))
94 {
95 writer.WriteStartDocument();
96 this.Write(writer);
97 writer.WriteEndDocument();
98 }
99 }
100
101 /// <summary>
102 /// Processes an XmlReader and builds up the output object.
103 /// </summary>
104 /// <param name="reader">Reader to get data from.</param>
105 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
106 /// <returns>The Output represented by the Xml.</returns>
107 internal static WindowsInstallerData Read(XmlReader reader, bool suppressVersionCheck)
108 {
109 if (!reader.LocalName.Equals(WindowsInstallerData.XmlElementName))
110 {
111 throw new XmlException();
112 }
113
114 var empty = reader.IsEmptyElement;
115 var output = new WindowsInstallerData(SourceLineNumber.CreateFromUri(reader.BaseURI));
116 Version version = null;
117
118 while (reader.MoveToNextAttribute())
119 {
120 switch (reader.LocalName)
121 {
122 case "codepage":
123 output.Codepage = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture.NumberFormat);
124 break;
125 case "type":
126 switch (reader.Value)
127 {
128 case "Bundle":
129 output.Type = OutputType.Bundle;
130 break;
131 case "Module":
132 output.Type = OutputType.Module;
133 break;
134 case "Patch":
135 output.Type = OutputType.Patch;
136 break;
137 case "PatchCreation":
138 output.Type = OutputType.PatchCreation;
139 break;
140 case "Product":
141 output.Type = OutputType.Product;
142 break;
143 case "Transform":
144 output.Type = OutputType.Transform;
145 break;
146 default:
147 throw new XmlException();
148 }
149 break;
150 case "version":
151 version = new Version(reader.Value);
152 break;
153 }
154 }
155
156 if (!suppressVersionCheck && null != version && !WindowsInstallerData.CurrentVersion.Equals(version))
157 {
158 throw new WixException(ErrorMessages.VersionMismatch(SourceLineNumber.CreateFromUri(reader.BaseURI), WindowsInstallerData.XmlElementName, version.ToString(), WindowsInstallerData.CurrentVersion.ToString()));
159 }
160
161 // loop through the rest of the xml building up the Output object
162 TableDefinitionCollection tableDefinitions = null;
163 var tables = new List<Table>();
164 if (!empty)
165 {
166 var done = false;
167
168 // loop through all the fields in a row
169 while (!done && reader.Read())
170 {
171 switch (reader.NodeType)
172 {
173 case XmlNodeType.Element:
174 switch (reader.LocalName)
175 {
176 case "subStorage":
177 output.SubStorages.Add(SubStorage.Read(reader));
178 break;
179 case "table":
180 if (null == tableDefinitions)
181 {
182 throw new XmlException();
183 }
184 tables.Add(Table.Read(reader, tableDefinitions));
185 break;
186 case "tableDefinitions":
187 tableDefinitions = TableDefinitionCollection.Read(reader);
188 break;
189 default:
190 throw new XmlException();
191 }
192 break;
193 case XmlNodeType.EndElement:
194 done = true;
195 break;
196 }
197 }
198
199 if (!done)
200 {
201 throw new XmlException();
202 }
203 }
204
205 output.Tables = new TableIndexedCollection(tables);
206 return output;
207 }
208
209 /// <summary>
210 /// Ensure this output contains a particular table.
211 /// </summary>
212 /// <param name="tableDefinition">Definition of the table that should exist.</param>
213 /// <param name="section">Optional section to use for the table. If one is not provided, the entry section will be used.</param>
214 /// <returns>The table in this output.</returns>
215 public Table EnsureTable(TableDefinition tableDefinition)
216 {
217 if (!this.Tables.TryGetTable(tableDefinition.Name, out var table))
218 {
219 table = new Table(tableDefinition);
220 this.Tables.Add(table);
221 }
222
223 return table;
224 }
225
226 /// <summary>
227 /// Persists an output in an XML format.
228 /// </summary>
229 /// <param name="writer">XmlWriter where the Output should persist itself as XML.</param>
230 internal void Write(XmlWriter writer)
231 {
232 writer.WriteStartElement(WindowsInstallerData.XmlElementName, XmlNamespaceUri);
233
234 writer.WriteAttributeString("type", this.Type.ToString());
235
236 if (0 != this.Codepage)
237 {
238 writer.WriteAttributeString("codepage", this.Codepage.ToString(CultureInfo.InvariantCulture));
239 }
240
241 writer.WriteAttributeString("version", WindowsInstallerData.CurrentVersion.ToString());
242
243 // Collect all the table definitions and write them.
244 var tableDefinitions = new TableDefinitionCollection();
245 foreach (var table in this.Tables)
246 {
247 tableDefinitions.Add(table.Definition);
248 }
249 tableDefinitions.Write(writer);
250
251 foreach (var table in this.Tables.OrderBy(t => t.Name))
252 {
253 table.Write(writer);
254 }
255
256 foreach (var subStorage in this.SubStorages)
257 {
258 subStorage.Write(writer);
259 }
260
261 writer.WriteEndElement();
262 }
263 }
264}