aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/Library.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/Library.cs')
-rw-r--r--src/WixToolset.Data/Library.cs243
1 files changed, 0 insertions, 243 deletions
diff --git a/src/WixToolset.Data/Library.cs b/src/WixToolset.Data/Library.cs
deleted file mode 100644
index 13a670b0..00000000
--- a/src/WixToolset.Data/Library.cs
+++ /dev/null
@@ -1,243 +0,0 @@
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
4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml;
9
10 /// <summary>
11 /// Object that represents a library file.
12 /// </summary>
13 public sealed class Library
14 {
15 public const string XmlNamespaceUri = "http://wixtoolset.org/schemas/v4/wixlib";
16 private static readonly Version CurrentVersion = new Version("4.0.0.0");
17
18#if false
19 private string id;
20 private List<string> embedFilePaths;
21 private Dictionary<string, Localization> localizations;
22 private List<Section> sections;
23
24 /// <summary>
25 /// Instantiates a new empty library which is only useful from static creating methods.
26 /// </summary>
27 private Library()
28 {
29 this.embedFilePaths = new List<string>();
30 this.localizations = new Dictionary<string, Localization>();
31 this.sections = new List<Section>();
32 }
33
34 /// <summary>
35 /// Instantiate a new library populated with sections.
36 /// </summary>
37 /// <param name="sections">Sections to add to the library.</param>
38 /// <param name="localizationsByCulture"></param>
39 public Library(IEnumerable<Section> sections, IDictionary<string, Localization> localizationsByCulture, IEnumerable<string> embedFilePaths)
40 {
41 this.id = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).TrimEnd('=').Replace('+', '.').Replace('/', '_');
42 this.embedFilePaths = new List<string>(embedFilePaths);
43 this.localizations = new Dictionary<string, Localization>(localizationsByCulture);
44 this.sections = new List<Section>(sections);
45
46 foreach (Section section in this.sections)
47 {
48 section.LibraryId = this.id;
49 }
50 }
51
52 /// <summary>
53 /// Get the sections contained in this library.
54 /// </summary>
55 /// <value>Sections contained in this library.</value>
56 public IEnumerable<Section> Sections => this.sections;
57
58 /// <summary>
59 /// Gets localization files from this library that match the cultures passed in, in the order of the array of cultures.
60 /// </summary>
61 /// <param name="cultures">The list of cultures to get localizations for.</param>
62 /// <returns>All localizations contained in this library that match the set of cultures provided, in the same order.</returns>
63 public IEnumerable<Localization> GetLocalizations(string[] cultures)
64 {
65 foreach (string culture in cultures ?? new string[0])
66 {
67 if (this.localizations.TryGetValue(culture, out var localization))
68 {
69 yield return localization;
70 }
71 }
72 }
73
74 /// <summary>
75 /// Loads a library from a path on disk.
76 /// </summary>
77 /// <param name="path">Path to library file saved on disk.</param>
78 /// <param name="tableDefinitions">Collection containing TableDefinitions to use when reconstituting the intermediates.</param>
79 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
80 /// <returns>Returns the loaded library.</returns>
81 public static Library Load(string path, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck)
82 {
83 using (FileStream stream = File.OpenRead(path))
84 {
85 return Load(stream, new Uri(Path.GetFullPath(path)), tableDefinitions, suppressVersionCheck);
86 }
87 }
88
89 /// <summary>
90 /// Loads a library from a stream.
91 /// </summary>
92 /// <param name="stream">Stream containing the library file.</param>
93 /// <param name="uri">Uri for finding this stream.</param>
94 /// <param name="tableDefinitions">Collection containing TableDefinitions to use when reconstituting the intermediates.</param>
95 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
96 /// <returns>Returns the loaded library.</returns>
97 public static Library Load(Stream stream, Uri uri, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck)
98 {
99 using (FileStructure fs = FileStructure.Read(stream))
100 {
101 if (FileFormat.Wixlib != fs.FileFormat)
102 {
103 throw new WixUnexpectedFileFormatException(uri.LocalPath, FileFormat.Wixlib, fs.FileFormat);
104 }
105
106 using (XmlReader reader = XmlReader.Create(fs.GetDataStream(), null, uri.AbsoluteUri))
107 {
108 try
109 {
110 reader.MoveToContent();
111 return Library.Read(reader, tableDefinitions, suppressVersionCheck);
112 }
113 catch (XmlException xe)
114 {
115 throw new WixCorruptFileException(uri.LocalPath, fs.FileFormat, xe);
116 }
117 }
118 }
119 }
120
121 /// <summary>
122 /// Saves a library to a path on disk.
123 /// </summary>
124 /// <param name="path">Path to save library file to on disk.</param>
125 /// <param name="resolver">The WiX path resolver.</param>
126 public void Save(string path)
127 {
128 Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));
129
130 using (FileStream stream = File.Create(path))
131 using (FileStructure fs = FileStructure.Create(stream, FileFormat.Wixlib, embedFilePaths))
132 using (XmlWriter writer = XmlWriter.Create(fs.GetDataStream()))
133 {
134 writer.WriteStartDocument();
135
136 this.Write(writer);
137
138 writer.WriteEndDocument();
139 }
140 }
141
142 /// <summary>
143 /// Parse the root library element.
144 /// </summary>
145 /// <param name="reader">XmlReader with library persisted as Xml.</param>
146 /// <param name="tableDefinitions">Collection containing TableDefinitions to use when reconstituting the intermediates.</param>
147 /// <param name="suppressVersionCheck">Suppresses check for wix.dll version mismatch.</param>
148 /// <returns>The parsed Library.</returns>
149 private static Library Read(XmlReader reader, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck)
150 {
151 if (!reader.LocalName.Equals("wixLibrary"))
152 {
153 throw new XmlException();
154 }
155
156 bool empty = reader.IsEmptyElement;
157 Library library = new Library();
158 Version version = null;
159
160 while (reader.MoveToNextAttribute())
161 {
162 switch (reader.LocalName)
163 {
164 case "version":
165 version = new Version(reader.Value);
166 break;
167 case "id":
168 library.id = reader.Value;
169 break;
170 }
171 }
172
173 if (!suppressVersionCheck && null != version && !Library.CurrentVersion.Equals(version))
174 {
175 throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(reader.BaseURI), "library", version.ToString(), Library.CurrentVersion.ToString()));
176 }
177
178 if (!empty)
179 {
180 bool done = false;
181
182 while (!done && (XmlNodeType.Element == reader.NodeType || reader.Read()))
183 {
184 switch (reader.NodeType)
185 {
186 case XmlNodeType.Element:
187 switch (reader.LocalName)
188 {
189 case "localization":
190 Localization localization = Localization.Read(reader);
191 library.localizations.Add(localization.Culture, localization);
192 break;
193 case "section":
194 Section section = Section.Read(reader, tableDefinitions);
195 section.LibraryId = library.id;
196 library.sections.Add(section);
197 break;
198 default:
199 throw new XmlException();
200 }
201 break;
202 case XmlNodeType.EndElement:
203 done = true;
204 break;
205 }
206 }
207
208 if (!done)
209 {
210 throw new XmlException();
211 }
212 }
213
214 return library;
215 }
216
217 /// <summary>
218 /// Persists a library in an XML format.
219 /// </summary>
220 /// <param name="writer">XmlWriter where the library should persist itself as XML.</param>
221 private void Write(XmlWriter writer)
222 {
223 writer.WriteStartElement("wixLibrary", XmlNamespaceUri);
224
225 writer.WriteAttributeString("version", CurrentVersion.ToString());
226
227 writer.WriteAttributeString("id", this.id);
228
229 foreach (Localization localization in this.localizations.Values)
230 {
231 localization.Write(writer);
232 }
233
234 foreach (Section section in this.sections)
235 {
236 section.Write(writer);
237 }
238
239 writer.WriteEndElement();
240 }
241#endif
242 }
243}