@joebigelow / wix-1 / commits / fe37cb90

Reduce code footprint in WixToolset.Data

Rob Mensching committed Dec 30, 2017 at 17:02 UTC fe37cb9023cbb69d0a32409144ea0142172fe3fd
2 files changed +2 -338
src/WixToolset.Data/Intermediate.cs
+1 -33
@@ -58,38 +58,6 @@ namespace WixToolset.Data
58 /// </summary>
59 public IList<IntermediateSection> Sections { get; }
60
61 - /// <summary>
62 - /// Adds a localization to the intermediate.
63 - /// </summary>
64 - /// <param name="localization">Localization to add to the intermediate.</param>
65 - public void AddLocalization(Localization localization)
66 - {
67 - if (this.localizationsByCulture.TryGetValue(localization.Culture, out var existingCulture))
68 - {
69 - existingCulture.Merge(localization);
70 - }
71 - else
72 - {
73 - this.localizationsByCulture.Add(localization.Culture, localization);
74 - }
75 - }
76 -
77 - /// <summary>
78 - /// Gets localization files from this library that match the cultures passed in, in the order of the array of cultures.
79 - /// </summary>
80 - /// <param name="cultures">The list of cultures to get localizations for.</param>
81 - /// <returns>All localizations contained in this library that match the set of cultures provided, in the same order.</returns>
82 - public IEnumerable<Localization> GetLocalizationsForCultures(IEnumerable<string> cultures)
83 - {
84 - foreach (string culture in cultures ?? Array.Empty<string>())
85 - {
86 - if (this.localizationsByCulture.TryGetValue(culture, out var localization))
87 - {
88 - yield return localization;
89 - }
90 - }
91 - }
92 -
61 /// <summary>
62 /// Loads an intermediate from a path on disk.
63 /// </summary>
@@ -223,7 +191,7 @@ namespace WixToolset.Data
191 /// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
192 /// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
193 /// <returns>Returns the loaded intermediate.</returns>
226 - internal static Intermediate Load(Stream stream, Uri baseUri, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
194 + private static Intermediate Load(Stream stream, Uri baseUri, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
195 {
196 JsonObject jsonObject;
197
src/WixToolset.Data/Localization.cs
+1 -305
@@ -4,11 +4,7 @@ namespace WixToolset.Data
4 {
5 using System;
6 using System.Collections.Generic;
7 - using System.Diagnostics;
8 - using System.Globalization;
9 - using System.Xml;
7 using SimpleJson;
11 - using WixToolset.Data.Msi;
8 using WixToolset.Data.Bind;
9
10 /// <summary>
@@ -27,7 +23,7 @@ namespace WixToolset.Data
23 public Localization(int codepage, string culture, IDictionary<string, BindVariable> variables, IDictionary<string, LocalizedControl> localizedControls)
24 {
25 this.Codepage = codepage;
30 - this.Culture = String.IsNullOrEmpty(culture) ? String.Empty : culture.ToLowerInvariant();
26 + this.Culture = culture?.ToLowerInvariant() ?? String.Empty;
27 this.variables = new Dictionary<string, BindVariable>(variables);
28 this.localizedControls = new Dictionary<string, LocalizedControl>(localizedControls);
29 }
@@ -56,25 +52,6 @@ namespace WixToolset.Data
52 /// <value>The localized controls.</value>
53 public ICollection<KeyValuePair<string, LocalizedControl>> LocalizedControls => this.localizedControls;
54
59 - /// <summary>
60 - /// Merge the information from another localization object into this one.
61 - /// </summary>
62 - /// <param name="localization">The localization object to be merged into this one.</param>
63 - public void Merge(Localization localization)
64 - {
65 - foreach (BindVariable wixVariableRow in localization.Variables)
66 - {
67 - if (!this.variables.TryGetValue(wixVariableRow.Id, out BindVariable existingWixVariableRow) || (existingWixVariableRow.Overridable && !wixVariableRow.Overridable))
68 - {
69 - variables[wixVariableRow.Id] = wixVariableRow;
70 - }
71 - else if (!wixVariableRow.Overridable)
72 - {
73 - throw new WixException(ErrorMessages.DuplicateLocalizationIdentifier(wixVariableRow.SourceLineNumbers, wixVariableRow.Id));
74 - }
75 - }
76 - }
77 -
55 internal JsonObject Serialize()
56 {
57 var jsonObject = new JsonObject
@@ -143,286 +120,5 @@ namespace WixToolset.Data
120
121 return new Localization(codepage, culture, variables, controls);
122 }
146 -
147 - /// <summary>
148 - /// Loads a localization file from a stream.
149 - /// </summary>
150 - /// <param name="reader">XmlReader where the intermediate is persisted.</param>
151 - /// <param name="tableDefinitions">Collection containing TableDefinitions to use when loading the localization file.</param>
152 - /// <returns>Returns the loaded localization.</returns>
153 - internal static Localization Read(XmlReader reader)
154 - {
155 - Debug.Assert("localization" == reader.LocalName);
156 -
157 - int codepage = 0;
158 - string culture = null;
159 - bool empty = reader.IsEmptyElement;
160 -
161 - while (reader.MoveToNextAttribute())
162 - {
163 - switch (reader.Name)
164 - {
165 - case "codepage":
166 - codepage = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
167 - break;
168 - case "culture":
169 - culture = reader.Value;
170 - break;
171 - }
172 - }
173 -
174 - Dictionary<string, BindVariable> variables = new Dictionary<string, BindVariable>();
175 - Dictionary<string, LocalizedControl> localizedControls = new Dictionary<string, LocalizedControl>();
176 -
177 - if (!empty)
178 - {
179 - bool done = false;
180 -
181 - while (!done && reader.Read())
182 - {
183 - switch (reader.NodeType)
184 - {
185 - case XmlNodeType.Element:
186 - switch (reader.LocalName)
187 - {
188 - case "string":
189 - BindVariable row = Localization.ReadString(reader);
190 - variables.Add(row.Id, row);
191 - break;
192 -
193 - case "ui":
194 - LocalizedControl ui = Localization.ReadUI(reader);
195 - localizedControls.Add(ui.GetKey(), ui);
196 - break;
197 -
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 new Localization(codepage, culture, variables, localizedControls);
215 - }
216 -
217 - /// <summary>
218 - /// Writes a localization file into an XML format.
219 - /// </summary>
220 - /// <param name="writer">XmlWriter where the localization file should persist itself as XML.</param>
221 - internal void Write(XmlWriter writer)
222 - {
223 - writer.WriteStartElement(Localization.XmlElementName, Library.XmlNamespaceUri);
224 -
225 - if (-1 != this.Codepage)
226 - {
227 - writer.WriteAttributeString("codepage", this.Codepage.ToString(CultureInfo.InvariantCulture));
228 - }
229 -
230 - if (!String.IsNullOrEmpty(this.Culture))
231 - {
232 - writer.WriteAttributeString("culture", this.Culture);
233 - }
234 -
235 - foreach (BindVariable wixVariableRow in this.variables.Values)
236 - {
237 - writer.WriteStartElement("string", Library.XmlNamespaceUri);
238 -
239 - writer.WriteAttributeString("id", wixVariableRow.Id);
240 -
241 - if (wixVariableRow.Overridable)
242 - {
243 - writer.WriteAttributeString("overridable", "yes");
244 - }
245 -
246 - writer.WriteCData(wixVariableRow.Value);
247 -
248 - writer.WriteEndElement();
249 - }
250 -
251 - foreach (string controlKey in this.localizedControls.Keys)
252 - {
253 - writer.WriteStartElement("ui", Library.XmlNamespaceUri);
254 -
255 - string[] controlKeys = controlKey.Split('/');
256 - string dialog = controlKeys[0];
257 - string control = controlKeys[1];
258 -
259 - if (!String.IsNullOrEmpty(dialog))
260 - {
261 - writer.WriteAttributeString("dialog", dialog);
262 - }
263 -
264 - if (!String.IsNullOrEmpty(control))
265 - {
266 - writer.WriteAttributeString("control", control);
267 - }
268 -
269 - LocalizedControl localizedControl = this.localizedControls[controlKey];
270 -
271 - if (Common.IntegerNotSet != localizedControl.X)
272 - {
273 - writer.WriteAttributeString("x", localizedControl.X.ToString());
274 - }
275 -
276 - if (Common.IntegerNotSet != localizedControl.Y)
277 - {
278 - writer.WriteAttributeString("y", localizedControl.Y.ToString());
279 - }
280 -
281 - if (Common.IntegerNotSet != localizedControl.Width)
282 - {
283 - writer.WriteAttributeString("width", localizedControl.Width.ToString());
284 - }
285 -
286 - if (Common.IntegerNotSet != localizedControl.Height)
287 - {
288 - writer.WriteAttributeString("height", localizedControl.Height.ToString());
289 - }
290 -
291 - if (MsiInterop.MsidbControlAttributesRTLRO == (localizedControl.Attributes & MsiInterop.MsidbControlAttributesRTLRO))
292 - {
293 - writer.WriteAttributeString("rightToLeft", "yes");
294 - }
295 -
296 - if (MsiInterop.MsidbControlAttributesRightAligned == (localizedControl.Attributes & MsiInterop.MsidbControlAttributesRightAligned))
297 - {
298 - writer.WriteAttributeString("rightAligned", "yes");
299 - }
300 -
301 - if (MsiInterop.MsidbControlAttributesLeftScroll == (localizedControl.Attributes & MsiInterop.MsidbControlAttributesLeftScroll))
302 - {
303 - writer.WriteAttributeString("leftScroll", "yes");
304 - }
305 -
306 - if (!String.IsNullOrEmpty(localizedControl.Text))
307 - {
308 - writer.WriteCData(localizedControl.Text);
309 - }
310 -
311 - writer.WriteEndElement();
312 - }
313 -
314 - writer.WriteEndElement();
315 - }
316 -
317 - /// <summary>
318 - /// Loads a localization file from a stream.
319 - /// </summary>
320 - /// <param name="reader">XmlReader where the intermediate is persisted.</param>
321 - /// <param name="tableDefinitions">Collection containing TableDefinitions to use when loading the localization file.</param>
322 - /// <returns>Returns the loaded localization.</returns>
323 - private static BindVariable ReadString(XmlReader reader)
324 - {
325 - Debug.Assert("string" == reader.LocalName);
326 -
327 - string id = null;
328 - string value = null;
329 - bool overridable = false;
330 - bool empty = reader.IsEmptyElement;
331 -
332 - while (reader.MoveToNextAttribute())
333 - {
334 - switch (reader.Name)
335 - {
336 - case "id":
337 - id = reader.Value;
338 - break;
339 - case "overridable":
340 - overridable = reader.Value.Equals("yes");
341 - break;
342 - }
343 - }
344 -
345 -
346 - if (!empty)
347 - {
348 - reader.Read();
349 -
350 - value = reader.Value;
351 -
352 - reader.Read();
353 -
354 - if (XmlNodeType.EndElement != reader.NodeType)
355 - {
356 - throw new XmlException();
357 - }
358 - }
359 -
360 - BindVariable wixVariableRow = new BindVariable();
361 - wixVariableRow.SourceLineNumbers = SourceLineNumber.CreateFromUri(reader.BaseURI);
362 - wixVariableRow.Id = id;
363 - wixVariableRow.Overridable = overridable;
364 - wixVariableRow.Value = value;
365 -
366 - return wixVariableRow;
367 - }
368 -
369 - private static LocalizedControl ReadUI(XmlReader reader)
370 - {
371 - Debug.Assert("ui" == reader.LocalName);
372 -
373 - string dialog = null;
374 - string control = null;
375 - int x = Common.IntegerNotSet;
376 - int y = Common.IntegerNotSet;
377 - int width = Common.IntegerNotSet;
378 - int height = Common.IntegerNotSet;
379 - int attributes = Common.IntegerNotSet;
380 - string text = null;
381 - bool empty = reader.IsEmptyElement;
382 -
383 - while (reader.MoveToNextAttribute())
384 - {
385 - switch (reader.Name)
386 - {
387 - case "dialog":
388 - dialog = reader.Value;
389 - break;
390 - case "control":
391 - control = reader.Value;
392 - break;
393 - case "x":
394 - x = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
395 - break;
396 - case "y":
397 - y = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
398 - break;
399 - case "width":
400 - width = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
401 - break;
402 - case "height":
403 - height = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
404 - break;
405 - case "attributes":
406 - attributes = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
407 - break;
408 - }
409 - }
410 -
411 - if (!empty)
412 - {
413 - reader.Read();
414 -
415 - text = reader.Value;
416 -
417 - reader.Read();
418 -
419 - if (XmlNodeType.EndElement != reader.NodeType)
420 - {
421 - throw new XmlException();
422 - }
423 - }
424 -
425 - return new LocalizedControl(dialog, control, x, y, width, height, attributes, text);
426 - }
123 }
124 }