| 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 | |
| 3 | namespace WixToolset.BootstrapperApplicationApi |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Xml; |
| 8 | using System.Xml.XPath; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Default implementation of <see cref="IOverridableVariables"/>. |
| 12 | /// </summary> |
| 13 | public class OverridableVariablesInfo : IOverridableVariables |
| 14 | { |
| 15 | /// <inheritdoc /> |
| 16 | public VariableCommandLineType CommandLineType { get; internal set; } |
| 17 | |
| 18 | /// <inheritdoc /> |
| 19 | public IDictionary<string, IOverridableVariableInfo> Variables { get; internal set; } |
| 20 | |
| 21 | internal OverridableVariablesInfo() { } |
| 22 | |
| 23 | /// <summary> |
| 24 | /// Parses the overridable variable info from the BA manifest. |
| 25 | /// </summary> |
| 26 | /// <param name="root">XML root</param> |
| 27 | /// <returns>The parsed information.</returns> |
| 28 | public static IOverridableVariables ParseFromXml(XPathNavigator root) |
| 29 | { |
| 30 | XmlNamespaceManager namespaceManager = new XmlNamespaceManager(root.NameTable); |
| 31 | namespaceManager.AddNamespace("p", BootstrapperApplicationData.XMLNamespace); |
| 32 | XPathNavigator commandLineNode = root.SelectSingleNode("/p:BootstrapperApplicationData/p:WixStdbaCommandLine", namespaceManager); |
| 33 | XPathNodeIterator nodes = root.Select("/p:BootstrapperApplicationData/p:WixStdbaOverridableVariable", namespaceManager); |
| 34 | |
| 35 | var overridableVariables = new OverridableVariablesInfo(); |
| 36 | IEqualityComparer<string> variableNameComparer; |
| 37 | |
| 38 | if (commandLineNode == null) |
| 39 | { |
| 40 | overridableVariables.CommandLineType = VariableCommandLineType.CaseSensitive; |
| 41 | variableNameComparer = StringComparer.InvariantCulture; |
| 42 | } |
| 43 | else |
| 44 | { |
| 45 | string variablesValue = BootstrapperApplicationData.GetAttribute(commandLineNode, "VariableType"); |
| 46 | |
| 47 | if (variablesValue == null) |
| 48 | { |
| 49 | throw new Exception("Failed to get command line variable type."); |
| 50 | } |
| 51 | |
| 52 | if (variablesValue.Equals("caseInsensitive", StringComparison.InvariantCulture)) |
| 53 | { |
| 54 | overridableVariables.CommandLineType = VariableCommandLineType.CaseInsensitive; |
| 55 | variableNameComparer = StringComparer.InvariantCultureIgnoreCase; |
| 56 | } |
| 57 | else if (variablesValue.Equals("caseSensitive", StringComparison.InvariantCulture)) |
| 58 | { |
| 59 | overridableVariables.CommandLineType = VariableCommandLineType.CaseSensitive; |
| 60 | variableNameComparer = StringComparer.InvariantCulture; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | throw new Exception(String.Format("Unknown command line variable type: '{0}'", variablesValue)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | overridableVariables.Variables = new Dictionary<string, IOverridableVariableInfo>(variableNameComparer); |
| 69 | |
| 70 | foreach (XPathNavigator node in nodes) |
| 71 | { |
| 72 | var variable = new OverridableVariableInfo(); |
| 73 | |
| 74 | string name = BootstrapperApplicationData.GetAttribute(node, "Name"); |
| 75 | if (name == null) |
| 76 | { |
| 77 | throw new Exception("Failed to get name for overridable variable."); |
| 78 | } |
| 79 | variable.Name = name; |
| 80 | |
| 81 | overridableVariables.Variables.Add(variable.Name, variable); |
| 82 | } |
| 83 | |
| 84 | return overridableVariables; |
| 85 | } |
| 86 | } |
| 87 | } |