From fe37cb9023cbb69d0a32409144ea0142172fe3fd Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 30 Dec 2017 17:02:28 -0800 Subject: Reduce code footprint in WixToolset.Data --- src/WixToolset.Data/Intermediate.cs | 34 +--- src/WixToolset.Data/Localization.cs | 306 +----------------------------------- 2 files changed, 2 insertions(+), 338 deletions(-) diff --git a/src/WixToolset.Data/Intermediate.cs b/src/WixToolset.Data/Intermediate.cs index ae507058..4914ef88 100644 --- a/src/WixToolset.Data/Intermediate.cs +++ b/src/WixToolset.Data/Intermediate.cs @@ -58,38 +58,6 @@ namespace WixToolset.Data /// public IList Sections { get; } - /// - /// Adds a localization to the intermediate. - /// - /// Localization to add to the intermediate. - public void AddLocalization(Localization localization) - { - if (this.localizationsByCulture.TryGetValue(localization.Culture, out var existingCulture)) - { - existingCulture.Merge(localization); - } - else - { - this.localizationsByCulture.Add(localization.Culture, localization); - } - } - - /// - /// Gets localization files from this library that match the cultures passed in, in the order of the array of cultures. - /// - /// The list of cultures to get localizations for. - /// All localizations contained in this library that match the set of cultures provided, in the same order. - public IEnumerable GetLocalizationsForCultures(IEnumerable cultures) - { - foreach (string culture in cultures ?? Array.Empty()) - { - if (this.localizationsByCulture.TryGetValue(culture, out var localization)) - { - yield return localization; - } - } - } - /// /// Loads an intermediate from a path on disk. /// @@ -223,7 +191,7 @@ namespace WixToolset.Data /// ITupleDefinitionCreator to use when reconstituting the intermediate. /// Suppress checking for wix.dll version mismatches. /// Returns the loaded intermediate. - internal static Intermediate Load(Stream stream, Uri baseUri, ITupleDefinitionCreator creator, bool suppressVersionCheck = false) + private static Intermediate Load(Stream stream, Uri baseUri, ITupleDefinitionCreator creator, bool suppressVersionCheck = false) { JsonObject jsonObject; diff --git a/src/WixToolset.Data/Localization.cs b/src/WixToolset.Data/Localization.cs index 4ddef77b..673e0155 100644 --- a/src/WixToolset.Data/Localization.cs +++ b/src/WixToolset.Data/Localization.cs @@ -4,11 +4,7 @@ namespace WixToolset.Data { using System; using System.Collections.Generic; - using System.Diagnostics; - using System.Globalization; - using System.Xml; using SimpleJson; - using WixToolset.Data.Msi; using WixToolset.Data.Bind; /// @@ -27,7 +23,7 @@ namespace WixToolset.Data public Localization(int codepage, string culture, IDictionary variables, IDictionary localizedControls) { this.Codepage = codepage; - this.Culture = String.IsNullOrEmpty(culture) ? String.Empty : culture.ToLowerInvariant(); + this.Culture = culture?.ToLowerInvariant() ?? String.Empty; this.variables = new Dictionary(variables); this.localizedControls = new Dictionary(localizedControls); } @@ -56,25 +52,6 @@ namespace WixToolset.Data /// The localized controls. public ICollection> LocalizedControls => this.localizedControls; - /// - /// Merge the information from another localization object into this one. - /// - /// The localization object to be merged into this one. - public void Merge(Localization localization) - { - foreach (BindVariable wixVariableRow in localization.Variables) - { - if (!this.variables.TryGetValue(wixVariableRow.Id, out BindVariable existingWixVariableRow) || (existingWixVariableRow.Overridable && !wixVariableRow.Overridable)) - { - variables[wixVariableRow.Id] = wixVariableRow; - } - else if (!wixVariableRow.Overridable) - { - throw new WixException(ErrorMessages.DuplicateLocalizationIdentifier(wixVariableRow.SourceLineNumbers, wixVariableRow.Id)); - } - } - } - internal JsonObject Serialize() { var jsonObject = new JsonObject @@ -143,286 +120,5 @@ namespace WixToolset.Data return new Localization(codepage, culture, variables, controls); } - - /// - /// Loads a localization file from a stream. - /// - /// XmlReader where the intermediate is persisted. - /// Collection containing TableDefinitions to use when loading the localization file. - /// Returns the loaded localization. - internal static Localization Read(XmlReader reader) - { - Debug.Assert("localization" == reader.LocalName); - - int codepage = 0; - string culture = null; - bool empty = reader.IsEmptyElement; - - while (reader.MoveToNextAttribute()) - { - switch (reader.Name) - { - case "codepage": - codepage = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture); - break; - case "culture": - culture = reader.Value; - break; - } - } - - Dictionary variables = new Dictionary(); - Dictionary localizedControls = new Dictionary(); - - if (!empty) - { - bool done = false; - - while (!done && reader.Read()) - { - switch (reader.NodeType) - { - case XmlNodeType.Element: - switch (reader.LocalName) - { - case "string": - BindVariable row = Localization.ReadString(reader); - variables.Add(row.Id, row); - break; - - case "ui": - LocalizedControl ui = Localization.ReadUI(reader); - localizedControls.Add(ui.GetKey(), ui); - break; - - default: - throw new XmlException(); - } - break; - case XmlNodeType.EndElement: - done = true; - break; - } - } - - if (!done) - { - throw new XmlException(); - } - } - - return new Localization(codepage, culture, variables, localizedControls); - } - - /// - /// Writes a localization file into an XML format. - /// - /// XmlWriter where the localization file should persist itself as XML. - internal void Write(XmlWriter writer) - { - writer.WriteStartElement(Localization.XmlElementName, Library.XmlNamespaceUri); - - if (-1 != this.Codepage) - { - writer.WriteAttributeString("codepage", this.Codepage.ToString(CultureInfo.InvariantCulture)); - } - - if (!String.IsNullOrEmpty(this.Culture)) - { - writer.WriteAttributeString("culture", this.Culture); - } - - foreach (BindVariable wixVariableRow in this.variables.Values) - { - writer.WriteStartElement("string", Library.XmlNamespaceUri); - - writer.WriteAttributeString("id", wixVariableRow.Id); - - if (wixVariableRow.Overridable) - { - writer.WriteAttributeString("overridable", "yes"); - } - - writer.WriteCData(wixVariableRow.Value); - - writer.WriteEndElement(); - } - - foreach (string controlKey in this.localizedControls.Keys) - { - writer.WriteStartElement("ui", Library.XmlNamespaceUri); - - string[] controlKeys = controlKey.Split('/'); - string dialog = controlKeys[0]; - string control = controlKeys[1]; - - if (!String.IsNullOrEmpty(dialog)) - { - writer.WriteAttributeString("dialog", dialog); - } - - if (!String.IsNullOrEmpty(control)) - { - writer.WriteAttributeString("control", control); - } - - LocalizedControl localizedControl = this.localizedControls[controlKey]; - - if (Common.IntegerNotSet != localizedControl.X) - { - writer.WriteAttributeString("x", localizedControl.X.ToString()); - } - - if (Common.IntegerNotSet != localizedControl.Y) - { - writer.WriteAttributeString("y", localizedControl.Y.ToString()); - } - - if (Common.IntegerNotSet != localizedControl.Width) - { - writer.WriteAttributeString("width", localizedControl.Width.ToString()); - } - - if (Common.IntegerNotSet != localizedControl.Height) - { - writer.WriteAttributeString("height", localizedControl.Height.ToString()); - } - - if (MsiInterop.MsidbControlAttributesRTLRO == (localizedControl.Attributes & MsiInterop.MsidbControlAttributesRTLRO)) - { - writer.WriteAttributeString("rightToLeft", "yes"); - } - - if (MsiInterop.MsidbControlAttributesRightAligned == (localizedControl.Attributes & MsiInterop.MsidbControlAttributesRightAligned)) - { - writer.WriteAttributeString("rightAligned", "yes"); - } - - if (MsiInterop.MsidbControlAttributesLeftScroll == (localizedControl.Attributes & MsiInterop.MsidbControlAttributesLeftScroll)) - { - writer.WriteAttributeString("leftScroll", "yes"); - } - - if (!String.IsNullOrEmpty(localizedControl.Text)) - { - writer.WriteCData(localizedControl.Text); - } - - writer.WriteEndElement(); - } - - writer.WriteEndElement(); - } - - /// - /// Loads a localization file from a stream. - /// - /// XmlReader where the intermediate is persisted. - /// Collection containing TableDefinitions to use when loading the localization file. - /// Returns the loaded localization. - private static BindVariable ReadString(XmlReader reader) - { - Debug.Assert("string" == reader.LocalName); - - string id = null; - string value = null; - bool overridable = false; - bool empty = reader.IsEmptyElement; - - while (reader.MoveToNextAttribute()) - { - switch (reader.Name) - { - case "id": - id = reader.Value; - break; - case "overridable": - overridable = reader.Value.Equals("yes"); - break; - } - } - - - if (!empty) - { - reader.Read(); - - value = reader.Value; - - reader.Read(); - - if (XmlNodeType.EndElement != reader.NodeType) - { - throw new XmlException(); - } - } - - BindVariable wixVariableRow = new BindVariable(); - wixVariableRow.SourceLineNumbers = SourceLineNumber.CreateFromUri(reader.BaseURI); - wixVariableRow.Id = id; - wixVariableRow.Overridable = overridable; - wixVariableRow.Value = value; - - return wixVariableRow; - } - - private static LocalizedControl ReadUI(XmlReader reader) - { - Debug.Assert("ui" == reader.LocalName); - - string dialog = null; - string control = null; - int x = Common.IntegerNotSet; - int y = Common.IntegerNotSet; - int width = Common.IntegerNotSet; - int height = Common.IntegerNotSet; - int attributes = Common.IntegerNotSet; - string text = null; - bool empty = reader.IsEmptyElement; - - while (reader.MoveToNextAttribute()) - { - switch (reader.Name) - { - case "dialog": - dialog = reader.Value; - break; - case "control": - control = reader.Value; - break; - case "x": - x = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture); - break; - case "y": - y = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture); - break; - case "width": - width = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture); - break; - case "height": - height = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture); - break; - case "attributes": - attributes = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture); - break; - } - } - - if (!empty) - { - reader.Read(); - - text = reader.Value; - - reader.Read(); - - if (XmlNodeType.EndElement != reader.NodeType) - { - throw new XmlException(); - } - } - - return new LocalizedControl(dialog, control, x, y, width, height, attributes, text); - } } } -- cgit v1.2.3-55-g6feb